Before it decides what a word means, every word looks back at all the others.
That is the whole invention that made modern AI work, and you can watch it happen. Every weight below was pulled out of a real model by running its published parameters forward. Each one was checked against the reference implementation before it was allowed on this page.
The problem attention solves
A word does not mean the same thing twice.
The previous module gave every word a fixed position in space. That buys a great deal, and it cannot buy this: bank in a sentence about a river and bank in a sentence about money have to end up somewhere different, and they start out identical. Something has to move them apart, using nothing but the rest of the sentence.
Attention is that something. Before settling what a word means here, every position gets to look back over everything earlier in the text and pull in a share of each one. How much it takes from each is a weight, and the weights are not rules anybody wrote. They are computed on the spot, from the sentence in front of it. That is why the same word can resolve two ways in two sentences with no extra machinery.
One word, one head, five beats, and you set the pace. Then the same thing seventy-two times over, further down.
Embeddings gave every word a fixed position in space. But a word does not mean the same thing everywhere. Bank in a river sentence and bank in a money sentence need to end up somewhere different.
Loading the extracted weights…
Three things in that figure are worth carrying: it can only reach backwards, the amounts are a share of one, and a great deal of that share goes somewhere meaningless. All three are consequences of the arithmetic rather than decisions about language, which is a useful way to hold the whole subject.
Now you try
Guess where the beam goes.
You have seen one head resolve one word. Commit to a prediction on a head you have not seen, and the real extracted weights will tell you how you did.
- Score
- 0
- Streak
- ×0
- Round
- 1/8
Loading the attention weights…
How to play
Say which earlier word this attention head is looking at.
- 1.One word in the sentence is lit up, and one of the model's 72 attention heads is switched on.
- 2.Click the earlier word you think that head sends most of its attention to.
- 3.The real row of weights comes up as bars. The first word is set aside, because it soaks up attention for reasons unrelated to meaning.
ControlsTap or click a word.
All seventy-two heads
Scrub through the layers and heads. What is worth noticing is not any single map. It is how little they resemble each other.
Loading the attention weights…
Attention
NextHow an LLM answers+What is actually going onWhere do the weights come from?Each word produces a question and a label. The weight is how well one word's question matches another's label.
Every position turns its vector into three others: a query (what am I looking for?), a key (what am I?) and a value(what do I pass on?). The weight from one word to another is how strongly the first one’s query lines up with the second one’s key. Those weights are then squashed so each row adds up to one, and used to mix the values together.
Everything here follows from that. The row summing to one is why a head with nothing to look for still has to spend its attention somewhere. The mask is applied before the squashing, which is why the future is not merely unlikely but impossible. And the three projections are learnt during training. Nobody assigned a head its job.
Take it apart yourself: EmbeddingsMagnet · 9 min. Optional.
+What is actually going onIs this a small model? Does that matter?Very. It is 82 million parameters, which is thousands of times smaller than a production model.
DistilGPT-2 was chosen so the extraction could be run honestly on a laptop and checked line by line. Its mechanism is exactly the one in a frontier model; its competence is not. Expect the heads here to look messier and to resolve fewer things cleanly than the tidy diagrams you may have seen elsewhere.
That messiness is worth having. Published attention pictures are usually the two or three heads that came out beautifully. This is all of them, including the ones doing something nobody has a name for. That is the honest state of the art on what attention heads are actually for.
Take it apart yourself: What an LLM actually isBeat the Predictor · 7 min. Optional.
+Watch somebody explain itThe clearest account of the mechanism you just wired by hand.
Optional: watch someone explain it
Attention in transformers, step-by-step
3Blue1Brown
The clearest account of the mechanism you just wired by hand.
+Go and try this on a real assistantGive it a sentence where one word decides another.
Go and try this
Write a sentence that breaks it
Take the pair of sentences in the map above about the trophy and the suitcase. Write your own pair with the same shape, where a single adjective flips which noun the pronoun refers to.
Then give both to an assistant and ask which thing the pronoun means.
What to watch for: That you can change the answer by changing only the meaning, not a single word of the structure. Nothing in the grammar tells the model which way to go, so whatever resolves it came out of the training text.
+Check yourselfIt marks itself, and nothing is sent anywhere.
Why is the top-right of every map empty?
Sources
- Attention Is All You Need
Vaswani et al. (arXiv:1706.03762, 2017)
The paper that introduced the transformer, and the definition of the attention step this page extracts.
- DistilGPT-2
Hugging Face
The model the attention weights on this page were extracted from. Six layers, twelve heads, 82 million parameters.
Apache 2.0
- Efficient Streaming Language Models with Attention Sinks
Xiao, Tian, Chen, Han & Lewis (arXiv:2309.17453, 2023)
The named account of the effect you can see in the first column of every map here: a large share of attention landing on the initial token regardless of meaning.
- Attention in transformers, step-by-step
3Blue1Brown
The clearest visual account of the same mechanism, with the maths drawn out.