11.3 — Attention heads as bigram, skip-bigram, and copying circuits
Goal: Derive the QK and OV circuits of a single attention head in a 1-layer attention-only Transformer. Show that the QK and OV circuits compose into recognisable algorithmic primitives: bigram (look at previous token, predict next), skip-bigram (look at a related token earlier in context, copy or transform), copying (look at a previous token, write its embedding). The first 'algorithm we can read'.
1. Why this session exists
Cap 11.2 gave us the language: residual stream, additive contributions, QK and OV circuits per head. This session puts that language to work on the simplest possible Transformer — one layer, attention-only, no MLPs — and shows that even this minimal model implements recognisable algorithms in its heads.
The framework is from Elhage et al. (Anthropic, 2021, A Mathematical Framework for Transformer Circuits §2). It is the cleanest possible setting for mech-interp: no MLPs, no inter-layer composition, no MoE, no residual stream chemistry. Just the matrices and the softmax.
By the end of this session you should be able to look at a 1-layer attention-only model and read off what each head is computing by inspecting two matrices per head — the QK matrix and the OV matrix — and recognise the bigram, skip-bigram, and copying algorithms.[2]
2. The 1-layer attention-only Transformer
The minimal model. Token at position . Single attention head with parameters and . Token embedding (columns = embeddings); unembedding .
Forward pass (ignoring positional encodings and LayerNorm for clarity):
Two "paths" from the input to the logit:
- Direct path. . Logit contribution of the current token's embedding alone. Implements a constant "bigram" component — what would the model predict if it only looked at the current token?
- Attention path. . Logit contribution mediated by attention to other tokens.
This is the path decomposition of the model: the logit is a sum of paths through different components.
3. The OV circuit
The OV ("output-value") circuit is the matrix product
It tells you: if token is attended to with weight , what gets written to the residual stream? The contribution to from attending to position with weight is .
3.1 The OV-to-logit composition
To understand the effect of attending to on the prediction of , we compose with the unembed:
This is the "OV-output" matrix: entry tells you "if the model attends to a token , how much does the logit for token change?"
If the OV-output matrix were diagonal (), the head would copy: attending to token predicts token as the next token. If it were the bigram-frequency matrix of natural language, the head would bigram-predict: attending to token predicts whatever statistically follows .
3.2 What the OV circuit is not
The OV circuit doesn't choose which token to attend to. That's the QK circuit's job. OV only says: given that some token is attended to, what gets written.
Two heads with the same OV circuit but different QK circuits are computing very different functions — they're applying the same "what" to different "where" choices. Pre-2021 interpretability conflated these; the QK/OV factorisation cleanly separates them.
4. The QK circuit
The QK ("query-key") circuit is the bilinear matrix
The attention-score between positions and is
Pre-softmax, the score is a bilinear form in .
4.1 The token-token QK score
Composing with embeddings:
A matrix of pairwise token-token scores. The QK circuit says: "score the query position's token against the key position's token using this bilinear form, before softmax."
If this matrix is highest on the diagonal (), the head looks for repetitions of the current token.
If it is highest off-diagonal with specific patterns (" = surname, = matching first name"), the head looks for related tokens.
If it is near-uniform, the head attends roughly equally to all tokens — a "global summary" head.
4.2 Position-token interactions
In models with positional embeddings, the QK score has additional terms. With learned absolute PEs:
so
Different "components" of attention live in different terms. The position-position term gives positional patterns like "always attend to the previous token" (which a model can implement via a QK matrix that scores highly when queried by ).
For RoPE-based models (Cap 6.7), the position-position interactions are baked into a rotation; the decomposition still goes through but with rotation matrices in the bilinear form.
5. Bigram heads
The simplest algorithmic primitive: bigram = "predict the next token from the current token alone".
A head implements bigram-prediction if:
- Its QK circuit attends almost exclusively to the current position ().
- Its OV-output matrix is approximately the empirical bigram-transition matrix of the corpus.
Operationally: the head doesn't really "attend" to anything except itself; the OV-output then writes the most-likely-next-token distribution into the logits.
In a 1-layer attention-only model, you often find – such heads. They implement the conditional next-token distribution — the bigram baseline (Cap 5.4).
5.1 The direct path is also a bigram
Note: the direct path is itself a matrix giving "if the input is token , what logit boost does each next token get directly from the embedding/unembedding?" In weight-tied models (W^U = W^E^\top), this is approximately the inner-product matrix of the embeddings — and the highest-inner-product token to is itself (the embedding of is similar to itself). So the direct path's biggest contribution is to predict the same token repeats, which is a degenerate bigram.
This is one reason 1-layer models are bad at generation — they have nowhere to put a real bigram circuit except in attention heads, and those heads must compete with the direct path's repeat-prediction.
6. Skip-bigram heads
A skip-bigram is a pattern where and are not adjacent. E.g., "Once upon a time, in a faraway kingdom, there lived a " — the model wants to predict "princess" because "kingdom" is in scope, even though several tokens intervened.
A skip-bigram head implements this:
- Its QK circuit attends from position to some position where is related to .
- Its OV-output writes something like into the logit (or a transformation of it).
Concretely, in Elhage et al.'s 1-layer attention-only model trained on text:
- Several heads attend strongly to the previous occurrence of the current token — what you might call a "duplicate-token-detector". When the input has a repeated token for some earlier , the head fires.
- Other heads attend to semantically related tokens (e.g., from "Paris" to "France" earlier in context).
The OV-output then writes the relevant continuation. For a duplicate-token-detector + copy OV, the algorithm is "if the current token appeared before, predict whatever followed it last time" — a skip-bigram of order 1.
6.1 Why this matters
Skip-bigram heads are the primitive from which induction heads (Cap 11.4) are built. A 2-layer model can compose a duplicate-token-detector head (in layer 1) with a copy head (in layer 2) to form an induction circuit — a general-purpose mechanism for in-context learning. The composition is the whole point of stacking layers.
7. Copying heads
A copying head is one whose OV-output matrix is approximately diagonal: attending to token predicts .
Concretely: for some scalar . Then attending to adds to the logit — i.e., the logit of "the next token = " is boosted.
Whether the model predicts depends on the size of the boost relative to the rest of the logit landscape. Often the boost is large enough that, when the head fires (i.e., the QK circuit selects a specific ), the prediction is essentially "copy the attended-to token."
7.1 Identifying copying heads empirically
Two diagnostic tests:
- Eigenvalue structure of . Copying heads have large positive eigenvalues on the diagonal — the matrix is dominated by an identity-like component. Numerically: extract the trace and compare to the spectral norm.
- OV-output behaviour on synthetic prompts. Construct prompts where attending to a specific token is forced (e.g., by token rarity). Check what the head's contribution to the logit is. A copy head boosts the attended-to token's logit.
Both tests are routine in mech-interp toolkits (TransformerLens, Penzai, the Anthropic interpretability stack).
8. Composition in deeper models
Everything above is for 1-layer attention-only models. The story compounds in deeper models.
8.1 K-composition, Q-composition, V-composition
In a 2-layer model, the residual stream after layer 1 contains the embeddings plus whatever layer-1 heads wrote. Layer-2 heads read this enriched stream. Three composition modes (Elhage et al., 2021 §4)[1]:
- K-composition. A layer-2 head's key projection reads a layer-1 head's output. The layer-2 head's QK pattern depends on what layer 1 wrote.
- Q-composition. Same, but for query.
- V-composition. A layer-2 head's value projection reads a layer-1 head's output. The layer-2 head's OV writes are partly built from layer-1 features.
These compositions are the substrate for circuits. Induction heads (Cap 11.4) are a K-composition: a layer-2 head's keys read a "previous-token" feature written by a layer-1 head.
8.2 Read-write subspace alignment
Whether a layer-1 head's output is visible to a layer-2 head depends on whether the layer-1 head's writer subspace overlaps with the layer-2 head's reader subspace. With and , each head's writer/reader subspace is of the total. Most pairs of heads have small overlap; specific composition pairs have learned-to-align subspaces and communicate strongly.
Identifying these "communication channels" is one of the structural tools mech-interp uses to find circuits. The subspace-overlap of all pairs (or triples) of heads, computed once per model, gives a sparse graph of likely composition.
9. What we keep going forward
Three things.
- A head = QK circuit + OV circuit. QK chooses what to attend to; OV chooses what to write given an attended-to position. Both are matrices in (or their token-space images, , via the embeddings).
- Three algorithmic primitives identifiable in 1-layer attention-only models: bigram (attend to self, predict next), skip-bigram (attend to a related earlier token, predict its continuation), copy (attend to a token, write that token's embedding back).
- Composition in deeper models. Layer- heads can read what layer- heads wrote, via K/Q/V-composition. This is the substrate for circuits across layers — the topic of Cap 11.4 (induction heads as the canonical example).
Cap 11.4 next: induction heads. The first identified, general-purpose, cross-layer algorithm — the 2-head circuit that does in-context pattern matching A B … A → B.
(a) Write the forward pass of a 1-layer attention-only Transformer. Identify the direct path and the attention path through to the logit.
(b) Define the OV circuit and the OV-output matrix . What does the entry mean?
(c) Define the QK circuit and the token-token score . What does a diagonal-dominant QK matrix mean operationally?
(d) A bigram head: state the QK pattern and the OV-output structure that characterise it.
(e) A copying head: state the OV-output structure that characterises it. Give an eigenvalue-based diagnostic test.
(f) K-composition between a layer-1 and a layer-2 head: state what it means structurally and why subspace alignment determines whether composition actually happens.
References
- [1]
Elhage, N., Nanda, N., Olsson, C., et al. (2021). "A mathematical framework for transformer circuits". Transformer Circuits Thread. transformer-circuits.pub — Residual stream, QK/OV decomposition, K/Q/V composition (11.2, 11.3).
↩ - [2]
Wang, K., Variengien, A., Conmy, A., et al. (2023). "Interpretability in the wild: A circuit for indirect object identification in GPT-2 small". arXiv:2211.00593 — IOI circuit (11.2, 11.3).
↩