The attention block adds its W_O output back into the residual stream: x_out = x_in + W_O(concat(heads)). The skip connection keeps deep training stable.
Imagine a long whiteboard where every meeting in a building adds a few notes without erasing what was already there. Each meeting reads the current state, decides what to contribute, and writes those new notes alongside the old ones. The whiteboard never gets wiped. By the end of the day, the bottom of the board shows the original meeting and the top shows the latest, with everything in between visible. A transformer block is a meeting. The residual stream is the whiteboard. Attention writes its conclusions onto the stream by addition, never by overwrite, so signal from the first layer is still readable at the last layer.
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 residual addition at the end of every transformer sub-block is one of the smallest pieces of arithmetic in the whole architecture and one of the most consequential. Remove it and deep transformers stop training. Misunderstand it and you cannot explain how features compose across layers.
This deep dive walks through the exact write-back operation, contrasts it with the wrong alternatives, explains why the addition is what makes deep transformers trainable, and connects the design choice to modern interpretability work.
Mental model: attention produces a delta that is added to the residual stream. The stream never gets overwritten. Every layer reads the cumulative state and contributes a new term.
The exact write-back operation
The formula
In a pre-norm transformer block, the attention sub-block performs:
The order of operations is:
- Apply LayerNorm to x_in (in pre-norm).
- Project to Q, K, V through W_Q, W_K, W_V.
- Run scaled dot-product attention per head.
- Concatenate per-head outputs.
- Project through W_O.
- Add the result to the original unnormalized x_in.
Shape check
After concatenation the per-token tensor is (seq_len, d_model). W_O is (d_model, d_model), so the post-projection output is also (seq_len, d_model). The residual x_in is (seq_len, d_model). The addition is element-wise and shape-preserving.
Why the addition uses unnormalized x_in
In pre-norm, LayerNorm normalizes only the input to the sub-layer, not the residual itself. The residual stream stays unnormalized through the whole stack, and each layer's LN happens locally. This is what gives pre-norm its stable depth-scaling behavior.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Every modern frontier transformer (Llama 4 Maverick, GPT-5.5, Claude Opus 4.7, Gemini 3.1 Pro, Mistral, DeepSeek V4) uses pre-norm with residual additions inherited from the original 2017 transformer block.
- ResNet (2015) introduced the residual connection in vision and is the direct architectural ancestor.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat changes if you switch from post-norm to pre-norm?
Post-norm normalizes after the residual add (LN(x + attn(x))); pre-norm normalizes before the sub-layer (x + attn(LN(x))). Pre-norm trains stably at much greater depths because the residual stream remains unnormalized and identity gradients pass cleanly. Modern stacks use pre-norm.
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.
Thinking attention replaces the residual stream. It does not. The output is added to the residual, that addition is what makes deep transformers trainable.
60 second bullets to scan on the way to the call.
The formula for the attention sub-block output
Why residual addition prevents gradient vanishing
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.