How to read this chapter
A language model repeatedly predicts what comes next. This chapter explains how text becomes features, how attention connects them, and how training changes the prediction.
Read the parts in order to follow the data through a decoder. The orange map marks the component under discussion. After each drawing, explain what its arrows carry and why the component is needed. The recap boxes and reasoning exercises are for review.
Each part ends with an “In one breath” recap. The interview page has questions, graded exercises, and worked solutions; try the reasoning before reading the answer.
Meet Finch-19
Finch-19 is our small teaching decoder with the 2019 GPT-2 recipe. The next guide rebuilds it as Finch-24 so we can compare design choices without changing the basic task.
| Setting | Symbol | Finch-19 | What it controls |
|---|---|---|---|
| Vocabulary size | V |
32,000 | How many distinct tokens the model knows |
| Model width | d_model |
512 | How many numbers describe each token |
| Layers | N |
8 | How many decoder blocks are stacked |
| Attention heads | H |
8 | How many separate attention patterns per layer |
| Head size | d_head |
64 | 512 / 8, the width of one head |
| MLP hidden size | d_ff |
2,048 | 4 × 512, the width inside each MLP |
| Context length | T_max |
1,024 | The most tokens it can read at once |
| Positions | learned table | 1,024 rows of 512 numbers, added at the bottom | |
| Normalization | LayerNorm, pre-norm | Keeps numbers at a steady size | |
| Output head | tied to embeddings | Reuses the input table to score the vocabulary | |
| Total parameters | 42,128,384 | Every learned number, counted in Part XII |
We follow The cat sat because it was tired. Attention helps the representation of it use information about cat. The tokenizer’s illustrative IDs stay fixed throughout the chapter; ␠ marks a leading space.
| Position | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Token | The |
␠cat |
␠sat |
␠because |
␠it |
␠was |
␠tired |
| Id | 791 | 8415 | 7731 | 1606 | 433 | 574 | 19781 |
What is inside
Thirteen parts follow one sentence through the whole machine, in the order the data actually flows. The map below is the whole model on one page; every part opens with the same map and lights up the piece it covers.
- 01Tokens: Turning Text into NumbersSections 1 to 8. A decoder needs numbers rather than raw text.
- 02Embeddings: Giving Each Token a MeaningSections 9 to 14. Token IDs name pieces but describe no meaning.
- 03Positions: Order Is Invisible Until You Add ItSections 15 to 20. The same words can mean different things in another order.
- 04Sinusoidal Positions: A Ruler Made of WavesSections 21 to 26. A learned table stops at its last row.
- 05RoPE: Rotate, Don't AddSections 27 to 32. Position can change a comparison instead of an embedding.
- 06Queries, Keys and ValuesSections 33 to 37. A token needs information from other positions.
- 07Scaled Dot-Product AttentionSections 38 to 42. Matching scores must become a useful blend.
- 08The Causal Mask: No PeekingSections 43 to 47. Training already contains the answers later in the text.
- 09Multi-Head AttentionSections 48 to 52. One attention pattern may mix incompatible relationships.
- 10The n × n Matrix: Where the Cost LivesSections 53 to 57. Full attention compares many position pairs.
- 11The KV Cache: Keep the Keys, Skip the RedoSections 58 to 62. Naive generation repeatedly processes an unchanged prefix.
- 12The Decoder Block and the Whole ModelSections 63 to 68. Attention exchanges information but cannot do every transformation alone.
- 13Training: From Random Numbers to a ModelSections 69 to 75. A decoder learns through repeated predictions and corrections.
- +Interview prepInterview question bank, graded exercises and worked solutions.