Grouped-Query Attention (GQA) and MLA Explained for LLMs
By LatestLLM Editorial · Published · Facts verified · 8 min read
Also called: GQA, multi-query attention, MQA, multi-head latent attention, MLA, KV heads
Before this: Attention, KV cache
In one line
Grouped-query attention lets several query heads share one set of key and value heads, cutting the KV cache by the group size at little cost in quality.
Grouped-query attention, simply and technically
In plain English
Attention uses many “heads” side by side, each looking at the text for a different kind of connection. Every head used to keep its own full set of notes about every earlier word, which made the notes enormous. Grouped-query attention is a simple saving: the heads still ask their own questions, but groups of them share one set of notes.
The result is a model that behaves almost as before while keeping a fraction of the notes in memory. Most recent open models use it, because it makes long conversations cheaper and faster to serve.
Think of it like this: Think of a newsroom with 64 reporters. In the old design each reporter kept a private filing cabinet of source notes. In grouped-query attention, groups of eight reporters share one cabinet: each still asks her own question, but all eight read from the same files, so only eight cabinets are needed. The comparison holds for how many cabinets are needed, but a real head also decides for itself how much weight to give each file.
Everyday example: Sharing cabinets
A model has 64 reporters (query heads) and shares one cabinet among each group of eight.
Input:
64 query heads, 8 groups
Output:
8 sets of notes instead of 64: one eighth of the cache
What it shows: Eight cabinets instead of 64 means one eighth of the filing space for notes. The model's answers stay close to those of the 64-cabinet version because each reporter can still ask a different question of the shared notes.
Technically
In multi-head attention (MHA) every one of H query heads has its own key and value head, so the cache stores H key-value pairs per token per layer. Multi-query attention (Shazeer 2019) shares a single key-value head across all query heads, and grouped-query attention (GQA; Ainslie et al. 2023) uses G key-value heads with 1 < G < H, so each KV head serves H / G query heads. The cache per token per layer is 2 × G × head_dim × bytes. gpt-oss has 64 query heads and 8 KV heads, an 8× reduction against MHA; Gemma 4 31B has 32 query heads and 16 KV heads on local layers and only 4 (of width 512) on global layers; Gemma 4 E2B has 8 query heads and one KV head, which is multi-query attention.
Multi-head latent attention (MLA, DeepSeek-V2) compresses instead of sharing. Keys and values are reconstructed from a low-rank latent: DeepSeek-V3 has 128 heads but caches only a 512-number latent plus a 64-number shared RoPE key, 576 numbers per token per layer, 71 times fewer than caching a key and value for every head, while keeping 128 distinct heads at computation time.
Technical example: Cache per design, DeepSeek-V3 shape
The same model shape (61 layers, 128 heads, 192-wide keys, 128-wide values, 131,072 tokens, bfloat16) is costed under four attention designs.
Code (python):
layers, heads, ctx = 61, 128, 131072
key_w, val_w = 128 + 64, 128 # per-head key width (without and with RoPE) and value width
per_head = (key_w + val_w) * 2 # bytes for one head's key and value
designs = {
"multi-head (128 KV heads)": heads * per_head,
"grouped-query (8 KV heads)": 8 * per_head,
"multi-query (1 KV head)": per_head,
"latent attention (512 + 64)": (512 + 64) * 2,
}
for name, b in designs.items():
print(f"{name:28s} {b:>7,} B per token per layer {layers * ctx * b / 1e9:8.2f} GB")
Output:
multi-head (128 KV heads) 81,920 B per token per layer 654.98 GB
grouped-query (8 KV heads) 5,120 B per token per layer 40.94 GB
multi-query (1 KV head) 640 B per token per layer 5.12 GB
latent attention (512 + 64) 1,152 B per token per layer 9.21 GB
What it shows: These totals match the figures in the DeepSeek-V3 architecture guide: 655 GB for full multi-head, 40.9 GB for eight KV heads, 5.12 GB for one and 9.21 GB for latent attention. Latent attention keeps about 1.8 times what a single KV head would store but retains 128 separately computed heads.
Many heads, many notes
In plain English: Attention splits its work among heads, each of which asks its own question of the text. In the original design each head also had its own notes about every earlier word. With 64 heads, that is 64 sets of notes per word per layer.
Technically:
With H heads of width d_k, multi-head attention projects the input into H queries, H keys and H values. Only the keys and values must be kept for later tokens, so the cache cost scales with H. During decoding every layer reads all cached keys and values for every new token, so more KV heads also mean more memory traffic and slower generation.
Query heads, in contrast, are used once and discarded. This asymmetry is what GQA exploits: queries can stay numerous and diverse while keys and values are shared.
From multi-query to grouped-query
In plain English: Taking sharing to the extreme gives all heads one set of notes. That is the cheapest in memory, but quality can suffer because every head has to make do with the same view of the text. Sharing within groups is the middle road.
Technically:
Shazeer (2019) showed that multi-query attention greatly reduces the memory bandwidth needed for incremental decoding with only minor quality loss. Ainslie et al. (2023) observed that MQA can degrade quality and be unstable to train, and proposed GQA with an intermediate number of KV heads; they also showed that an existing multi-head checkpoint can be converted to MQA using 5% of the original pre-training compute, and that uptrained GQA reaches quality close to multi-head attention with speed comparable to MQA.
Converting a checkpoint merges the key and value heads inside each group by mean-pooling, then trains briefly. The group size is a design choice: gpt-oss uses 8 query heads per KV head, Gemma 4 31B uses 2 on local layers and 8 on global layers (32 query heads on 4 KV heads).
What is uptraining?
Converting an existing trained model to a new design, such as MHA to GQA, with a small amount of extra training instead of training from scratch.
Compressing instead of sharing
In plain English: Another way to save notes is to write them in shorthand. Multi-head latent attention stores one short summary per word and expands it into full notes only when needed. Every head still gets its own view, but the stored summary is tiny.
Technically:
MLA projects the hidden state to a small latent vector c (512 numbers in DeepSeek-V3) and derives each head's keys and values from c with learned up-projections, which can be folded into the query and output projections at inference time so the full keys are never materialised. Rotary position information cannot be compressed this way, so a separate 64-number RoPE key shared by all heads is cached next to the latent.
DeepSeek-V2 reports a 93.3% smaller KV cache than DeepSeek 67B and a 5.76 times higher maximum generation throughput, a comparison across different models, so it is not a pure measurement of MLA alone. In DeepSeek-V3 the per-token cache is 576 numbers per layer, about 1.8 times the cost of multi-query attention yet with 128 independently computed heads.
Go deeper: details for practitioners
- Because the up-projection can be absorbed, attention at decode time runs directly on the latent, which makes MLA both memory- and compute-friendly.
Choosing the group size
In plain English: Fewer KV heads means a smaller cache but a bit less variety in what each head can see. Designers pick the smallest number of groups that keeps quality, and several recent models go very small on a few layers where it matters less.
Technically:
Gemma 4 varies the number across layer types and sizes: the 31B model has 16 KV heads on its local layers and 4 on global layers, the 12B has 8 and 1, and the small E2B model has a single KV head on every layer. The global layers, which cache the whole context, are where reducing KV heads pays most, so those are cut hardest and compensated with wider heads (512 against 256) and keys reused as values.
GQA combines with the other cache savers: sliding windows cap how many tokens local layers store, quantised caches lower bytes per number, and latent attention stacks a second reduction on top of fewer heads. These multiply, which is how a 131,072-token conversation fits in a few gigabytes.
Common misconceptions
Myth: Grouped-query attention uses fewer attention heads.
Reality: It keeps all the query heads. Only the key and value heads, the part that is cached, are fewer and shared.
Myth: Sharing key-value heads costs a lot of quality.
Reality: Experiments report quality close to full multi-head attention for GQA, which is why most recent open models use it.
Myth: Latent attention is the same as grouped-query attention.
Reality: GQA shares heads; MLA compresses keys and values into a small latent and keeps every head distinct when computing attention.
Where you'll see this
- gpt-oss architecture: gpt-oss uses 64 query heads on 8 KV heads in every layer.
- Gemma 4 architecture: Gemma 4 uses different KV-head counts for local and global layers, down to a single KV head in the E2B model.
- DeepSeek-V3 architecture: DeepSeek-V3 uses multi-head latent attention with a 512-number latent and a 64-number RoPE key.
Key takeaways
- Grouped-query attention keeps all query heads but gives groups of them a shared key-value head, so the cache shrinks by the group size.
- Multi-query attention is the extreme case with one KV head; multi-head attention is the other extreme with one per query head.
- gpt-oss uses 64 query heads on 8 KV heads; Gemma 4 E2B uses a single KV head; DeepSeek-V3 compresses 128 heads into a 576-number latent.
- Models can be converted: an MHA checkpoint can be uptrained to GQA with about 5% of the original pre-training compute.
Frequently asked questions
What is grouped-query attention?
An attention variant in which several query heads share one key-value head, reducing the KV cache by the group size with little loss in quality.
What is the difference between MHA, MQA and GQA?
MHA gives every query head its own key-value head, MQA gives all of them one, and GQA gives each group of query heads one.
What is multi-head latent attention?
DeepSeek's design that caches a compressed latent vector per token and reconstructs keys and values for every head from it.
How much memory does GQA save?
A factor equal to the number of query heads per KV head: 8 times for gpt-oss, with 64 query heads and 8 KV heads.
Can an existing model be converted to GQA?
Yes. Ainslie et al. showed that a multi-head checkpoint can be uptrained to multi-query attention with about 5% of the original pre-training compute, and GQA works in a similar way.
Sources
Facts last verified November 23, 2026.