Skip to content

GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints

main track #8corefundamentalpaperrubric 11/122023 · EMNLP 2023 · ~35 min

Joshua Ainslie, James Lee-Thorp, Michiel de Jong, …, Sumit Sanghai

Status

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.

With HH query heads split into GG groups, each group shares one key and value head:

headi=softmax⁡ ⁣(QiKg(i)⊤dh)Vg(i),g(i)=⌊iH/G⌋.\text{head}_i = \operatorname{softmax}\!\left(\frac{Q_i K_{g(i)}^\top}{\sqrt{d_h}}\right) V_{g(i)}, \qquad g(i) = \left\lfloor \frac{i}{H/G} \right\rfloor .

GQA-1 is MQA and GQA-HH is MHA. The KV cache per token is

bytes/token=2×L×G×dh×bytes per value,\text{bytes/token} = 2 \times L \times G \times d_h \times \text{bytes per value},

so it falls by H/GH/G. 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 α\alpha of the original steps (“uptraining”).

Multi-head, grouped-query and multi-query attention, and what each costs in KV cache Three panels, each with eight query heads on top. In multi-head attention every query head has its own key-value head, so eight KV heads are cached. In grouped-query attention with two groups, four query heads share each KV head, so two KV heads are cached. In multi-query attention all eight query heads share one KV head. Bottom: the KV cache per token is 2 times layers times KV heads times head dimension times bytes, so it shrinks in proportion to the number of KV heads. MHA (H = 8 KV heads) cache: 8 K/V pairs per layer best quality, biggest cache GQA (G = 2 groups) cache: 2 K/V pairs per layer init: mean-pool the 4 heads MQA (1 KV head) cache: 1 K/V pair per layer smallest cache; quality dips query head key + value head (cached) KV bytes per token = 2 (K, V) × layers × n_kv_heads × d_head × bytes Example (hypothetical 32 layers, 32 query heads, d_head = 128, bf16): MHA 32 KV heads → 512 KiB/token GQA 8 groups → 128 KiB/token MQA → 16 KiB/token At 128K tokens of retrieved context: 64 GiB vs 16 GiB vs 2 GiB per sequence.
Original sketch. MHA, GQA and MQA differ only in how many key-value heads are cached. The worked numbers use a hypothetical 32-layer model.
  • 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.
  • 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.

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Done with this page?

Status