- Key idea
- LayerNorm = (x − mean) ÷ std × γ + β; RMSNorm = x ÷ rms × γ; pre-norm keeps the residual path clean
- Real world
- Grading on a curve. A raw mark of 72 means little until you know the class average was 60 and the spread was 8. Converting every mark to “1.5 spreads above average” makes marks from a hard exam and an easy exam comparable.
- In an LLM
- GPT-2’s residual vector for “ is” has length 5 at the bottom and 430 at the top. Each layer reads it through a LayerNorm, so every layer sees numbers of the same scale, and the final norm shrinks it back to about 255 before the prediction.
Why normalise
The residual stream keeps growing: every layer adds to it. If each layer read the raw vector, the early layers would see vectors of length about 55 and the late ones vectors several times longer (in GPT-2 small, from about 55 to about 430: see the chart on the previous page), and every layer would need weights tuned to its own scale. Worse, during training the scale shifts as earlier layers change, so later layers would be chasing a moving target.
The fix is to rescale each token’s vector to a standard size before a layer reads it. The layer always sees numbers of roughly the same size, however long the stream has grown.
LayerNorm
takes one token’s vector with numbers and does three things:
- Subtract the mean. . After this the numbers average to zero.
- Divide by the standard deviation. . After this they have spread 1.
- Scale and shift. Multiply each number by a learned gain and add a learned bias .
The tiny (usually ) stops a division by zero. The gain and bias let the model put back any scale it actually needs, per dimension. Each token is normalised on its own: no information moves between tokens.
is the cheaper version that most models now use. It skips the mean and the bias:
Drag the bars and watch both at work:
Two things to try. Move the scale slider: the input bars grow or shrink, the output does not change at all. That is the point of normalising: the size of the input no longer matters, only its shape. Then move the shift slider: LayerNorm ignores it (it subtracts the mean), RMSNorm does not (it never removes the mean). Zhang and Sennrich, 2019 showed that the re-centring step barely matters in practice, so RMSNorm gets the same quality with fewer operations. Llama, Mistral, Qwen and Gemma all use it.
A class sits two exams. On the hard one the marks are 40, 48, 55, 61; on the easy one 70, 78, 85, 91. The raw numbers can’t be compared. Subtract each exam’s average and divide by its spread, and both become about −1.4, −0.4, +0.5, +1.3: the same students did equally well relative to their class. LayerNorm does this to the 768 numbers of a token’s vector, so every layer sees “how much above or below average is each dimension” rather than raw sizes.
At the last token of “The capital of France is”, GPT-2 small’s residual vector has length 55 after the first layer and 430 before the output. The LayerNorm in front of each layer turns both into vectors of the same length (the square root of 768, about 28, before the gain is applied). Without it, a layer reading the 430-long vector instead of the 55-long one would see numbers about 8 times bigger, so its attention scores (dot products of two such vectors) would come out roughly 60 times bigger, which would push its softmax into saturation.
Pre-norm or post-norm
There are two places to put the norm. The original Transformer (Vaswani et al., 2017) normalised after adding each sub-layer’s output: post-norm, . GPT-2 moved it to the start of each branch: , .
The difference looks small but matters a lot for deep models. In pre-norm the main path from the input to the output has nothing on it but additions, so the gradient flows straight down it (the identity path from the residual stream). In post-norm the gradient must pass through a LayerNorm at every sub-layer, and each one rescales it. Xiong et al., 2020 showed that post-norm gradients near the output are large at the start of training, which is why post-norm models need a slow learning-rate warm-up and still often diverge when they are deep.
Pre-norm has one side effect: since nothing on the main path normalises it, the stream grows with depth (you saw this in GPT-2). So pre-norm models add one final norm after the last block, before the output layer.
QK-norm
Some recent models (Gemma 3, OLMo 2, Qwen 3) add one more norm inside attention: they apply RMSNorm to every query and key vector before the dot product. This caps how large the attention scores can get, which stops a failure in large-scale training where a few scores grow without limit, softmax becomes one-hot, and the loss spikes. It is a small, cheap change: two extra norms per layer.
In code
def layer_norm(x, gamma, beta, eps=1e-5): # x: (..., d)
mu = x.mean(-1, keepdim=True)
var = ((x - mu) ** 2).mean(-1, keepdim=True) # biased variance
return (x - mu) / torch.sqrt(var + eps) * gamma + beta
def rms_norm(x, gamma, eps=1e-5):
return x / torch.sqrt((x ** 2).mean(-1, keepdim=True) + eps) * gammaAfter normalising (before ), a -dimensional vector has RMS 1, so its length is : about 27.7 for .
Parameters
Each norm has gains (and biases for LayerNorm). GPT-2 small has 25 LayerNorms × 2 × 768 = 38,400 parameters in norms, 0.03% of the model.
In papers
- LayerNorm: Ba, Kiros and Hinton, 2016.
- RMSNorm: Zhang and Sennrich, 2019.
- Pre-norm vs post-norm: Xiong et al., 2020, On Layer Normalization in the Transformer Architecture.
- QK-norm: Dehghani et al., 2023, Scaling Vision Transformers to 22 Billion Parameters; also used in OLMo 2.