Skip to content
mlmentorship

Grouped-query and multi-query attention (GQA, MQA)

Share K and V heads across query heads to shrink the KV cache 4-8x with negligible quality loss. Standard in modern decoder LLMs.

Published · 3 min read ·Core ·Intermediate

Visual quick review

Visual first · depth when needed

For eight query heads and two key/value heads, identify which queries share each key/value pair and derive the fourfold KV-cache reduction while all eight query heads remain distinct.

Preparing the visual…

Summary

GQA and MQA reduce the number of distinct K/V projection heads while keeping the full set of Q heads, so multiple query heads share the same key and value tensors. MQA is the extreme case: one K/V head total. GQA picks an intermediate number of K/V groups.

The KV cache dominates LLM serving memory at long contexts (see KV cache). Cutting the number of K/V heads cuts cache size proportionally:

  • MHA (standard, e.g. GPT-3): K and V have the same number of heads as Q.
  • MQA (Shazeer, 2019): 1 K and 1 V head shared across all Q heads. ~num_heads× smaller cache.
  • GQA (Ainslie et al., 2023): G groups, each shared across num_heads / G Q heads. Tunable midpoint.

Llama 2 70B uses GQA with 8 K/V groups for 64 query heads (8× cache reduction). Llama 3, Mistral, Qwen, and most modern decoders default to GQA.

The mechanism

In standard multi-head attention, for each head :

with and .

In GQA with groups, the query heads are partitioned into contiguous groups of size . All query heads in the same group attend to the same shared . MQA is GQA with .

Implementation: project K and V to dimension instead of , then broadcast (repeat) across the matching Q heads before the matmul.

Learning objective

Which heads are shared when 8 query heads use 2 K/V heads?

The 8 query heads stay distinct

Each group reuses one key head and one value head; it does not merge its queries.

GroupQuery headsShared pair
1Q1, Q2, Q3, Q4K1 + V1reused 4×
2Q5, Q6, Q7, Q8K2 + V2reused 4×

H/G = 8 queries ÷ 2 groups = 4 queries per pair

Only cached K/V head count shrinks

Holding sequence length and head dimension fixed, cache size scales with the number of K/V pairs.

VariantQ headsK/V pairsCache
MHA888/8 = 1×
GQA822/8 = ¼×4× smaller
MQA811/8 = ⅛×

Q count: unchanged · cached K/V pairs: 8 → 2

Read it this way: read across the left table first: Q1 through Q4 remain four separate query heads but all use K1 and V1; the next four queries use K2 and V2. Then compare counts on the right: caching 2 K/V pairs instead of 8 makes the head-dependent cache one quarter as large without removing any query heads. MQA takes the same sharing idea to one K/V pair. Original comparison checked against the primary GQA paper and PyTorch GQA documentation.

Tradeoffs

VariantKV headsCache sizeQualityUsed by
MHAbaselineGPT-3, original Llama
GQA-88×~baselineLlama 2/3 70B, Mistral
MQA1×small dropPaLM, Falcon

GQA recovers nearly all MHA quality while keeping most of MQA’s cache savings. The dominant choice in 2026.

Common pitfalls

  • Confusing K/V heads with Q heads. GQA shrinks K/V only; Q stays full-rank.
  • Assuming the speedup is in compute. GQA mostly saves memory (cache + bandwidth), not FLOPs. The matmul cost barely changes.
  • Re-training cost. You generally cannot convert MHA → GQA post-hoc; the K/V projections were trained per-head. Distillation or partial re-training is required.