Batch norm vs layer norm: how does each work, and why do transformers use layer norm?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Both normalize activations to zero mean and unit variance, then apply a learned scale γ and shift β. The difference is the axis. BatchNorm normalizes each feature/channel across the batch - statistics are computed over the N examples in the mini-batch. LayerNorm normalizes across the feature dimension within a single example (per token, in a transformer) - no dependence on other examples at all.
Full worked answer with diagram
Both normalize activations to zero mean and unit variance, then apply a learned scale γ and shift β. The difference is the axis. BatchNorm normalizes each feature/channel across the batch - statistics are computed over the N examples in the mini-batch. LayerNorm normalizes across the feature dimension within a single example (per token, in a transformer) - no dependence on other examples at all.
Why BN is wrong for transformers/LLMs:
- Variable-length sequences and padding make per-position batch statistics ill-defined and noisy.
- Batch dependence: BN's quality degrades at small batch sizes, and in distributed training the per-device batch may be tiny (SyncBatchNorm exists but adds communication).
- Train/inference mismatch: BN needs running statistics for inference - a persistent source of train/eval bugs - and is awkward for autoregressive decoding, where you generate one token at a time with effective batch size 1 per position.
- LayerNorm has none of these issues: each token normalizes itself, identical math at train and inference, any batch size.
Modern refinements worth knowing: RMSNorm (Llama, Mistral, most current LLMs) drops mean-centring and just divides by the root-mean-square, with a learned gain - cheaper, empirically as good. Placement matters more than flavour: the original transformer used post-norm (normalize after the residual add), which is unstable at depth and demands careful warmup; pre-norm (normalize inside the branch, before attention/MLP) keeps an unimpeded identity path through the residual stream and trains stably - the dominant default since GPT-2. Recent models add extra norms on top: QK-norm on attention queries/keys to prevent attention-logit blowups at scale, and a second norm on each sublayer's output before the residual add (Gemma's "sandwich" norm, OLMo 2's reordered norm) to keep the residual stream's magnitude bounded.
Worth sketching. Naming the axis each one reduces over is the answer; everything else on the diagram follows from that single choice.
flowchart TD
A["Activations:<br/>batch x tokens x features"] --> B["BatchNorm: statistics<br/>along the batch axis"]
A --> C["LayerNorm: statistics along<br/>features, one token at a time"]
B --> D["Needs running statistics<br/>at inference"]
B --> E["Degrades at small batch,<br/>padding, variable length"]
C --> F["Identical math at<br/>train and inference"]
C --> G["RMSNorm: drop the mean,<br/>divide by root mean square"]
What a good answer covers
- Both normalize activations to zero mean and unit variance, then apply a learned scale γ and shift β
- BatchNorm normalizes each feature/channel across the batch - statistics are computed over the N examples…
- LayerNorm normalizes across the feature dimension within a single example (per token, in a transformer)…
- Why BN is wrong for transformers/LLMs
- Modern refinements worth knowing
Key terms: BatchNorm · LayerNorm · Variable-length sequences and padding · Batch dependence · Train/inference mismatch · RMSNorm · Placement matters more than flavour · post-norm · pre-norm
In the glossary: BatchNorm · LayerNorm · RMSNorm · Warmup · Pre-norm · Residual stream
Interviewer follow-ups to expect
Why does BN generally underperform even in encoder-only transformers where decoding isn't an issue?
What does RMSNorm remove and why is that OK?
Modern refinements worth knowing: RMSNorm (Llama, Mistral, most current LLMs) drops mean-centring and just divides by the root-mean-square, with a learned gain - cheaper, empirically as good. Placement matters more than flavour: the original transformer used post-norm (normalize after the residual add), which is unstable at depth and demands careful warmup; pre-norm (normalize inside the branch, before attention/MLP) keeps an unimpeded identity path through the residual stream and trains stably - the dominant default since GPT-2. Recent models add extra norms on top: QK-norm on attention queries/keys to prevent attention-logit blowups at scale, and a second norm on each sublayer's output before the residual add (Gemma's "sandwich" norm, OLMo 2's reordered norm) to keep the residual stream's magnitude bounded.
Explain mechanically why post-norm at depth is unstable.
Modern refinements worth knowing: RMSNorm (Llama, Mistral, most current LLMs) drops mean-centring and just divides by the root-mean-square, with a learned gain - cheaper, empirically as good. Placement matters more than flavour: the original transformer used post-norm (normalize after the residual add), which is unstable at depth and demands careful warmup; pre-norm (normalize inside the branch, before attention/MLP) keeps an unimpeded identity path through the residual stream and trains stably - the dominant default since GPT-2. Recent models add extra norms on top: QK-norm on attention queries/keys to prevent attention-logit blowups at scale, and a second norm on each sublayer's output before the residual add (Gemma's "sandwich" norm, OLMo 2's reordered norm) to keep the residual stream's magnitude bounded.
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep