TensorTonic
Problems
Layer Normalization as a CUDA Kernel
Neural NetworksLinear AlgebraGPU Programming
Medium

Implement layer normalization on the GPU for a 2D input matrix. Given input\texttt{input}input of shape (M,N)(M, N)(M,N), normalize each row independently across the last dimension NNN, then apply a learnable affine transform with per-feature scale γ\gammaγ and shift β\betaβ (both length NNN).

For each row iii, compute the row mean and population variance (divisor NNN):

μi=1N∑j=0N−1input[i,j]\mu_i = \frac{1}{N} \sum_{j=0}^{N-1} \texttt{input}[i, j]μi​=N1​j=0∑N−1​input[i,j] σi2=1N∑j=0N−1(input[i,j]−μi)2\sigma_i^2 = \frac{1}{N} \sum_{j=0}^{N-1} \big( \texttt{input}[i, j] - \mu_i \big)^2σi2​=N1​j=0∑N−1​(input[i,j]−μi​)2

Then write the normalized and affined output:

output[i,j]=input[i,j]−μiσi2+ε⋅γ[j]+β[j]\texttt{output}[i, j] = \frac{\texttt{input}[i, j] - \mu_i}{\sqrt{\sigma_i^2 + \varepsilon}} \cdot \gamma[j] + \beta[j]output[i,j]=σi2​+ε​input[i,j]−μi​​⋅γ[j]+β[j]

ε\varepsilonε is a small positive float for numerical stability. Buffers are row-major device pointers, float32.

Loading visualization...

Examples

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.52.52.5, population variance is 1.251.251.25, so each element is shifted and scaled by 1/1.251/\sqrt{1.25}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.

Hint 1

Map one CUDA block to one row. Threads inside a block cooperate via shared memory to reduce that row.

Hint 2

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.

Hint 3

Use rsqrtf(var + eps) for the inverse standard deviation; it is faster and more accurate than 1.0f / sqrtf(...) on the GPU.

Requirements

  • Launch one block per row of the (M,N)(M, N)(M,N) matrix so each row reduces independently.
  • Compute the row mean and variance with a single shared-memory reduction over ∑x\sum x∑x and ∑x2\sum x^2∑x2, using σ2=E[x2]−μ2\sigma^2 = E[x^2] - \mu^2σ2=E[x2]−μ2.
  • Fuse the normalize and affine step into one element-wise write: output[i,j]=(input[i,j]−μi)⋅inv_stdi⋅γ[j]+β[j]\texttt{output}[i,j] = (\texttt{input}[i,j] - \mu_i) \cdot \texttt{inv\_std}_i \cdot \gamma[j] + \beta[j]output[i,j]=(input[i,j]−μi​)⋅inv_stdi​⋅γ[j]+β[j].
  • Add ε\varepsilonε inside the inverse square root for stability when a row has zero variance.
  • Do not change the solve\texttt{solve}solve signature; the harness depends on the exact 7-parameter prototype.

Constraints

  • 1≤M≤40961 \le M \le 40961≤M≤4096
  • 1≤N≤40961 \le N \le 40961≤N≤4096
  • input\texttt{input}input, gamma\texttt{gamma}gamma, beta\texttt{beta}beta, output\texttt{output}output are device pointers, float32.
  • ε\varepsilonε is a positive float, typically in [10−5,10−3][10^{-5}, 10^{-3}][10−5,10−3].
  • Row-major flat layout: element (i,j)(i, j)(i,j) lives at index i⋅N+ji \cdot N + ji⋅N+j.
Try Similar Problems
RMS Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumTiled Matrix Multiplication as a CUDA KernelHard

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Layer Normalization as a CUDA Kernel

Layer Normalization as a CUDA Kernel

Neural NetworksLinear AlgebraGPU Programming
Medium

Implement layer normalization on the GPU for a 2D input matrix. Given input\texttt{input}input of shape (M,N)(M, N)(M,N), normalize each row independently across the last dimension NNN, then apply a learnable affine transform with per-feature scale γ\gammaγ and shift β\betaβ (both length NNN).

For each row iii, compute the row mean and population variance (divisor NNN):

μi=1N∑j=0N−1input[i,j]\mu_i = \frac{1}{N} \sum_{j=0}^{N-1} \texttt{input}[i, j]μi​=N1​j=0∑N−1​input[i,j] σi2=1N∑j=0N−1(input[i,j]−μi)2\sigma_i^2 = \frac{1}{N} \sum_{j=0}^{N-1} \big( \texttt{input}[i, j] - \mu_i \big)^2σi2​=N1​j=0∑N−1​(input[i,j]−μi​)2

Then write the normalized and affined output:

output[i,j]=input[i,j]−μiσi2+ε⋅γ[j]+β[j]\texttt{output}[i, j] = \frac{\texttt{input}[i, j] - \mu_i}{\sqrt{\sigma_i^2 + \varepsilon}} \cdot \gamma[j] + \beta[j]output[i,j]=σi2​+ε​input[i,j]−μi​​⋅γ[j]+β[j]

ε\varepsilonε is a small positive float for numerical stability. Buffers are row-major device pointers, float32.

Loading visualization...

Examples

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.52.52.5, population variance is 1.251.251.25, so each element is shifted and scaled by 1/1.251/\sqrt{1.25}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.

Hint 1

Map one CUDA block to one row. Threads inside a block cooperate via shared memory to reduce that row.

Hint 2

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.

Hint 3

Use rsqrtf(var + eps) for the inverse standard deviation; it is faster and more accurate than 1.0f / sqrtf(...) on the GPU.

Requirements

  • Launch one block per row of the (M,N)(M, N)(M,N) matrix so each row reduces independently.
  • Compute the row mean and variance with a single shared-memory reduction over ∑x\sum x∑x and ∑x2\sum x^2∑x2, using σ2=E[x2]−μ2\sigma^2 = E[x^2] - \mu^2σ2=E[x2]−μ2.
  • Fuse the normalize and affine step into one element-wise write: output[i,j]=(input[i,j]−μi)⋅inv_stdi⋅γ[j]+β[j]\texttt{output}[i,j] = (\texttt{input}[i,j] - \mu_i) \cdot \texttt{inv\_std}_i \cdot \gamma[j] + \beta[j]output[i,j]=(input[i,j]−μi​)⋅inv_stdi​⋅γ[j]+β[j].
  • Add ε\varepsilonε inside the inverse square root for stability when a row has zero variance.
  • Do not change the solve\texttt{solve}solve signature; the harness depends on the exact 7-parameter prototype.

Constraints

  • 1≤M≤40961 \le M \le 40961≤M≤4096
  • 1≤N≤40961 \le N \le 40961≤N≤4096
  • input\texttt{input}input, gamma\texttt{gamma}gamma, beta\texttt{beta}beta, output\texttt{output}output are device pointers, float32.
  • ε\varepsilonε is a positive float, typically in [10−5,10−3][10^{-5}, 10^{-3}][10−5,10−3].
  • Row-major flat layout: element (i,j)(i, j)(i,j) lives at index i⋅N+ji \cdot N + ji⋅N+j.
Try Similar Problems
RMS Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumTiled Matrix Multiplication as a CUDA KernelHard

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1