Why does the transformer block use LayerNorm or RMSNorm instead of BatchNorm?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.