GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints
TL;DR. During decoding, an LLM re-reads its whole key-value cache for every new token, so speed is limited by memory bandwidth. Multi-query attention (MQA, Shazeer 2019) shares one K/V head across all query heads, which shrinks the cache but costs quality. GQA shares one K/V head per group of query heads, keeping quality close to multi-head at nearly MQA speed. It also shows how to convert an existing checkpoint cheaply.
Why it matters now. Long-context RAG, listwise reranking and RL rollouts of search agents are all decode-heavy, and the KV cache decides how many of them fit on a GPU. Many open models share KV heads this way, so the cache arithmetic below is the first calculation to do before planning an academic-budget experiment.
Key idea
Section titled “Key idea”With query heads split into groups, each group shares one key and value head:
GQA-1 is MQA and GQA- is MHA. The KV cache per token is
so it falls by . To convert a multi-head checkpoint, mean-pool the K and V projection matrices of the heads in each group, then continue pre-training for a fraction of the original steps (“uptraining”).
Key results
Section titled “Key results”- Table 1 (T5-XXL, α = 0.05, time per sample on TPUv4): MHA-XXL 1.51 s with average score 47.2; MQA-XXL 0.24 s, 46.6; GQA-8-XXL 0.28 s, 47.1. MHA-Large is 0.37 s, 46.0, so uptrained MQA is both faster and better than a smaller MHA model.
- Fig. 4: mean pooling > first head > random initialisation for the converted heads.
- Fig. 5: GQA is usable right after conversion; MQA needs uptraining. Gains flatten after 5–10%.
- Fig. 6: going from 1 to 8 groups adds a small cost; the cost grows as G approaches H. The authors pick 8.
- Appendix A: T5-Large models trained from scratch with MQA had frequent loss spikes and diverged on long-input fine-tuning; uptrained GQA models were stable.
Limitations & open questions
Section titled “Limitations & open questions”- Tested only on T5 encoder-decoder models, and GQA is not applied to encoder self-attention. The authors expect a different trade-off for decoder-only models.
- ROUGE is the main metric, which the authors call flawed, and the longest generations, where GQA matters most, are the hardest to judge.
- No comparison with a GQA model trained from scratch.
- Open: does sharing K/V heads hurt the fine-grained position-sensitive retrieval that long-context benchmarks probe? A careful study would compare MHA and GQA variants on RULER-style tasks at matched compute.
Connections
Section titled “Connections”- Before: Attention Is All You Need (multi-head attention).
- Alongside: PagedAttention removes wasted cache memory; speculative decoding reads the cache fewer times per token; FlashAttention-2 supports GQA heads natively.
- After: Multi-head latent attention compresses the cache further; Llama 3 shows how a modern report sets its attention configuration.
- Long context: smaller caches make long windows affordable, but RULER and Lost in the Middle test whether the model can use them.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
Take a hypothetical decoder with 32 layers, 32 query heads, head dimension 128 and bf16 KV cache. Compute the KV-cache bytes per token for MHA, GQA with 8 groups and MQA, then the cache for one 128K-token RAG prompt. On an 80 GB GPU with 16 GB of weights, how many 8K-token sequences fit for each variant?
What a strong answer covers
- Bytes per token = 2 (K and V) × layers × KV heads × d_head × 2 bytes. MHA: 2·32·32·128·2 = 524,288 B = 512 KiB. GQA-8: 2·32·8·128·2 = 128 KiB. MQA: 16 KiB.
- At 131,072 tokens: MHA 64 GiB, GQA-8 16 GiB, MQA 2 GiB. With MHA, a single 128K prompt nearly fills the GPU before any batching.
- For 8K tokens: MHA 4 GiB, GQA-8 1 GiB, MQA 0.125 GiB per sequence. With about 64 GB free, roughly 16, 64 and about 500 sequences fit (ignoring activations and fragmentation).
- Batch size is what sets decoding throughput, so the KV-head count directly limits how many search-agent rollouts or RAG requests run at once.
- The query heads, FLOPs per token and parameter count barely change; only the K and V projections shrink.
- Mechanism
Shazeer's analysis gives a memory-to-arithmetic ratio of Θ(n/d + 1/b) for incremental multi-head decoding and Θ(1/d + n/(dh) + 1/b) for multi-query. Explain each term, why the n/d term is the problem during decoding but not during training, and what GQA with G groups does to it.
What a strong answer covers
- n is the number of generated or cached positions, d the model width, h the number of heads and b the batch. 1/b comes from reading the weights once per batch; n/d comes from reading every cached key and value for every new token.
- In training or prefill, all positions are processed in parallel, so each K and V is loaded once and reused across many queries. In decoding, one query per step must read the whole cache, so the ratio approaches 1 and the GPU is bandwidth-bound.
- MQA shares one K/V head among h query heads, dividing the cache traffic, and the n/d term, by h.
- GQA with G groups keeps G KV heads, so the term becomes roughly n·G/(d·h): a factor h/G reduction rather than h.
- The ratio to compute matters because decoding speed is set by bytes read per token, not by FLOPs.
- What if…
The paper ablates how to build the shared K/V heads (mean, first head, random) and how much uptraining to do (α from 0 to 0.1). Summarise what each ablation shows and give the mechanism behind the ordering. What does it imply if you want to convert your own fine-tuned multi-head reranker?
What a strong answer covers
- Conversion (Fig. 4, T5-Large to MQA, α = 0.05): mean pooling beats taking the first head, which beats random init. The ordering follows how much of the pretrained K/V information survives.
- Uptraining (Fig. 5): GQA-8 is already reasonable right after conversion, while MQA is poor until it is uptrained. Both gain from 5% extra pre-training, with diminishing returns at 10%.
- Mechanism: with 8 groups each pooled head averages only a few heads, so less information is lost than when all heads collapse into one.
- Implication: convert with mean pooling, prefer a moderate G, and budget a short uptraining phase on pre-training-like data before re-fine-tuning on ranking data. Check whether the fine-tuned heads have drifted far apart, which would make pooling lossier.
- Critique
The evidence for GQA comes from T5 encoder-decoder models evaluated mostly with ROUGE, and it never compares an uptrained GQA model with one trained from scratch. Critique what these choices leave open for today's decoder-only LLMs used as long-context readers, and design an experiment that could falsify "GQA-8 is as good as MHA".
What a strong answer covers
- Decoder-only models have no separate cross-attention, and GQA was not applied to encoder self-attention, so the share of attention that is converted differs. The authors themselves expect a different trade-off for decoder-only models.
- ROUGE is a weak proxy (the paper says so), and the longest outputs, where the cache matters most, are the hardest to evaluate.
- No from-scratch baseline: we do not know whether uptraining loses something that native GQA training would keep. MQA from scratch was unstable in Appendix A, which is itself a warning.
- Experiment: train matched decoder-only models (MHA, GQA-8, MQA) at fixed compute, then test multi-document QA with the gold passage at varied positions and a long-context benchmark such as RULER. Falsified if GQA-8 loses clearly more than MHA as context grows.
- Design
You run RL training for a search agent whose rollouts reach 16K tokens, with 64 rollouts in flight on one 80 GB GPU. Your base model is multi-head. Decide between serving it as is, converting it to GQA by uptraining, or switching to a GQA base model, and say what you would measure.
What a strong answer covers
- Compute the cache first: with the 32-layer, 32-head, d = 128 example, MHA needs 8 GiB per 16K rollout, so 64 rollouts need 512 GiB, which does not fit. GQA-8 needs 128 GiB, still too much; you also need paging, fewer rollouts or shorter trajectories.
- Converting costs about 5% of the original pre-training compute, which is out of reach for most academic labs at 7B scale. A base model that already uses GQA is usually the practical choice.
- Since rollouts are generated by the policy, KV-cache size sets rollout throughput, which is often the bottleneck in RL for LLMs.
- Measure rollouts per hour, reward per GPU-hour and final task accuracy, and compare against a smaller MHA model at equal memory.
- Connect
GQA shrinks bytes per token; PagedAttention shrinks wasted bytes. Explain why the two gains multiply rather than overlap, and work out the effective number of concurrent sequences for MHA with contiguous allocation against GQA-8 with paging, if contiguous allocation uses only 30% of reserved KV memory.
What a strong answer covers
- GQA changes the size of each token's entry; paging changes how many reserved slots actually hold tokens. They act on different factors of total memory = sequences × tokens × bytes per token / utilisation.
- With 64 GB free and 8K-token sequences: MHA needs 4 GiB per sequence. At 30% utilisation it is effectively 13.3 GiB, so about 4–5 sequences.
- GQA-8 with paging needs 1 GiB per sequence at near-full utilisation (waste of at most one block), so about 64 sequences: roughly 13× more.
- The 30% figure is inside the 20.4–38.2% range vLLM measured for earlier systems.