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

Implement row-wise RMS normalization with a learnable scale. Given an input matrix of shape (M,N)(M, N)(M,N) and a length-NNN scale vector γ\gammaγ, normalize each row by its root-mean-square then apply the per-column scale. For row iii:

rmsi=1N∑j=0N−1input[i,j]2+ϵ\text{rms}_i = \sqrt{\frac{1}{N}\sum_{j=0}^{N-1} \text{input}[i, j]^2 + \epsilon}rmsi​=N1​j=0∑N−1​input[i,j]2+ϵ​ output[i,j]=input[i,j]rmsi γ[j]\text{output}[i, j] = \frac{\text{input}[i, j]}{\text{rms}_i}\,\gamma[j]output[i,j]=rmsi​input[i,j]​γ[j]

Unlike LayerNorm, RMSNorm does not subtract the mean and has no bias term. The normalization is taken over the last axis (each row independently). All matrices are row-major.

Loading visualization...

Examples

Input: M = 1, N = 4, input = [[1, 2, 3, 4]], gamma = [1, 1, 1, 1], eps = 1e-5

Output: [[0.3651, 0.7303, 1.0954, 1.4606]]

The row RMS is 7.5≈2.7386\sqrt{7.5} \approx 2.73867.5​≈2.7386; each element is divided by it.

Input: M = 2, N = 2, input = [[3, 4], [1, 1]], gamma = [1, 1], eps = 1e-5

Output: [[0.8485, 1.1314], [1.0, 1.0]]

Each row is normalized by its own RMS independently.

Hint 1

One block per row is a clean mapping: each block reduces the sum of squares for its row in shared memory.

Hint 2

rsqrtf(meanSquare + eps)\texttt{rsqrtf(meanSquare + eps)}rsqrtf(meanSquare + eps) gives the inverse RMS in a single fast instruction.

Requirements

  • Reduce the sum of squares across each row
  • Divide by the root-mean-square with an ϵ\epsilonϵ for stability
  • Apply the per-column scale γ[j]\gamma[j]γ[j]
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤40961 \le M, N \le 40961≤M,N≤4096
  • ϵ\epsilonϵ is a small positive float (around 10−610^{-6}10−6 to 10−310^{-3}10−3)
  • Input, γ\gammaγ, and output are device pointers
  • Values fit in 32-bit float
Try Similar Problems
Layer 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: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

RMS Normalization as a CUDA Kernel

RMS Normalization as a CUDA Kernel

Neural NetworksLinear AlgebraGPU Programming
Medium

Implement row-wise RMS normalization with a learnable scale. Given an input matrix of shape (M,N)(M, N)(M,N) and a length-NNN scale vector γ\gammaγ, normalize each row by its root-mean-square then apply the per-column scale. For row iii:

rmsi=1N∑j=0N−1input[i,j]2+ϵ\text{rms}_i = \sqrt{\frac{1}{N}\sum_{j=0}^{N-1} \text{input}[i, j]^2 + \epsilon}rmsi​=N1​j=0∑N−1​input[i,j]2+ϵ​ output[i,j]=input[i,j]rmsi γ[j]\text{output}[i, j] = \frac{\text{input}[i, j]}{\text{rms}_i}\,\gamma[j]output[i,j]=rmsi​input[i,j]​γ[j]

Unlike LayerNorm, RMSNorm does not subtract the mean and has no bias term. The normalization is taken over the last axis (each row independently). All matrices are row-major.

Loading visualization...

Examples

Input: M = 1, N = 4, input = [[1, 2, 3, 4]], gamma = [1, 1, 1, 1], eps = 1e-5

Output: [[0.3651, 0.7303, 1.0954, 1.4606]]

The row RMS is 7.5≈2.7386\sqrt{7.5} \approx 2.73867.5​≈2.7386; each element is divided by it.

Input: M = 2, N = 2, input = [[3, 4], [1, 1]], gamma = [1, 1], eps = 1e-5

Output: [[0.8485, 1.1314], [1.0, 1.0]]

Each row is normalized by its own RMS independently.

Hint 1

One block per row is a clean mapping: each block reduces the sum of squares for its row in shared memory.

Hint 2

rsqrtf(meanSquare + eps)\texttt{rsqrtf(meanSquare + eps)}rsqrtf(meanSquare + eps) gives the inverse RMS in a single fast instruction.

Requirements

  • Reduce the sum of squares across each row
  • Divide by the root-mean-square with an ϵ\epsilonϵ for stability
  • Apply the per-column scale γ[j]\gamma[j]γ[j]
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤40961 \le M, N \le 40961≤M,N≤4096
  • ϵ\epsilonϵ is a small positive float (around 10−610^{-6}10−6 to 10−310^{-3}10−3)
  • Input, γ\gammaγ, and output are device pointers
  • Values fit in 32-bit float
Try Similar Problems
Layer 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: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1