Glossary · Attention

Grouped-query attention (GQA)

Also: GQAKV headsnum_key_value_heads

Read it at the depth you need: one sentence · a paragraph · the math · in the models here

1In one sentence

Attention where groups of query heads share one key/value head, shrinking the KV cache by the group size with little loss in quality.

2In a paragraph

In grouped-query attention the model keeps all its query heads but only a few key/value heads; each KV head serves a group of query heads. Llama 3 8B has 32 query heads and 8 KV heads, so four query heads read the same keys and values. The KV cache, which is what limits long contexts and big batches, shrinks by that factor of four. It sits between full multi-head attention (one KV head per query head) and multi-query attention (one KV head for all).

3The math, shapes and parameters

With H query heads, K key/value heads (K divides H) and head size h, the projections are

WQ: [H·h, d]   WK: [K·h, d]   WV: [K·h, d]   WO: [d, H·h]

Query head j uses KV head ⌊j / (H/K)⌋. In code the K key and value heads are repeated H/K times before the usual attention. In config.json K is num_key_value_heads.

KV cache per token = 2 × L × K × h × bytes

Against MHA that is a factor H/K smaller, and the k and v projections shrink by the same factor (a small saving in parameters). The compute per token barely changes: every query head still scores every cached key.

Term by term on Notation Decoder, the sister site: Multi-head attention, decoded

4In the 96 models on this site

81 of the 96 models here use grouped (or multi-query) attention. The largest of each series, per token in BF16, against what the same layers would cache with a KV head for every query head:

ModelQuery / KV headsGroupKV per tokenIf it were MHAKV at 32K
SmolLM3 3B16 / 44×73.7 KB294.9 KB2.4 GB
Granite 3.3 8B32 / 84×163.8 KB655.4 KB5.4 GB
Falcon3 10B12 / 43×163.8 KB491.5 KB5.4 GB
Gemma 3 27B32 / 162×507.9 KB1.0 MB16.6 GB
Gemma 2 27B32 / 162×376.8 KB753.7 KB3.1 GB (its max, 8K)
Gemma 4 31B32 / 48×901.1 KB2.3 MB29.5 GB
OLMo 3 32B40 / 85×262.1 KB1.3 MB8.6 GB
OLMo 2 32B40 / 85×262.1 KB1.3 MB1.1 GB (its max, 4K)
Seed-OSS 36B80 / 810×262.1 KB2.6 MB8.6 GB
Phi-3.5 MoE32 / 84×131.1 KB524.3 KB4.3 GB
DeepSeek-R1-Distill-Llama-70B64 / 88×327.7 KB2.6 MB10.7 GB
Qwen2.5 72B64 / 88×327.7 KB2.6 MB10.7 GB
Qwen3-Next 80B-A3B16 / 28×24.6 KB196.6 KB805 MB
gpt-oss-120b64 / 88×73.7 KB589.8 KB2.4 GB
Mixtral 8x22B48 / 86×229.4 KB1.4 MB7.5 GB
MiniMax M248 / 86×254.0 KB1.5 MB8.3 GB
Qwen3 235B-A22B64 / 416×192.5 KB3.1 MB6.3 GB
GLM-4.596 / 812×376.8 KB4.5 MB12.3 GB
Qwen3.5 397B-A17B32 / 216×30.7 KB491.5 KB1.0 GB
Llama 4 Maverick (17B-128E)40 / 85×196.6 KB983.0 KB6.4 GB
Llama 3.1 405B128 / 816×516.1 KB8.3 MB16.9 GB
Nemotron 3 Ultra 550B-A55B64 / 232×12.3 KB393.2 KB403 MB
Inkling64 / 88×495.6 KB2.2 MB16.2 GB
Ling-1T64 / 88×327.7 KB2.6 MB10.7 GB

Introduced in Ainslie et al., “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints” (2023).

Related

Multi-head attention (MHA) · Multi-query attention (MQA) · Multi-head latent attention (MLA) · KV cache · every term