TensorTonic
Problems
RMSNorm Forward in Triton
Neural NetworksGPU Programming
Medium

Implement Root Mean Square Layer Normalization (RMSNorm) in Triton. For each row of x∈RM×Nx \in \mathbb{R}^{M \times N}x∈RM×N and column jjj:

out[i,j]=x[i,j]1N∑kx[i,k]2+ϵ⋅γ[j]\texttt{out}[i, j] = \frac{x[i, j]}{\sqrt{\frac{1}{N} \sum_k x[i, k]^2 + \epsilon}} \cdot \gamma[j]out[i,j]=N1​∑k​x[i,k]2+ϵ​x[i,j]​⋅γ[j]

Unlike LayerNorm, RMSNorm does not subtract the mean and has no bias. It only rescales each row by its root-mean-square and multiplies by a learned scale γ\gammaγ. This is the normalization used in modern LLMs such as LLaMA and Mistral because it saves the mean subtraction and the bias parameter without measurably hurting quality.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0, 4.0]], gamma = [1, 1, 1, 1], eps = 1e-5

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

Input: x = [[2, 2], [3, 4]], gamma = [1, 2], eps = 1e-5

Output: [[1.0, 2.0], [0.8485, 2.2627]]

Hint 1

Each program owns one row. Load with other=0.0\texttt{other=0.0}other=0.0 on the column mask so masked lanes contribute zero to ∑x2\sum x^2∑x2 without special-casing the boundary.

Hint 2

Use tl.math.rsqrt\texttt{tl.math.rsqrt}tl.math.rsqrt on mean_sq+ϵ\text{mean\_sq} + \epsilonmean_sq+ϵ for the inverse standard deviation; it is faster and more accurate than the reciprocal of a square root.

Hint 3

Multiply by γ[j]\gamma[j]γ[j] only - no mean subtraction, no β\betaβ. Reuse the same column mask on the final store.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Reduce ∑x2\sum x^2∑x2 in a single pass, divide by NNN, add ϵ\epsilonϵ, and take the inverse square root
  • Apply only the scale γ\gammaγ; there is no mean subtraction and no β\betaβ
  • Mask the column tail on every load and store
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M≤2561 \le M \le 2561≤M≤256, 1≤N≤10241 \le N \le 10241≤N≤1024.
  • All tensors are torch.float32 on cuda. eps is a Python float in [10−6,10−1][10^{-6}, 10^{-1}][10−6,10−1].
  • Use BLOCK_SIZE as a tl.constexpr; pick it as triton.next_power_of_2(N) in the launcher.
  • Comparisons use atol = 1e-2 with rtol = 1e-2 to absorb accumulation drift across the row reduction.
Try Similar Problems
LayerNorm Forward in TritonMediumLayerNorm Backward in TritonHardDropout (Inverted Scaling) in TritonEasyCross Entropy Loss (Mean Reduction) in TritonMediumRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumDropout as a CUDA KernelEasyRow-Wise LogSumExp in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMedium

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

RMSNorm Forward in Triton

RMSNorm Forward in Triton

Neural NetworksGPU Programming
Medium

Implement Root Mean Square Layer Normalization (RMSNorm) in Triton. For each row of x∈RM×Nx \in \mathbb{R}^{M \times N}x∈RM×N and column jjj:

out[i,j]=x[i,j]1N∑kx[i,k]2+ϵ⋅γ[j]\texttt{out}[i, j] = \frac{x[i, j]}{\sqrt{\frac{1}{N} \sum_k x[i, k]^2 + \epsilon}} \cdot \gamma[j]out[i,j]=N1​∑k​x[i,k]2+ϵ​x[i,j]​⋅γ[j]

Unlike LayerNorm, RMSNorm does not subtract the mean and has no bias. It only rescales each row by its root-mean-square and multiplies by a learned scale γ\gammaγ. This is the normalization used in modern LLMs such as LLaMA and Mistral because it saves the mean subtraction and the bias parameter without measurably hurting quality.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0, 4.0]], gamma = [1, 1, 1, 1], eps = 1e-5

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

Input: x = [[2, 2], [3, 4]], gamma = [1, 2], eps = 1e-5

Output: [[1.0, 2.0], [0.8485, 2.2627]]

Hint 1

Each program owns one row. Load with other=0.0\texttt{other=0.0}other=0.0 on the column mask so masked lanes contribute zero to ∑x2\sum x^2∑x2 without special-casing the boundary.

Hint 2

Use tl.math.rsqrt\texttt{tl.math.rsqrt}tl.math.rsqrt on mean_sq+ϵ\text{mean\_sq} + \epsilonmean_sq+ϵ for the inverse standard deviation; it is faster and more accurate than the reciprocal of a square root.

Hint 3

Multiply by γ[j]\gamma[j]γ[j] only - no mean subtraction, no β\betaβ. Reuse the same column mask on the final store.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Reduce ∑x2\sum x^2∑x2 in a single pass, divide by NNN, add ϵ\epsilonϵ, and take the inverse square root
  • Apply only the scale γ\gammaγ; there is no mean subtraction and no β\betaβ
  • Mask the column tail on every load and store
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M≤2561 \le M \le 2561≤M≤256, 1≤N≤10241 \le N \le 10241≤N≤1024.
  • All tensors are torch.float32 on cuda. eps is a Python float in [10−6,10−1][10^{-6}, 10^{-1}][10−6,10−1].
  • Use BLOCK_SIZE as a tl.constexpr; pick it as triton.next_power_of_2(N) in the launcher.
  • Comparisons use atol = 1e-2 with rtol = 1e-2 to absorb accumulation drift across the row reduction.
Try Similar Problems
LayerNorm Forward in TritonMediumLayerNorm Backward in TritonHardDropout (Inverted Scaling) in TritonEasyCross Entropy Loss (Mean Reduction) in TritonMediumRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumDropout as a CUDA KernelEasyRow-Wise LogSumExp in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMedium

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