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.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
The choice of normalization in a transformer block is one of those decisions that looks like personal preference until you trace what each option actually does at training and serving time. BatchNorm and LayerNorm are not interchangeable; the axis each one normalizes over has cascading consequences for variable-length sequences, autoregressive decoding, and the train versus serve statistical contract.
This card walks the math of each normalization, lays out the three specific failure modes BatchNorm has in a transformer, and explains why each wrong option in the multiple choice is wrong with concrete evidence.
The three normalizations side by side
All three normalizations operate on a tensor with batch, sequence-length, and model-width axes. They differ in which axes they pool over to compute the statistics.
BatchNorm. Pool over the batch and sequence axes to get one mean and one variance per channel. The statistics tensor has one scalar per channel, computed by averaging over the entire batch and sequence. Each activation is then whitened by subtracting the per-channel mean and dividing by the per-channel standard deviation, then scaled and shifted by per-channel learned parameters.
LayerNorm. Pool over the model-width axis only. The statistics tensor carries one scalar per token, computed from that token's own d_model features. Each activation is whitened using the per-token mean and variance, then scaled and shifted by per-channel learned parameters. Notice the statistics never cross the batch or sequence dimension.
RMSNorm. Same axis as LayerNorm (per-token along d_model), but with two simplifications. First, drop the mean subtraction: just divide by the root mean square of the features. Second, drop the learned bias. The result is a single learned per-channel scale, and the kernel is about 10-20% cheaper than LayerNorm at no quality cost. Modern LLMs (Llama, Mistral, Qwen, DeepSeek) all use RMSNorm.
The load-bearing observation is the axis. LayerNorm and RMSNorm normalize WITHIN a token, using only that token's features. BatchNorm normalizes ACROSS the batch and sequence, using statistics pooled from every other token in the batch.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
| 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.
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?
Two reasons. First, dropping mean subtraction and the bias makes the kernel ~10-20% cheaper at no measurable quality cost in head to head ablations. Second, the bias term was empirically rarely useful in transformer LayerNorms (often near zero after training). Llama popularized RMSNorm and the rest of the open-weight ecosystem followed. Strict Pareto improvement: same quality, lower cost.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases 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.
60 second bullets to scan on the way to the call.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.