Part F · Chapter 25 · Capstones

25. Trace a next-word prediction

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
The start of an English sentence

Anna has a small → flower · 13.1 %

Reading the English · 3 steps
  1. 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.

  2. Token embedding“small” becomes 64 numbers; the first is −0.260.

    Each token's number picks its row of the embedding table, 64 numbers. Unlike the paper's model, the row is not multiplied by √d_model.

  3. 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 · 6 steps
  1. 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.

  2. 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.

  3. Block 1 · Add the attention to xThe vector of “small” changes by 685.8 % of its length.

    The residual connection: the sub-layer's result is added to its input. Nothing normalises the sum here; the next block normalises its own copy.

  4. Block 1 · LayerNorm before the feed-forward network“small”: mean −0.093 and variance 2.104 before normalising.

    The vector is normalised again, this time before the feed-forward network.

  5. 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.

  6. Block 1 · Add the feed-forward result to xThe vector of “small” changes by 533.0 % of its length.

    The residual connection: the sub-layer's result is added to its input. Nothing normalises the sum here; the next block normalises its own copy.

Block 2 · 6 steps
  1. 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.

  2. 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.

  3. Block 2 · Add the attention to xThe vector of “small” changes by 57.4 % of its length.

    The residual connection: the sub-layer's result is added to its input. Nothing normalises the sum here; the next block normalises its own copy.

  4. Block 2 · LayerNorm before the feed-forward network“small”: mean 0.861 and variance 109.480 before normalising.

    The vector is normalised again, this time before the feed-forward network.

  5. 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.

  6. Block 2 · Add the feed-forward result to xThe vector of “small” changes by 89.5 % of its length.

    The residual connection: the sub-layer's result is added to its input. Nothing normalises the sum here; the next block normalises its own copy.

Predicting the next word · 4 steps
  1. 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.

  2. Linear: a score for every wordThe highest score is “flower”, 8.11.

    The last position's vector is compared with every word's row of the same embedding matrix the input used (weight tying): one score, a logit, per word.

  3. Softmax: the next word's probabilities“flower” has probability 13.1 %.

    The softmax turns the scores into probabilities that add up to 1. This is what an LLM API samples from, after temperature and top-k.

  4. The word chosenNext word: “flower” (13.1 %).

    Greedy writing takes the most likely word. Sampling would draw one with these probabilities.

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 this Let the model write on, one word at a time, with your own temperature and top-k.

The idea

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
The start of an English sentence

Tom has a red ball . He …

1. Remove a head

Block
Head

With this head removed from the whole forward pass, will the most likely next word change?

2. Change the temperature

Temperature

At temperature 2.0, what happens to the probability of the most likely word?

3. Set top-k

top-k

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 formula

P_{T,k}(w) = \frac{e^{z_w / T}}{\sum_{v \in \mathrm{top}_k} e^{z_v / T}} \quad \text{for } w \in \mathrm{top}_k, \text{ else } 0, \qquad z = \mathrm{LayerNorm}\big(h^{(N)}_{\text{last}}\big)\, E^{T}
\(z_w\)
the score (logit) of word w at the last position
\(T\)
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.
Remember A 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.

Your answer
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.

Your answer
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?

Your answer
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.

In the paper: section 3.2.3, 3.4

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.