All courses

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

▶ Watch this lesson Start the whole course

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.

What does one head actually compute?

A head carries four weight matrices: $W_Q$, $W_K$, $W_V$ and its slice of $W_O$. Look at where they appear. $W_Q$ and $W_K$ only ever meet inside a query·key dot product. $W_V$'s output only ever passes through $W_O$. So are four matrices the real description? Or is a head really two objects, one deciding *where* to look and one deciding *what* to write back?

Two separable jobs: where, and what

**QK circuit**: a scoring rule. It compares the destination token's vector with each source token's vector and says how strongly to attend. **OV circuit**: a fixed map. It turns a source token's vector into the vector added to the destination's running vector. The attention pattern only sets the mixing weights. Both jobs squeeze through a $d_\text{head}$-wide bottleneck, so each is low-rank.

QK: one bilinear score on full vectors

Let $X_t$ be row $t$ of the head's input $X$. The score of query position $t$ on key position $s$ is computed before the causal mask $M$ is added; $M$ does not depend on any weights. It is $$\frac{(X_tW_Q)(X_sW_K)^\top}{\sqrt{d_\text{head}}}=\frac{X_t\,W_{QK}\,X_s^\top}{\sqrt{d_\text{head}}},\quad W_{QK}=W_QW_K^\top$$ $W_{QK}$ is $d\times d$, a **bilinear score** on pairs of full vectors. Its inner dimension is $d_\text{head}$, so its rank is $\le d_\text{head}$. Only the product matters: many $(W_Q,W_K)$ pairs give the same head.

OV: what a source writes back

Concatenating the heads and then multiplying by $W_O$ equals a sum over heads. Split $W_O$ into row blocks $W_O^{(i)}\in\mathbb{R}^{d_\text{head}\times d}$. Then the output is $\sum_i A_iV_iW_O^{(i)}$. For head $i$, with $W_V$ its value projection: $$A_i\,X\,W_VW_O^{(i)}=A_i\,X\,W_{OV},\quad W_{OV}=W_VW_O^{(i)}$$ $W_{OV}$ is $d\times d$ with rank $\le d_\text{head}$. It reads at most $d_\text{head}$ directions of a source vector and writes into at most $d_\text{head}$ directions.

Tiny head: d = 2, $d_{\text{head}}$ = 1

Take $W_Q=\begin{bmatrix}1\\2\end{bmatrix}$ and $W_K=\begin{bmatrix}3\\-1\end{bmatrix}$. Then $W_{QK}=\begin{bmatrix}3&-1\\6&-2\end{bmatrix}$, which has rank 1 (row 2 = 2×row 1). Query $X_t=[1,1]$, key $X_s=[2,0]$. Here $\sqrt{d_\text{head}}=1$, so the dot product is the score. - Separately: $q=3$, $k=6$, $qk=18$. - Combined: $X_tW_{QK}=[9,-3]$, dot $[2,0]$ gives $18$. Same. OV: $W_V=\begin{bmatrix}1\\0\end{bmatrix}$ and $W_O^{(i)}=[2,1]$ give $W_{OV}=\begin{bmatrix}2&1\\0&0\end{bmatrix}$. Source $[2,0]$ writes $[4,2]$ per unit of attention.

A head is two low-rank d×d maps

A head is $W_{QK}=W_QW_K^\top$ plus $W_{OV}=W_VW_O^{(i)}$. - $W_{QK}$ decides where to attend, through the score $X_tW_{QK}X_s^\top$. - $W_{OV}$ decides what a source writes into the running vector. Both are $d\times d$ but factor through $d_\text{head}$, so their rank is $\le d_\text{head}$. With $d=4096$ and $d_\text{head}=128$, each uses at most 128 of 4096 directions.

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.