techtree / Attention

Queries, keys, and values

Three matrices turn each token vector into a query, a key, and a value.

43 seconds

q=WQxk=WKxv=WVx\mathbf{q} = W_Q \mathbf{x} \qquad \mathbf{k} = W_K \mathbf{x} \qquad \mathbf{v} = W_V \mathbf{x}
Transcript

Here is one token's vector. The model keeps three matrices for this step.

Multiply the vector by the first, and you get a query. By the second, a key. By the third, a value.

Every token goes through the same three matrices, so every token gets its own query, key, and value.

The query says what this token is looking for. The key says what it has to offer. The value is what it hands over.

A query and a key match when they point the same way, so their dot product is large.

One vector in. Three vectors out: a query, a key, and a value.