Attention Mechanisms · June 2017
Multi-Head Attention
intermediate
Let attention specialize. Instead of one big attention over the whole d_model space, run H independent attention heads in lower-rank subspaces and concatenate — each head free to learn a different relation type.
§ 1 · Premise
One attention head cannot carry every relation type
Vaswani et al. (2017) opened the transformer paper with a deliberately weak baseline: a single scaled dot-product attention over the full model width . With queries, keys and values living in one inner-product space, every relation a token might want to express — subject-verb agreement, coreference, positional adjacency, topical similarity — collapses onto the same set of weights. The bet of “Attention Is All You Need” §3.2.2 is that splitting the projection into smaller heads, each with its own subspace, gives the optimizer enough independent capacity to specialize.
Concretely: at the per-head and each head holds parameters. Across 8 heads plus the output projection , the attention sub-layer carries M parameters — exactly matching the cost of a single full-rank attention while distributing expressivity across heads. The paper’s Table 3 ablation (row B, ) shows base-model perplexity rising from 4.92 to 5.29 and BLEU dropping by 0.9 points on WMT En-De when is forced to 1 — the empirical case for multiple heads.
The lineage runs from the Bahdanau (2014) additive attention used in seq2seq translators, through Luong (2015) multiplicative attention, to Cheng et al. (2016) intra-attention which first applied attention inside a single sequence. Vaswani’s contribution is twofold: dropping recurrence entirely (the entire sequence is consumed in parallel), and breaking attention into parallel subspaces. The first move is what made the architecture trainable at scale; the second is what made it expressive enough to compete with LSTM seq2seq at translation quality.
What this entry sets up: the layer’s exact math, its parameter and compute footprint, the mechanical reason its KV cache eventually became intolerable at long context, and what survives in every descendant — GQA, MQA, MLA, linear-attention variants — that has supplanted it.
§ 2 · Derivation
From dot-product attention to H parallel subspaces
The starting point is the scaled dot-product attention of Vaswani et al. (2017) §3.2.1. For a sequence of tokens at width , with the input, the operator is
where are queries and keys, are values, and is the key dimension. The scaling is justified in §3.2.1: for random with i.i.d. zero-mean unit-variance components, has variance , so dividing by keeps the softmax temperature constant as the head dimension changes — without it, large pushes the softmax into saturated regions where gradients vanish.
Multi-head attention replaces the single projection with per-head projections. For head at head dim :
with . Each head computes its own attention
and the outputs concatenate before a final linear projection :
Why split the projection rather than run full-width attentions? Two reasons in §3.2.2. First, parameter parity: the concatenated per-head projection matrices stack to a single matrix , so multi-head attention with heads at costs the same number of parameters as single-head attention at . The split is free. Second, the lower per-head keeps each head’s softmax inputs bounded, which interacts well with the scaling.
Why concatenate instead of average? Concatenation preserves all output dimensions and lets the downstream choose how to mix them. Averaging would force heads to agree on a single output and erase the specialization the architecture is designed to encourage.
The causal-decoder version applies a mask inside the softmax to forbid attending to future positions: with for . This is what the language-model variants of the transformer use.
Parameter count. The attention sub-layer carries parameters: three projection matrices (stacked from per-head pieces) plus one output projection. For Llama-1-65B at , this is M parameters per layer.
FLOP cost. Per layer, the projections cost FLOPs and the attention-score computation costs FLOPs. The score matrix is the quadratic term that dominates at long .
KV cache size. At inference, each generated token writes to the cache, so cumulative cache size is
where is layer count and is context length. At , fp16 ( bytes), — the headline DeepSeek-grade long-context setting — the cache is GB. This is the cost that killed pure MHA for long-context production inference and forced the MQA/GQA/MLA lineage.
The asymmetry between train and inference matters: at training time, the per-head K and V are recomputed from every forward pass and never cached, so MHA’s compute per layer is the quadratic-in- score matrix plus projections. At inference, the projections of the new token cost but every prior K, V must remain resident in HBM to answer the next softmax. The architecture that is cheap to train is the same architecture that is expensive to decode — and decode is what production serves.
§ 3 · Reference implementation
Sketch
The implementation below shows the structural pieces — projection, head split, per-head softmax, concatenation, output projection. It is illustrative, not optimized: the in-place softmax and matmul fusion that production paths use (FlashAttention’s tile loop) hide the per-head shape inside a kernel, but the operator definition is the one shown.
def mha(x, W_Q, W_K, W_V, W_O, H, mask=None):
# x: [B, T, d_model]
B, T, D = x.shape
d_h = D // H
# Project and split into heads.
q = (x @ W_Q).view(B, T, H, d_h).transpose(1, 2) # [B, H, T, d_h]
k = (x @ W_K).view(B, T, H, d_h).transpose(1, 2) # [B, H, T, d_h]
v = (x @ W_V).view(B, T, H, d_h).transpose(1, 2) # [B, H, T, d_h]
# Per-head scores: each head sees its own d_h-dim subspace.
logits = (q @ k.transpose(-2, -1)) / d_h**0.5 # [B, H, T, T]
if mask is not None:
logits = logits.masked_fill(mask, float("-inf"))
# Softmax over the key axis, then mix V.
out = logits.softmax(-1) @ v # [B, H, T, d_h]
# Concatenate heads (transpose then reshape) and project.
return out.transpose(1, 2).reshape(B, T, D) @ W_O.T # [B, T, d_model]
The load-bearing structure: K and V are written per-head, with a separate tile per head per layer. Every variant in the lineage modifies exactly that step. MQA collapses to one shared K, V; GQA partitions heads into groups; MLA replaces per-head K, V with a single low-rank latent. Everything else — the softmax, the scaling, the per-head split, the concatenation — survives.
§ 4 · Empirical evidence
What the original paper reported, what later work confirmed
The base Transformer at scored 27.3 BLEU on WMT 2014 En-De and 38.1 BLEU on En-Fr, beating every prior published result while training in 12 hours on 8 P100s (Vaswani et al. 2017, Table 2). The transformer-big variant at pushed En-De to 28.4 and En-Fr to 41.8 (Table 2 rows 4–5). The head-count ablation in Table 3 sweeps at fixed parameter budget: loses 0.9 BLEU vs ; are within 0.2 BLEU of each other; regresses by 0.4 BLEU because per-head shrinks to 16 and individual heads lose representational capacity. This is the empirical justification for the canonical ” between 64 and 128” range that every descendant inherits.
Independent reproductions of head specialization came quickly. Voita et al. (2019, “Analyzing Multi-Head Self-Attention”, arXiv 1905.09418) showed by attention-head pruning that a subset of heads in a trained transformer specialize on interpretable patterns — positional (attending to the previous token), syntactic (attending to the dependency parent), and rare-token attention — while a large fraction (up to 48 of 48 heads in some layers) can be pruned with 1 BLEU loss. This is the strongest evidence that the multi-head split actually delivers specialization, not just nominal parallelism. Michel et al. (2019, “Are Sixteen Heads Really Better Than One?”, arXiv 1905.10650) reached a similar conclusion: many heads are redundant after training, but the redundancy emerges from training dynamics that benefit from over-parameterization — pruning at initialization fails.
For the KV-cache scaling story, the GQA paper of Ainslie et al. (2023, arXiv 2305.13245, Figure 6) measured T5-XXL decode throughput as a function of cache size. MHA hits a memory wall at sequence 8K on a single TPU chip; collapsing to MQA gives a 12× decode speedup at modest quality cost; GQA-8 recovers most of the quality gap at 11.6× speedup. The MLA paper (DeepSeek-V2, 2024, arXiv 2405.04434, Table 1) records that MHA at the same parameter budget would consume the KV cache of MLA at 128K context — the quantitative reason no frontier 2024+ decoder ships plain MHA.
The 2024–25 open-decoder census is telling: the original Llama-1 (65B) ran MHA across all 80 layers at ; Llama-2 and every subsequent Llama variant switched to GQA; OLMo-1 shipped MHA at OLMo’s choice to mirror Llama-1’s recipe, then OLMo-2 moved to GQA. Gemma-1 ran MHA at an unusually wide (Gemma team 2024, arXiv 2403.08295, §2), which is the last public production deployment of plain MHA at scale; Gemma-2 also switched. The transition is essentially complete in dense decoders; the only contexts where MHA still appears unmodified are short-context encoders (BERT-class) where the cache cost does not bind and full per-head expressivity is preferred.
No public sensitivity study exists that disentangles the per-head choice from the total at modern scale — most ablations vary at fixed , which conflates the two. I don’t know of a study at that varies independently.
One under-discussed empirical detail: Vaswani et al. (2017) §5.3 trained with Adam at the warmup schedule with warmup steps, dropout on the attention output and the sub-layer residual, and label smoothing . Every subsequent MHA-using decoder we document picked up this recipe (or a close variant) before substituting GQA/MLA — the head-split was rarely the lever that mattered for fine-tuning behavior; the choice, the normalization placement, and the optimizer schedule were.
Adopted by
- Llama 1 65B · Meta — Original Llama, before GQA was incorporated in Llama 2. [source]
- OLMo 1 7B · Allen Institute for AI (AI2) — First OLMo, deliberately Llama-1-derived with MHA. [source]
- Gemma 1 7B · Google DeepMind — MHA with unusually wide head_dim 256 (16 query heads, 16 KV heads). GQA only arrived in Gemma 2. [source]
Cite
BibTeX entry for the original paper
@article{arxiv1706_03762,
title = {Attention Is All You Need},
author = {Ashish Vaswani and others (Google Brain)},
year = {2017},
eprint = {1706.03762},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1706.03762}
} Or cite the paper directly: arXiv:1706.03762.
Export
BibTeX
@article{arxiv_1706_03762,
title = {Attention Is All You Need},
author = {Ashish Vaswani et al. (Google Brain)},
year = {2017},
eprint = {1706.03762},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1706.03762}
} CSL JSON
{
"id": "arxiv_1706_03762",
"type": "article-journal",
"title": "Attention Is All You Need",
"author": [
{
"literal": "Ashish Vaswani et al. (Google Brain)"
}
],
"issued": {
"date-parts": [
[
2017
]
]
},
"URL": "https://arxiv.org/abs/1706.03762",
"number": "1706.03762",
"source": "arXiv"
} RIS
TY - JOUR
TI - Attention Is All You Need
AU - Ashish Vaswani et al. (Google Brain)
PY - 2017
JO - arXiv
AN - arXiv:1706.03762
UR - https://arxiv.org/abs/1706.03762
ER -