"Data visualization empowers users to" → 6 tokens:
Data · visualization · em · powers · users · to
" the", "the" and " The" are 3 different tokens.Same sentence in Portuguese: how many tokens per word?
gato = g + ato, cachorro = 4 pieces.Logits of the prompt: (1, 6, 50257) = (sentences, positions, vocabulary) — one distribution per position. GPT-2, measured:
| after… | top guess | true next | p(true) | |
|---|---|---|---|---|
Data |
. |
visualization |
11.91 | |
Data visualization |
\n |
em |
11.86 | |
Data visualization em |
powers |
powers |
0.423 | 0.86 |
…empowers |
you |
users |
0.109 | 2.22 |
…empowers users |
to |
to |
0.847 | 0.17 |
…users to |
visualize |
? | — | — |
x: the residual stream, n × d (one vector per token) · Norm: LayerNorm in GPT-2 · h: the stream after attention
Neither sublayer replaces the vector: each adds a correction. The MLP holds most of the stored knowledge.
Block 0 of GPT-2 small, the 6-token prompt. Fill it in before the next slide.
| step | shape |
|---|---|
| ? | |
| Q, K, V per head | ? |
| ? | |
| ? | |
| MLP hidden layer | ? |
| ? |
12 heads, each with
GPT-2's MLP:
Answer as (heads ×) tokens × numbers.
| step | shape | where it comes from |
|---|---|---|
6 × 768 |
||
| Q, K, V per head | 12 × 6 × 64 |
|
12 × 6 × 6 |
per head, |
|
6 × 768 |
||
| MLP hidden layer | 6 × 3072 |
|
6 × 768 |
3072 → 768, then add |
Block 0 of GPT-2 with nothing but @, a mask, a softmax, LayerNorm and GELU — the model's own weights:
| compared with the library | max difference |
|---|---|
block 0 output, 6 × 768 |
|
attention weights, 12 × 6 × 6 |
|
logits after all 12 blocks + final norm + wteᵀ |
wte: the token embedding table, 50,257 × 768; transposed, it is the output layer (768 → 50,257).
Float32 rounding, nothing else: the two equations of "Inside a block" are the model.
The toy (d = 4, one block, V = 12), last token of "the cat climbed the":
␣roof = | token | ||
|---|---|---|
␣roof |
2.491 | 0.395 |
␣wall |
1.463 | 0.141 |
␣sofa |
0.999 | 0.089 |
␣floor |
0.970 | 0.086 |
␣cat |
0.634 | 0.062 |
| 7 others | 0.227 |
Every row predicts its next token: p(target) = 0.500, 0.444, 0.274, 0.311, 0.395 → mean −ln p = 0.979 nats (uniform: ln 12 = 2.485). Without attention the last row says ␣cat 0.455, ␣roof 0.143.
GPT-2 small, one block, measured (
| part | count | parameters |
|---|---|---|
| attention: |
2,362,368 | |
| MLP: 768 → 3072 → 768 | 4,722,432 | |
| two LayerNorms (gain and bias) | 3,072 | |
| the block | the sum | 7,087,872 · MLP 66.6% |
Zero the output of every attention sublayer, keep the MLPs. What is the prediction based on now?
the:…cat… climbed onto the |
…cash… walked to the |
|
|---|---|---|
| with attention | roof 0.083 · bed 0.059 |
bank 0.092 · front 0.028 |
| attention removed | same 0.047 · first 0.019 |
same 0.047 · first 0.019 |
the and its index (9). Mean loss on the lab's 72-token paragraph: 3.96 → 5.71 nats.144 heads (12 blocks × 12), scored on three jobs — GPT-2 small, measured:
| job | best head (block.head) | weight |
|---|---|---|
| look at the previous token | 4.11 | 1.00 |
| look at the first token ("sink") | 5.1 | 0.99 — 64 of 144 heads put > half there |
| induction: "last time I saw this, what came next?" | 5.5 | 0.95 |
4.11 = block 4, head 11, counting from 0 · weight = the mean attention the head puts on the target (0 to 1)
GPT-2 small running in your browser, every stage drawn for the text you type — poloclub.github.io/transformer-explainer (Cho et al., 2024).
The first visit downloads about 660 MB of weights; the chapter has a step-by-step guide to all seven stages.
| piece | 2017 | GPT-2 (2019) | 2026 |
|---|---|---|---|
| norm placement | after the sum (post-LN) | before the sublayer (pre-LN) | pre-LN |
| normalizer | LayerNorm | LayerNorm | RMSNorm |
| MLP | ReLU, 2 matrices, |
GELU, 2 matrices, |
SwiGLU, 3 matrices, |
| attention | MHA | MHA | GQA — a 4× smaller KV cache (8 K/V heads for 32 Q heads) |
| position | sinusoidal, at the input | learned, at the input | RoPE, on Q and K |
| bias | everywhere | everywhere | none |
The mechanism did not change; the engineering around it did. GPT-2 — the model in the Explainer and in the lab — already had the first row. RoPE: rotary position embedding — Q and K are rotated by an angle proportional to the position, so their score depends on the distance between the tokens.
Apply the final norm and the output layer to the stream after each block ("logit lens"). Final answer: visualize. Rank 1 = the most likely of 50,257.
| read after block | embedding | 3 | 6 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|
rank of visualize |
8532 | 381 | 62 | 12 | 2 | 1 | 1 | 1 |
| its probability | 0.000 | 0.000 | 0.002 | 0.012 | 0.285 | 0.919 | 0.941 | 0.158 |
RMSNorm keeps the rescaling, drops the centring.
One reduction instead of two, no measurable quality cost.
SwiGLU — one branch gates the other:
Swish(u) = u·σ(u), σ: the sigmoid · ⊙: elementwise product
| family | attention | trained by | in 2026 |
|---|---|---|---|
| encoder-only — BERT | bidirectional | masked tokens | embeddings, retrieval, rerankers; cannot generate |
| encoder–decoder — T5, BART | both + cross | sequence to sequence | where the two sides differ: speech (Whisper), text into images |
| decoder-only — GPT, Llama, Claude | causal | next token | won — every frontier language model |
Decoder-only sees less context per token. Why did it win?
BERT trains on [MASK], a token that never appears at inference. Next-token prediction has no such gap: training is deployment.
zi: the logit of token i (the model's output before the softmax) · T: the temperature
GPT-2, after …empowers users to:
p( visualize) |
tokens for 90% | |
|---|---|---|
| 0.2 | 0.976 | 1 |
| 0.5 | 0.656 | 6 |
| 0.8 | 0.294 | 97 |
| 1.0 | 0.158 | 294 |
| 2.0 | 0.013 | 5,956 |
tokens for 90%: how many of the most likely add up to 0.9
| prompt | most likely (top-1) | top-p 0.9 keeps |
|---|---|---|
…44th President of the United |
States 0.998 |
1 token |
Once upon a time, there was a |
man 0.044 |
3,651 tokens |
␣roof |
␣wall |
␣sofa |
|
|---|---|---|---|
| 0.5 | 0.761 | 0.097 | 0.039 |
| 1 | 0.395 | 0.141 | 0.089 |
| 2 | 0.208 | 0.124 | 0.098 |
␣roof 0.737, ␣wall 0.263␣roof, ␣sofa| step | input ids | next | |
|---|---|---|---|
| 1 | [0, 2, 4, 5, 1] | ␣roof |
0.395 |
| 2 | … + 6 | . |
0.538 |
| 3 | … + 10 | <|endoftext|> |
0.401 |
the ␣cat ␣climb ed ␣the ␣roof . → "the cat climbed the roof."The budget is compute
Chinchilla: 70B (
| model | tokens / parameter, |
|||
|---|---|---|---|---|
| GPT-3 (2020) — go bigger | 175B | 0.3T | 1.7 | |
| Chinchilla (2022) — together | 70B | 1.4T | 20 | |
| Llama 3 8B (2024) — overtrain | 8B | 15T | 1875 ≈ 1900 |
The Transformer's real product is not one design choice: it is that loss falls predictably with parameters, data and compute.
Colab notebook, ~2 h, GPT-2 small on CPU. Each section ends in a checkpoint with a check value.
Outline: (1) the full path, text to next token, on a real GPT-2; (2) inside a block: talk, then think; (3) Transformer Explainer; (4) the six things that changed since 2017; (5) which family won; (6) choosing the next token; (7) scaling. Closes with the Colab lab.
Let the class answer "the next word" first, then correct: a distribution. Choosing is a separate step, with its own knobs, at the end of the class.
In the lab (section 4) the bloco_a_mao function prints exactly these six shapes.
The lab's block_by_hand with one head, 3 tokens, d = 4 and hand-picked weights. Step with Next and read the caption: the formula with the numbers of token 3 (A row 3 = 0.107, 0.787, 0.107; y31 = 1.651). Then the cases: "row 2 × 10" (u does not change, only the residual carries the size), "only the last token changes" (rows 1 and 2 do not move: causal mask), "attention removed" (h = x, each row alone).
The pages of the chapter walk the toy from text to here, every number checkable. GPT-2 does the same three lines with d = 768 and V = 50,257, and its W_U is wte transposed; the toy has its own W_U.
Ten minutes in class, or as homework before the lab. The guide on the page ties each stage to a section of the chapter. GPT-2 is a 2019 block: next slide shows which parts are already modern.
Start from the default (2026). Switch every control to its first option and tick the biases: the 2017 paper. Watch the residual line turn grey with post-LN, and the parameter split: two-thirds MLP with MHA, three-quarters with GQA.
Greedy writes ␣roof every time. Sampling sometimes writes ␣sofa: a good sentence the model also considered. The end-of-text token is how the model stops itself.
Default: T = 0.7, top-p 0.9, u = 0.5 → ␣roof; the last line runs again and predicts "." (0.538). Then: greedy (u does not matter), hot T = 2 (the tail grows), top-k 2; move u to 0.8 → ␣sofa.
Find the minimum of the curve at 1e23: about 20 tokens per parameter. Then tick "price inference too" and drag the tokens to serve from 1e10 to 1e15: the cheapest model shrinks, and is trained on far more tokens than Chinchilla would say.