JA EN
LearnHow Transformers Work
·★ MEMBER·PAPER·9 min read

A Field Guide to Attention Variants — MQA, GQA, Sliding Windows, Linear Attention

MQA, GQA, sliding windows and linear attention are not four unrelated tricks. One multiplication decides how large a KV cache gets, and every variant is a decision about which factor in it to attack. Lined up against that formula, the family tree shows exactly what each one gave up and what it bought.

ModalitytextTaskattention

Fast Transformer Decoding: One Write-Head is All You Need


Attention is not one thing

Attention from scratch covered the textbook version: every token looks at every other token, dot products set the weights, and the values get mixed. It is a beautiful mechanism. Open the config file of a model that is actually in production, though, and the textbook form is the exception rather than the rule.

Keep the meeting metaphor going. You are the note-taker. You compare everyone's name tag (Key) against your own question (Query) and blend what people said (Value) into your notes. With ten people in the room this works fine. But a real LLM sits in a meeting where attendees keep arriving and nobody ever leaves. A context of 10,000 tokens means 10,000 name tags and 10,000 statements piled on the desk, none of them thrown away, every time you decide on the next word.

The variants are all answers to the question of how to fold that desk down. And the thing worth noticing is that they are not four unrelated inventions. There are only so many places you can fold, and each variant is named by the fold it chose.

Two different things are expensive

Start by separating the costs, because they get conflated constantly and they are not the same problem.

During training, and during the prefill stage where a whole prompt is read at once, the n×nn \times n score table itself is the burden (nn being the sequence length). Double the sequence and the table quadruples. That is a story about compute and memory.

During generation, when tokens come out one at a time, there is only ever one row of that table: the new token's Query against every past Key. And yet generation is slow. The reason is not arithmetic — it is that every past K and V has to be read back, in full, for every single token. That is the KV cache, and its size is decided entirely by a product.

MKV=2×L×hkv×dhead×n×B×pM_{\mathrm{KV}} = 2 \times L \times h_{kv} \times d_{\mathrm{head}} \times n \times B \times p
(1)

In words: the cache size is layers × KV heads × head width × tokens accumulated × concurrent requests, multiplied by two (for K and V) and by the bytes per number. Reading it term by term: the leading 22 covers K and V. LL is the number of layers, each holding its own cache. hkvh_{kv} is the number of KV heads. dheadd_{\mathrm{head}} is the width of one head. nn is how many tokens have piled up. BB is how many conversations you are serving at once. pp is bytes per number — 2 for FP16.

Everything here is multiplication, and that is the whole point. Cut any single factor to 1/81/8 and the total drops to 1/81/8. Which also means there is nowhere to save that is not already in this formula. So the family tree of variants sorts itself by which term got hit:

FIG 1How far O(n²) and O(n) drift apart as sequences grow. Switch the vertical axis to linear and the n² curve simply leaves the screen — that gap is why long context costs what it costs

MQA — one shared roster

The first move was also the bluntest subtraction. Multi-Query Attention (MQA), proposed by Noam Shazeer in 2019, keeps all the Query heads but collapses K and V down to a single head. Everyone may ask their own question; everyone consults the same one roster, passed around the table. In the formula hkvh_{kv} becomes 11, so the cache shrinks by a factor of the head count.

Why does that translate so directly into speed? Because generating one token means reading the entire KV cache back from memory. The arithmetic involved is trivial; memory bandwidth is what sets the pace. Cut the bytes read to 1/h1/h and the wait shrinks with it. This is the explanation for the initially odd-sounding result that FLOPs barely change and yet the model gets faster.

The price is expressive power. The appeal of multiple heads was that each could look at something different — and MQA makes them all look at the same thing. The reported quality loss is small, but it is not zero.

In 2023 Ainslie et al. published the compromise: Grouped-Query Attention (GQA). Divide the Query heads into groups and give each group its own shared K and V. Set and you have MQA; set and you have ordinary multi-head attention. It is a continuous dial with both prior methods at its ends.

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Fast Transformer Decoding: One Write-Head is All You Need. arXiv:1911.02150Paper page·PDF
  2. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints. arXiv:2305.13245Paper page·PDF
  3. Longformer: The Long-Document Transformer. arXiv:2004.05150Paper page·PDF
  4. Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention. arXiv:2006.16236Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment