Part B · Chapter 8 · Attention

8. Queries, keys and values

Attention gives every word three new vectors, made by three small matrices: a query that asks, a key that answers, and a value that is passed on.

Three matrices, three vectors for every word
Playback speed

Charles's example, “She eats apples”, with each word as two numbers. Step by step, each word's vector is multiplied by the three small matrices W_Q, W_K and W_V, and the products fill three tables: the queries Q, the keys K and the values V. The readout writes each multiplication out.

The idea

Charles's post lists the steps. First, “each token in the input sequence is embedded as a vector” (chapter 5). Then “for each token, three distinct vectors are computed using learned linear transformations”: a query, a key and a value. Each comes from multiplying the word's vector by its own matrix (chapter 4): \(W^Q\), \(W^K\) and \(W^V\).

Think of a library. The query is what a word is looking for; the key is the label on each word's spine, saying what it offers; the value is the book's content, what a word actually hands over when it is picked. A word compares its query with every key (chapter 9 does it with dot products), and takes in the values in proportion to how well they match.

Why three vectors and not one? Because what a word looks for, what it is found by and what it passes on need not be the same. “it” may look for a noun, be found as a pronoun, and pass on that it is the object of the verb. Three matrices let the model learn each separately.

A worked example: “She eats apples”

The post gives She the vector [1, 0], eats [0, 1] and apples [1, 1], and says what the projections produce: the query of She is [1, 0], the key of apples [1, 1], the value of apples [0, 1]. It does not show the matrices. These three, chosen by hand, produce exactly those numbers:

W^Q = \begin{bmatrix}1 & 0 \\ 1 & 1\end{bmatrix}, \qquad W^K = \begin{bmatrix}0 & 1 \\ 1 & 0\end{bmatrix}, \qquad W^V = \begin{bmatrix}1 & 0 \\ -1 & 1\end{bmatrix}

Multiply them out, as the animation does: q_She = [1, 0] × W^Q = [1, 0]; k_apples = [1, 1] × W^K = [1, 1]; v_apples = [1, 1] × W^V = [0, 1]. Every word gets all three: the query of eats is [1, 1], its key [1, 0], its value [−1, 1].

The paper's model does the same with 64 numbers per word and learned matrices. Here are the first eight numbers of the query, key and value of “She” in head 1 of the first encoder layer, for “She eats apples .”:

q, k and v of “She”, layer 1, head 1 (first 8 of 16 numbers)
Token12345678… 8 more
q−0.111.250.110.51−0.89−1.930.67−0.49…
k−1.490.321.291.52−2.14−0.04−0.370.96…
v−0.43−0.42−0.12−0.88−0.500.500.820.01…

The formula

For all the words at once, stacked as the rows of X:

Q = XW^Q, \qquad K = XW^K, \qquad V = XW^V
\(X\)
the input: one row per token, d_model numbers each
\(W^Q,\ W^K,\ W^V\)
three learned matrices, d_model rows each
\(Q,\ K,\ V\)
the queries, keys and values: one row per token
\(d_k,\ d_v\)
the length of each query and key, and of each value: 64 in the paper, 16 in our model

In the paper each head has its own three matrices, \(W_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}\), with \(d_k = d_v = d_{\text{model}}/h = 64\) for its 8 heads (section 3.2.2). Ours: 64 / 4 = 16.

Why it matters

These matrices are a large share of a model's parameters: per attention layer, W^Q, W^K, W^V and the output matrix W^O (chapter 11) are 4 × 64 × 64 = 16,384 numbers in our model, and 4 × 512 × 512, about a million, in the paper's. They also explain a trick inference servers use: the keys and values of tokens already read never change, so they can be kept and reused while the answer is written. That cache is part of what makes long conversations affordable, and part of why they need memory.

Where it fails

  • The names are an analogy. Nothing forces a key to be a label or a value to be content; training shapes the three matrices into whatever predicts best. The library picture helps to start, and stops there.
  • The example's matrices are hand-made. They were chosen so the post's numbers come out; a trained model's are learned, and nobody picks them.
  • Two numbers per word is a toy. With two numbers, She and apples can only differ in two ways. The real model's 64 numbers per word, split into four heads of 16, leave room for many relations at once.
Remember Each word's vector is multiplied by three learned matrices: W^Q gives the query (what it looks for), W^K the key (what it offers), W^V the value (what it passes on).

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

The query of eats

eats is [0, 1] and W^Q = [[1, 0], [1, 1]]. What is the second number of the query of eats?

Your answer
A whole number

A query meets a key

The query of eats is [1, 1] and the key of apples is [1, 1]. What is their dot product?

Your answer
A whole number

What is passed on?

Which matrix makes the vector a word hands over to the words that attend to it?

Counting the projections

Our model's d_model is 64. How many numbers are in W^Q, W^K, W^V and W^O of one attention layer together, each 64 × 64?

Your answer
A whole number

In the paper

Section 3.2 opens with the definition this chapter illustrates: “An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors.” Section 3.2.2 adds the learned projections, \(W_i^Q\), \(W_i^K\) and \(W_i^V\), one set per head.

In the paper: section 3.2

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.