The same end-to-end look for the GPT-style model: your English through every block to the probabilities of the next word, each step linked to its chapter. Then three experiments, predicted first, ending with the two settings every LLM API offers: temperature and top-k.
The full trace of a next-word prediction
Anna has a small → flower · 13.1 %
Reading the English· 3 steps
Input → tokens5 tokens: <s> Anna has a small
The text is split into the model's tokens, each with its number in the vocabulary, after the start token <s>. There is no </s>: the sentence is not finished, and the model is asked what comes next.
Learned positionsPosition 4 adds 0.152 to the first number of “small”.
Each position adds its own row of 64 numbers, learned in training like every other weight, instead of the paper's sine waves. The model knows 32 positions, so it reads at most 32 tokens: its context window.
Block 1 · LayerNorm before attention“small”: mean 0.001 and variance 0.040 before normalising.
Pre-normalisation: the vector is normalised before it goes into the attention, and the attention's result is added to the vector as it was. The paper normalised after the addition.
Block 1 · Masked multi-head self-attention“small” looks most at “small” (38.4 %, the average of the 4 heads).
Each token looks at itself and the tokens before it, never after, through 4 heads; the scores of later positions are set to −∞ before the softmax. This is the decoder's masked self-attention without an encoder to look at.
Block 1 · Feed forward with GELU214 of 256 values are negative after the GELU, none below −0.170.
The same two small layers for every position: 64 numbers widened to 256, passed through the GELU, a smooth version of the ReLU that lets small negative values through, and narrowed back to 64.
Block 2 · LayerNorm before attention“small”: mean 0.746 and variance 68.004 before normalising.
Pre-normalisation: the vector is normalised before it goes into the attention, and the attention's result is added to the vector as it was. The paper normalised after the addition.
Block 2 · Masked multi-head self-attention“small” looks most at “has” (50.0 %, the average of the 4 heads).
Each token looks at itself and the tokens before it, never after, through 4 heads; the scores of later positions are set to −∞ before the softmax. This is the decoder's masked self-attention without an encoder to look at.
Block 2 · Feed forward with GELU220 of 256 values are negative after the GELU, none below −0.170.
The same two small layers for every position: 64 numbers widened to 256, passed through the GELU, a smooth version of the ReLU that lets small negative values through, and narrowed back to 64.
Final LayerNorm“small”: mean 1.453 and variance 336.542 before normalising.
After the last block, one more LayerNorm, as in GPT-2: with normalisation before each sub-layer, the stack's output would otherwise never be normalised.
Type the start of an English sentence or pick one. The GPT-style model reads it after <s>, and every step at the last position is listed in order: the tokens, the embedding and the learned positions, each block's normalisation, attention, feed-forward network and residual sums, then the final normalisation, the scores and the probabilities. Open any step for what happens, its numbers and its links.
Try thisLet the model write on, one word at a time, with your own temperature and top-k.
A next-word prediction is the decoder of chapter 24 alone, with the four changes of chapter 22: no encoder to read, LayerNorm before each sub-layer, learned positions and the GELU. Only the last position's output matters for the next word, so the trace follows that position, although every position is computed, each seeing only itself and the tokens before it.
At the end the model has a probability for every word. What happens next is a choice the model does not make: greedy writing takes the top word; sampling draws one at random with the probabilities, after two adjustments you set in an API call. The temperature divides the scores before the softmax (chapter 6), and top-k keeps only the k most likely words and shares the probability among them.
A worked example: the next word after “Anna has a small”
The model reads 5 tokens, “<s> Anna has a small”, and rates “flower” most likely, with 13.1 %. The next four are “cat” 12.7 %, “horse” 11.0 %, “bird” 10.7 %, “car” 9.6 %: the model knows a small something comes next, not which.
At temperature 2 the probability of “flower” falls from 13.1 % to 10.0 %, and the others close in; at temperature 0.5 it rises to 17.0 %. With top-k 1, sampling has one word left: “flower” every time.
Removing a head matters where the model is undecided. After “Tom has a red ball . He” the top two are “sees” 27.7 % and “draws” 27.4 %. Remove head 4 of block 1 and run the model again: “draws” becomes most likely, with 27.7 %.
Three experiments: predict, then run
Remove a head, change the temperature, set top-k
Tom has a red ball . He …
1. Remove a head
1
4
With this head removed from the whole forward pass, will the most likely next word change?
2. Change the temperature
2.0
At temperature 2.0, what happens to the probability of the most likely word?
3. Set top-k
1
Five draws with five different seeds, at temperature 1 and top-k 1: what do they give?
Three experiments on the GPT-style model. For each, choose your prediction first; then run it. Removing a head reruns the model with that head's output set to zero and compares the five most likely words; the temperature compares the top word's probability; top-k draws five words with five seeds.
the temperature: 1 leaves the model's probabilities as they are; below 1 sharpens them, above 1 flattens them
\(\mathrm{top}_k\)
the k words with the highest scores; every other word gets probability 0
\(h^{(N)}_{\text{last}}\)
the last position's vector after the last block, normalised once more
\(E^{T}\)
the embedding matrix the input used, turned to score every word
Why it matters
This is what happens behind every completion call: the model computes one set of probabilities, and the temperature and top-k you send decide how the next token is drawn from them. Temperature 0 (or top-k 1) makes the answer repeatable and usually safe; a higher temperature makes it varied and more likely to wander. Neither changes what the model knows, only how its probabilities are used.
The head experiment explains a familiar surprise: a tiny change in a model, or a prompt, can flip the answer when two continuations are almost equally likely, and do nothing when one is far ahead.
Where it fails
Our vocabulary is tiny. The model knows fewer than a hundred English words and simple sentences, so its predictions are sensible only inside that world.
Top-k is one of several cut-offs. APIs also offer top-p (keep the smallest set of words whose probabilities add up to p); the idea is the same.
The draws are seeded here. The lab fixes the seed so that a run repeats; an API usually draws afresh each time unless you ask otherwise.
RememberA next-word prediction runs the decoder-only stack once and ends with a probability for every word. Temperature divides the scores before the softmax, top-k keeps only the k most likely words, and greedy writing or a seeded draw picks the next one.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
How sure is the model?
What probability does the model give its most likely next word after “Anna has a small”? In percent, one decimal.
Within 0.1 of the answer
At temperature 2
At temperature 2, with every word kept, what probability does that word get? In percent, one decimal.
Within 0.1 of the answer
Top-k of one
What does sampling do with top-k = 1?
Tokens read
How many tokens does the GPT-style model read for “Anna has a small”, the start token included?
Within 0.5 of the answer
In the paper
Section 3.2.3 explains the mask the GPT-style model keeps from the paper's decoder: “We need to prevent leftward information flow in the decoder to preserve the auto-regressive property.” Section 3.4 describes the last steps: the shared embedding matrix as the pre-softmax linear transformation, then the softmax.