Queries, Keys and Values
A token needs information from other positions. Matching and supplied content are different jobs. Queries, keys, and values separate them before softmax blends the relevant contributions.
#Section 33Every Token Plays Three Roles
Attention lets one position obtain information from other positions. In The cat sat because it was tired, it can help the representation of it use information about cat.
The names attention uses come from searching a library or a database, so let us start there. You walk into a library with a question written on a slip of paper: “who is it?”. That is your query. Every book has a label on its spine, like cat or because. Those labels are the keys. You compare your question with the spines and find the best match. Then you open that book and read what is inside. That is the value.
There is one big difference from a real library. Attention does not pick a single book. It reads a little from every book, a lot from the good matches and only a trace from the poor ones, and blends what it read into one answer. That blending is what makes attention trainable: a smooth mixture can be nudged a little at a time during training, whereas a hard “pick one” cannot.
The deep idea is that every token plays all three roles at once. it asks a question with its query. But it also has a key and a value, so that later tokens, was and tired, can find it and borrow from it. Every token is simultaneously a reader with a question, a book with a label, and the contents of that book.
#Section 34Making Q, K and V
Where do the three vectors come from? Each one is the token’s vector multiplied by a different learned weight matrix. Same input, three different views of it:
The input has one feature vector per position. Three learned projections produce queries, keys, and values. Sharing each projection across positions applies the same operation to every token.
A matrix projection forms new features by taking weighted sums of the input features. Training adjusts those weights so matching features and useful content can differ.
Separate projections learn what to seek, how to be found, and what to contribute. Finch-19 also uses a learned bias, a fixed vector added to each projection's output.
In code it is three linear layers, or more commonly one fused layer three times as wide whose output is cut into three:
qkv = nn.Linear(512, 3 * 512)(x) # x: (B, T, 512) -> (B, T, 1536)
q, k, v = qkv.split(512, dim=-1) # each (B, T, 512)#Section 35Matching: The Score
Now we use them. To decide how much token should attend to token , compare token 's query with token 's key using the dot product from Section 12: multiply them entry by entry and add up the results. A large positive score means "this key matches what I am looking for".
The figure compares it's query with the visible keys. A larger dot product means a stronger match; it does not yet give a normalized weight.
Softmax exponentiates scores and divides by their sum, producing positive weights that sum to one. Here cat receives the strongest weight.
The tallest bar is cat. The model has learned to make the query of a pronoun match the key of a recently mentioned noun of the right kind. Nobody wrote that rule. Training found matrices and that produce queries and keys with this property, because resolving it correctly helps predict what comes next (was tired is about the cat, so it predicts purring better than parking).
(Part VII adds one step we have skipped: the scores are divided by before the softmax. With our tiny 4-number vectors it would not change the story.)
#Section 36Mixing: The Weighted Sum of Values
Now the weights do their job. Each token's value vector is scaled by its weight, and the scaled values are added up into one output vector for the asking token:
Read it out loud: the output for token is the sum, over every token it can see, of the weight from to times 's value. Take toy 3-number values. cat's value is , and scaling it by its weight 0.554 gives . sat's value scaled by 0.119 gives . Doing the same for all five and adding them up gives the output .
The result is a blend, not a copy of the strongest value. Even a smaller attention weight can contribute information from another token.
#Section 37Why Keys and Values Are Separate
Why have both a key and a value? Couldn't one vector do both jobs?
Look at the box for cat. The label on the outside, the key, might encode "singular animal noun, grammatical subject". That is what helps a pronoun find it. The contents, the value, might encode "furry, purrs, the one who did the sitting". That is what is useful once you have found it. What makes you findable and what you have to offer are different things. A library would be useless if a book's spine label had to be its entire contents.
Two separate matrices, and , let the model learn each role without compromise. Queries and keys must have the same length, because they are dotted together, but values can be a different length, because they are only ever scaled and summed. In standard multi-head attention all three are the same size (64 per head in Finch), but nothing forces it.
Shapes, to scale
Here is the whole computation for one head, as shapes. is . turned on its side, , is . Multiplying them gives , a grid holding one score for every pair of tokens: row , column is . Softmax along each row turns into the weight grid , still . Then times (shape ) gives the output , shape , one row per token again.
The grid matters. It grows with the square of the text length: 49 entries for our sentence, a million for a thousand tokens, ten billion for a hundred thousand. Part X is entirely about it.
Self-attention and cross-attention
In self-attention, the version in GPT-style models and in Finch, the queries, keys and values all come from the same sequence: the text looks at itself. In cross-attention, the queries come from one sequence and the keys and values from another. The original 2017 Transformer was built for translation and used cross-attention in its decoder: the sentence being written asked questions of the sentence being translated. Decoder-only models such as GPT, Llama and Finch use only self-attention.
Sharp edges of Q, K and V
Scores are not symmetric. How much it attends to cat is not the same as how much cat attends to it, because the first uses it’s query and cat’s key, and the second uses cat’s query and it’s key. Read attention maps row by row: one row is one token’s point of view.
Tokens attend to themselves. A token’s query is also scored against its own key, and tokens often give themselves a sizeable weight (0.148 for it above). That is normal and useful.
Attention weights are not explanations. A large weight does not prove the model “used” that token for its final answer; many other parts of the model also shape the result. Treat attention maps as clues, not proof.
Biases or no biases. Finch-19 and GPT-2 put biases on the projections. Llama and most modern models drop them. Qwen keeps them on Q, K and V only. Loading weights into code that expects the other choice either crashes or silently gives wrong answers.