Stacking blocks
What changes layer-to-layer.
Check yourself ↓ three questions and what to remember, at the end
A decoder is the same block stacked N times, and the residual stream is what runs up the stack. Below, one position's stream gains an attention update and a feed-forward update in each block; after the embedding and after every block, the "logit lens" applies the final LayerNorm and output head early, to show which next character the stream would predict if the model stopped there.
Loading the animation…
With the trained weights (the default), the lens moves: at the "!" of "hello!", the stream read out straight after the embedding guesses one letter, and after each block it settles on a space, the model's answer. Switch to random init and each block's update is only about 1% of the stream, so the lens hardly moves from layer to layer. In large trained models the read-out typically sharpens towards the final prediction as the layers go by; the idea and the name come from "interpreting GPT: the logit lens". This model ties its output head to the embedding table and ends with a single LayerNorm, so the lens is exactly the model's own read-out, applied early.
Concept
A real Transformer doesn't run one block — it runs N of them, with
the output of block i becoming the input of block i + 1. GPT-3 has
96 of these stacked. Each block looks identical from the outside (the
same dimensions in and out) but learns to specialise.
What changes layer to layer? Two things, mostly:
- The information available. Block 0 sees raw token + position embeddings. By block 5, every position's vector has already been mixed with every previous position's vector five times. Higher blocks get to operate on much richer features.
- The attention pattern. Empirically, early blocks tend to attend locally (a few tokens back), middle blocks pick up syntactic relationships, and late blocks integrate longer-range semantic structure. Our two-block toy, trained on one book in 8-letter windows, is far too small for those layers, but its blocks already differ: block 1's heads stay local, block 2's reach further back.
Below, the same input runs through 1, 2, 3, or 4 blocks. Each grid is the head-0 attention pattern for that block. Compare them.
Maths
If we name a block's transformation , then a stack of blocks is just function composition:
…with a final LayerNorm and the output projection at the end. The residual stream — the running vector at each position — is the link between blocks. Each block reads the stream, computes a contribution (via attention and the FFN), and adds the contribution back.
Code
// src/lib/transformer/model.ts (excerpt)
let x = addMat(tokEmb, posEmb);
for (let i = 0; i < config.n_blocks; i++) {
x = block(x, weights.blocks[i]!, config.n_heads, traceFor(i));
}
const xFinal = layernormRows(x, weights.ln_final.gamma, weights.ln_final.beta);
const logits = matmul(xFinal, transpose(weights.tok_emb));
A for loop and a final tied projection. That's the whole stack.
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: A space
a space. Straight after the embedding the lens guesses a letter; each block's update moves the read-out towards a space, the model's final answer. Step through the animation to watch it settle.
Pick one, then reveal.
Reveal the answer
Answer: 1.80 (counted right within 10%)
1.80 times, not 2: the embedding table (shared with the head) and the final LayerNorm are paid for once, whatever the depth. In this tiny model the embedding is a large share; in a large model the factor would be close to 2.
Commit to a number, then reveal.
Reveal the answer
Answer: Exactly the model’s output
Exactly the output: this model ties its head to the embedding and ends with a single LayerNorm, so the lens is the model's own read-out. Applied after an earlier block, it is the same read-out taken early.
Pick one, then reveal.
Comments
Be the first to leave a comment on this section.
Sign in (top-right) to leave a comment.