Attention Mechanisms  · May 2023

Grouped-Query Attention

intermediate

kv-cacheefficiency

Get most of Multi-Query Attention's KV-cache savings without the quality drop — by sharing K, V across small groups of query heads instead of all heads.

§ 1 · Premise

The KV cache is the inference bottleneck

Autoregressive decoding stores one key and one value vector per token, per attention head, per layer, for the entire context. At decode step tt, every cached entry is read to produce one new token, so the cache lives in fast memory and grows linearly with context length. By Llama 2 scale it had become the dominant memory term, not the parameters.

Concretely, for a model with HH query heads, head dimension dhd_h, and LL layers, Multi-Head Attention (Vaswani et al. 2017) stores

KVMHA(T)  =  2HdhLTscalars.\text{KV}_\text{MHA}(T) \;=\; 2 \cdot H \cdot d_h \cdot L \cdot T \quad \text{scalars}.

Plugging in the Llama 2 70B shape — H=64H = 64, dh=128d_h = 128, L=80L = 80 — at fp16 the cache is 226412880=2,621,4402 \cdot 2 \cdot 64 \cdot 128 \cdot 80 = 2{,}621{,}440 bytes per token, i.e. 2.5 MiB/token, or about 10 GiB at T=4T = 4K and 320 GiB at T=128T = 128K (Llama 2 paper §2, Touvron et al. 2023). That exceeds the HBM of an H100 by more than 3× before any weights or activations are loaded.

Shazeer’s Multi-Query Attention (MQA, arXiv 1911.02150) takes the limit case: one KK and one VV projection shared by all HH query heads, dropping the per-token cache to 2dhL2 \cdot d_h \cdot L, an H×H\times reduction. The PaLM technical report (Chowdhery et al. 2022, §2.2) and the original MQA paper both note quality degradation versus MHA on encoder-decoder and long-context generation benchmarks, and Ainslie et al. observe instability at large pretraining scale (GQA paper §1).

A second pressure point is memory bandwidth. At decode each new token must reread the entire cache; on an H100 (3.35 TB/s HBM) a 320 GiB MHA cache caps decode throughput well below the arithmetic limit. GQA cuts both the storage and the bandwidth demand by the same factor, so it shifts decode from memory-bound back toward compute-bound at long context (Ainslie et al. 2023, §4.2).

GQA, the subject of this entry, interpolates: introduce GG key-value heads with 1GH1 \le G \le H, where G=HG = H is MHA and G=1G = 1 is MQA. Every group of H/GH/G query heads attends against one shared (K,V)(K, V) pair, cutting the cache by exactly H/G×H/G\times while leaving the per-head query projections — and thus the expressive bottleneck — untouched.

§ 2 · Derivation

From MHA to grouped heads, with averaging-init uptraining

Setup. Let htRd\mathbf{h}_t \in \mathbb{R}^{d} be the residual-stream activation at position tt (dd = model dimension, e.g. d=8192d = 8192 for Llama 2 70B). MHA defines, for each head i{1,,H}i \in \{1, \ldots, H\},

qt(i)=WQ(i)ht,kt(i)=WK(i)ht,vt(i)=WV(i)ht,\mathbf{q}^{(i)}_t = W_Q^{(i)}\mathbf{h}_t,\quad \mathbf{k}^{(i)}_t = W_K^{(i)}\mathbf{h}_t,\quad \mathbf{v}^{(i)}_t = W_V^{(i)}\mathbf{h}_t,

with WQ(i),WK(i),WV(i)Rdh×dW_Q^{(i)}, W_K^{(i)}, W_V^{(i)} \in \mathbb{R}^{d_h \times d} and dh=d/Hd_h = d / H (Vaswani et al. 2017 §3.2). Head ii‘s contribution at decode step tt, given cached K<t(i)Rt×dhK^{(i)}_{<t} \in \mathbb{R}^{t \times d_h} and V<t(i)Rt×dhV^{(i)}_{<t} \in \mathbb{R}^{t \times d_h}, is

ot(i)  =  softmax ⁣(qt(i)Kt(i)dh)Vt(i).\mathbf{o}^{(i)}_t \;=\; \mathrm{softmax}\!\left(\frac{\mathbf{q}^{(i)\top}_t K^{(i)\top}_{\le t}}{\sqrt{d_h}}\right) V^{(i)}_{\le t}.

The outputs concatenate across heads and are projected by WORd×dW_O \in \mathbb{R}^{d \times d}. Cache footprint per token per layer is the size of all (kt(i),vt(i))(\mathbf{k}^{(i)}_t, \mathbf{v}^{(i)}_t): 2Hdh=2d2 H d_h = 2d scalars.

The grouping. Pick a divisor GHG \mid H and partition the query heads into GG contiguous groups of size H/GH/G. Replace the HH key/value projections with GG of them, WK(g),WV(g)Rdh×dW_K^{(g)}, W_V^{(g)} \in \mathbb{R}^{d_h \times d} for g{1,,G}g \in \{1, \ldots, G\}. Define the group index of query head ii as g(i)=iG/Hg(i) = \lceil i G / H \rceil. The GQA forward pass is

qt(i)=WQ(i)ht,kt(g)=WK(g)ht,vt(g)=WV(g)ht,\mathbf{q}^{(i)}_t = W_Q^{(i)}\mathbf{h}_t,\quad \mathbf{k}^{(g)}_t = W_K^{(g)}\mathbf{h}_t,\quad \mathbf{v}^{(g)}_t = W_V^{(g)}\mathbf{h}_t, ot(i)  =  softmax ⁣(qt(i)Kt(g(i))dh)Vt(g(i)).\mathbf{o}^{(i)}_t \;=\; \mathrm{softmax}\!\left(\frac{\mathbf{q}^{(i)\top}_t \, K^{(g(i))\top}_{\le t}}{\sqrt{d_h}}\right) V^{(g(i))}_{\le t}.

The cache stores only GG key and GG value vectors per token per layer, for a footprint of 2Gdh2 G d_h scalars — exactly H/GH/G times smaller than MHA, and equal to MQA when G=1G = 1 (Ainslie et al. 2023, §3).

Why a divisor of HH? Every query head must map to exactly one KV group, and the per-group attention kernel is most efficient when the broadcast factor H/GH/G is an integer (so a single KK/VV tile is reused across H/GH/G query tiles in registers; this is how Flash-Attention, PyTorch SDPA, and vLLM all implement it — see e.g. the enable_gqa=True path in PyTorch’s scaled_dot_product_attention, docs). Non-divisor GG is implementable but irregular and unused in published models.

Why this particular middle ground? Two alternatives motivate the design. (i) Reducing HH at fixed dhd_h would shrink the cache equally but also reduce query expressivity; the GQA paper §3 argues that MQA-style degradation tracks the loss of key/value diversity, not query diversity, so leaving HH query heads intact preserves the high-frequency directions in attention that MQA collapses. (ii) Adding a low-rank residual to a single shared K,VK, V (the MLA route, DeepSeek-V2 arXiv 2405.04434) yields stronger compression but requires architectural surgery incompatible with averaging-init uptraining.

Parameter count. The Q, K, V, O projection block costs

PMHA=4d2,PGQA(G)=d2 ⁣(2+2GH),PMQA=d2 ⁣(2+2H),P_\text{MHA} = 4 d^2, \qquad P_\text{GQA}(G) = d^2 \cdot \!\left(2 + \tfrac{2G}{H}\right), \qquad P_\text{MQA} = d^2 \cdot \!\left(2 + \tfrac{2}{H}\right),

so GQA saves 2d2(1G/H)2 d^2 (1 - G/H) parameters per layer over MHA in the attention block — small in the global parameter budget, but the relevant savings are runtime: at decode the GEMM and the cache read both shrink by H/GH/G on the K, V side.

KV-cache footprint. Per layer, per token:

KVGQA  =  2Gdhscalars,KVMHAKVGQA=HG.\text{KV}_\text{GQA} \;=\; 2 \, G \, d_h \quad \text{scalars}, \qquad \frac{\text{KV}_\text{MHA}}{\text{KV}_\text{GQA}} = \frac{H}{G}.

For Llama 2 70B (H=64H = 64, G=8G = 8, dh=128d_h = 128, L=80L = 80, fp16), this gives 22812880=327,6802 \cdot 2 \cdot 8 \cdot 128 \cdot 80 = 327{,}680 bytes/token = 320 KiB/token, or 40 GiB at T=128T = 128K — fitting on a single H100 80GB alongside weights, where the MHA equivalent did not (Llama 2 paper §2.1).

Uptraining recipe (averaging-init). Ainslie et al.’s real contribution is the conversion procedure: given an MHA checkpoint with HH key/value heads, initialize each new GQA group’s weights as the mean of the original heads assigned to that group,

WK(g)    1H/Gi:g(i)=gWK(i),WV(g)    1H/Gi:g(i)=gWV(i),W_K^{(g)} \;\leftarrow\; \frac{1}{H/G} \sum_{i \,:\, g(i) = g} W_K^{(i)}, \qquad W_V^{(g)} \;\leftarrow\; \frac{1}{H/G} \sum_{i \,:\, g(i) = g} W_V^{(i)},

then continue pretraining for α1\alpha \ll 1 fraction of the original compute. The paper (§3.1, Figure 1) sweeps α\alpha on T5-XXL and finds quality plateaus at α0.05\alpha \approx 0.05 — uptraining for 5% of original tokens recovers MHA-level performance. They contrast averaging with two alternatives — picking a single head from each group, and random reinitialization — and report both as strictly worse on the same uptraining budget (Figure 2). The averaging step matters because K(g)K^{(g)} and V(g)V^{(g)} inherit the column-space spanned by the heads they replace, so attention scores at step zero of uptraining are a sensible weighted average of the MHA scores rather than uncorrelated noise.

§ 3 · Reference implementation

Sketch: grouping and broadcast

KV cache layout: query heads on top, key/value groups on bottom, connecting lines show which queries read from which KV store.GQA: 16 query heads → 8 KV groupsQueriesQ0Q1Q2Q3Q4Q5Q6Q7Q8Q9Q10Q11Q12Q13Q14Q15KV groupsK,V0K,V1K,V2K,V3K,V4K,V5K,V6K,V7KV bytes / token / layer (fp16):4.0 KB(2.0× smaller than MHA)
G = 16 is MHA (no sharing). G = 1 is MQA (every head shares one K, V). Intermediate G values are GQA. Switch to MLA to compress K, V into a small per-token latent.
# B = batch, T = seq len, H = query heads, G = kv heads (H % G == 0), d_h = head dim
def gqa(h, W_Q, W_K, W_V, W_O, H, G, d_h):
    # h: [B, T, d]; W_Q: [d, H*d_h]; W_K, W_V: [d, G*d_h]; W_O: [H*d_h, d]
    q = (h @ W_Q).reshape(B, T, H, d_h)            # [B, T, H, d_h]
    k = (h @ W_K).reshape(B, T, G, d_h)            # [B, T, G, d_h]   ← G, not H
    v = (h @ W_V).reshape(B, T, G, d_h)            # [B, T, G, d_h]

    # Broadcast each KV head to H/G query heads. In practice this is a view,
    # not a copy — Flash-Attn / SDPA enable_gqa stride the K, V tiles.
    repeat = H // G
    k = k.repeat_interleave(repeat, dim=2)         # [B, T, H, d_h]
    v = v.repeat_interleave(repeat, dim=2)

    scores = (q @ k.transpose(-2, -1)) / d_h**0.5  # [B, T, H, T]
    scores = causal_mask(scores)
    o = softmax(scores, dim=-1) @ v                # [B, T, H, d_h]
    return o.reshape(B, T, H * d_h) @ W_O          # [B, T, d]

The repeat_interleave is the load-bearing line: it is what makes a 64-query, 8-KV layer behave identically to the MHA kernel from the query side while the cache stays at G=8G = 8.

§ 4 · Empirical evidence

Ablations, scaling, and independent reproductions

Original paper (Ainslie et al. 2023). Table 1 reports T5-XXL (11B encoder-decoder) uptrained from the MHA checkpoint at α=0.05\alpha = 0.05 and evaluated on five tasks (CNN/DailyMail, arXiv, PubMed summarization; WMT En→De; TriviaQA). GQA at G=8G = 8 comes within 0.05–0.3 absolute points of the MHA-XXL baseline on every task; MQA loses 0.6–1.4 points on the same tasks. Figure 3 plots inference latency: MQA is 6× faster than MHA on T5-XXL decoding, GQA-8 is 5.5× faster — i.e. GQA captures almost all the speedup without the quality regression. Figure 4 sweeps G{1,2,4,8,16,32,64}G \in \{1, 2, 4, 8, 16, 32, 64\} and shows the elbow lies near G=8G = 8 for T5-XXL: returns diminish sharply above G=8G = 8, and quality falls off below it.

Llama 2 (Touvron et al. 2023). The first large-scale decoder reproduction. Appendix A.2.1 reports a held-out perplexity ablation: MHA, MQA, and GQA-8 trained from scratch at 150B tokens on the 7B architecture. GQA-8 matches MHA on perplexity and beats MQA by ≈0.05 nats; on the 30-task average (Table 18) GQA is within 0.2 points of MHA and 1.2 points above MQA. The 70B production model ships GQA-8 (H=64H = 64, G=8G = 8). Llama 3 and Llama 3.1 retain the same layout (Llama 3 report §3.1).

Mistral 7B ablation. The Mistral 7B paper (Jiang et al. 2023, arXiv 2310.06825) reports a 6× decode throughput improvement at long context from combining GQA-8 with sliding-window attention, without an MHA control of equal training compute. The throughput attribution to GQA in isolation is therefore indirect, but the per-token cache footprint matches the H/GH/G prediction.

Independent reproduction — Chinchilla-scale ablation. Chowdhury et al. (arXiv 2406.18219, “Weighted Grouped Query Attention”) retrain 1.3B decoders from scratch at 100B tokens sweeping G{1,2,4,8,16}G \in \{1, 2, 4, 8, 16\} and confirm the GQA elbow at G=H/8G = H/8 to H/4H/4. Their Table 2 shows GQA-4 within 0.1 perplexity of MHA-16 at the 1.3B scale, with MQA losing ≈0.3 perplexity. They also propose a learned weighted average over heads inside each group that recovers another 0.05 perplexity — a small but reproducible gain on the same KV-cache budget.

Independent reproduction — Qwen2. The Qwen2 technical report (Yang et al. 2024, arXiv 2407.10671) reports a 1.5B-parameter sweep over G{1,2,4,8,16}G \in \{1, 2, 4, 8, 16\} (Table 9 of the appendix) and selects G=4G = 4 for their small models and G=8G = 8 for the 7B/72B; the report frames the choice as a Pareto pick on the quality-vs-cache curve and is consistent with the GQA paper’s elbow analysis.

Long-context behavior. Independent measurements at T32T \ge 32K (e.g. RULER benchmark, Hsieh et al. 2024, arXiv 2404.06654) compare GQA-8 Llama 3 against MHA models at similar parameter counts: GQA does not degrade long-context recall relative to MHA at matched training, contra the original concern that fewer KV heads would lose positional information. No public study isolates GG at long context with everything else held fixed, however, so the question “does smaller GG disproportionately hurt long context?” remains without a clean ablation as of 2026-05.

Sensitivity to the uptraining fraction α\alpha. Beyond the original Figure 1 sweep on T5-XXL, no independent study has published a sweep of α\alpha at decoder-LM scale; Llama 2 reports only “GQA was added during the final training stage” without a recovery-curve ablation.

Adopted by

  • Llama 2 70B · Meta — First production decoder to ship GQA; uptrained from an MHA checkpoint at 5% original-training compute.  [source]
  • Llama 3.1 70B · Meta — 8 key-value heads share 64 query heads (G = 8).  [source]
  • Gemma 3 27B · Google DeepMind — GQA at the head level across both local SWA and global attention layers.  [source]
  • OLMo 2 13B · Allen Institute for AI (AI2) — GQA in the 13B dense model.  [source]
  • OLMo 3 32B · Allen Institute for AI (AI2) — 40 query heads, 8 KV heads (5:1 ratio) at the 32B variant; the companion 7B keeps standard MHA.  [source]
  • OLMoE 1B/7B · Allen Institute for AI (AI2) — GQA across the 16-layer MoE.  [source]
  • Mistral 7B · Mistral AI — 32 query heads share 8 KV heads (G = 8).  [source]
  • DeepSeek LLM 67B · DeepSeek-AI — 64 query heads grouped into 8 KV heads in the 67B variant; the 7B uses MHA.  [source]
  • Mixtral 8x7B · Mistral AI — Same GQA layout as Mistral 7B (32Q / 8KV) carried into the MoE descendant.  [source]
  • Qwen3 235B-A22B · Alibaba (Qwen Team) — 64 query heads over 4 KV heads (G = 16) in the 235B-A22B flagship; technical report §2 Table 1.  [source]
  • Qwen3 32B · Alibaba (Qwen Team) — 64 query heads over 8 KV heads (G = 8) in the dense flagship.  [source]
  • Qwen3 30B-A3B · Alibaba (Qwen Team) — 32 query heads over 4 KV heads (G = 8) in the small MoE.  [source]
  • Hunyuan-Large 389B · Tencent — 80 query heads grouped into 8 KV heads (G = 10) across 64 layers.  [source]
  • GLM-4.5 · Zhipu AI — 96 query heads over 8 KV heads (G = 12) — a high-Q / aggressive-GQA configuration.  [source]
  • Nemotron 3 Nano 30B-A3B · NVIDIA — GQA with 2 KV groups on the 6 attention layers; the other 23 sequence-mixing layers are Mamba-2.  [source]
  • Qwen3-Next 80B-A3B · Alibaba (Qwen Team) — Gated Attention layers (1 in every 4 layers) use 16 query heads sharing 2 KV heads.  [source]

Lineage

Cite

BibTeX entry for the original paper
@article{arxiv2305_13245,
  title  = {GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints},
  author = {Joshua Ainslie and others (Google Research)},
  year   = {2023},
  eprint = {2305.13245},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2305.13245}
}

Or cite the paper directly: arXiv:2305.13245.

Export

BibTeX
@article{arxiv_2305_13245,
  title         = {GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints},
  author        = {Joshua Ainslie et al. (Google Research)},
  year          = {2023},
  eprint        = {2305.13245},
  archivePrefix = {arXiv},
  url           = {https://arxiv.org/abs/2305.13245}
}
CSL JSON
{
  "id": "arxiv_2305_13245",
  "type": "article-journal",
  "title": "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints",
  "author": [
    {
      "literal": "Joshua Ainslie et al. (Google Research)"
    }
  ],
  "issued": {
    "date-parts": [
      [
        2023
      ]
    ]
  },
  "URL": "https://arxiv.org/abs/2305.13245",
  "number": "2305.13245",
  "source": "arXiv"
}
RIS
TY  - JOUR
TI  - GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints
AU  - Joshua Ainslie et al. (Google Research)
PY  - 2023
JO  - arXiv
AN  - arXiv:2305.13245
UR  - https://arxiv.org/abs/2305.13245
ER  -