- Key idea
- Score every key against the query, softmax the scores into weights, return the weighted average of the values
- Real world
- An estate agent prices a 3-bedroom house nobody has sold before by looking at houses that have sold, and averaging their prices, giving the most similar houses the most say.
- In an LLM
- In “The cat sat on the mat because it was tired”, the word “it” has to find out what it refers to. A GPT-2 attention head gives 84% of “it”’s attention to “cat” and copies information about the cat to the position of “it”.
Why tokens need to talk
After the embedding step, every token is a vector, but each vector only knows about its own token. The vector for “it” is the same in every sentence: it has no idea whether “it” means a cat, a cup or the weather.
To predict the next word, the model needs context. Something has to move information from one position to another: from “cat” to “it”, from “France” to the position that will say “Paris”. is that step. It is the only place in a transformer where tokens exchange information; every other part works on each position separately.
A lookup that never fails
A normal dictionary (a Python dict, a phone book) is a hard lookup. You bring a key, it finds the entry with exactly that key, and returns its value. Ask for a key that isn’t there and you get nothing.
Attention is a soft lookup:
- Compare the question, called the query, with every entry’s key and give each a score.
- Turn the scores into weights that are positive and add up to 1 (with softmax).
- Return the weighted average of all the values.
The answer is never “not found”. Close matches dominate, poor matches contribute a little. Play with the three kinds of lookup:
Two things to notice. First, the soft answer changes smoothly as the query moves. A hard lookup jumps from one entry to the next, and you can’t learn by gradient descent through a jump; you can through a smooth curve. Second, the sharpness slider: when the scores are very spread out, softmax puts nearly all the weight on the best match and the soft lookup behaves like the nearest match. How spread out the scores are matters a lot, and the scaling lesson is about keeping that under control.
An agent has records of four sold houses: 1, 2, 4 and 5 bedrooms. A seller asks about a 3-bedroom house. A strict database query (WHERE bedrooms = 3) returns nothing. A sensible agent instead looks at all the records, trusts the 2- and 4-bedroom houses most, the others less, and quotes a weighted average. That is attention: query = the house asked about, keys = the bedrooms on file, values = the prices.
Queries, keys and values as vectors
In a transformer the query, the keys and the values are all vectors, and the score is a dot product: multiply the two vectors number by number and add up. A dot product is large when two vectors point the same way, near zero when they are at right angles, and negative when they point apart.
Each token makes three vectors from its embedding:
- a : what am I looking for?
- a : what do I contain?
- a : what do I hand over if someone attends to me?
Below is a two-number version of our running sentence, “The cat sat on the”. The horizontal axis means is a thing, the vertical one is an action. The query belongs to the last “the”, which needs a noun.
Drag the query towards “sat” and the weight moves to “sat”; the output moves towards sat’s value. Make the query short and all scores are near 0, so the weights are nearly equal and the output is a bland average. Make it long and the best score wins almost everything. The length of the query (and of the keys) acts like the sharpness slider above.
Notice that the key and the value of a token are different vectors. The key decides who gets attention; the value decides what information moves. A token can be easy to find for one reason and pass on something else entirely.
You type “French cookery” into a library search (query). It is compared with every book’s subject labels (keys). Instead of handing you one book, imagine it hands you a summary built from all of them: mostly from the cookery books, a little from the French history books, almost nothing from the physics shelf. The labels decided how much each book counts; the books themselves (values) supplied the content.
The real thing: “it” finds “cat”
Here is a head from GPT-2 small, a real 124-million-parameter model, reading a real sentence. Nobody programmed it to resolve pronouns: its query and key matrices were learned during training because moving information this way helped predict the next word.
The output of attention for “it” is mostly cat’s value vector. That output is added to the vector at the position of “it”, so from this layer on, the model’s vector for “it” carries information about the cat. Later layers can use that: a cat can be tired, sleepy or hungry; a mat can’t.
At layer 4, head 3 (GPT-2 numbers its layers and heads from 0), the query of “it” matches the key of “cat” better than any other key, and softmax gives “cat” 84% of the weight and “mat” 4%. Averaged over all 12 heads of the layer, “it” sends only 10% to “cat” and 51% to the first token: each head does its own job, and the multi-head lesson shows why a layer has several.
What comes next
The next page makes this exact. The queries, keys and values come from three learned matrices; all the dot products are done at once as one matrix product; and a mask stops tokens from looking at words that haven’t been written yet.
The soft lookup as a formula
For one query , keys and values :
The weights are a softmax of the scores, so they are positive and sum to 1, and the output is a convex combination of the values: it always lies inside the region the values span. Transformers also divide the scores by ; see scaling.
The house-price widget uses a negative squared distance, , as its score instead of a dot product. With that score, the soft lookup is the classic Nadaraya–Watson kernel regression from 1964 statistics; attention is the same idea with a learned dot-product similarity.
Hard versus soft
As the scores are multiplied by a growing factor, softmax tends to a one-hot vector on the largest score, and the soft lookup becomes the nearest-match lookup. Soft is differentiable everywhere; hard is not. That is the reason attention is soft.
In papers
- Attention for translation, where a decoder softly looks up encoder states: Bahdanau et al., 2014.
- The transformer, attention as the only way tokens communicate: Vaswani et al., 2017.
- Attention as a differentiable key–value memory: Graves et al., Neural Turing Machines, 2014.