QK and OV circuits of a head
Rewrite a head as a bilinear score W_Q W_Kᵀ that decides where to attend and a map W_V W_O that decides what gets written back into the running per-token vector. Explain why both are low-rank (rank ≤ d_head) d×d matrices.
Builds on: Multi-head attention, Rank and low-rank factorization, The transformer block and the running vector
What the animation says
Four matrices, or two jobs?
One attention head carries four weight matrices: query, key, value, and its own slice of the output matrix. Watch where each one is used. Query and key only ever meet inside one dot product: the attention score. Value's output only ever passes through the output slice. So what does the score between two tokens really depend on?
QK: one product, same score
It depends only on the product of query and key-transposed. Call it the QK matrix. Tiny head: two dimensions in, one dimension per head. Query token one, one gives three; key token two, zero gives six. Their product, the score, is eighteen. Now fold the weights first. The QK matrix is three, minus one; six, minus two. The query times that matrix is nine, minus three. Dot it with the key: eighteen again. Same score. Only the product matters, so many query-key pairs make the same head.
Square, but low-rank
The QK matrix is d by d, here two by two. It scores pairs of full token vectors. But it was built through a width of one: the head dimension. So its rank is at most one. Both rows point along a single direction. In a real model, d might be four thousand ninety-six and the head width one hundred twenty-eight. Then QK is four thousand ninety-six square, but rank at most one hundred twenty-eight. Tap the row that is exactly twice the other.
OV: what a source writes
Now the second job: what gets written back. Concatenating heads then multiplying by the output matrix is a sum over heads, each using its own row block. Value times that block gives one d by d map, the OV matrix, with rank at most the head width. Tiny head: value is one, zero; output slice two, one. So OV is two, one; zero, zero. Source token two, zero writes four, two per unit of attention. With attention one half on that source, what lands in the destination's running vector?
A head is two low-rank maps
Half of four, two is two, one. Attention only sets the mixing weight; OV fixes the content. So a head is not really four matrices. It is two d by d maps. QK decides where to look. OV decides what to write. Both are square in the model width, but squeezed through the head width, so each has rank at most the head width.
Two separable jobs: where, and what
QK: one bilinear score on full vectors
OV: what a source writes back
Tiny head: d = 2, $d_{\text{head}}$ = 1
A head is two low-rank d×d maps
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses