AI / ML Theory
Chapter 3 · 20 min

3.8 — Layer Normalization and RMSNorm

Goal: Define LayerNorm, see why it normalises across features per token rather than across the batch, derive the RMSNorm simplification used by modern LLMs, and understand the pre-norm placement.

1. Why this session exists

Every modern Transformer uses LayerNorm, not BatchNorm. RMSNorm — a stripped-down LayerNorm — is the version in LLaMA, Mistral, and most LLMs from 2023 onward. The reason is structural: BN's dependence on the batch breaks down for variable-length sequences and small per-GPU batches, both of which are the norm in Transformer training. LayerNorm sidesteps the issue by normalising across the feature dimension for each individual token, with no batch coupling at all.

Past this point, every architecture choice we make for LLMs builds on LayerNorm / RMSNorm. (If you want a rigorous treatment of BatchNorm first — useful context but not required here — see Appendix 3.C.)[3]


2. The LayerNorm operation

Take a single hidden vector x∈Rdx \in \mathbb{R}^d (typically: the activation at one token position, in a single example, at one layer). Compute the per-vector mean and variance:

μ=1d∑k=1dxk,σ2=1d∑k=1d(xk−μ)2.\mu = \frac{1}{d} \sum_{k=1}^d x_k, \qquad \sigma^2 = \frac{1}{d} \sum_{k=1}^d (x_k - \mu)^2.

Normalise:

x^k=xk−μσ2+ε,\hat x_k = \frac{x_k - \mu}{\sqrt{\sigma^2 + \varepsilon}},

then apply a learnable per-coordinate affine:

 yk=γk x^k+βk. \boxed{\ y_k = \gamma_k\, \hat x_k + \beta_k.\ }

The trainable parameters are γ,β∈Rd\gamma, \beta \in \mathbb{R}^d, one (γ,β)(\gamma, \beta) pair per feature. The statistics μ,σ2\mu, \sigma^2 are computed per token by averaging over the dd features of that token's hidden vector.

That's it. There's no batch in the formula. Same operation at train and inference time. No running EMAs needed.


3. The single picture: BN vs LN

Take a 3-D activation tensor A∈RB×T×dA \in \mathbb{R}^{B \times T \times d} — batch ×\times sequence-length ×\times hidden-dim. The two normalisations operate along different axes:

  • BatchNorm: compute μ,σ2\mu, \sigma^2 for each feature coordinate, averaging over B×TB \times T (batch and time together) → one (μ,σ2)(\mu, \sigma^2) per of the dd features. The normalised value at position (b,t,k)(b, t, k) uses statistics that depend on every (b′,t′)(b', t') for that kk.
  • LayerNorm: compute μ,σ2\mu, \sigma^2 for each position, averaging over the dd features → one (μ,σ2)(\mu, \sigma^2) per of the B×TB \times T tokens. The normalised value at (b,t,k)(b, t, k) uses statistics that depend only on (b,t)(b, t).

Same trainable γ,β∈Rd\gamma, \beta \in \mathbb{R}^d in both cases. Different axis being averaged.


4. Why LN beats BN for Transformers

Three reasons, in order of importance:

(a) Variable sequence length. A Transformer batch contains sequences of different lengths, padded. BN would compute statistics over the padded tokens, which are garbage. LN computes statistics per token, so each token's normalisation is unaffected by what's in the rest of the batch.

(b) Small effective batches. Cutting-edge LLM training distributes a global batch of, say, 4M tokens across thousands of GPUs. Per-GPU batch size in tokens-per-update can be tiny (1–4 sequences). BN's μB,σB2\mu_\mathcal{B}, \sigma_\mathcal{B}^2 are very noisy at small BB, and the EMA estimates at inference get correspondingly noisy. LN doesn't see the batch dimension at all — it works identically at B=1B = 1 as at B=1000B = 1000.

(c) Causality. In autoregressive language modelling, the prediction at token tt may only depend on tokens 1,…,t1, \dots, t. BN's per-feature mean involves all time steps, so token tt's normalisation depends on tokens >t> t — a leak from the future. LN's per-token statistic is purely local in time and respects causality automatically.

For these reasons, every Transformer (encoder, decoder, encoder–decoder) since Attention Is All You Need uses LayerNorm.


5. Backward pass

Like BN, LN's backward couples the normalised entries — but it couples them along the feature axis (one token's dd features) rather than the batch axis. The structure of the formula is identical with BB replaced by dd and the sum over the batch replaced by a sum over features:

 ∂L∂xk=1d⋅σ2+ε[d⋅∂L∂x^k−∑j=1d∂L∂x^j−x^k∑j=1dx^j⋅∂L∂x^j]. \boxed{\ \frac{\partial L}{\partial x_k} = \frac{1}{d \cdot \sqrt{\sigma^2 + \varepsilon}}\left[d \cdot \frac{\partial L}{\partial \hat x_k} - \sum_{j=1}^d \frac{\partial L}{\partial \hat x_j} - \hat x_k \sum_{j=1}^d \hat x_j \cdot \frac{\partial L}{\partial \hat x_j}\right]. \ }

The sum is over features of a single token. This means LN's backward parallelises beautifully across tokens (no cross-batch sum), which is why it scales cleanly on multi-GPU systems.


6. RMSNorm: the modern stripped-down version

Zhang & Sennrich (2019)[2] noticed that the mean-centring of LN can be removed without hurting performance, leaving just the rescaling by RMS:

 RMSNorm⁡(x)k=γk⋅xk1d∑j=1dxj2+ε. \boxed{\ \operatorname{RMSNorm}(x)_k = \gamma_k \cdot \frac{x_k}{\sqrt{\tfrac{1}{d}\sum_{j=1}^d x_j^2 + \varepsilon}}. \ }

No μ\mu, no β\beta. Just a per-vector rescaling by the root-mean-square of the features. Saves one mean computation, one subtraction, and one bias parameter per coordinate — about 7% of the parameters compared to LN.

LLaMA 1/2/3, Mistral, Qwen, Gemini, Claude all use RMSNorm. The empirical case for it is that it trains slightly faster than LN with no quality regression. The theoretical case (Zhang & Sennrich): re-centring is doing little useful work in a deep network where the activations are already roughly centred by the previous layer's normalisation; the rescaling is what matters.

For the rest of this course, when we say "LayerNorm" we mean the LN of Ba et al.; when we discuss modern LLM implementations specifically we will use RMSNorm.


7. Pre-norm vs post-norm

Where do you put the LN inside a Transformer block? Two conventions:

  • Post-norm (original Transformer, Attention Is All You Need): y=LN⁡(x+f(x))y = \operatorname{LN}(x + f(x)). Apply the residual, then normalise.
  • Pre-norm (GPT-2 and everything since): y=x+f(LN⁡(x))y = x + f(\operatorname{LN}(x)). Normalise the input to the sublayer, then add the residual.

Pre-norm is now standard. The reason is that pre-norm leaves a clean additive residual stream with no normalisation between layers — gradients flow through the residual with no bottleneck. Post-norm sandwiches each residual addition in an LN, which empirically makes very deep training less stable.

We'll do this in detail in Chapter 6 (Transformers). For now: modern Transformers are pre-norm + RMSNorm.


8. The whole course's normalisation menu

For reference, the four normalisations we will eventually see in code:

| Name | Average over | Used in | |---|---|---| | BatchNorm | batch ×\times spatial | ConvNets (ResNet, EfficientNet) | | LayerNorm | features (per token) | Transformers (BERT, GPT-2/3, T5) | | RMSNorm | features (per token), no mean | Modern LLMs (LLaMA, Mistral, Gemini) | | GroupNorm | groups of features | Vision, when batch is too small for BN |

Pick the one whose averaging axis is independent of the dimension you don't have stable statistics for. Transformers don't have stable batch statistics → average over features. ConvNets do have stable batch statistics → average over batch. The right normalisation falls out of the data structure.


9. Takeaways

  • LayerNorm normalises per token, across the feature dimension. No batch coupling.
  • Same operation at train and test time; no EMAs needed.
  • Why Transformers use it: variable sequence length, small effective batches, causal correctness.
  • RMSNorm = LN without the mean-centring; what every recent LLM uses.
  • Pre-norm placement (LN⁡(x)\operatorname{LN}(x) inside the sublayer, residual outside) is the modern convention.
  • Backward: same structure as BN's but summed over features instead of batch.

10. End-of-chapter recap

This chapter built the optimiser and regulariser stack used by every modern LLM training run. Main path:

  • 3.1 SGD with mini-batches gives an unbiased gradient estimator; variance scales as Σ/B\Sigma/B.
  • 3.2 EMAs give low-variance estimates with bias correction 1/(1−βt)1/(1-\beta^t).
  • 3.3 Momentum is just an EMA of gradients — Polyak's κ\sqrt{\kappa} speedup.
  • 3.4 AdaGrad / RMSProp = (cumulative or EMA) of g2g^2, used as a per-parameter rescaling.
  • 3.5 Adam = momentum + RMSProp + bias correction.
  • 3.6 AdamW = Adam with decoupled weight decay (every LLM uses this).
  • 3.7 Schedule = linear warmup + cosine decay (or WSD for extendability) + global-norm gradient clipping at c=1.0c = 1.0.
  • 3.8 LayerNorm and RMSNorm = per-token mean/var (or RMS) normalisation over features; the Transformer choice.

Optional appendices, for cultural completeness — none block Chapters 4–12:

  • 3.A L2 weight decay = Gaussian prior MAP. λ=1/τ2\lambda = 1/\tau^2. The Bayesian reading of the regulariser already used in 3.6.
  • 3.B Dropout = approximate ensemble of 2N2^N sub-networks; 1/(1−p)1/(1-p) scaling. Rare in modern LLM pre-training (dropout = 0); still used in fine-tuning small models.
  • 3.C BatchNorm = per-feature mean/var normalisation over the batch. Standard in CNNs; not used in Transformers (LN/RMSNorm wins for the reasons in §4 above).

Chapter 4 will use the main-path stack to revisit CNNs and then bridge to sequences (Chapter 5) and attention (Chapter 6).


Self-test

(a) Write the LN operation in NumPy as a one-line function def layernorm(x, gamma, beta, eps=1e-5). Verify the output has zero mean and unit variance along its last axis when γ=1,β=0\gamma = 1, \beta = 0.

(b) Explain in one sentence each why BN fails for: (i) variable-length padded sequences; (ii) B=1B = 1; (iii) the autoregressive prediction at token tt.

(c) Show that RMSNorm with γ=1\gamma = 1 applied to x∼N(0,Id)x \sim \mathcal{N}(0, I_d) produces a vector with squared-norm →d\to d as d→∞d \to \infty (i.e., it normalises to a length-d\sqrt{d} vector, not unit norm). What does this say about how the magnitude of activations propagates through stacked RMSNorms?

(d) A LayerNorm has d=4096d = 4096 features. How many learnable parameters does it have? Compared to a Linear(4096, 4096) it precedes, what fraction of the weights is in the LN?


References

  1. [1]

    Ba, J. L., Kiros, J. R., & Hinton, G. E. (2016). "Layer normalization". arXiv:1607.06450 — LayerNorm (3.8).

    ↩
  2. [2]

    Zhang, B., & Sennrich, R. (2019). "Root mean square layer normalization". NeurIPS. arXiv:1910.07467 — RMSNorm (3.8).

    ↩
  3. [3]

    Wu, Y., & He, K. (2018). "Group normalization". ECCV. arXiv:1803.08494 — GroupNorm (mentioned in 3.8).

    ↩