01Why it matters
Who does “it” mean?
In "the cat ate because it was hungry", you know at once that it is the cat. A model reading the sentence has to work that out too: the word it means nothing on its own until it borrows meaning from another word.
Attention is how a transformer does the borrowing. Every token asks a question, every token offers an answer, and each token blends in information from the tokens whose answers fit its question best. Every large language model is built from stacks of this one operation.
02Key parts
The pieces you will see
- query q
- What a token is looking for. "it" looks for a noun.
- key k
- What a token offers to others. "cat" advertises itself as a noun.
- value v
- The information a token hands over to whoever attends to it.
- score
- The dot product , divided by (the number of dimensions): large when the query and key point the same way.
- softmax
- Turns scores into weights between 0 and 1 that add up to 1: .
- mask
- In a decoder like GPT, a token may not look at later tokens: their scores become , so their weights are exactly 0.
03See it work
One query, step by step
Here every vector has just two numbers, so you can see them as arrows; real models use 64 or 128 per head, and learn them from data instead of having them set by hand. The recipe is the same:
Doing this for every token at once gives a matrix of weights, one row per query: . With tokens that is scores, which is why long contexts are expensive.
04Predict
Your turn: which word wins?
Make 5 predictions: step forward and answer each question before the next step plays.
0 of 5 made05See it work
Generating text: never compute the same key twice
A decoder writes one token at a time, and each new token attends to everything before it. The keys and values of the earlier tokens don't change, so the model keeps them in a KV cache and computes only one new row per step.
06Try it
Steer the attention yourself
Drag the query arrow q, or type a query into the box:
- Make "it" attend to "ate". Which direction does q have to point?
- Set q to (0, 0). Every score is 0, so every token gets the same weight: the output is the plain average of the values.
- Make q long. The scores spread apart and softmax picks one winner almost outright. That is why scores are divided by .
07Check yourself
Check yourself
Question 1 of 6
A query and a key point in opposite directions. What is their score?
Your answer comes back in Review in a few days, so it sticks.
Question 2 of 6
What do the attention weights of one query add up to?
Question 3 of 6
Why does a decoder like GPT mask later tokens?
Question 4 of 6
A model has already processed 100 tokens and now takes in the 101st. With a KV cache, how many new rows of K does it compute?
Question 5 of 6
The query of “it” matches the key of “cat” a little better than the key of “mat”. What does the output contain?
Question 6 of 6
In “a small dog chased the red ball”, the query of “ball” looks for adjectives. Which word can get a sizeable share of its attention by mistake?
08Explain it back
Say it in your own words
How does attention let a word like “it” borrow meaning from other words, and why is it called a soft lookup?
09Recap
What to remember
- Each token has a query (what it wants), a key (what it offers) and a value (what it hands over).
- Scores are ; softmax turns them into weights that add up to 1; the output is .
- A mask hides later tokens from a decoder, so it can learn to predict the next token.
- While generating, a KV cache keeps the keys and values of earlier tokens: one new row per step, at the cost of memory.