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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Batch Normalization Revisited
- Layer Normalization
- RMSNorm
- Pre-Norm vs Post-Norm
- Gradient Analysis
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Contrast batch normalization (statistics over batch) with layer normalization (statistics over features).
- Derive the LayerNorm forward pass and its Jacobian.
- Derive RMSNorm as a simplified LayerNorm without mean centering.
- Explain why transformers prefer LayerNorm over BatchNorm.
- Analyze Pre-Norm vs Post-Norm placement in residual blocks.
Notation
- — hidden representation (single token/sample)
- — mean over features
- — variance over features
- — learnable scale and shift parameters
- — 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
Batch Normalization Revisited
For a mini-batch of samples, BatchNorm computes statistics per feature:
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:
Properties:
- Independent of batch size and other samples.
- Same computation at training and inference (no running statistics).
- Learnable affine parameters restore representational power.
Jacobian. Let . Then:
RMSNorm
RMSNorm (Zhang & Sennrich, 2019) removes the mean centering step:
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): — normalizes after the sublayer.
Pre-Norm (GPT-2 onwards): — normalizes before the sublayer.
Analysis. Pre-Norm produces better-behaved gradients:
- The gradient through the skip connection is unmodified ( 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 blocks:
where is the Jacobian of block . The leading term 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 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 , normalize over the last dimension (features), not (sequence) or (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: (ignoring ).
Exercise 3. Prove that RMSNorm projects inputs onto the unit sphere of radius , scaled by .
Exercise 4. For a 2-layer Post-Norm transformer, derive the gradient and identify conditions for instability.
Exercise 5. Compare the computational cost (FLOPs) of LayerNorm vs RMSNorm for a vector of dimension .