Sam Babichenko

Reading notes / Superposition

A Mathematical Framework for Transformer Circuits

Elhage et al., 2021

Context. Circuit-level reverse engineering had worked for vision models because many conv-net neurons are individually interpretable, so one can read the weights between two of them. A transformer has no such interpretable units: the residual stream has no privileged basis (no coordinate direction with a special meaning), attention is a softmax-gated bilinear operation, and the keys-queries-values description hides how the parts connect. This paper strips the model down (attention only, no biases, layer norm folded into the weights) and reads zero-, one- and two-layer models off their weights. The residual stream is a sum, so the model is a sum over paths, each a product of weight matrices. With the attention patterns frozen every path is linear in the tokens, and each head splits into a QK circuit that decides where to look and an OV circuit that decides what looking there does to the logits. Depth adds composition, a later head's queries or keys reading what an earlier head wrote, and that is how two layers build induction heads, the in-context copying mechanism.

Setting. Attention-only decoder transformers. Positional information enters only the inputs of WQW_Q and WKW_K, never the residual stream. With one-hot tokens tt, embedding WEW_E, unembedding WUW_U, per-head WQh,WKh,WVh∈Rdhead×dmodelW_Q^h,W_K^h,W_V^h\in\mathbb R^{d_{\rm head}\times d_{\rm model}} and WOh∈Rdmodel×dheadW_O^h\in\mathbb R^{d_{\rm model}\times d_{\rm head}}, define WOVh=WOhWVh,WQKh=WQh⊤WKh,Ah=softmax∗(x⊤WQKhx),W_{OV}^h=W_O^hW_V^h,\qquad W_{QK}^h=W_Q^{h\top}W_K^h,\qquad A^h=\mathrm{softmax}^*\big(x^\top W_{QK}^hx\big), two dmodel×dmodeld_{\rm model}\times d_{\rm model} matrices of rank at most dheadd_{\rm head} and the causally masked pattern. On an activation matrix xx with one row per position, A⊗WA\otimes W acts as x↦AxW⊤x\mapsto AxW^\top: mix positions with AA, transform each position's vector with WW. Biases fold into the weights, and layer norm is a fixed affine map times a per-vector scalar. The identities below are exact for this simplified model; the readings of trained weights (copying, induction) are empirical.

Main results. (i) A zero-layer model is T=WUWET=W_UW_E, so it can only learn bigram statistics. (ii) A one-layer model is exactly T=Id⊗WUWE+∑h∈H1Ah⊗WUWOVhWE,Ah=softmax∗(t⊤WE⊤WQKhWE t),T=\mathrm{Id}\otimes W_UW_E+\sum_{h\in H_1}A^h\otimes W_UW_{OV}^hW_E,\qquad A^h=\mathrm{softmax}^*\big(t^\top W_E^\top W_{QK}^hW_E\,t\big), a bigram table plus one skip-trigram model per head (patterns "A … B →\to C"), all nvocab×nvocabn_{\rm vocab}\times n_{\rm vocab} tables that can be read without running the model. (iii) In a two-layer model the logits gain virtual-head terms (Ah2Ah1)⊗WUWOVh2WOVh1WE(A^{h_2}A^{h_1})\otimes W_UW_{OV}^{h_2}W_{OV}^{h_1}W_E, and a second-layer head's attention scores gain terms in which first-layer heads have moved information on the query side, the key side, or both. Empirically, most one-layer heads copy (10 of 12 in one model have strongly positive OV eigenvalues), and the trained two-layer model relies on K-composition (a second-layer head's keys reading a first-layer head's output) between a previous-token head and several second-layer heads, which form induction heads.

Step 1

The residual stream is the sum of every layer's output, so heads act independently and add. Every layer reads from the stream with a linear map and adds its output back, so the stream at any depth is the embedding plus everything written so far. Any two layers are therefore joined by virtual weights WI2WO1W_I^2W_O^1, the later layer's read times the earlier layer's write, however many layers sit between them. The concatenate-then-project form of multi-head attention is likewise a sum, WOH(rh1rh2⋮)=∑iWOhirhi,W_O^H\begin{pmatrix}r^{h_1}\\ r^{h_2}\\ \vdots\end{pmatrix}=\sum_iW_O^{h_i}r^{h_i}, so each head writes to the stream on its own and can be studied alone. Rotating the stream together with every matrix that touches it leaves the model unchanged, so the paper works with weight products, never stream coordinates.

Step 2

A head is an attention pattern tensored with one low-rank matrix. Write a head's three stages (a value vector per position, mixing across positions, an output projection per position) as tensor products and collapse them by the mixed-product property: h(x)=(Id⊗WO)(A⊗Id)(Id⊗WV) x=(A⊗WOV) x.h(x)=(\mathrm{Id}\otimes W_O)(A\otimes\mathrm{Id})(\mathrm{Id}\otimes W_V)\,x=(A\otimes W_{OV})\,x. So a head does two separate things. AA, computed from the bilinear form WQKW_{QK}, says between which positions information moves, and WOVW_{OV} says what is read at the source and written at the destination. Queries, keys and values are only one factorization of these two matrices. The softmax is the only nonlinearity, so running the model once to record every pattern and again with the patterns frozen makes the logits linear in the tokens. Products of heads are again heads, (Ah2⊗WOVh2)(Ah1⊗WOVh1)=(Ah2Ah1)⊗WOVh2WOVh1(A^{h_2}\otimes W_{OV}^{h_2})(A^{h_1}\otimes W_{OV}^{h_1})=(A^{h_2}A^{h_1})\otimes W_{OV}^{h_2}W_{OV}^{h_1}, which is where virtual heads come from.

Step 3

A one-layer model is a bigram table plus skip-trigram tables. Expand (Id⊗WU)(Id+∑hAh⊗WOVh)(Id⊗WE)(\mathrm{Id}\otimes W_U)\big(\mathrm{Id}+\sum_hA^h\otimes W_{OV}^h\big)(\mathrm{Id}\otimes W_E) into the sum over paths in (ii). The direct path cannot move information between positions, so it is a bigram table (Barack is followed by Obama). Each head adds two vocabulary-by-vocabulary tables, the QK circuit WE⊤WQKhWEW_E^\top W_{QK}^hW_E, which scores how much a destination token attends to a source token, and the OV circuit WUWOVhWEW_UW_{OV}^hW_E, which gives the logit change from attending to that source. Together they encode skip-trigrams "A … B →\to C": from B attend back to A and raise C. The largest entries are mostly copying: attend back to tokens that could plausibly come next and raise them. Because the form is factored, the logit effect depends only on the attended source, so a head that raises "keep … in mind" and "keep … at bay" also raises "keep … in bay". Copying is summarized by the OV circuit's eigenvalues (cheap to compute, since λ(AB)=λ(BA)\lambda(AB)=\lambda(BA)): a copying matrix maps a combination of tokens to the same combination of logits, so its eigenvalues are positive. The authors call this suggestive only, since non-orthogonal eigenvectors allow an all-positive spectrum while some tokens lower their own logits.

Step 4

Two layers add composition, and only Q- and K-composition adds expressiveness. A second-layer head reads x1=x0+∑h∈H1h(x0)x_1=x_0+\sum_{h\in H_1}h(x_0) instead of the embeddings. In the logits this only adds the virtual heads of Step 2 (V-composition), with pattern Ah2Ah1A^{h_2}A^{h_1}, which still only moves information between positions. What is new appears in the attention scores. Substituting x1x_1 on both sides of x1⊤WQKhx1x_1^\top W_{QK}^hx_1 gives terms Aq⊗Ak⊗WA_q\otimes A_k\otimes W, where Aq,AkA_q,A_k are Id\mathrm{Id} or a first-layer pattern according to whether a head first moved the query-side or key-side information and WW is the bilinear form, for example Id⊗Ah1⊗WE⊤WQKhWOVh1WE(K-composition).\mathrm{Id}\otimes A^{h_1}\otimes W_E^\top W_{QK}^hW_{OV}^{h_1}W_E\qquad\text{(K-composition)}. A second-layer head can thus attend by what an earlier head moved into a position, which no table over token pairs expresses. The paper scores each pair of heads by ∥WQKh2WOVh1∥F/(∥WQKh2∥F∥WOVh1∥F)\|W_{QK}^{h_2}W_{OV}^{h_1}\|_F/(\|W_{QK}^{h_2}\|_F\|W_{OV}^{h_1}\|_F) against random matrices. Most heads do not compose, and the significant composition is K-composition from one first-layer head that attends mainly to the previous token.

Step 5

An induction head is K-composition with a previous-token head plus a copying OV circuit. An induction head completes [a][b] … [a] →\to [b]. Take the K-composition term Id⊗Ah−1⊗W\mathrm{Id}\otimes A^{h_{-1}}\otimes W with a previous-token pattern Ah−1A^{h_{-1}}: the key at position jj carries the token at j−1j-1, so a same-token bilinear form WW raises the score of exactly the positions whose predecessor matches the current token, and a copying OV circuit raises the token found there. Unlike one-layer copying heads, it uses how the token was used earlier, whatever the corpus statistics. Two checks support this. The heads behave the same way on uniformly random tokens repeated three times, where corpus statistics cannot help. And every induction head has strongly positive eigenvalues in both its OV circuit and its K-composition QK term; the authors note this is not circular, since the heads were selected for large composition, which says nothing about its sign.

Step 6

Path ablations show that the virtual heads barely matter here. Freeze every attention pattern at its recorded value and rerun the model several times. The first frozen run replaces every head's output with zero, leaving the direct path, and each later run adds to the stream, in place of each head's current output, the output that head produced on the run before. The nn-th run then contains exactly the OV paths through at most n−1n-1 heads, and successive loss differences give the marginal value of each order. In the two-layer model the second-order (V-composition) terms add little, and among first-order terms the second-layer heads carry most of the gain. Since the patterns are frozen, this says nothing about Q- and K-composition, which act only through the patterns.

Consequences and limits. For attention-only models, interpretation becomes the problem of summarizing large, very low-rank matrices: nothing algorithmic is hidden, but each vocabulary table has billions of entries. The sharp qualitative finding is that one layer can only build skip-trigrams while two layers build an algorithm, so in-context copying changes character at depth two. The main limit is the one the authors stress: MLP layers hold about two thirds of a standard transformer's parameters, a GeLU cannot be linearized through, and the path picture then needs interpretable neurons, which were rare. They expect superposition in the residual stream, far narrower than the heads and neurons that read and write it, which the later superposition papers take up.

Terms

Residual stream
embedding plus every layer's output; linear, with no privileged basis
WOVh=WOhWVhW_{OV}^h=W_O^hW_V^h
what a head reads at the source and writes at the destination; rank at most dheadd_{\rm head}
WQKh=WQh⊤WKhW_{QK}^h=W_Q^{h\top}W_K^h
bilinear form that produces the head's attention scores
A⊗WA\otimes W
acts as x↦AxW⊤x\mapsto AxW^\top: mix positions by AA, map each vector by WW
QK circuit WE⊤WQKhWEW_E^\top W_{QK}^hW_E
token-by-token table of attention scores
OV circuit WUWOVhWEW_UW_{OV}^hW_E
token-by-token table of logit effects of attending to a token
Skip-trigram
A … B →\to C: from B attend to A and raise C; one-layer heads are made of these
Q-, K-, V-composition
a later head's query, key or value reads what an earlier head wrote
Virtual head
(Ah2Ah1)⊗WOVh2WOVh1(A^{h_2}A^{h_1})\otimes W_{OV}^{h_2}W_{OV}^{h_1}, the path through two heads
Induction head
[a][b] … [a] →\to [b]; K-composition with a previous-token head plus copying

Quiz

  1. Why does freezing the attention patterns make a trained attention-only model's logits linear in the tokens?
  2. Why does the paper never look for interpretable coordinates in the residual stream?
  3. Positive OV eigenvalues are used as the paper's summary of copying. What is the authors' caveat?
  4. Induction heads behave the same on uniformly random tokens repeated three times. What does this check establish?

Sources