TensorTonic
Problems
LayerNorm Forward in Triton
Neural NetworksGPU Programming
Medium

Implement the forward pass of LayerNorm in Triton. For each row of the input matrix x∈RM×Nx \in \mathbb{R}^{M \times N}x∈RM×N, compute the mean and variance across the feature dimension, then normalize and apply a learned affine transform with vectors γ,β∈RN\gamma, \beta \in \mathbb{R}^{N}γ,β∈RN.

out[i,j]=x[i,j]−μiσi2+ϵ⋅γ[j]+β[j]\texttt{out}[i, j] = \frac{x[i, j] - \mu_i}{\sqrt{\sigma_i^2 + \epsilon}} \cdot \gamma[j] + \beta[j]out[i,j]=σi2​+ϵ​x[i,j]−μi​​⋅γ[j]+β[j]

where:

μi=1N∑jx[i,j],σi2=1N∑j(x[i,j]−μi)2\mu_i = \frac{1}{N} \sum_j x[i, j], \qquad \sigma_i^2 = \frac{1}{N} \sum_j (x[i, j] - \mu_i)^2μi​=N1​j∑​x[i,j],σi2​=N1​j∑​(x[i,j]−μi​)2

This is the row-parallel pattern: one program per row, the entire row held in a single tile, statistics computed with in-register reductions, and normalized affine output stored in place. Computing variance from centered values avoids cancellation while keeping the row in registers.

Loading visualization...

Examples

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

Output: [[-1.3416, -0.4472, 0.4472, 1.3416]]

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

Output: [[-1.0, 3.0], [-1.0, 3.0]]

Hint 1

Each program owns one row. Form column offsets across the row and load with other=0.0\texttt{other=0.0}other=0.0 so masked lanes contribute zero to the mean reduction.

Hint 2

After computing the mean, form centered = tl.where(mask, x - mean, 0.0)\texttt{centered = tl.where(mask, x - mean, 0.0)}centered = tl.where(mask, x - mean, 0.0), then compute tl.sum(centered * centered, axis=0) / N\texttt{tl.sum(centered * centered, axis=0) / N}tl.sum(centered * centered, axis=0) / N. The explicit mask prevents padded lanes from contributing (−μ)2(-\mu)^2(−μ)2.

Hint 3

Multiply the centered values by tl.math.rsqrt(var+ϵ)\texttt{tl.math.rsqrt}(\text{var} + \epsilon)tl.math.rsqrt(var+ϵ) before applying γ\gammaγ and β\betaβ. Reuse the same column mask on the final store.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Compute the mean first, then compute variance from masked centered values using σ2=E[(x−μ)2]\sigma^2 = E[(x - \mu)^2]σ2=E[(x−μ)2]
  • Add ϵ\epsilonϵ inside the inverse square root for stability when a row has zero variance
  • Mask the column tail on every load and store, and zero masked lanes after centering
  • 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 in the row reductions.
Try Similar Problems
RMSNorm 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: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

LayerNorm Forward in Triton

LayerNorm Forward in Triton

Neural NetworksGPU Programming
Medium

Implement the forward pass of LayerNorm in Triton. For each row of the input matrix x∈RM×Nx \in \mathbb{R}^{M \times N}x∈RM×N, compute the mean and variance across the feature dimension, then normalize and apply a learned affine transform with vectors γ,β∈RN\gamma, \beta \in \mathbb{R}^{N}γ,β∈RN.

out[i,j]=x[i,j]−μiσi2+ϵ⋅γ[j]+β[j]\texttt{out}[i, j] = \frac{x[i, j] - \mu_i}{\sqrt{\sigma_i^2 + \epsilon}} \cdot \gamma[j] + \beta[j]out[i,j]=σi2​+ϵ​x[i,j]−μi​​⋅γ[j]+β[j]

where:

μi=1N∑jx[i,j],σi2=1N∑j(x[i,j]−μi)2\mu_i = \frac{1}{N} \sum_j x[i, j], \qquad \sigma_i^2 = \frac{1}{N} \sum_j (x[i, j] - \mu_i)^2μi​=N1​j∑​x[i,j],σi2​=N1​j∑​(x[i,j]−μi​)2

This is the row-parallel pattern: one program per row, the entire row held in a single tile, statistics computed with in-register reductions, and normalized affine output stored in place. Computing variance from centered values avoids cancellation while keeping the row in registers.

Loading visualization...

Examples

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

Output: [[-1.3416, -0.4472, 0.4472, 1.3416]]

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

Output: [[-1.0, 3.0], [-1.0, 3.0]]

Hint 1

Each program owns one row. Form column offsets across the row and load with other=0.0\texttt{other=0.0}other=0.0 so masked lanes contribute zero to the mean reduction.

Hint 2

After computing the mean, form centered = tl.where(mask, x - mean, 0.0)\texttt{centered = tl.where(mask, x - mean, 0.0)}centered = tl.where(mask, x - mean, 0.0), then compute tl.sum(centered * centered, axis=0) / N\texttt{tl.sum(centered * centered, axis=0) / N}tl.sum(centered * centered, axis=0) / N. The explicit mask prevents padded lanes from contributing (−μ)2(-\mu)^2(−μ)2.

Hint 3

Multiply the centered values by tl.math.rsqrt(var+ϵ)\texttt{tl.math.rsqrt}(\text{var} + \epsilon)tl.math.rsqrt(var+ϵ) before applying γ\gammaγ and β\betaβ. Reuse the same column mask on the final store.

Requirements

  • Use one Triton program per row, holding the full row in registers
  • Compute the mean first, then compute variance from masked centered values using σ2=E[(x−μ)2]\sigma^2 = E[(x - \mu)^2]σ2=E[(x−μ)2]
  • Add ϵ\epsilonϵ inside the inverse square root for stability when a row has zero variance
  • Mask the column tail on every load and store, and zero masked lanes after centering
  • 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 in the row reductions.
Try Similar Problems
RMSNorm 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: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1