Multi-query attention

On this page 7

Multi-query attention (MQA) is a variant of the transformer attention mechanism in which many query heads share a single key projection and a single value projection, instead of each head having its own. In neural networks built on the transformer, it was introduced to cut the memory traffic of autoregressive decoding, which is the main bottleneck when large language models generate text one token at a time.

The idea was proposed in 2019 as a modification of standard multi-head attention and later adopted in several large models for production inference. Its central trade-off is simple: MQA makes the key-value cache dramatically smaller and decoding faster, at the cost of a small and sometimes noticeable loss in model quality and training stability.

How multi-query attention restructures the heads

In standard multi-head attention, each of the h heads has its own query, key and value projections. Multi-query attention keeps h separate query projections but replaces the h key and value projections with one shared key head and one shared value head. Every query head computes attention scores against the same keys and reads from the same values.

The output of each head still differs, because each head uses its own queries and therefore produces its own attention pattern over the shared keys. The heads are concatenated and passed through the usual output projection, so the layer has the same interface as multi-head attention and fits into an existing transformer block without other changes.

The table below shows how the projections differ for a layer with h heads and head dimension d.

ComponentMulti-head attentionMulti-query attention
Query projectionsh heads of size dh heads of size d
Key projectionsh heads of size d1 shared head of size d
Value projectionsh heads of size d1 shared head of size d
Cached keys and values per token2 × h × d values2 × d values
Attention patternsh distinct patternsh distinct patterns over shared keys
Projection layout in one attention layer

Why sharing keys and values speeds up inference

Autoregressive decoding is usually memory-bandwidth bound, not compute bound. To generate each new token, the model must read the cached keys and values of all previous tokens from accelerator memory. With multi-head attention, the size of this key-value cache grows with the number of heads, the number of layers, the sequence length and the batch size.

Multi-query attention shrinks the key-value cache by a factor equal to the number of heads, for example 32 times in a layer with 32 heads. Less data must be loaded per generated token, so decoding latency falls and the same memory can hold longer contexts or larger batches. The arithmetic cost of the attention computation changes far less than the memory cost.

The following estimate illustrates the effect for a hypothetical model; real figures depend on precision, architecture and implementation.

SettingMulti-head attentionMulti-query attention
Layers and heads32 layers, 32 heads32 layers, 32 query heads
Head dimension128128
Cache per token (16-bit)about 512 KBabout 16 KB
Cache for 8,000 tokensabout 4 GBabout 128 MB
Illustrative key-value cache size per sequence

The training speed benefit is much smaller. During training, all positions are processed in parallel and the workload is compute bound, so MQA mainly matters for serving cost and latency rather than for training throughput.

Multi-query attention sits at one end of a spectrum of key-value sharing schemes. Grouped-query attention generalizes it by letting groups of query heads share a key-value head, and multi-head latent attention compresses keys and values into a low-dimensional latent vector instead of sharing them.

VariantKey-value headsCache sizeTypical quality
Multi-head attentionh (one per query head)LargestReference quality
Grouped-query attentiong groups, 1 < g < hReduced by h / gClose to multi-head
Multi-query attention1Smallest of the threeSlight degradation
Multi-head latent attentionCompressed latentSmallClose to multi-head
Attention variants and their trade-offs

In practice, many recent large language models chose grouped-query attention with 4 to 8 key-value heads as a compromise: it captures most of the speed gain of MQA while staying closer to the quality of full multi-head attention. MQA remains attractive where memory budgets are extremely tight, such as long-context serving or deployment on smaller accelerators.

Training, conversion and implementation notes

A model can use multi-query attention from the start of pretraining, or an existing multi-head model can be converted. Conversion, sometimes called uptraining, typically averages the original key and value projections into one head and then continues training for a small fraction of the original compute, often a few percent, to recover quality.

  • Tensor parallelism: with a single key-value head, the shared head must be replicated across devices, which reduces part of the memory saving in large multi-device deployments.
  • Head count: because key and value parameters shrink, some designs enlarge the feedforward layers slightly to keep the total parameter count comparable.
  • Kernel support: optimized attention kernels and inference servers support MQA directly, but older code may assume equal numbers of query and key-value heads.
  • Compatibility: positional schemes such as rotary position embedding work with MQA unchanged, since they act on queries and keys per position.

Limitations and common misconceptions

The main limitation is reduced representational capacity. All heads must read from the same key and value space, which limits how differently they can specialize. Reported effects include a modest drop in benchmark quality and occasional training instability, which grouped-query attention largely avoids.

A common misconception is that MQA reduces the number of attention patterns to one. It does not: each query head still attends differently. Another misconception is that MQA makes the model smaller overall; the parameter saving is minor, and the real benefit lies in the inference-time cache and bandwidth.

Explained without jargon

When a language model writes text, it keeps notes about every earlier word. In the usual design, each of its many attention heads keeps its own copy of these notes. That takes much memory and time to read.

Multi-query attention lets all heads share one set of notes. Each head still asks its own questions, but they all look things up in the same place. Imagine a team that shares one reference binder instead of each member carrying a personal copy. The model answers faster and uses far less memory, with only a small loss in accuracy.

A typical use case

An engineering team serves a code-completion model to thousands of developers. With standard multi-head attention, each accelerator can hold the key-value caches of only about 16 concurrent sessions with 8,000-token contexts, and latency rises sharply at peak hours.

The team trains the next version with multi-query attention while keeping the rest of the architecture the same. The key-value cache per session shrinks by a factor of 32, so each accelerator now serves several times more concurrent sessions, and the time per generated token falls noticeably. Offline tests show a small drop in completion accuracy, which the team accepts because responsiveness matters more to users of an interactive editor.