         }
             
         float* dX_t = &dX[t * O];
-        const float* d_sum_b = &d_sum_b[b * O];
+        const float* d_sum_b = &d_sum[b * O];
 
         for (int i=0; i < O; ++i) 
         {

        // Go to the regions we're working on
	float* output_b = &output[b*O];
        // Find the sequence item we're working on
	const float* X_t = X;
        for (int i=0; i < b; ++i) {
	    X_t += lengths[i] * O;
        }
        int length = lengths[b];
        // Each invocation of the kernel sums one batch.
        for (int _=0; _ < length; ++_) // Iterate over rows
        {
            for (int i=0; i < O; ++i) 
            {
              output_b[i] += X_t[i];
            }
	    X_t += O;
        }
    }
}

           b += 1;
        }
            
        float* dX_t = &dX[t * O];
        const float* d_mean_b = &d_mean_b[b * O];
        int lengths_b = lengths[b];

        for (int i=0; i < O; ++i) 
        {
            dX_t[i] = d_mean_b[i] / lengths_b;
        }
    }
}
