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 really 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, and the small variations across them (pre-norm vs post-norm, LayerNorm vs RMSNorm) explain a surprising fraction of empirical performance differences across model families.
This deep dive starts with the formula, walks through why the residual is load-bearing for trainability via the gradient-flow argument, explains the pre-norm vs post-norm split and why modern LLMs converged on pre-norm, distinguishes LayerNorm from RMSNorm, and closes with the mechanistic-interpretability framing that treats the residual stream as the architectural backbone of the entire transformer.
The goal: by the end you should be able to recite the wrapper pattern, derive why it is necessary, and explain the design choices that distinguish 2017 Vaswani transformers from 2026 Llama 4.
The wrapper formula and what each piece does
The canonical modern transformer block, in pseudocode:
def transformer_block(x):
x = x + attention(rmsnorm(x))
x = x + ffn(rmsnorm(x))
return x
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), and the dot products inside attention land in regimes where softmax saturates. The norm keeps the scale predictable so attention sees inputs at a roughly fixed magnitude regardless of which layer we are at.
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 80+ blocks.
- Mistral Large 2 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, which expanded has a leading I (the identity path) ensuring 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.