# Multi-Query vs Grouped-Query Attention

> How multi-query and grouped-query attention share key and value heads to make LLM replies faster, and what each one costs, for your AI interview.

**In short:** Both cut the memory a model reads for each new word by sharing keys and values across query heads. Multi-query shares one set and saves the most, while grouped-query shares a few sets and keeps quality closer to full attention.

AI and LLM interview question Q058, Transformer architecture. Advanced, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/multi-query-vs-grouped-query-attention

## Say it in 30 seconds

Multi-query and grouped-query attention are two ways to make a language model faster when it writes. Normal attention keeps a separate key and value for every head, and reading that memory on every word is slow. Multi-query shares one set across all heads. Grouped-query shares one set per small group. Grouped-query is the balance Meta chose for its larger Llama 2 models and for Llama 3. With extra training, quality can stay close to full attention, and speed is close to multi-query. I would still test quality on real questions first.

## Say it in 2 minutes

When a model writes a reply, it writes one word at a time. For each new word, every attention head looks back at the keys and values of all the earlier words. Those saved keys and values are the KV cache. In standard multi-head attention, each head has its own set, so the cache grows fast in a long chat, and the chip has to read all of it for every word. That read is often what makes generation slow.

Multi-query attention, from Noam Shazeer's 2019 paper, shares one key and one value across all the heads. The cache becomes much smaller, so replies get faster, but quality can drop. Grouped-query attention, from a 2023 paper by Ainslie and colleagues, splits the heads into groups and gives each group one key and value. The paper reports that a grouped-query model given extra training reaches quality close to full multi-head attention, at a speed close to multi-query.

In practice, Meta's Llama 2 paper says its larger models use grouped-query attention for better inference, and its Llama 3 paper says it uses 8 key-value heads. In an interview, lay out the trade-off: multi-head is the baseline, multi-query is the fastest with the most risk, and grouped-query is the balance. Then say how you would test quality on your own tasks before you pick one.

## What are multi-query and grouped-query attention, in plain words?

Attention is how a model decides which earlier words matter for the next one. Each attention head has three parts: a query, which asks what the head needs; a key, which says what each word offers; and a value, which holds the content passed on.

Standard multi-head attention gives every head its own keys and values. The other two share them, so fewer have to be stored and read while the model writes.

Picture a branch with eight teller windows. In the standard setup, each window keeps its own filing cabinet. Multi-query gives all eight windows one shared cabinet. Grouped-query gives two cabinets, and each one serves four windows. The cabinets stand for the keys and values.

A real case: a bank's support chatbot reads a long chat before each reply. The saved keys and values of those words are the KV cache, and that store is what these designs shrink.

- **Query head:** The part of attention that asks what it needs from earlier words.
- **Key and value:** The stored label and content of each earlier word that a head can read.
- **KV cache:** The saved keys and values for every word so far, kept while the reply is written.

## Why does sharing keys and values make replies faster?

Writing each new word means reading the whole KV cache from GPU memory (the chip that runs the model). The bigger the cache, the longer that read takes, and the reply slows with it.

Meta's Llama 2 paper makes the same point for larger models, and cites earlier work for it: when the cache becomes a bottleneck, keys and values can be shared across heads with little loss of quality.

*Diagram, How many key-value sets each design reads: A made-up eight-head model. Each design sets how many sets every new word reads, which sets the memory traffic and the quality trade-off.*
1. Eight sets to read for each new word: the most memory traffic.
2. Two sets to read: a quarter of the traffic, near full quality with extra training.
3. One set to read: an eighth of the traffic, but quality can drop.

Fewer sets to read means less memory traffic for each word, and that is where the speed comes from.

## How much memory does sharing actually save?

A made-up legal team chatbot: 32 layers, 8 query heads, 2 bytes per number, and 4,000-token contract chats (tokens are pieces of words). Say 20 GiB of GPU memory is left for the cache. A GiB is about a gigabyte, and MiB is a smaller unit.

1. **Start from the full setup:** With 8 key-value heads, one contract chat's cache is about 500 MiB, so only 40 chats fit in the 20 GiB.
2. **Share into two groups:** Grouped-query with 2 key-value heads cuts a chat to about 125 MiB, so 163 chats fit.
3. **Share one set:** Multi-query with 1 key-value head is about 62.5 MiB, so 327 chats fit, with the quality risk.

```python
# A legal team's made-up chatbot: 32 layers, 8 query heads, 128 numbers per head, 2 bytes per number.
# Each chat is 4,000 tokens. 20 GiB of GPU memory is left for the cache.
LAYERS, HEAD_SIZE, BYTES, TOKENS = 32, 128, 2, 4000
FREE_MIB = 20 * 1024

def cache_mib(kv_heads):
    # Keys and values (two of them) for every layer, head and token.
    per_token = 2 * LAYERS * kv_heads * HEAD_SIZE * BYTES
    return per_token * TOKENS / (1024 * 1024)

for name, kv_heads in [("Multi-head", 8), ("Grouped-query", 2), ("Multi-query", 1)]:
    per_chat = cache_mib(kv_heads)
    print(f"{name:<13} {per_chat:6.1f} MiB per chat, {int(FREE_MIB // per_chat):3d} chats fit")
```

Output:

```text
Multi-head     500.0 MiB per chat,  40 chats fit
Grouped-query  125.0 MiB per chat, 163 chats fit
Multi-query     62.5 MiB per chat, 327 chats fit
```

The code is a short calculation.Real systems add other memory, so treat the numbers as the shape of the saving only.

## What does each option cost you?

- **Multi-head: Best quality, biggest cache.** Every head keeps its own keys and values. It is the baseline for quality and the slowest to read.
- **Multi-query: Smallest cache, some risk.** One key and value for all heads. Fastest to read, but quality can drop, so it needs testing.
- **Grouped-query: The balance.** A few key-value sets, one per group. In the paper, a model given extra training keeps quality close to full attention, at speed close to multi-query.

Uptrained means an existing model gets some extra training to change its design. The sharing is set in the model itself, so you get it by choosing a model built that way, or by uptraining an existing multi-head model. You cannot flip it on a running model.

> "results in a significant cost savings at autoregressive decoding time" (Google Research, PaLM paper (2022))
>
> "However, MQA can lead to quality degradation" (Ainslie and colleagues, GQA paper (2023))
>
> "We show that uptrained GQA achieves quality close to multi-head attention with comparable speed to MQA." (Ainslie and colleagues, GQA paper (2023))

> **Tip:** Asked which one to pick? Name grouped-query as the balance, then say you would measure quality on your own tasks before you choose multi-query.

## Which real models use them?

Google's PaLM paper, from Google Research, describes multi-query attention. It says the change had a neutral effect on quality and training speed, and it cut the cost of generating text.

Meta's Llama 2 paper says its larger models use grouped-query attention for better inference. Its Llama 3 paper (2024) says it uses grouped-query attention with 8 key-value heads.

### How would you cut the cache for a contract chatbot?

A legal team's chatbot slows down and runs out of GPU memory when staff paste in long contracts. The worked example above is its cache math.

1. **Measure first:** Log the memory left for the cache and how many contract chats fit.
2. **Try a grouped-query model:** Grouped-query can keep quality close to full attention, as the paper shows after extra training, while more chats fit. You get it from a model built that way, or by uptraining one.
3. **Test quality on real questions:** Score answers on past contract questions. Only if quality holds, consider multi-query for even more chats.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: heads share keys and values, so the KV cache is smaller.
- [ ] Why it is faster: each new word reads the cache, so less to read means quicker replies.
- [ ] Multi-query: one shared set, the smallest cache, and possible quality loss.
- [ ] Grouped-query: a few sets, one per group, the balance Meta chose for larger Llama 2 and for Llama 3.
- [ ] The trade-off, with a test on your own tasks before you pick.

**Common traps**

- Trap: "Multi-query is always the better choice." Better: It is the fastest, but quality can drop. Grouped-query is often the balance.
- Trap: "Sharing heads just saves disk space." Better: The saving is the memory read for every word, and the cache held while a reply is written.
- Trap: "Fewer heads means a smaller model." Better: Mostly it shrinks the cache of keys and values for the words so far, and leaves most of the model alone.

## Can you spot the mistake?

**Spot the mistake:** A legal team's bot searches 40,000 contracts. Which line in a teammate's notes is the mistake?

A. Multi-query has the smallest cache, but it can lose some quality, so test it on clause questions.
B. Grouped-query, after extra training, can keep quality close to full attention, at a speed close to multi-query.
C. We picked a model with half the key-value heads, so the cache is half the size. The bot is safe to ship without a quality test.

**Answer:** C. A smaller cache means less to read for each word, so replies can be faster. It proves nothing about quality. Both shared designs can cost quality, so test on real clause questions before you ship.

**Quick check:** A made-up insurer's claims assistant runs out of GPU memory on long claim files. Which model design best cuts the cache and keeps quality close to full attention?

A. A longer system prompt
B. A model with multi-head attention, where every head keeps its own keys and values
C. A model built with grouped-query attention, with a few shared key-value heads
D. A lower temperature

**Answer:** C. Grouped-query shares key and value heads in groups, so the cache shrinks while quality can stay close to full attention. The sharing is set in the model itself. A prompt or a temperature setting does not change the cache.

## Can you answer these from memory?

**Flash cards**

- **What is the KV cache?** The saved keys and values for earlier words, so they are not recomputed for each new word.
- **Multi-query attention?** All query heads share one key and one value set. Smallest cache, but quality can drop.
- **Grouped-query attention?** Query heads share a few key-value sets. Near full quality after extra training, near multi-query speed.
- **Why is a big cache a problem?** Each new word reads the whole cache from GPU memory, so a big cache slows every reply.
- **Which models use them?** Google's PaLM paper describes multi-query. Llama 2's larger models and Llama 3 use grouped-query.
- **What is the trade-off?** Shared heads save memory and time but may cost quality, so test on your own tasks.

## Likely follow-up questions

- Q055 What is the KV cache, and why does it speed up generation? https://designervenkat.online/interview/ai/what-is-the-kv-cache
- Q057 What is FlashAttention? https://designervenkat.online/interview/ai/what-is-flashattention
- Q074 What uses memory at inference time? https://designervenkat.online/interview/ai/what-uses-memory-at-inference-time
- Q056 Why is attention quadratic in sequence length? https://designervenkat.online/interview/ai/why-is-attention-quadratic-in-sequence-length
- Q059 Sliding-window and sparse attention? (full answer coming)

## Learn it in depth

- [Inference Engineering](https://designervenkat.online/learn/ai-engineering/inference-engineering)
