Machine learning · 20 min

Attention: every token looks at every other

Queries, keys and values: score with dot products, softmax into weights, blend the values, mask the future, and cache keys while generating.

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 q⋅kq \cdot k, divided by d\sqrt{d} (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: wj=esj/∑iesiw_j = e^{s_j} / \sum_i e^{s_i}.
mask
In a decoder like GPT, a token may not look at later tokens: their scores become −∞-\infty, so their weights are exactly 0.

03See it work

One query, step by step

The query of “it” is compared with every key. Softmax turns the scores into weights, and the output is the weighted blend of the values.

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:

out=∑jwj vjwherew=softmax⁡ ⁣(q⋅kjd).\text{out} = \sum_j w_j\, v_j \quad\text{where}\quad w = \operatorname{softmax}\!\left(\frac{q \cdot k_j}{\sqrt{d}}\right).

Doing this for every token at once gives a matrix of weights, one row per query: softmax⁡(QK⊤/d) V\operatorname{softmax}(QK^\top/\sqrt{d})\,V. With nn tokens that is n2n^2 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 made

05See 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.

Without a cache, every step recomputes K and V for the whole context. With one, each step adds a single row.

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 d\sqrt{d}.

07Check yourself

Check yourself

  1. 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.

  2. Question 2 of 6

    What do the attention weights of one query add up to?

  3. Question 3 of 6

    Why does a decoder like GPT mask later tokens?

  4. 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?

  5. 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?

  6. 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 q⋅k/dq \cdot k / \sqrt{d}; softmax turns them into weights that add up to 1; the output is ∑jwjvj\sum_j w_j v_j.
  • 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.
Back to path