Every transformer block wraps attention in two things: a normalization (LayerNorm or RMSNorm) on the input and a residual skip that adds the attention output back to the original input.
Picture a long assembly line where each station does something to a product, but the product is also carried past every station on a conveyor belt. Each station gets to see the product, do its little adjustment, and then add that adjustment to whatever was on the belt. If the station messes up, the original product still arrives at the end. That conveyor belt is the residual stream. And before any station touches the product, an inspector standardizes its size so the station gets predictable input. That is the normalization. Both wrappers exist because without them deep stacks of stations would either break the product completely or fail to train at all.
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.
Every transformer block wraps its attention sub-layer in two pieces of architectural plumbing: a normalization and a residual connection. These are not optional decorations. They are the reason deep transformers train at all. The small variations across them (pre-norm vs post-norm, LayerNorm vs RMSNorm) explain a surprising fraction of empirical performance differences across model families.
The wrapper formula and what each piece does
The standard modern transformer block, in pseudocode:
def transformer_block(x):
x = x + attention(rmsnorm(x))
x = x + ffn(rmsnorm(x))
return x
There are two sub-layers (attention and FFN), each wrapped identically: normalization on the input, sub-layer applied, result added back to the original via the residual skip.
What the normalization does
LayerNorm or RMSNorm standardizes the per token activation vector. Without it, the activation scale can drift across layers (some heads push it up, others pull it down). The dot products inside attention then land in regimes where softmax saturates. The norm keeps the scale predictable so attention sees inputs at a roughly fixed magnitude regardless of layer depth.
What the residual does
The x + ... is the skip connection. The original input passes through unchanged; the sub-layer's contribution is ADDED to it rather than replacing it. The output preserves the input, with the sub-layer's delta layered on top.
This is the same idea as ResNet from 2016 vision: an identity path through the depth of the network that guarantees gradient flow back to early layers.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Llama 4 Maverick uses pre-norm RMSNorm wrapping attention and FFN sub-layers across all 48 blocks.
- Mistral 7B ships pre-norm RMSNorm with sliding-window attention inside the wrapper.
What an interviewer would ask next. Try answering before peeking at the approach.
QWalk through how the gradient flows through a residual connection and explain why this prevents vanishing gradients.
For y = x + F(x), the Jacobian dy/dx = I + dF/dx. Stacking blocks gives a product of (I + dF_i/dx) terms. Expanded, this product has a leading I (the identity path). That term ensures the gradient cannot vanish to zero across depth, regardless of what F does.
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.
Forgetting the residual or treating it as 'just nice to have'. Without the residual, gradients vanish across stacked layers and the network does not train.
60 second bullets to scan on the way to the call.
Name both wrappers: normalization and residual
Difference between pre-norm and post-norm
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.