What multiple attention heads can represent

โ† Back to Blog & Articles

Natural language relationships are not one-dimensional in this way.

the companion book defined scaled dot-product attention as a function of three matrices derived from an input sequence ๐—โˆˆโ„nร—dmodel\mathbf{X} \in \mathbb{R}^{n \times d_{\text{model}}}:

Attention(๐,๐Š,๐•)=softmax(๐๐ŠโŠคdk)๐•,๐=๐—๐–Q,๐Š=๐—๐–K,๐•=๐—๐–V, \text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}\!\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\mathbf{V}, \qquad \mathbf{Q} = \mathbf{X}\mathbf{W}^Q,\ \mathbf{K} = \mathbf{X}\mathbf{W}^K,\ \mathbf{V} = \mathbf{X}\mathbf{W}^V,

with ๐–Q,๐–K,๐–Vโˆˆโ„dmodelร—dk\mathbf{W}^Q, \mathbf{W}^K, \mathbf{W}^V \in \mathbb{R}^{d_{\text{model}} \times d_k} a single, shared set of learned projections. This single-head formulation has a structural limitation that has nothing to do with capacity in the parameter-counting sense and everything to do with what one softmax can represent. For a fixed query position ii, the attention weights ฮฑij=softmaxj(๐ชiโŠค๐คj/dk)\alpha_{ij} = \text{softmax}_j\!\left(\mathbf{q}_i^\top \mathbf{k}_j / \sqrt{d_k}\right) form a single probability distribution over key positions jj, and the output โˆ‘jฮฑij๐ฏj\sum_j \alpha_{ij}\mathbf{v}_j is a single convex combination of value vectors. One head, one query vector, one similarity function, one distribution, one weighted average โ€” every kind of relationship the model might need to represent at position ii (which token is the syntactic subject, which token is the most recent noun, which token is three positions back, which token shares a coreference chain) has to be blended into that one distribution and that one averaged output.

Natural language relationships are not one-dimensional in this way. A pronounโ€™s representation may simultaneously need information about its syntactic antecedent (typically nearby), its semantic referent (which may be many tokens back), and its own local position in the clause. Forcing all of that through a single softmax over a single learned similarity metric means the model has to find one compromise attention pattern that serves every relationship at once, which is exactly the representational bottleneck multi-head attention is designed to remove.

The fix, introduced alongside scaled dot-product attention itself (Vaswani et al., 2017), is to stop using one full-width projection and instead use hh independent, lower-dimensional projections, each producing its own query, key, and value matrices, each running its own scaled dot-product attention, each therefore free to learn its own similarity function and produce its own distribution over key positions. Concretely, the model dimension dmodeld_{\text{model}} is split into hh heads of dimension

dk=dmodelh, d_k = \frac{d_{\text{model}}}{h},

and each head operates entirely within its own dkd_k-dimensional subspace of the representation. โ€œSubspaceโ€ here is doing real work: head iiโ€™s query and key projections, ๐–iQ,๐–iKโˆˆโ„dmodelร—dk\mathbf{W}_i^Q, \mathbf{W}_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k}, map the full dmodeld_{\text{model}}-dimensional token representation down into a dkd_k-dimensional space specific to that head, and the dot-product similarity that drives head iiโ€™s attention pattern is computed entirely within that subspace, independent of what any other headโ€™s projections do with the same input. Two heads reading the same token representation ๐ฑi\mathbf{x}_i can therefore extract and compare completely different projections of it โ€” one subspace might end up encoding something like relative token distance, another something closer to a semantic-role relationship โ€” without either headโ€™s softmax having to compromise with the otherโ€™s.

Crucially, this added representational flexibility is close to free in terms of raw compute, because splitting dmodeld_{\text{model}} into hh pieces of size dk=dmodel/hd_k = d_{\text{model}}/h and running hh attention computations of width dkd_k costs, in aggregate, almost exactly what one attention computation of width dmodeld_{\text{model}} would have cost. The next section makes this precise for both parameters and FLOPs; the short version is that multi-head attention does not multiply cost by hh โ€” it redistributes a fixed budget across hh parallel, lower-dimensional computations. The number of heads hh is therefore best understood not as a knob that trades cost for quality, but as a knob that trades subspace width for subspace count at roughly fixed total cost: fewer, wider heads compute richer per-head similarity functions over fewer distinct relationship types; more, narrower heads compute cheaper per-head similarity functions over more distinct relationship types. Neither extreme is free โ€” a single head with dk=dmodeld_k = d_{\text{model}} recovers exactly the companion bookโ€™s mechanism and its one-compromise-pattern limitation, while very large hh eventually starves each head of enough dimensions to represent a useful similarity function at all โ€” which is why hh is a tuned architectural hyperparameter rather than a quantity derived from first principles.

Published architectures make this tradeoff differently at different scales, and the numbers are worth having as concrete anchors. The original Transformer (Vaswani et al., 2017) used dmodel=512d_{\text{model}} = 512 with h=8h = 8, giving dk=64d_k = 64. GPT-2โ€™s largest published configuration (Radford et al., 2019) uses dmodel=1600d_{\text{model}} = 1600 with h=25h = 25, again giving dk=64d_k = 64. GPT-3 (Brown et al., 2020) scales up to dmodel=12,288d_{\text{model}} = 12{,}288 with h=96h = 96, once more landing on dk=128d_k = 128. The recurrence of dkd_k values clustered around 64โ€“128 across three orders of magnitude of dmodeld_{\text{model}} is not a coincidence forced by the mathematics derived above โ€” nothing in the multi-head formalism requires dkd_k to stay roughly fixed as dmodeld_{\text{model}} grows โ€” but an empirical convention: practitioners have generally chosen hh to scale roughly in proportion to dmodeld_{\text{model}}, keeping each headโ€™s subspace width in a range that has been found to work well, rather than fixing hh and letting dkd_k grow arbitrarily large or fixing dkd_k at some other value. Treat this as a documented design convention, not a theorem; later chapters revisit the memory-bandwidth motivations (the companion bookโ€™s treatment of multi-query and grouped-query attention) that push some modern architectures away from this convention specifically for the key/value projections.

Whether individual heads specialize into cleanly interpretable roles is an empirical question, not a guarantee of the architecture. It is a documented phenomenon: Vaswani et al. (2017) themselves illustrate, via attention-weight visualizations of their trained model, heads that appear to track distinct patterns โ€” some concentrating on immediate neighbors or fixed positional offsets, others attending across long distances to what look like syntactically or referentially related tokens. Subsequent work probing trained transformer encoders has repeatedly found similar structure: some heads with attention patterns that align closely with syntactic dependencies, others that behave close to positional or โ€œlocal windowโ€ detectors, and others still that are harder to characterize with any single human-readable label. This should be read as an observed regularity in specific trained models on specific tasks, not as a proof that heads always specialize, that a given headโ€™s role is stable across layers or training runs, or that the resulting attention weights constitute a faithful explanation of the modelโ€™s computation โ€” attention weights show what a head averages over, not why the rest of the network uses that average, and the interpretability literature (returned to in the companion book) treats attention patterns as suggestive evidence rather than a complete causal account of model behavior.

Continue with the book

Explore the full treatment, worked examples, and exercises in LLMs Derived.


Companion article for LLMs Derived, published by Choral Software. Article text and original examples ยฉ 2026 Choral Software. All rights reserved.

Scroll to Top