Paper · 2023-05-22 · cs.CL, cs.LG
GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints
Grouped-query attention (GQA) splits query heads into G groups that each share one key and one value head, sitting between multi-head (G=H) and multi-query (G=1) attention. Multi-head T5 checkpoints are converted by mean-pooling K/V heads, then uptrained for 5% of pre-training steps. GQA-8 T5-XXL averages 47.1 vs 47.2 for MHA at 0.28 s vs 1.51 s per sample.
Grouped-query attention (GQA)Multi-query attention (MQA)KV-cache managementattentioninferencekv-cacheuptraining
AI-compiled by claude-opus-5-5 on 2026-10-18, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.
In plain words
When a Transformer writes text one token at a time, every step has to read back the keys and values it stored for all earlier tokens. With standard multi-head attention, each attention head keeps its own keys and values, so this cache is large and reading it from memory, not arithmetic, dominates the time. A known fix, multi-query attention, lets all heads share a single set of keys and values. That is much faster, but quality can drop and training can become unstable.
Grouped-query attention is the middle road. Query heads are split into a few groups, and each group shares one key head and one value head. Think of a library where, instead of every reader having a private copy of a book or everyone sharing one copy, each table gets its own copy: far fewer copies to fetch, but less crowding than a single shared one. With 8 groups in a T5-XXL model, the authors report quality almost equal to full multi-head attention at a speed close to multi-query attention.
The second idea is cheap conversion. Rather than training a new model from scratch, you take an existing multi-head checkpoint, average the key and value heads within each group into one, and continue pre-training for a small fraction (5%) of the original steps so the model adapts.
TL;DR
GQA partitions the H query heads into G groups; each group shares one key and one value projection, shrinking the KV cache by a factor H/G. GQA-1 is multi-query attention (MQA), GQA-H is multi-head attention (MHA). Existing MHA checkpoints are converted by mean-pooling the K/V projection matrices of the heads in each group, then "uptrained" for a fraction α (0.05 in the main results) of the original pre-training steps. On T5-XXL (encoder-decoder), uptrained GQA-8 reaches an average score of 47.1 vs 47.2 for MHA-XXL and 46.6 for MQA-XXL, at 0.28 s vs 1.51 s and 0.24 s inference time per sample (author-reported). Only encoder-decoder models were evaluated.
Problem
Autoregressive decoding is bottlenecked by memory bandwidth: each step loads the decoder weights plus all cached keys and values §1. MQA cuts the key/value load by using a single K/V head, but it can degrade quality and cause training instability, and many public models (T5, LLaMA) were trained with MHA, so getting an MQA model would mean training another model from scratch §1. Moving from MHA to MQA also cuts K/V capacity by a factor H, which grows with model size since larger models have more heads, making MQA an increasingly aggressive cut §2.2. Under standard model sharding, the single K/V head of MQA is replicated across partitions, wasting memory §2.2.
Method
Two independent contributions: a checkpoint conversion plus uptraining recipe, and the GQA attention variant §2.
Grouped-query attention §2.2 fig. 2. Query heads are divided into G groups; all query heads in a group attend using that group's single key head and single value head. GQA-G denotes G groups. GQA-1 equals MQA; GQA-H (G equal to the number of heads H) equals MHA. The KV cache, and thus the data loaded per decoding step, shrinks by H/G relative to MHA (the paper states the factor H for MQA §2.2; H/G for general G follows from the definition).
In the experiments GQA/MQA is applied to decoder self-attention and to cross-attention, but not to encoder self-attention, because encoder representations are computed in parallel and are not bandwidth-bound §2.2 §3.1.
Checkpoint conversion §2.1 fig. 1. To build a group's key (value) head, mean-pool the key (value) projection matrices of all original heads in that group. The paper compared this with taking the first head and with random initialization of the new K/V heads; mean pooling was best §3.3 fig. 4.
Uptraining §2.1 §3.1. Continue pre-training the converted checkpoint for a proportion α of its original pre-training steps, using the original pre-training recipe and dataset.
Algorithm: MHA -> GQA uptraining [§2.1] [§2.2] [§3.1]
Input: MHA checkpoint with H heads per attention layer; number of groups G;
uptraining proportion alpha; original pre-training recipe and data
1. For each decoder self-attention and cross-attention layer
(encoder self-attention is left unchanged):
a. Partition the H heads into G groups.
b. For each group g: W_K[g] = mean of W_K[h] over heads h in group g;
W_V[g] = mean of W_V[h] over heads h in group g.
c. Keep all H query projections and the output projection unchanged.
2. Continue pre-training with the original objective, data and optimizer
settings for alpha * (original number of pre-training steps).
3. Fine-tune on downstream tasks as usual.
Output: GQA-G checkpointHow heads are assigned to groups (contiguous blocks or otherwise) is not stated; step 1c is implied by the method (only K/V heads are pooled) but not spelled out. Attention per query head then works as in MHA, with query head h using the K/V head of its group:
\mathrm{head}_h = \mathrm{softmax}\!\left(\frac{(X W_Q^{h})(X' W_K^{g(h)})^\top}{\sqrt{d_k}}\right) X' W_V^{g(h)}
This equation is our notation, not one from the paper (the paper has no numbered equations); g(h) is the group of query head h, X' is the key/value source (decoder states for self-attention, encoder outputs for cross-attention). The scaling factor follows standard attention and is not discussed in the paper.
Hyperparameters
| Name | Value | Where | Notes |
|---|---|---|---|
| base architecture | T5.1.1 (Large, XXL) | §3.1 | JAX, Flax, Flaxformer |
| initialization | public T5.1.1 checkpoints | §3.1 | |
| groups G (main) | 8 | §3.3 tab. 1 | chosen as "a favorable middle ground" from fig. 6 |
| heads H (T5-XXL) | 64 (read off fig. 6) | fig. 6 | x-axis runs 1 to 64 groups with 64 = MHA; not stated in text |
| conversion | mean pooling of K/V projection matrices | §2.1 §3.1 | |
| uptraining proportion α | 0.05 | §3.1 §3.2 | 0, 0.05, 0.1 compared in fig. 5 |
| uptraining cost (α=0.05) | ~600 TPUv3 chip-days | §3.1 | T5-XXL |
| uptraining optimizer | Adafactor, same hyperparameters and LR schedule as T5 | §3.1 | values not restated |
| uptraining data | original T5 pre-training setup and dataset | §3.1 | |
| GQA/MQA applied to | decoder self-attention and cross-attention | §3.1 | not encoder self-attention |
| fine-tune learning rate | 0.001, constant | §3.1 | |
| fine-tune batch size | 128 | §3.1 | |
| fine-tune dropout | 0.1 | §3.1 | |
| fine-tune stopping | train until convergence; pick best dev checkpoint | §3.1 | |
| lengths CNN/DM, WMT | input 512, output 256 | §3.1 | |
| lengths arXiv, PubMed, MediaSum, MultiNews | input 2048, output 512 | §3.1 | |
| lengths TriviaQA | input 2048, output 32 | §3.1 | |
| decoding | greedy | §3.1 | |
| timing hardware | 8 TPUv4 chips, xprof | §3.1 | largest batch that fits, up to 32 per TPU; parallelization tuned per model |
| MQA runs on unstable tasks | mean of 3 fine-tuning runs | app. A |
Results
Author-reported, dev sets. T5-Large and T5-XXL with MHA vs T5-XXL uptrained with α=0.05 to MQA or GQA-8. T_infer is time per sample per TPUv4 chip §3.1 tab. 1.
| Model | T_infer (s) | Average | CNN R1 | arXiv R1 | PubMed R1 | MediaSum R1 | MultiNews R1 | WMT BLEU | TriviaQA F1 |
|---|---|---|---|---|---|---|---|---|---|
| MHA-Large | 0.37 | 46.0 | 42.9 | 44.6 | 46.2 | 35.5 | 46.6 | 27.7 | 78.2 |
| MHA-XXL | 1.51 | 47.2 | 43.8 | 45.6 | 47.5 | 36.4 | 46.9 | 28.4 | 81.9 |
| MQA-XXL | 0.24 | 46.6 | 43.0 | 45.0 | 46.9 | 36.1 | 46.5 | 28.5 | 81.3 |
| GQA-8-XXL | 0.28 | 47.1 | 43.5 | 45.4 | 47.7 | 36.3 | 47.2 | 28.4 | 81.6 |
Source: tab. 1. fig. 3 plots the same averages against inference time.
Ablations on the average of CNN/Daily Mail, MultiNews and TriviaQA §3.3. Values are the plotted data points from the figure sources.
| Ablation | Setting | Score | Source |
|---|---|---|---|
| Conversion (T5-Large → MQA, α=0.05) | mean pooling | 55.55 | fig. 4 |
| first head | 55.45 | fig. 4 | |
| random init | 55.23 | fig. 4 | |
| Uptraining proportion (T5-XXL) | MHA reference | 57.54 | fig. 5 |
| GQA-8, α = 0 / 0.05 / 0.1 | 56.71 / 57.40 / 57.56 | fig. 5 | |
| MQA, α = 0 / 0.05 / 0.1 | 53.93 / 56.92 / 57.15 | fig. 5 |
GQA is usable right after conversion (α=0), whereas MQA needs uptraining; both improve with 5% uptraining, with smaller gains from 10% §3.3 fig. 5.
Groups vs speed (GQA-XXL, input 2048, output 512) fig. 6:
| Groups | 1 (MQA) | 4 | 8 | 16 | 32 | 64 (MHA) |
|---|---|---|---|---|---|---|
| Time per sample (s) | 0.489 | 0.476 | 0.514 | 0.594 | 0.800 | 2.531 |
Paper vs code
No official code. The paper points to Flaxformer as its framework and to an
independently developed GQA implementation by Markus Rabe in Flaxformer
(flaxformer/components/attention/memory_efficient_attention.py) §3.1 §4,
but neither is released as this paper's code.
Pitfalls
- Encoder-decoder only. All experiments are T5 encoder-decoder models; decoder-only models were not evaluated. The authors expect GQA's advantage over MQA to be larger there, but this is a conjecture [Limitations].
- Which layers. GQA/MQA replaces decoder self-attention and cross-attention; encoder self-attention stays MHA §3.1. Converting only self-attention would not reproduce the setup.
- Group assignment is unstated. The paper does not say which heads form a group. Contiguous blocks of H/G heads is the natural reading of fig. 2 but is not specified; with mean pooling, the choice changes the initial weights.
- Pool the projections, not the cache. Conversion averages the K and V projection matrices §2.1 fig. 1; query and output projections are untouched (implied, not stated).
- Timing setups differ. tab. 1 reports MQA-XXL at 0.24 s, but fig. 6 reports 0.489 s for 1 group: fig. 6 uses fixed input 2048 / output 512, while tab. 1 averages across tasks. The axis of fig. 3 is labelled "ms" although its values match the seconds in tab. 1.
- Non-monotone speed. In fig. 6, 4 groups (0.476 s) is slightly faster than 1 group (0.489 s); the paper does not comment. Per §2.2, MQA's single K/V head is replicated across model partitions, which may be related, but the paper does not tie the two together.
- MQA instability. T5-Large MQA models trained from scratch had frequent pre-training loss spikes and diverged immediately when fine-tuned on long-input tasks; uptrained MQA was more stable but high-variance (MQA scores on unstable tasks are averages of 3 runs). Uptrained GQA appeared stable app. A.
- Hidden cost of uptraining. α=0.05 for T5-XXL was ~600 TPUv3 chip-days §3.1, and requires access to the original pre-training data and recipe.
- No from-scratch baseline. GQA-XXL was never compared with a GQA model trained from scratch, so uptraining vs scratch is unknown [Limitations].
- Metric caveat. Summarization is scored with ROUGE-1, which the authors call flawed for judging these trade-offs [Limitations].
- Mixed ablation model sizes. The conversion ablation uses T5-Large fig. 4, the uptraining ablation T5-XXL fig. 5; the ablation averages (around 55 to 57) are over a 3-task subset and are not comparable with tab. 1 averages.
- Fine-tuning optimizer details (optimizer, warmup) beyond LR, batch size and dropout are not stated §3.1.
Lineage
- Builds on: multi-query attention, Shazeer, "Fast transformer decoding: One write-head is all you need" (arXiv:1911.02150); generalizes it to G K/V heads.
- Builds on: sparse upcycling (arXiv:2212.05055), which inspired the uptraining of existing T5 checkpoints §4.
- Uses: T5.1.1 checkpoints. MQA is used in PaLM §1.
- Related: speculative decoding is listed as another way to ease the memory-bandwidth bottleneck §4.
- Compared against: MHA-Large, MHA-XXL, uptrained MQA-XXL tab. 1.
Source and license
Adapted from “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints” by Joshua Ainslie, James Lee-Thorp, Michiel de Jong, Yury Zemlyanskiy, Federico Lebrón, Sumit Sanghai (arXiv:2305.13245), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors.