Multi-Query Attention
An attention variant that shares one key-value head across all query heads to minimize key-value cache memory.
Multi-query attention (MQA) is an attention variant that keeps multiple query heads but shares one key-value pair across them, shrinking the key-value cache for long-context inference.
At a glance
Released
November 2019
Authors
Noam Shazeer
Optimizes
- Kv Cache
- Memory Bandwidth
What It Is
Multi-query attention (MQA) is an attention variant derived from multi-head attention. Every query head still computes its own attention scores, but all heads read from the same key head and value head.Why It Exists
MQA targets key-value cache size and memory bandwidth during autoregressive decoding. Sharing one key-value pair across all query heads minimizes the number of tensors stored per token.How It Works
Linear projections still produce multiple query heads, but the key and value projections collapse to a single head each. Every query head attends against the shared key and value tensors, and the outputs are then concatenated and projected as usual.QueriesqqqqValuesKeysVkV to kk to qk to qk to qk to q
Math Or Compute Schema
The formulas below contrast how multi-head attention pairs every query head with its own key-value heads versus multi-query attention, which routes all query heads through one shared key-value pair.Compared To Nearby Modules
Compared with multi-head attention, MQA stores one key-value pair instead of one pair per head. Compared with grouped-query attention, MQA compresses further by sharing a single key-value head across all query heads rather than several groups.| Comparison dimension | Multi-Query Attention | Multi-Head Attention | Grouped-Query Attention |
|---|---|---|---|
| Key-value head count | 1 key head and 1 value head | H key heads and H value heads | G key heads and G value heads |
| Query-head flexibility | H query heads share one key-value pair | H independent query and key-value head pairs | H distinct query heads grouped into G shared key-value pairs |
| Cache footprint per token | 2 tensors (single shared key-value cache) | 2H tensors (full multi-head attention cache) | 2G tensors (G keys plus G values) |