DL0076 GQA: Grouped-Query Attention

What is grouped-query attention (GQA), and why is it introduced?

Answer

Grouped-query attention splits the H query heads of a layer into H_{kv} groups and gives each group a single shared key head and value head, so the number of distinct K/V projections drops from H to H_{kv} while every query head keeps its own W_Q. It is the interpolation between two known extremes: H_{kv} = H is ordinary multi-head attention (MHA), and H_{kv} = 1 is multi-query attention (MQA). It exists because autoregressive decoding is memory-bandwidth bound, not compute bound: generating one token requires re-reading the entire KV cache from HBM, and that cache scales linearly with H_{kv}. MQA already fixed the bandwidth problem but lost quality and was unstable to train, so GQA was proposed to recover almost all of MQA’s decode speed at close to MHA quality by keeping a small number of K/V heads (typically 8). Nearly every major recent open-weight family ships with it, including Llama 3, where all sizes use 8 KV heads; the notable exception is DeepSeek-V2 and V3, which replace head sharing with multi-head latent attention.

(1) The Bottleneck It Targets: the KV cache, not the weights, is what grows with batch size and context, and decode re-reads all of it per token, so shrinking it by H / H_{kv} directly raises arithmetic intensity and tokens per second.
(2) A Dial Between Two Extremes: the lineage MHA → MQA → GQA is a single knob H_{kv}; 8 KV heads recovers most of the memory win of 1 while avoiding the quality drop that MQA shows on summarization and long-input tasks.
(3) Cheap To Retrofit: an existing MHA checkpoint is converted by mean-pooling the K and V projections within each group and then uptraining on roughly 5% of the original pretraining tokens, so no full retrain is needed.
(4) Set H_{kv} To The Tensor-Parallel Degree: with H_{kv} equal to the number of shards, each GPU owns exactly one KV head and nothing is replicated; a smaller H_{kv} forces the same K/V to be duplicated across shards and gives back part of the saving.
(5) Almost No FLOP Change: the query projections and the QK^{\top} product are untouched, so prefill latency barely moves; GQA is a memory and bandwidth optimization, not a compute one.

Three panels each showing four query head boxes above their key/value heads: MHA has four separate KV heads, GQA with group size two has two KV heads each fed by two query heads, and MQA has one KV head shared by all four query heads

Figure 1: The only structural change is how many distinct K/V heads exist; query heads are never merged, so each still has its own projection and its own attention pattern over the shared keys.

Mathematical Formulation:
H_{kv} = H / G
g(i) = \lfloor i / G \rfloor
M_{tok} = 2 L H_{kv} d_h b
W_K^{(g)} = \frac{1}{G} \sum_{i \in \mathcal{G}_g} W_K^{(i)}

Where:

  • H is the number of query heads, G the group size, and H_{kv} the resulting number of key/value heads; G = 1 recovers MHA and G = H recovers MQA.
  • i \in \{0,\ldots,H-1\} indexes query heads and g(i) is the KV head that head i reads, so heads are assigned to groups by contiguous blocks.
  • M_{tok} is the KV cache bytes per token, with L layers, head dimension d_h, bytes per element b, and the factor 2 covering K and V; total cache is M_{tok} times batch times sequence length.
  • \mathcal{G}_g is the set of query-head indices in group g, and the last line is the mean-pooling initialization used when converting an MHA checkpoint; the same averaging is applied to W_V.

The payoff is easiest to see with a concrete configuration. A Llama-3-70B-shaped model has L = 80, H = 64, and d_h = 128; in FP16 that is 2.5 MiB per token of KV cache under MHA, 320 KiB under GQA with 8 KV heads, and 40 KiB under MQA. At a 32K context and batch size 1 those become 85.9 GB, 10.7 GB, and 1.34 GB. On a single H100 with 3.35 TB/s of HBM bandwidth, reading a 10.7 GB cache once costs roughly 3.2 ms per decoded token against roughly 26 ms for the 85.9 GB version; tensor parallelism divides both numbers, but the ratio is fixed by H_{kv} and does not improve with more hardware. That freed memory is also what lets a server hold many more concurrent sequences, which is usually a larger throughput win than the per-token latency itself.

Log-scale bar chart of FP16 KV cache size at 32K context for a 70B-shaped model: MHA with 64 KV heads needs 85.9 GB, GQA with 8 KV heads needs 10.7 GB, and MQA with 1 KV head needs 1.34 GB, with a dashed line marking 80 GB of H100 HBM

Figure 2: FP16 KV cache for one 32K-token sequence on an 80-layer, 64-head, 128-dim model. Under MHA the cache alone would consume an entire 80 GB H100 before any weights are loaded.

PropertyMHAGQA (8 KV heads)MQA
KV heads64 (one per query head)8 (group size 8)1 (shared by all)
Cache per token (FP16)2.5 MiB320 KiB40 KiB
Decode speedSlowest, bandwidth bound on the cacheClose to MQA at long contextFastest
QualityReferenceWithin noise of MHA on most benchmarksMeasurable drop, notably on long-input summarization
Tensor parallelismShards cleanlyShards cleanly when H_{kv} equals the shard countKV must be replicated on every shard
Training stabilityStable baselineStable; uptrainable from an MHA checkpointReported instability at scale

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *