Attention Variants

Standard multi-head attention (MHA) uses independent heads, each with its own query, key, and value projections . This means the KV cache during autoregressive inference scales as per layer, which becomes a memory bottleneck for long sequences and large batch sizes. Several variants reduce this cost by sharing key-value projections across heads while keeping separate queries.

Multi-Query Attention (MQA) uses a single shared key-value pair across all heads:

where each head has its own query projection but all heads share the same and . This reduces the KV cache by a factor of and speeds up inference significantly, at the cost of slightly reduced model quality.

Grouped-Query Attention (GQA) interpolates between MHA and MQA by dividing the query heads into groups, each sharing one set of key-value projections:

where head belongs to group . Setting recovers MQA; setting recovers full MHA. GQA achieves quality close to MHA with memory and speed close to MQA, and is used in LLaMA 2/3, Mistral, and Gemma.

KV Cache and Inference Cost values (factor 2 for K and V, layers, heads, sequence length). For a 70B model with , this can exceed 10 GB per sequence. GQA with instead of reduces this by .

During autoregressive generation, each new token requires attending to all previous tokens. In MHA, the KV cache stores

Intuition

The key insight is that different query heads often need similar contextual information from keys and values—they don’t all need completely independent KV representations. By sharing keys and values, the model can maintain most of its representational power (via diverse queries) while dramatically reducing the memory footprint. This is especially important at inference time, where the KV cache is the dominant memory cost and the main bottleneck for serving long contexts or large batches.

Properties

Multi-Head Attention (MHA)

(Standard MHA) Each head computes independent projections , , with and , where typically . The outputs are concatenated and projected: .

(Parameter Count) MHA has parameters per layer (when ), plus for .

(Implementation) In practice, instead of computing separate matrix multiplications, a single large projection is computed and then split/reshaped into heads. This is more efficient on GPUs.

Multi-Query Attention (MQA)

(MQA) (Shazeer, 2019) All heads share a single key and value projection. Only the query projection is per-head. The KV cache per layer drops from to per token.

(Quality Trade-off) MQA typically incurs a small quality degradation (0.5–1% on benchmarks) compared to full MHA, but inference throughput can increase by – for memory-bound workloads.

Grouped-Query Attention (GQA)

(GQA) (Ainslie et al., 2023) The query heads are partitioned into groups of heads each. Each group shares one set of key-value projections. The KV cache per layer is per token—a factor reduction over MHA.

(Uptraining) Existing MHA models can be converted to GQA by mean-pooling the key and value projection matrices within each group, then continuing pre-training for a small fraction of the original compute. This is how LLaMA 2 70B was adapted to GQA.

Examples

KV Cache Size Comparison

For a model with , heads, , layers, and sequence length :