Why does the transformer block use LayerNorm or RMSNorm instead of BatchNorm?
BatchNorm's statistics depend on the batch axis, which is broken by padding and by batch-size-1 inference. LayerNorm normalizes per token along d_model and is immune to both.
Picture a classroom where you grade each student by comparing them to the rest of the class that day. If the class is empty, you cannot grade anyone. If half the class is absent and the seats are filled with cardboard cutouts to keep the room looking full, the cutouts drag the class average down and now every real student looks above average. That is BatchNorm in a transformer: the batch is the 'class', padded positions are the cutouts, and a batch of one at inference time is the empty room. LayerNorm grades each student by comparing them to their own internal features (height, weight, age, scores), never looking at the rest of the class. It works with one student, with full classes, with cardboard cutouts, with anything. That self-contained grading is exactly what an autoregressive language model needs because it generates one new token at a time and the batch composition changes constantly.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: contrast the axis BatchNorm normalizes over with the axis LayerNorm normalizes over, give the three concrete failure modes (variable-length padding, batch=1 inference, cross-example leakage), and refute each wrong option with one sentence.
| Property | BatchNorm | LayerNorm | RMSNorm |
|---|---|---|---|
| Axis normalized over | Batch + spatial/sequence | d_model (per token) | d_model (per token) |
| Subtracts mean? | Yes | Yes | No |
| Has learned bias (beta)? | Yes | Yes | No |
| Robust to variable-length sequences? | No (padding contaminates) | Yes | Yes |
| Robust to batch size 1? | No (uses running stats) | Yes | Yes |
| Train-time vs inference-time stats? | Different (EMA running stats) | Identical | Identical |
| Typical use | Vision CNNs | Transformers, RNNs | Modern LLM transformers |
Real products, models, and research that use this idea.
- Llama, Mistral, Qwen, DeepSeek all use RMSNorm (the cheaper LayerNorm cousin) and would refuse to ship BatchNorm in their attention blocks for exactly the variable-length and batch-1 inference reasons above.
- BERT and the original 2017 transformer use LayerNorm. The decision to avoid BatchNorm predates the modern decoder-only era by years.
- ResNet50 (the canonical CNN backbone) uses BatchNorm with residual connections in every bottleneck block. This directly refutes the 'BatchNorm cannot represent residuals' option.
- vLLM and TGI inference stacks routinely serve a single user's request at batch=1 (especially in chat completion mode). LayerNorm and RMSNorm are invariant to this; BatchNorm would be running on stale training-time statistics.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy did the field move from LayerNorm to RMSNorm in modern LLMs?
QIf you had to use BatchNorm in a transformer, how would you make it work?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Calling BatchNorm 'too expensive' for transformers. Cost is not the issue. The issue is that BatchNorm's statistics are contaminated by padding and break entirely at batch size 1 during autoregressive inference.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.