Lesson 5 of 14
Shrinking the cache: MQA, GQA, MLA
In a long chat, the notes a model keeps on every word, not the model itself, decide how many people fit on one GPU. This lesson shows the three tricks newer models use to keep smaller notes (sharing them, compressing them, throwing old ones away) and what each one costs.
Fewer stacks, more conversations
The 3D view could not load. The four stages are described below.
01 · Share
Readers can share a stack
The last lesson left each layer writing a card for every token: a label half (the key) and a content half (the value). Each layer also has a team of readers, called attention heads, each looking back over those cards for something different. Llama 3.1 8B has 32 per layer.
If every reader keeps its own stack, that is multi-head attention: 524 KB of cards per token. Llama 3.1 8B puts its readers in groups of 4 that share one stack, grouped-query attention: 131 KB, so 4 times as many conversations fit. One stack for everybody, multi-query attention, is 16.4 KB, but loses the most quality.
How many stacks for 32 readers?
Llama 3.1 8B's shape, one layer. Plays by itself; move the slider to take over.
- Readers per stack
- Cards per token, 32 layers
- 8,192-token chats on one H100
02 · Compress
Or write in shorthand
DeepSeek’s models keep one compact note per token in each layer instead of cards: 576 numbers, shared by every reader. Each reader learns to read the note directly, which costs extra arithmetic at every step. This is multi-head latent attention.
DeepSeek-V3 is a far larger model than Llama 3.1 8B, with almost twice the layers, yet keeps 70.3 KB per token, about half as much. DeepSeek reports it lost no quality, though that result comes from one lab.
One token's entry in one layer, to scale
- Key half
- Value half
- Compressed note
- Position tag
- Reader doing extra arithmetic
03 · Forget
Or throw old cards away
A sliding window keeps cards only for the most recent tokens. Mistral 7B keeps the last 4,096, writing each new token’s cards over the oldest. In a 32,768-token conversation its cache is 8 times smaller; under 4,096 tokens it saves nothing.
Older text is not wiped out, since the kept cards were written by tokens that had read it. But the model can no longer look at it directly.
A window of 6 words
- A word's cards (key | value) in its numbered slot
- Cards kept, with the window
- Cards kept, without it
04 · The decision
You can’t switch it on
None of these is a setting. A model learns its cards during training, so the realistic path is switching models, and Llama 3, Mistral, Qwen and DeepSeek each use at least one trick. The config file tells you which: fewer key/value heads than attention heads means shared stacks.
Count the weights too. On one H100, 52 conversations of 8,192 tokens fit beside Llama 3.1 8B, against 12 with own stacks. Small groups usually cost little quality, but studies disagree, so test a new model on your own users’ conversations first.
Real models, cache only
Plays by itself; change a setting to take over.
05 · Playground
Keep the cards your way
Pick how the readers keep their cards, tick the window if you like, and press Play to run a sentence through one layer.
One layer, 32 readers, one word at a time
- Reader (query head). Its line shows which cards it reads; readers of one colour read the same cards, and neighbouring groups alternate colours
- Card, label half (key)
- Card, content half (value)
- Compressed note
- Small extra label that records where the word sits in the sentence (kept outside the note)
The same choice for a whole conversation on a real GPU
Each bar is one conversation's cards in a model shaped like Llama 3.1 8B (the longest bar is every reader keeping its own). The count is how many such conversations fit in an 80.0 GB NVIDIA H100, ignoring the 16.1 GB the model itself takes. The window box applies here too, at the 4,096 tokens Mistral 7B keeps.
Every reader its own cards* (chosen)
Groups of 4 share (chosen)
Everyone shares one set* (chosen)
One compressed note* (chosen)
06 · Glossary
The vocabulary, in one place
- Token
- The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
- Layer
- One stage of the model. Every token passes through all of them in order; Llama 3.1 8B has 32.
- Card (key and value)
- What a layer keeps for each token: a label half (the key) that later tokens check to judge how much it matters, and a content half (the value) they take from it.
- KV cache
- All the cards of one conversation, kept in GPU memory so earlier tokens are never run through the model again.
- Reader (attention head)
- One of the separate lookups a layer makes over the earlier tokens, each searching for something different. Also called a query head. Llama 3.1 8B has 32 per layer.
- Stack (KV head)
- One pile of cards, one card per token, that one or more readers consult. Llama 3.1 8B keeps 8 per layer.
- MHA
- Multi-head attention: every reader keeps its own stack. The original design.
- GQA
- Grouped-query attention: readers share a stack in groups, 4 to a stack in Llama 3.1 8B.
- MQA
- Multi-query attention: one stack shared by every reader in the layer.
- MLA
- Multi-head latent attention (DeepSeek): one compressed note per token per layer, 576 numbers, that every reader reads directly.
- Sliding window
- Keeping cards only for the most recent tokens, writing each new token's cards over the oldest ones.
- GPU memory
- The chip's own storage (80.0 GB on an H100). The model and every conversation's cards share it, with 10% kept free.
You can now
- Compute the KV cache per token for MHA, GQA, MQA and MLA from a model config, including the MLA case where num_key_value_heads in config.json misleads.
- Explain how sharing, compressing and windowing keys and values each shrink the cache, in what order a decode step writes and reads them, and what each design costs in quality or reach.
- Decide whether a smaller-cache model is worth switching to: estimate how many more conversations fit on a GPU once the weights are counted, and know these designs are fixed when a model is trained, not switched on at serving time.
Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.