esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

10 min readAdvancedCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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.

How many key-value sets each design reads

8 key-value heads

each query head has its own

2 key-value heads

one set per group of four heads

1 key-value head

one set, shared by all heads

Largest cache

best quality, slowest reads

The balance

near full quality after extra training

Smallest cache

fastest, but quality can drop

  1. 1Eight sets to read for each new word: the most memory traffic.
  2. 2Two sets to read: a quarter of the traffic, near full quality with extra training.
  3. 3One set to read: an eighth of the traffic, but quality can drop.
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.

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. 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. 2

    Share into two groups

    Grouped-query with 2 key-value heads cuts a chat to about 125 MiB, so 163 chats fit.

  3. 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

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. 1

    Measure first

    Log the memory left for the cache and how many contract chats fit.

  2. 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. 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."

    BetterIt is the fastest, but quality can drop. Grouped-query is often the balance.

  • Trap"Sharing heads just saves disk space."

    BetterThe saving is the memory read for every word, and the cache held while a reply is written.

  • Trap"Fewer heads means a smaller model."

    BetterMostly 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?

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?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q055What is the KV cache, and why does it speed up generation?12 min
  2. Q057What is FlashAttention?11 min
  3. Q074What uses memory at inference time?11 min
  4. Q056Why is attention quadratic in sequence length?11 min
  5. Q059Sliding-window and sparse attention?Coming soon

Learn it in depth

Last updated on 26 November 2026

Choosing a model setup for a product that has to answer fast?

I design and build AI products and agents, including the system behind the chat. Book a short call and tell me what your app needs to do.