Triton LayerNorm Forward Kernel
Problem Statement
Implement the forward pass of LayerNorm over the last dimension of a (M, N) tensor: subtract the row mean, divide by the row standard deviation, then scale by weight and shift by bias.
Background
Per row: mean = sum(x)/N, var = sum((x-mean)^2)/N, rstd = 1/sqrt(var + eps), out = (x-mean)rstdweight + bias. Mask invalid columns to 0 before the variance sum.
Your Task
Implement layernorm_kernel and run(M=64, N=256, eps=1e-5) comparing to torch.nn.functional.layer_norm.
How it is tested
Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.
Example:
M = 64, N = 256, eps = 1e-5
True
- The input values are used to launch the
layernorm_kernelfunction on a GPU, which implements the forward pass of LayerNorm over the last dimension of a(M, N)tensor. - For each row, the
meanis calculated as mean=โ(x)/N, and thevaris calculated as var=โ((xโmean)2)/N, masking invalid columns to 0 before the variance sum. - The row standard deviation
rstdis then calculated as rstd=1/var+epsโ, and the outputoutis computed as out=(xโmean)โrstdโweight+bias. - The output of the
layernorm_kernelfunction is compared to the output oftorch.nn.functional.layer_normusingtorch.allclose, which returnsTrueif the two outputs are close enough, indicating that the implementation is correct.
Constraints:
- Compute mean and variance per row using tl.sum
- rstd = 1 / tl.sqrt(var + eps)
- out = (x - mean) * rstd * weight + bias
Background Knowledge
Introduction to Layer Normalization
Layer normalization is a technique used in deep learning to normalize the inputs of each layer. It is similar to batch normalization, but instead of normalizing over the batch dimension, it normalizes over the feature dimension. This helps to reduce the effect of internal covariate shift, which can speed up training and improve the stability of the model.
Mathematical Formulation
The mathematical formulation of layer normalization involves calculating the mean and variance of the input data over the feature dimension. The mean is calculated as mean=N1โโi=1Nโxiโ, where xiโ is the ith feature of the input data. The variance is calculated as var=N1โโi=1Nโ(xiโโmean)2. The normalized data is then calculated as out=var+ฯตโxโmeanโโ weight+bias, where ฯต is a small value added for numerical stability.
Importance of Epsilon
The ฯต value is added to the variance to prevent division by zero. This is especially important when the variance is zero, which can happen when the input data is constant. The ฯต value is typically set to a small value, such as 1eโ5, to ensure that the division is stable.
Algorithm/Approach
The general approach to solving this problem involves the following steps:
- Calculate the mean of the input data over the feature dimension
- Calculate the variance of the input data over the feature dimension
- Calculate the normalized data using the mean and variance
- Scale and shift the normalized data using the weight and bias
This approach can be parallelized using a GPU, which can significantly speed up the computation.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.