Pidoku

MQA (2019) and GQA (2023)

Module 30 Lesson 30.d 35 min Difficulty 2/5

Prerequisites 12.e, 12.f, 10.h


Before you start: words you need#

WordPlain meaning
HeadOne of the parallel attentions inside a multi-head attention layer, each with its own query, key and value projections (10.h).
KV cacheThe keys and values of all earlier tokens, kept in GPU memory so each new token does not recompute them (12.e).
Incremental decodingThe paper’s name for generating one token at a time with a KV cache. This material calls it decode (12.c).
MHAMulti-head attention: every query head has its own key and value head.
MQAMulti-query attention: many query heads, but one key head and one value head shared by all.
GQAGrouped-query attention: query heads are split into G groups, and each group shares one key and one value head. G = 1 is MQA; G = number of heads is MHA.
UptrainingTaking a model already trained one way, changing its architecture, and training it a little more so it adapts.
Mean poolingAveraging several weight matrices into one.

1. The paper#

Two papers, read together:

  • MQA: Noam Shazeer (Google), “Fast Transformer Decoding: One Write-Head is All You Need”, arXiv preprint, November 2019. arXiv:1911.02150
  • GQA: Joshua Ainslie, James Lee-Thorp, Michiel de Jong, Yury Zemlyanskiy, Federico Lebrón, Sumit Sanghai (Google Research), “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints”, EMNLP 2023. arXiv:2305.13245

MQA is a short single-author note with code in it. GQA, four years later, is what made the idea mainstream.


2. The problem#

Training a Transformer is fast because the whole sequence is processed in parallel. Generating from it is slow, and Shazeer’s note was one of the first to say clearly why: at each decode step the chip must reload all the keys and values of every earlier token, for every head, from memory, to do only a little maths with them. His analysis gives the ratio of memory traffic to arithmetic as roughly Θ(n/d + 1/b) (n = sequence length, d = model width, b = batch). When n ≈ d or b ≈ 1 that ratio is near 1, and “memory bandwidth [is] a major performance bottleneck”. This is the roofline argument (30.a) applied to attention.

GQA’s problem was the next one: MQA is fast but loses some quality and can be unstable to train, and nobody wants to pretrain a second model from scratch just to serve it faster.


3. The idea#

MQA: keep many query heads, but give the layer a single key head and a single value head. The KV cache shrinks by a factor equal to the number of heads (8× in the paper’s model; 64× or more in a large model). Fewer bytes per step means faster decode.

GQA: pick a middle point. Split the H query heads into G groups; each group shares one K and one V head. The KV cache shrinks by H/G. And instead of training from scratch, convert an existing MHA checkpoint: mean-pool the K and V projection matrices of the heads in each group into one, then uptrain with about 5% of the original pretraining compute.

flowchart LR
  subgraph MHA["MHA: 8 Q, 8 KV"]
    q1["Q x8"] --- kv1["K,V x8"]
  end
  subgraph GQA["GQA-2: 8 Q, 2 KV"]
    q2["Q x8 in 2 groups"] --- kv2["K,V x2"]
  end
  subgraph MQA["MQA: 8 Q, 1 KV"]
    q3["Q x8"] --- kv3["K,V x1"]
  end
  class q1,q2,q3 compute
  class kv1,kv2,kv3 memory

The mechanics, and the KV-size arithmetic, are taught in 12.g. This lesson is about what the papers showed.


4. How it works#

MQA (2019). The note gives the incremental attention code for MHA and MQA side by side (TensorFlow einsums). The only change is that the K and V projection weights lose their head dimension. To keep the parameter count equal, the MQA model’s feed-forward layer is widened (d_ff 4096 → 5440 in the translation model).

GQA (2023).

  1. Convert: for each group of query heads, average their K projections into one K head and their V projections into one V head. Mean pooling beat “keep the first head” and “random initialisation”.
  2. Uptrain: continue pretraining with the same recipe for a fraction α of the original steps. α = 0.05 (5%) was enough.
  3. Choose G: the paper uses G = 8 for T5-XXL (64 heads). Going from 1 group to 8 costs little speed; the cost rises as G approaches the number of heads.

A side benefit the GQA paper points out: with tensor parallelism (30.c), MQA’s single KV head must be replicated on every GPU, which wastes memory. With GQA you can give each GPU its own KV heads, as long as G is at least the TP degree.

What it means for a 2026 model, in numbers: Llama-3-70B has 80 layers, 64 query heads, 8 KV heads and head size 128. In BF16 its KV cache is 2 × 80 × 8 × 128 × 2 bytes = 320 KiB per token. With full MHA (64 KV heads) it would be 2.5 MiB per token, 8× more, and a 128k-token context would need about 320 GB just for one sequence’s cache.


5. Results#

MQA (WMT14 English→German translation, one TPUv2 with 8 cores, Tables 1–2):

ModelBLEU (test, beam 4)Decoder time per token (greedy)Decoder time per token (beam 4)
Multi-head28.446 µs203 µs
Multi-query28.53.8 µs32 µs
  • Decode was about 12× faster with greedy search; training time was unchanged (13.2 vs 13.0 µs per token).
  • Quality: dev perplexity slightly worse (ln PPL 1.439 vs 1.424); BLEU roughly equal. On the billion-word language-model benchmark, dev perplexity 30.2 vs 29.9. MQA beat every alternative that shrinks the cache by cutting heads or head size.

GQA (T5-XXL, uptrained 5%, timed on TPUv4, Table 1):

ModelInference time per sampleAverage score across tasks
MHA-Large0.37 s46.0
MHA-XXL1.51 s47.2
MQA-XXL0.24 s46.6
GQA-8-XXL0.28 s47.1

GQA-8 recovered almost all of MHA-XXL’s quality (47.1 vs 47.2) at close to MQA’s speed (0.28 vs 0.24 s), about 5× faster than MHA-XXL. Uptraining cost was about 600 TPUv3 chip-days for 5%.


6. What held up and what did not#

Held up

  • The diagnosis: decode is limited by reading the KV cache and the weights, not by maths. Every inference paper since builds on it.
  • GQA became the default. Llama 2 70B, every Llama 3 size, Mistral, Qwen 2/3 dense models and many others use GQA with 8 (sometimes 4 or 16) KV heads.
  • Uptraining from a checkpoint as a way to change attention cheaply. DeepSeek and others later used similar conversion ideas for MLA and sparse attention.

Did not, or changed

  • Pure MQA mostly lost. It was used in PaLM and Falcon, but its quality cost, and the replication problem under tensor parallelism, pushed new models to GQA.
  • GQA is not the end point. Multi-head latent attention (MLA, DeepSeek-V2; 30.v) compresses K and V into a small latent vector and gets a smaller cache than GQA with MHA-like quality. Sliding-window layers, KV quantization (13.h) and sparse attention (30.y) cut the cache further.
  • GQA limits tensor parallelism: with 8 KV heads, TP beyond 8 must replicate KV heads (14.c).

7. Where it lives in 2026#

  • Taught in depth: MQA/GQA/MLA mechanics in 12.g, the KV arithmetic in 12.f, memory sizing in 17.h.
  • Every engine supports GQA natively. Attention kernels such as FlashAttention (30.f, 30.n) and FlashInfer (30.x) process a whole query-head group against one KV head at once, which raises the arithmetic intensity of decode attention by the group size.
  • In model configs, look for num_key_value_heads (Hugging Face): less than num_attention_heads means GQA; 1 means MQA.

Checked on 13 November 2026.


8. Questions#

  1. Why is decode attention memory-bound even when the batch is large?
  2. A model has 32 query heads and 8 KV heads. What is G, and how much smaller is its KV cache than MHA’s?
  3. How did the GQA paper turn an MHA checkpoint into a GQA one? Why mean pooling?
  4. Why does MQA interact badly with tensor parallelism?
  5. Compute the BF16 KV cache per token for a model with 64 layers, 8 KV heads, head size 128.
  6. MQA was 12× faster per decoded token in the paper. Why was training no faster?
  7. Why did most labs pick GQA over MQA, and what replaced GQA at DeepSeek?

9. Further reading#

  • arXiv:1911.02150 — short, with readable code; section 2.4 has the memory analysis.
  • arXiv:2305.13245 — Table 1 and Figure 6.
  • 12.g (MQA, GQA, MLA) and 12.f (KV cache math) in this material.
  • Next: GShard and Switch Transformer (30.e).

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in