Transformers and language models
Multi-head attention
Multi-head attention runs several attention patterns in parallel, each on a slice of the model width, then merges them with one output matrix.
One attention head learns one pattern of routing: one way of deciding which tokens matter. A real model needs many patterns at once, such as one head that tracks the previous word and another that matches a pronoun to its noun. Multi-head attention runs several heads in parallel, each with its own projections, and combines their outputs.
Concept
Before you start: a head is one independent attention computation with its own W_Q, W_K and W_V. A head dimension d_k is the width each head works in.
Splitting the width
Start with a model width d_model and h heads. Each head gets a width of d_k = d_model / h, so the total width stays the same. Each head computes its own attention, then the results are concatenated and projected back:
head_i = Attention(X W_Q^i, X W_K^i, X W_V^i)
MultiHead(X) = Concat(head_1, …, head_h) W_OW_O has shape d_model × d_model. Adding up the Q, K, V and O projections gives about 4·d_model² parameters per layer, ignoring biases.
Walking through the shapes
Take a Llama-style layer with d_model = 4096 and h = 32 heads:
- Input
Xhas shape(batch, 128, 4096)for 128 tokens. - Each projection gives
(batch, 128, 4096). Reshape it to(batch, 32, 128, 128), so each of the 32 heads works in width 128. - Each head computes attention, giving
(batch, 32, 128, 128). - Merge the heads back to
(batch, 128, 4096)and applyW_O.
Parameter count for the four projections is 4 × 4096² = 67,108,864 per layer, the same order as a single full-width head. You split the width. You do not multiply the cost by h.
For comparison, GPT-2 small used d_model = 768 with h = 12, so each head had d_k = 64.
Why several heads help
Different heads tend to specialize. Some copy patterns from earlier in the sequence, some follow syntax, and some attend to nearby tokens. Empirically, many small heads do better than one wide head at the same compute.
Trade-off
More heads or wider heads. With d_model fixed, raising h shrinks d_k. Too many tiny heads lose expressive power per head. Too few heads leave less room for different patterns. Common choices keep d_k between 64 and 128. Changing the head count is a capacity bet, not a free gain.
Common mistakes
- Assuming
hheads costhtimes as much. You split the width, so the cost stays close to a single full-width head. - Mixing up query heads and key-value heads. Variants such as MQA and GQA share keys and values across heads. Count the two kinds separately, because the KV count drives cache memory.
- Expecting each head to have a clean label. Heads specialize loosely, and production models rarely have heads you can name cleanly.
Check yourself
- A model has
d_model = 768andh = 12. What isd_k?768 / 12 = 64. - If you double
hand keepd_modelfixed, what happens tod_k? It halves.
Key takeaways
- Multi-head attention runs several attention patterns on slices of the model width.
- Each head has width
d_k = d_model / h, so the total cost stays about the same. - Concatenate the heads and project with
W_Oto get back tod_model. - The head count is a trade-off between pattern variety and width per head.
Further learning
The last step is the cost of attention itself, and what the variants do about it: cost and long context.
Related
Self-attention
The operation at the core of every modern language model. Each position builds a weighted average of the others, with weights set by query-key matches.
Queries, keys and values
Self-attention scores every pair of positions with a query and a key, then mixes value vectors using those scores. The √d_k factor keeps the softmax well behaved.
The transformer architecture
Every component of a modern decoder-only LLM, from embeddings to the LM head, with parameter and FLOP counts.
Modern LLM architectures
Mixture of experts, GQA and MLA, long context, state space models and multimodal transformers.