↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Depth 7 of 12
Loading recorded attention…

One knob: how many key/value heads

Multi-head attention (the MHA tab), multi-query attention (MQA) and grouped-query attention (GQA) differ in one number: how many key and value heads the query heads share. With query heads and key/value heads, query head reads key and value head :

is multi-head attention, every head with its own keys and values; is MQA, one key head and one value head for all; anything in between is GQA. The queries never change: each head still asks its own question. What changes is how many different keys and values there are to answer it, and so how much a model has to keep for every token it has seen. This page puts the three side by side and asks what sharing changes in the attention maps, in the cache and in speed and quality.

Who reads which key and value

The three cards are the members' own models: GPT-2 small (12 query heads, 12 key/value heads), SantaCoder (16 query heads, 1 key/value head) and Qwen2.5-0.5B (14 query heads in 2 groups of 7). The MHA and GQA tabs record the same three prompts; the MQA tab records SantaCoder on code, so this tab recorded SantaCoder again on the shared prompts with the MQA tab's own script. Each model splits the prompt with its own tokenizer, so the token counts differ slightly.

In each card the fan at the top draws a line from every query head to the key/value head it reads, and below it every query head's attention map at the chosen depth, boxed by that key/value head: twelve boxes of one for GPT-2, one box of sixteen for SantaCoder, two boxes of seven for Qwen2.5. The Depth control steps through twelve recorded depths; GPT-2's 12 layers line up with every other layer of the two 24-layer models. Click a map or a query head to follow it through the fan, the boxes and the overlap matrix.

How alike two maps are

Sharing a key head does not make the heads look at the same tokens. Every query head still has its own query, so its scores against the shared keys, and its softmax over them, are its own. The How alike two maps are matrix measures it: for two heads it is the share of attention both put on the same keys, , averaged over the queries, so 100 % means the same map. The dashed red blocks are the heads that read the same key/value head.

At most depths the heads of all three models put much of their weight on the first token, a resting place known as an attention sink (the Sinks tab); counted, it makes almost any two heads look alike. The matrix therefore leaves the first token out by default and renormalises the rest; the checkbox puts it back. Without it, Qwen2.5's heads that share a key/value head are at most a few points more alike than heads that do not, and at some depths less (the small chart under each matrix): a group shares the keys it can look at, not what it looks for. In SantaCoder, where all sixteen heads share, the maps beyond the first depth differ about as much as GPT-2's independent heads.

The cache

While a decoder generates, every layer keeps the keys and values of all earlier tokens, the KV cache. Per token it holds numbers for layers: query heads cost nothing here, only key/value heads do. The KV cache panel shows the bytes each model's own cache returned for the prompt, measured in 16-bit and 32-bit runs, which match the formula exactly.

GPT-2 small caches numbers a token, 36 KiB in 16 bits. SantaCoder and Qwen2.5-0.5B cache and , both 6,144 numbers or 12 KiB, a third of GPT-2's although both models are larger. With one key/value head per query head they would cache 192 KiB and 84 KiB, the pale bars. The table scales the cache to a context: at Qwen2.5's 32,768 positions one sequence holds 384 MiB. GPT-2 and SantaCoder have 1,024 and 2,048 positions and cannot use longer contexts.

Why a smaller cache is faster

Shazeer (2019) observed that incremental decoding does little arithmetic per byte: each new token is one query against every cached key, so a step's time goes into reading the cache and the weights from memory. Fewer key/value heads mean fewer bytes to read. The One decoding step chart measures it on one model, GPT-2 small with 12, 4, 2 or 1 key/value heads, in 16 bits on the GPU, with each group's query heads reading their shared cache together so nothing is copied.

With one sequence the four take the same time: the step is reading GPT-2's weights, and the cache is small next to them. The cache grows with the batch and the context while the weights are read once per step, so with many long sequences the step time follows the cache: at 128 sequences of 1,023 cached tokens a step takes 24.4 ms with 12 key/value heads, 20.0 with 4, 14.2 with 2 and 10.7 with 1. That is where MQA and GQA pay off, and it is why Pope et al. (2022) could serve far longer contexts on large PaLM models with multi-query attention.

Converting a trained model

The three cards cannot say what sharing costs in quality: they are different models trained on different data. Ainslie et al. (2023) showed how to measure it on one model. A multi-head checkpoint becomes GQA or MQA by replacing each group's key heads, and value heads, with their mean, and then training further, uptraining, with about 5 % of the original pre-training compute. Mean-pooling worked better than keeping one head of each group or starting fresh, and on T5-XXL the uptrained GQA with 8 groups came close to multi-head quality at nearly multi-query speed.

The One model, four ways panel does this to GPT-2 small: 12 heads kept as a control, or pooled to 4, 2 or 1, then all four trained the same way on WikiText-2 for 300 steps, 1.2 million tokens, none seen twice. Pooling alone roughly doubles the test loss, from 3.46 nats to between 6.84 (4 heads) and 7.25 (1 head), more the fewer heads are left. Training wins back more than half of the gap within 300 steps, yet after this short run every pooled model is still behind the control trained the same way, and the order stays MHA, GQA with 4 heads, GQA with 2, MQA at every step. The legend gives each variant's cache: 36, 12, 6 and 3 KiB a token.

Where it came from

Multi-head attention comes from the original Transformer (2017). Shazeer proposed multi-query attention in 2019 to speed up decoding; PaLM and SantaCoder (the MQA tab) were trained with it. Ainslie et al. generalised it to grouped-query attention in 2023, and GQA became the default of open decoders, Llama 2's largest models and the Qwen2.5 family (the GQA tab) among them. MLA (the MLA tab) shrinks the cache another way, caching a compressed latent instead of fewer heads. The Efficiency Explainer's KV cache tab follows the cache through prefill and decoding.

Papers

  1. Fast Transformer Decoding: One Write-Head is All You Need Noam Shazeer · arXiv 2019 Multi-query attention, and why incremental decoding is bound by reading keys and values from memory.
  2. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints Joshua Ainslie, James Lee-Thorp, Michiel de Jong, Yury Zemlyanskiy, Federico Lebrón, Sumit Sanghai · EMNLP 2023 Grouped-query attention, and the mean-pool-then-uptrain conversion the One model, four ways panel applies to GPT-2.
  3. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 Multi-head attention, one key and value head per query head.
  4. Efficiently Scaling Transformer Inference Reiner Pope et al. · MLSys 2023 How the cache bounds large-batch, long-context decoding, and what multi-query attention saves there.
  5. SantaCoder: don't reach for the stars! Loubna Ben Allal et al. · arXiv 2023 The multi-query model in the MQA card, a 1.1B code model.
  6. Qwen2.5 Technical Report Qwen Team (An Yang et al.) · arXiv 2024 The grouped-query model in the GQA card.
  7. Language Models are Unsupervised Multitask Learners Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever · 2019 GPT-2, the multi-head model in the MHA card and the one converted in the four-way panel.

See also