↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Layer 1 of 24 every third layer recorded Query heads KV 16 query heads · 1 key head · 1 value head · 128-D · scale 1/√128 · causal mask
Loading recorded attention…

What multi-query attention is

When a decoder generates text one token at a time, every layer keeps the keys and values of all earlier tokens, the KV cache, and each new token reads the whole cache in every layer. Shazeer (2019) pointed out that at this stage the time goes into loading those tensors from memory, not into arithmetic. Multi-query attention (MQA) keeps many query heads but gives all of them one shared key head and one shared value head, so the cache is as small as a single head's.

This page records it in SantaCoder (1.1B), a code model with 24 layers, of which every third is recorded, and 16 query heads of 128 dimensions that read 1 key head and 1 value head. The three prompts are code: Function arguments, Comprehension variable and JavaScript callback.

One key, one value, many queries

Each query head scores and mixes the same keys and values:

Compared with the MHA tab, only the head index on and is gone. The Query heads control in the toolbar draws all 16 query heads pointing at the one K and V. On the head card the Key and Value columns are marked “shared”: step through the query heads and they stay the same while the queries and the weight matrix change. Different heads still attend differently, because each asks its own question of the same keys. Clicking a K or V letter in the bank opens the projection of the one shared head, whichever query head is selected.

What it saves

Per token and layer, multi-head attention with these heads would cache numbers for keys and values. MQA caches , sixteen times fewer. The projection shrinks with it: SantaCoder's fused Q, K, V matrix produces numbers per token instead of . The query side is unchanged, , the model width.

At each decoding step a layer does little arithmetic, one new query against every cached key, but it has to read the whole cache from memory to do it, so the time per step follows the size of the cache. A smaller cache means each step reads less, and more sequences or longer contexts fit on one accelerator. Pope et al. found that on large PaLM models, with suitable partitioning across chips, the lower memory needs of multi-query attention allowed context lengths up to 32 times longer.

What it costs

Sharing one key and value head removes capacity: every head has to read from the same 128-dimensional summary of each token. Shazeer reported only minor quality degradation in his experiments; Ainslie et al. observed that MQA can lose quality and proposed grouped-query attention as the middle ground (the GQA tab). The training cost barely changes, since training processes all positions in parallel and is not bound by cache reads; the saving is at inference.

Where it came from

Shazeer introduced multi-query attention in 2019 as a fix for slow incremental decoding. PaLM (2022) used it at 540B parameters, and BigCode trained SantaCoder (2023), the model recorded here, with it. GQA (2023) generalised it to a few key and value heads; MLA (2024) compresses keys and values into a latent vector instead. The KV cache tab of the Efficiency Explainer shows the cache itself during prefill and decode.

Papers

  1. Fast Transformer Decoding: One Write-Head is All You Need Noam Shazeer · arXiv 2019 Introduces multi-query attention: keys and values shared across heads to cut the memory bandwidth of incremental decoding.
  2. SantaCoder: don't reach for the stars! Loubna Ben Allal et al. · arXiv 2023 The 1.1B code model this page records, trained with multi-query attention.
  3. Efficiently Scaling Transformer Inference Reiner Pope et al. · MLSys 2023 How multi-query attention and partitioning across chips make long-context inference of very large models practical.
  4. PaLM: Scaling Language Modeling with Pathways Aakanksha Chowdhery et al. · JMLR 2023 A 540B model built with multi-query attention.
  5. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints Joshua Ainslie et al. · EMNLP 2023 Grouped-query attention, the middle ground between MQA and multi-head attention (the GQA tab).
  6. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 Multi-head attention, the baseline MQA changes.

See also