Second ML Deep Dive post, after Mixture of Experts, but it sits just as naturally next to this blog's run of KV cache posts on the GPU side — PagedAttention and prefix caching both take the KV cache's size as a given and get clever about allocating or reusing it. Grouped-query attention attacks the size itself, in the architecture, before a single token gets served.
Why decoding is bandwidth-bound, not compute-bound
Generating one token during autoregressive decoding does very little
arithmetic — one new query vector, attending over a KV cache that
might hold thousands of previous tokens. What it does require is
loading that entire KV cache from memory for every head, every layer,
every single step. Shazeer (2019)
frames this precisely as a memory-bandwidth problem: standard
multi-head attention's incremental memory-to-arithmetic ratio scales
as roughly n/d + 1/b (sequence length over head dimension, plus an
inverse-batch-size term) — dominated, for any serving-size sequence
length, by how much K/V data has to move, not by how much math gets
done on it.
Multi-query attention: one K/V head for everyone
Shazeer's fix is blunt: keep separate query heads, but give them a
single, shared set of key and value projections instead of one pair
per head. "Multi-query attention is identical [to multi-head
attention] except that the different heads share a single set of keys
and values." That removes the per-head multiplier from the K/V side of
the memory traffic entirely — the paper's own framing is that it
"reduced the offensive nd term by a factor of h," where h is the
head count.
The measured effect, on a WMT14 English-German Transformer running on TPUv2, is large: a single incremental decoder step dropped from 46 microseconds with standard multi-head attention to 3.8 with multi-query. That didn't come free — the multi-query model scored very slightly worse (BLEU 27.5 vs. 27.7 on the test set at beam size 1; dev perplexity also ticked up a little), which the paper is upfront about: the multi-query model "seems to be slightly worse than the baseline." Small, consistent quality cost, large and consistent speed gain.
Grouped-query attention: pick a point between the two extremes
Multi-query attention is one extreme of a spectrum whose other end is ordinary multi-head attention: every query head gets its own K/V head. GQA (Ainslie et al., EMNLP 2023) names and formalizes the space in between: "Grouped-query attention divides query heads into G groups, each of which shares a single key head and value head." Set G to 1 and it's exactly multi-query attention; set G equal to the number of query heads and it's exactly multi-head attention. Anything in between trades some of multi-query's speed back for some of multi-head's quality.
The paper's other contribution is practical rather than architectural: you don't have to pretrain a GQA model from scratch. An existing multi-head checkpoint can be converted directly — "we construct each group['s] key and value head by mean-pooling all the original heads within that group" — and then briefly fine-tuned ("uptrained") to adjust to its new, smaller K/V representation. The paper reports this uptraining step costs about 5% of the model's original pretraining compute (around 600 TPUv3 chip-days for their setup), and that GQA specifically "already achieves reasonable performance after conversion while MQA requires uptraining to be useful" — mean-pooling several heads into one is a gentler change than collapsing straight down to a single shared head.
On their T5-XXL checkpoints, the numbers land where the framing promises: uptrained MQA-XXL runs inference in 0.24 seconds per sample against MHA-XXL's 1.51 seconds — roughly 6x faster — but at a quality cost; uptrained GQA-8-XXL runs in 0.28 seconds, nearly as fast, while landing its average benchmark score at 47.1, essentially matching MHA-XXL's 47.2 and clearly ahead of MQA-XXL's 46.6. The paper's own summary: "uptrained GQA achieves quality close to multi-head attention with comparable speed to MQA."
Where this actually ships
This isn't a paper-only idea. Mistral 7B uses GQA directly in its published architecture — 32 query heads grouped down to 8 key/value heads, head dimension 128 — and says so explicitly: "GQA significantly accelerates the inference speed, and also reduces the memory requirement during decoding, allowing for higher batch sizes hence higher throughput." LLaMA 2 applies it selectively rather than everywhere: its 7B and 13B variants use ordinary multi-head attention, while the 34B (trained but not released) and 70B variants switch to GQA specifically "to improve inference scalability for our larger models" — the KV cache cost that GQA is solving for only becomes a serious bottleneck once the model itself is big enough for it to matter relative to everything else competing for GPU memory.
A third, independent lever on the same cache
This gives the KV cache three separate, stackable fixes, each operating at a different layer of the stack: grouped-query attention shrinks how much K/V data exists per token in the model's own architecture; PagedAttention changes how that data gets allocated in GPU memory once it exists; prefix caching changes whether identical spans of it need to be computed more than once at all. None of them substitutes for the others — a GQA model still benefits from paged allocation and prefix reuse, and a smaller per-token KV footprint just means more of it fits in a cache block, and more requests' prefixes fit in memory long enough to actually get reused. Same shape of lesson this blog keeps landing on: "make the KV cache cheaper" was never one fix, it's a stack of them, each targeting a different reason it's expensive.
References
- Fast Transformer Decoding: One Write-Head is All You Need — Shazeer (2019, arXiv:1911.02150)
- GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints — Ainslie et al. (EMNLP 2023, arXiv:2305.13245)
- Mistral 7B — Jiang et al. (2023, arXiv:2310.06825)
- Llama 2: Open Foundation and Fine-Tuned Chat Models — Touvron et al. (2023, arXiv:2307.09288)
Head-grouping diagram above is original artwork made for this post.