Layer Normalization & RMSNorm

Derivation of layer normalization from the perspective of internal covariate shift, comparison with batch normalization, the RMSNorm simplification, and why transformers use LayerNorm while CNNs use BatchNorm.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Batch Normalization Revisited
  5. Layer Normalization
  6. RMSNorm
  7. Pre-Norm vs Post-Norm
  8. Gradient Analysis
  9. Common Pitfalls
  10. Summary
  11. Exercises

Learning Objectives

  1. Contrast batch normalization (statistics over batch) with layer normalization (statistics over features).
  2. Derive the LayerNorm forward pass and its Jacobian.
  3. Derive RMSNorm as a simplified LayerNorm without mean centering.
  4. Explain why transformers prefer LayerNorm over BatchNorm.
  5. Analyze Pre-Norm vs Post-Norm placement in residual blocks.

Notation

  • hRd\mathbf{h} \in \mathbb{R}^d — hidden representation (single token/sample)
  • μ=1di=1dhi\mu = \frac{1}{d}\sum_{i=1}^d h_i — mean over features
  • σ2=1di=1d(hiμ)2\sigma^2 = \frac{1}{d}\sum_{i=1}^d(h_i - \mu)^2 — variance over features
  • γ,βRd\gamma, \beta \in \mathbb{R}^d — learnable scale and shift parameters
  • RMS(h)=1dihi2\text{RMS}(\mathbf{h}) = \sqrt{\frac{1}{d}\sum_i h_i^2} — root mean square

Core Intuition

Normalization stabilizes training by preventing activations from growing or shrinking across layers. Batch normalization normalizes over the batch dimension (requiring batch statistics), which is problematic for variable-length sequences and small batches. Layer normalization normalizes over the feature dimension of a single sample, making it independent of batch size and applicable to recurrent/transformer architectures.

Layer Normalization

Before (raw activations)h₁h₂h₃h₄h₅h₆Norm axisAcross features (single sample) — unlike BatchNorm across batchAfter LayerNorm
μ = 0.88σ = 1.54
γ scale
1.00
β shift
0.00
BeforeAfterNorm axis
Explore: LayerNorm normalizes across features within one sample (dashed bracket). γ and β let the network recover scale and shift after normalization.

Batch Normalization Revisited

For a mini-batch of BB samples, BatchNorm computes statistics per feature:

h^i=hiμB(i)(σB(i))2+ϵ,μB(i)=1Bb=1Bhi(b).(1)\hat{h}_i = \frac{h_i - \mu_B^{(i)}}{\sqrt{(\sigma_B^{(i)})^2 + \epsilon}}, \quad \mu_B^{(i)} = \frac{1}{B}\sum_{b=1}^B h_i^{(b)}. \tag{1}

Limitations: Depends on batch statistics → requires running averages at inference; fails for batch size 1; not applicable to autoregressive models where future tokens shouldn't influence current normalization.


Layer Normalization

LayerNorm computes statistics over the feature dimension of a single sample:

μ=1di=1dhi,σ2=1di=1d(hiμ)2.(2)\mu = \frac{1}{d}\sum_{i=1}^d h_i, \qquad \sigma^2 = \frac{1}{d}\sum_{i=1}^d(h_i - \mu)^2. \tag{2} LayerNorm(h)=γhμσ2+ϵ+β.(3)\boxed{\text{LayerNorm}(\mathbf{h}) = \gamma \odot \frac{\mathbf{h} - \mu}{\sqrt{\sigma^2 + \epsilon}} + \beta.} \tag{3}

Properties:

  • Independent of batch size and other samples.
  • Same computation at training and inference (no running statistics).
  • Learnable affine parameters γ,β\gamma, \beta restore representational power.

Jacobian. Let h^=(hμ1)/σ2+ϵ\hat{\mathbf{h}} = (\mathbf{h} - \mu\mathbf{1})/\sqrt{\sigma^2 + \epsilon}. Then:

h^ihj=1σ2+ϵ(δij1dh^ih^jd).(4)\frac{\partial \hat{h}_i}{\partial h_j} = \frac{1}{\sqrt{\sigma^2+\epsilon}}\left(\delta_{ij} - \frac{1}{d} - \frac{\hat{h}_i\hat{h}_j}{d}\right). \tag{4}

RMSNorm

RMSNorm (Zhang & Sennrich, 2019) removes the mean centering step:

RMS(h)=1di=1dhi2.(5)\text{RMS}(\mathbf{h}) = \sqrt{\frac{1}{d}\sum_{i=1}^d h_i^2}. \tag{5} RMSNorm(h)=γhRMS(h).(6)\boxed{\text{RMSNorm}(\mathbf{h}) = \gamma \odot \frac{\mathbf{h}}{\text{RMS}(\mathbf{h})}.} \tag{6}

Motivation: Empirically, the re-centering in LayerNorm contributes little to performance but adds computation. RMSNorm is ~10–15% faster.

Used in: LLaMA, Gemma, and most modern LLMs.


Pre-Norm vs Post-Norm

In a transformer block with residual connection:

Post-Norm (original transformer): h+Norm(Attn(h))\mathbf{h} + \text{Norm}(\text{Attn}(\mathbf{h})) — normalizes after the sublayer.

Pre-Norm (GPT-2 onwards): h+Attn(Norm(h))\mathbf{h} + \text{Attn}(\text{Norm}(\mathbf{h})) — normalizes before the sublayer.

Analysis. Pre-Norm produces better-behaved gradients:

  • The gradient through the skip connection is unmodified (h(L)/h(l)\partial h^{(L)}/\partial h^{(l)} includes a direct path of magnitude 1).
  • Post-Norm can cause gradient explosion in very deep transformers without careful warmup.
  • Pre-Norm enables stable training without learning rate warmup.

Gradient Analysis

For Pre-Norm with LL blocks:

Lh(0)=Lh(L)+l=1LLh(L)k=l+1L(I+Jk)Jlblock,(7)\frac{\partial L}{\partial \mathbf{h}^{(0)}} = \frac{\partial L}{\partial \mathbf{h}^{(L)}} + \sum_{l=1}^L \frac{\partial L}{\partial \mathbf{h}^{(L)}} \prod_{k=l+1}^L (\mathbf{I} + \mathbf{J}_k) \cdot \mathbf{J}_l^{\text{block}}, \tag{7}

where Jk\mathbf{J}_k is the Jacobian of block kk. The leading term L/h(L)\partial L/\partial \mathbf{h}^{(L)} provides a direct gradient path, preventing vanishing.


Common Pitfalls

Pitfall 1. Using BatchNorm in transformers for NLP. Variable sequence lengths and autoregressive masking make batch statistics unreliable.

Pitfall 2. Forgetting ϵ\epsilon in the denominator. Without it, zero-variance features cause division by zero.

Pitfall 3. Applying LayerNorm to the wrong dimension. For a tensor of shape (B,T,d)(B, T, d), normalize over the last dimension dd (features), not TT (sequence) or BB (batch).


Summary

  • BatchNorm normalizes over the batch → dependent on other samples, problematic for inference.
  • LayerNorm normalizes over features of a single sample → independent, stable, used in transformers.
  • RMSNorm drops mean centering → faster, empirically equivalent, used in modern LLMs.
  • Pre-Norm placement enables stable deep training by preserving gradient flow through skip connections.

Exercises

Exercise 1. Derive the Jacobian of LayerNorm (equation 4).

Exercise 2. Show that LayerNorm is invariant to uniform scaling and shifting of its input: LN(ah+b1)=LN(h)\text{LN}(a\mathbf{h} + b\mathbf{1}) = \text{LN}(\mathbf{h}) (ignoring γ,β\gamma, \beta).

Exercise 3. Prove that RMSNorm projects inputs onto the unit sphere of radius d\sqrt{d}, scaled by γ\gamma.

Exercise 4. For a 2-layer Post-Norm transformer, derive the gradient L/h(0)\partial L/\partial \mathbf{h}^{(0)} and identify conditions for instability.

Exercise 5. Compare the computational cost (FLOPs) of LayerNorm vs RMSNorm for a vector of dimension dd.