Linear algebra for LLMsPart 6: The rest of the transformer
The residual stream
5 min
One transformer block
Each sublayer reads a normalized copy of and adds its result back. This “pre-norm” layout is what GPT-2 and Llama use. keeps its shape through every block.
Write
Unrolled, it's one big sum
The final vector is the embedding plus what every attention () and MLP () sublayer wrote. Nothing is overwritten; information is only added. (The numbers are Part 1’s toy example.)
A shared space that everyone reads and writes
In a transformer
Every matrix that reads the stream (, the MLP’s up and gate projections) projects it; every matrix that writes (, the MLP’s down projection) maps back into the same dimensions. Layers communicate by writing into directions that later layers read: the projections of Part 3. Interpretability work studies the stream exactly this way.
Why add instead of replace
The identity gives the gradient a straight path back to every earlier layer, whatever does. Without it, gradients would have to pass through every layer’s Jacobian (Part 8) in turn and could shrink or explode over dozens of layers.
Write
✏️ Your turn
Count the writers
A model has 32 blocks, each with one attention and one MLP sublayer. How many vectors are added together in its final residual stream, counting the embedding?
Work it out on paper, then go to the next card to check.
Answer: 65
The embedding plus 64 sublayer outputs. A final norm and the unembedding then read the result.
📌 Recap
The residual stream in one card
- ; .
- Final = embedding + every sublayer’s output.
- All layers read from and write into the same -dimensional space.
- The identity path keeps gradients flowing.
Write