Curriculum · Unit 6 · Attention and the transformer
How does each word decide which other words matter?
To understand "it" in a sentence, you have to know what "it" refers to. Attention is how a model works that out: every word asks a question, every other word offers an answer, and the best matches get listened to most. Stack that idea into layers and you have the transformer, the design inside nearly every chatbot.
Nib says: Tap any word in my sentence to see which earlier words it pays attention to. Thicker lines and warmer colors mean more attention. Then flip tired to wide and watch the meaning of "it" move. After that, press Next to walk one word through a whole transformer block. I set all the weights by hand, so they're illustrative.
Try itWho is "it"?
Tap a word. Faded, dashed words are hidden from it.
Attention weights are hand-made to show the idea; a real model has many attention heads per layer, and they're messier. Illustrative.
Step through itInside one transformer block
Tiny 4-number vectors with made-up values, so you can check the arithmetic. Real models use hundreds or thousands of numbers per token. The math is computed on this page and rounded for display. Illustrative.
The idea in plain words
Attention started as a fix for translation. In 2014, translation networks read a whole sentence and squeezed it into one fixed list of numbers before writing the translation, and long sentences got garbled. Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio let the network look back at every word of the original as it wrote each new word, and decide for itself which ones mattered right then. That learned looking-back is attention.
In 2017 a team at Google, with Ashish Vaswani as first author, asked, in effect: what if attention is the whole model? Their transformer gives every token three vectors. A query says what it's looking for, a key says what it offers, and a value is what it hands over. Each token compares its query with every key, turns the match scores into percentages with a softmax, and takes that blend of values. Several of these "heads" run side by side, each free to track something different. A small feed-forward network then works on each token, and residual connections add each layer's result back onto its input, so information can flow straight up through a tall stack.
Attention on its own ignores word order, so the transformer adds position information to each token at the start. And because every token can be processed at the same moment, rather than one after another as in older networks like the LSTM, the work splits neatly across the thousands of cores in a GPU. That's a big reason transformers could be scaled up so far.
GPT-style models keep only the decoder half and train on one job: predict the next token. To make that honest, each token may only look at tokens before it. That's the causal mask, and it's why "it" in the map above can't see "tired" yet. In a GPT, the meaning gets settled a bit later, when "tired" or "wide" arrives and looks back at both "it" and the noun. OpenAI's GPT-2 (2019) showed how far this goes: trained only to predict the next word of web text, it wrote fluent paragraphs and picked up tasks nobody had trained it for. How many tokens a model can look back across is its context window.
Three steps to GPT
- 2014
Attention for translation
Bahdanau, Cho and Bengio: while writing each word, look back over the whole source sentence.
- 2017
The transformer
Vaswani et al.: drop the step-by-step network and build everything from attention, run in parallel.
- 2019
GPT-2
OpenAI: a decoder-only transformer, causal mask, next-token prediction on lots of web text.
Three things to keep
- 1
Attention is a lookup by similarity. Each token's query is matched against every key, and the best matches' values get blended into that token.
- 2
GPTs only look backward. The causal mask stops a token from peeking at the future, which is what makes next-token prediction a fair test during training.
- 3
Parallel by design. Every token is handled at once, which suits GPUs and made it practical to scale transformers up.
Check yourself
Why does a GPT-style model need a causal mask during training?
It's being trained to predict the next token. If each position could see the tokens after it, it could simply copy the answer, and it would learn nothing useful. The mask hides the future so every prediction is a real guess.
In the GPT-style map, "it" can't see "tired". So where does the model work out what "it" refers to?
At later tokens. When "tired" (or "wide") arrives, it can look back at both "it" and the candidate nouns, and in a deep model the layers above build on that. Try clicking the last word in the map.
What do the residual connections in a transformer block do?
They add each layer's output back onto its input instead of replacing it. Each layer only has to learn a change to make, and the original signal can travel up through dozens of layers without fading.
Go deeper
- ★ Paper · 2017 · Ashish Vaswani et al.
Attention Is All You Need
The transformer paper. Its Figure 1 shows the whole model: an encoder stack beside a decoder stack, built from blocks like the one above.
Read the paper ↗ - ★ Paper · 2014 · Bahdanau, Cho, Bengio
Neural Machine Translation by Jointly Learning to Align and Translate
Where attention began. Its alignment pictures look a lot like the map above.
Read the paper ↗ - Article · 2019 · Alec Radford et al.
Better Language Models and Their Implications (GPT-2)
A next-token predictor that surprised everyone, and the debate over releasing it.
Read it ↗ - Paper · 2013 · Mikolov, Chen, Corrado, Dean
Efficient Estimation of Word Representations in Vector Space (word2vec)
The idea behind step 1 of the block: words as points in space.
Read the paper ↗ - Video · about 2 hours · Andrej Karpathy
Let's build GPT: from scratch, in code, spelled out.
Write self-attention, the causal mask and the whole block yourself, then train it on Shakespeare.
Watch on YouTube ↗ - Video · about 2 hours · Andrej Karpathy
Let's build the GPT Tokenizer
Where tokens come from, and why they explain so many odd model habits.
Watch on YouTube ↗ - Unit 0 · Start here
What actually happens when you ask a chatbot something?
See where the transformer block sits in the full trip, from your keyboard to the data center and back.
Trace a question - Interactive timeline
The transformer age
Attention, BERT, GPT-1 and GPT-2, and the scaling bet that came next.
Open the timeline - 🎓 Lecture hall · free
Two lectures for this unit
Learning Where to Look (attention, 2014) · Attention Is All You Need (the Transformer, 2017).
See the lectures - Next · Unit 7
Why did everyone bet on bigger, and how much bigger is the right size?
Scaling laws, Chinchilla, and AI turning into an infrastructure story.
On to Unit 7