Implement layer normalization on the GPU for a 2D input matrix. Given input of shape (M,N), normalize each row independently across the last dimension N, then apply a learnable affine transform with per-feature scale γ and shift β (both length N).
For each row i, compute the row mean and population variance (divisor N):
μi=N1j=0∑N−1input[i,j] σi2=N1j=0∑N−1(input[i,j]−μi)2Then write the normalized and affined output:
output[i,j]=σi2+εinput[i,j]−μi⋅γ[j]+β[j]ε is a small positive float for numerical stability. Buffers are row-major device pointers, float32.
Input: input = [[1, 2, 3, 4]], gamma = [1, 1, 1, 1], beta = [0, 0, 0, 0], eps = 1e-5
Output: [[-1.3416, -0.4472, 0.4472, 1.3416]]
Row mean is 2.5, population variance is 1.25, so each element is shifted and scaled by 1/1.25.
Input: input = [[1, 2, 3], [4, 5, 6]], gamma = [1, 1, 1], beta = [0, 0, 0], eps = 1e-5
Output: [[-1.2247, 0.0, 1.2247], [-1.2247, 0.0, 1.2247]]
Both rows have the same shape after centering and scaling because their variances are equal.
Map one CUDA block to one row. Threads inside a block cooperate via shared memory to reduce that row.
Accumulate both sum and sum_of_squares in the same pass, then compute var = sum_sq / N - mean * mean to avoid a second read of the row.
Use rsqrtf(var + eps) for the inverse standard deviation; it is faster and more accurate than 1.0f / sqrtf(...) on the GPU.
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Submit a correct solution to see your kernel performance.
Implement layer normalization on the GPU for a 2D input matrix. Given input of shape (M,N), normalize each row independently across the last dimension N, then apply a learnable affine transform with per-feature scale γ and shift β (both length N).
For each row i, compute the row mean and population variance (divisor N):
μi=N1j=0∑N−1input[i,j] σi2=N1j=0∑N−1(input[i,j]−μi)2Then write the normalized and affined output:
output[i,j]=σi2+εinput[i,j]−μi⋅γ[j]+β[j]ε is a small positive float for numerical stability. Buffers are row-major device pointers, float32.
Input: input = [[1, 2, 3, 4]], gamma = [1, 1, 1, 1], beta = [0, 0, 0, 0], eps = 1e-5
Output: [[-1.3416, -0.4472, 0.4472, 1.3416]]
Row mean is 2.5, population variance is 1.25, so each element is shifted and scaled by 1/1.25.
Input: input = [[1, 2, 3], [4, 5, 6]], gamma = [1, 1, 1], beta = [0, 0, 0], eps = 1e-5
Output: [[-1.2247, 0.0, 1.2247], [-1.2247, 0.0, 1.2247]]
Both rows have the same shape after centering and scaling because their variances are equal.
Map one CUDA block to one row. Threads inside a block cooperate via shared memory to reduce that row.
Accumulate both sum and sum_of_squares in the same pass, then compute var = sum_sq / N - mean * mean to avoid a second read of the row.
Use rsqrtf(var + eps) for the inverse standard deviation; it is faster and more accurate than 1.0f / sqrtf(...) on the GPU.
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Submit a correct solution to see your kernel performance.