Layer normalization normalizes across the feature dimension for each individual sample:
where and are the mean and variance computed over the features of a single input, and are learned scale and shift parameters.
LayerNorm vs BatchNorm:
| BatchNorm | LayerNorm | |
|---|---|---|
| Normalizes over | Batch dimension | Feature dimension |
| Depends on batch | Yes (problematic for small batches) | No |
| At inference | Uses running statistics | Same as training |
| Dominant in | CNNs | Transformers |
Why transformers use LayerNorm:
- Sequences have variable length → batch statistics across positions are meaningless
- No dependence on batch size → works with any batch, including batch size 1
- Stabilizes the residual stream by keeping activations on a consistent scale
Pre-Norm vs Post-Norm:
- Post-Norm (original transformer): — harder to train deep models
- Pre-Norm (GPT-2+, standard now): — more stable gradients, enables deeper models
RMSNorm — a simplified variant that skips the mean subtraction and the bias , dividing only by the root-mean-square:
Forcing unit RMS destroys learned scale, so the per-dimension gives it back: amplifies dimension , kills it. Cheaper than LayerNorm (no mean, no ) and equally stable in practice — used in LLaMA and other modern LLMs.
See also: Batch Normalization, Residual Connections, Self-Attention