Dive into Deep Learning · §10.6
What attention computes
the residual stream · QK and OV circuits · induction heads · in-context learning in miniature
Attention patterns and value transformations
Each position carries a d-dimensional vector from embedding to logits. Layers never overwrite it; heads only add:
\mathbf{h}_i^{(\ell)} = \mathbf{h}_i^{(\ell-1)} + \sum_{h=1}^{H} \sum_{j \leq i} \alpha_{ij}^{\ell h}\, \mathbf{W}_{\mathrm{OV}}^{\ell h}\, \mathbf{h}_j^{(\ell-1)}
Attention-only, no FFN, no LayerNorm (Elhage et al., 2021): with the patterns fixed, the map from embeddings to logits is linear and can be expanded exactly. TinyCharLM uses this restricted architecture.
Each head is described by two matrices that can be read from the checkpoint:
s_{ij} = \frac{\mathbf{h}_i^\top \mathbf{W}_{\mathrm{QK}} \mathbf{h}_j}{\sqrt{d_h}}, \qquad \mathbf{W}_{\mathrm{QK}} = \mathbf{W}_q^\top \mathbf{W}_k, \qquad \mathbf{W}_{\mathrm{OV}} = \mathbf{W}_o \mathbf{W}_v
W_QK: shape (128, 128), rank 32
W_OV: shape (128, 128), rank 32
With attention patterns as if–then rules, the one-layer logits expand into paths:
\mathbf{z}_i = \underbrace{\mathbf{E}\,\mathbf{e}_{x_i}}_{\textrm{low-rank bigram}} + \sum_{h}\sum_{j \leq i} \alpha_{ij}^{h}\; \underbrace{\mathbf{E}\,\mathbf{W}_{\mathrm{OV}}^{h}\,\mathbf{e}_{x_j}}_{\textrm{skip-trigram}}
Layer 1 writes each token’s predecessor into its stream; layer 2 matches “I am [\mathrm{A}]” against “follows [\mathrm{A}]” and copies. Whether gradient descent finds this circuit is an empirical question.
Each batch contains newly sampled tiled patterns. Because average bigram statistics are uniform, predicting later copies requires retrieval from the context:
pattern lengths: [8, 6]
[[2 2 6 5 1 7 2 0 2 2 6 5 1 7 2 0]
[1 5 8 1 4 1 1 5 8 1 4 1 1 5 8 1]]
With a fixed pattern length of 32, a one-block model obtains nearly perfect accuracy. This experiment uses bias=True:
first copy 4.41 nats, second copy 0.09 nats, second-copy accuracy 1.00
We probe the same model with patterns of length 24.
patterns of length 24: second-copy accuracy 0.02
attention mass at offset 31: 0.96
attention mass at offset 23: 0.00
qkv bias makes that head cheap: a constant, content-free query. With biases disabled, as in every model analyzed below, the shortcut is much less accurate.A synthetic benchmark may admit a simpler circuit than the intended mechanism. Sampling the period removes this fixed-offset solution.
Pattern lengths are uniform from 16 to 32, so no fixed offset solves every example.
2 blocks: first copy 4.45, later copies 0.45, accuracy 0.87
1 block: first copy 4.31, later copies 3.67, accuracy 0.11
Block 1 has a sharp line one step below the diagonal, consistent with a previous-token head. Block 2 has a stripe at offset L-1 beginning with the second copy, consistent with an induction head.
Average mass on the previous token vs. on the induction target j = i + 1 - L:
block 1: previous token 0.02 0.01 0.92 0.03 | induction target 0.01 0.00 0.00 0.01
block 2: previous token 0.04 0.04 0.04 0.04 | induction target 0.62 0.68 0.69 0.50
Every evaluation pattern is newly sampled, so the result is consistent with a content-based copying rule rather than memorized training sequences:
One exposure suffices for the displayed examples; restart locations are sampled independently of the token values.
If block 2 copies, attending to token a should boost logit a: C_{ab} = \mathbf{e}_b^\top \mathbf{W}_{\mathrm{OV}} \mathbf{e}_a should be diagonal-dominant. This property can be checked without a forward pass:
rows whose largest entry is the diagonal: 0.77 (chance 1/64)
Feed-forward layers, normalization, and superposed features make the same analysis harder in full transformers. Only a limited set of circuits, including induction heads, has comparable supporting evidence.
TinyCharLM learns the induction circuit after an abrupt loss reduction; a fixed-period task is solved by a positional shortcut instead.