Inside a transformer
Follow a sentence through a small working GPT: embeddings, attention heads, the MLP and the odds of the next word.
Follow a sentence through a small working GPT: embeddings, attention heads, the MLP and the odds of the next word.
This is a real, working transformer: the design of GPT-2 (OpenAI’s language model of 2019), shrunk until every number fits on your screen. It knows 68 words, holds 16 numbers for each word and has 2 blocks with 2 attention heads each. We trained it on simple made-up sentences about children and their things, food, sport and capital cities.
The sentence on top is what it has read. Tap a stage to open it, and tap a word to follow it through. The question: how does a transformer turn the words so far into a guess for the next word?
Next word, the model expects: ball 99%, bottle <1%, bicycle <1%
The stages: tap one to open it. Each square is word 14’s 16 numbers as they leave that stage. Each stage adds to them.
Each row adds up to 1. The hatched cells are later words: hidden (masked), so no word can peek ahead.
Where “purple” looks
The other 8 words: <0.1% between them.
Its query q: 8 numbers (the normalised stream × W_Q, this head’s share)
| word | its key k | q·k÷√8 | weight |
|---|---|---|---|
| riya | 8.12 | 0.000 | |
| has | 10.44 | 0.000 | |
| a | 13.78 | 0.014 | |
| blue | 1.73 | 0.000 | |
| kite | 16.63 | 0.238 | |
| and | 12.19 | 0.003 | |
| a | 13.29 | 0.008 | |
| purple | 2.90 | 0.000 | |
| ball | 17.76 | 0.736 | |
| . | 7.29 | 0.000 | |
| riya | 4.87 | 0.000 | |
| likes | 8.24 | 0.000 | |
| the | 8.05 | 0.000 | |
| purple | 4.66 | 0.000 |
Softmax turns the scores into weights: e^score, divided by the total, so they add up to 1.
Each word’s value v, times its weight, added up:
(weights under 0.05 not shown)
= this head’s output for the word
Both heads, back into the stream
A transformer reads all the words at once. Each becomes a vector of numbers, and the vectors flow up through the blocks. Each half of a block adds an update to every vector (the running sum is called the residual stream): attention mixes in information from earlier words, and the MLP processes each word on its own.
At the top, the vector of the last word is turned into a score for every word it knows, and the scores into probabilities. Training nudged all 7,936 numbers to make the real next word more likely. Its loss (−ln of the chance it gave the real next word) fell from 4.98 before training to 1.21 on test passages; a blind guess among 68 words scores 4.22.
Nobody programmed its heads. Training found a way to copy: in block 2, head 2, a word finds an earlier copy of itself and takes the word that came after it. Researchers call this an induction head, and big models have them too. We kept some sentences out of training to test it, among them the thing, colour, copying and one-thing examples here. On 2,000 of those it named the right thing 100% of the time, but the right colour only 52%: a coin toss between the two colours. To say what colour the ball is, it would have to find the earlier “ball” and take the word before it, a different trick this tiny model never learnt. (With only one thing, it just finds the one colour.)
Step 4 of 6: attention, block 2
Attention is how words share information. Each word scores every word up to itself, turns the scores into weights that add up to 1, and takes that blend of their values. Each head does this with its own weights, looking for something different. (First the 16 numbers are normalised: rescaled so that they average 0 with a spread of 1.)
| Size | This model | GPT-2 small |
|---|---|---|
| Words it knows | 68 | 50,257 tokens |
| Numbers per word | 16 | 768 |
| Blocks | 2 | 12 |
| Heads per block | 2 | 12 |
| MLP width | 64 | 3,072 |
| Words it reads at once | 16 | 1,024 tokens |
| Numbers it learnt | 7,936 | 124 million |
A transformer reads all the words at once. Each becomes a vector of numbers, and the vectors flow up through the blocks. Each half of a block adds an update to every vector (the running sum is called the residual stream): attention mixes in information from earlier words, and the MLP processes each word on its own.
At the top, the vector of the last word is turned into a score for every word it knows, and the scores into probabilities. Training nudged all 7,936 numbers to make the real next word more likely. Its loss (−ln of the chance it gave the real next word) fell from 4.98 before training to 1.21 on test passages; a blind guess among 68 words scores 4.22.
Nobody programmed its heads. Training found a way to copy: in block 2, head 2, a word finds an earlier copy of itself and takes the word that came after it. Researchers call this an induction head, and big models have them too. We kept some sentences out of training to test it, among them the thing, colour, copying and one-thing examples here. On 2,000 of those it named the right thing 100% of the time, but the right colour only 52%: a coin toss between the two colours. To say what colour the ball is, it would have to find the earlier “ball” and take the word before it, a different trick this tiny model never learnt. (With only one thing, it just finds the one colour.)
Things to try