Inferensys

Glossary

Multi-Query Attention (MQA)

Multi-Query Attention (MQA) is a variant of multi-head attention where all attention heads share a single set of Key and Value projection weights, dramatically reducing the size of the KV cache and memory bandwidth requirements during inference.
Developer testing AI inference on mobile phone in hand, laptop with optimization code visible, casual tech review moment.
INFERENCE OPTIMIZATION

What is Multi-Query Attention (MQA)?

Multi-Query Attention (MQA) is a variant of the multi-head attention mechanism where all attention heads share a single set of Key and Value projection weights, while maintaining separate Query projections.

Multi-Query Attention (MQA) is an architectural modification to the standard Transformer that eliminates the per-head Key and Value weight matrices. Instead of learning h distinct sets of Key and Value projections, all Query heads attend to the same shared Key and Value tensors. This design drastically reduces the size of the KV cache during autoregressive decoding, slashing memory bandwidth requirements and accelerating inference latency with minimal degradation in model quality.

The primary trade-off in MQA is between memory efficiency and representational capacity. While sharing Keys and Values removes the model's ability to attend to information in h different representation subspaces, the memory savings are substantial, often cutting the KV cache size by a factor equal to the number of heads. This makes MQA critical for deploying large language models in memory-constrained environments. Grouped-Query Attention (GQA) later emerged as an interpolation between MQA and standard Multi-Head Attention to recover some of this lost capacity.

ARCHITECTURE

Key Characteristics of MQA

Multi-Query Attention (MQA) is defined by a radical simplification of the standard multi-head attention mechanism, specifically targeting the memory bandwidth bottleneck during autoregressive decoding.

01

Shared Key-Value Projections

The defining architectural change in MQA is that all attention heads share a single set of Key and Value projection weights. Unlike standard Multi-Head Attention where each head has distinct K and V linear layers, MQA collapses them into one. The Query projections remain independent per head, allowing the model to still attend to different representation subspaces while drastically reducing the number of parameters and the size of the KV cache.

02

KV Cache Memory Reduction

The primary motivation for MQA is to slash the memory footprint of the Key-Value (KV) cache during inference. In a standard model with h heads, the cache stores h sets of keys and values per token. MQA stores only 1 set.

  • Memory Savings: Reduces KV cache size by a factor of h (e.g., 96x reduction for a 96-head model).
  • Bottleneck Relief: Shifts the inference bottleneck from memory bandwidth to compute, enabling much faster token generation.
96x
Max Cache Reduction
03

Inference Speedup vs. Quality Trade-off

MQA offers a significant speedup in autoregressive decoding by reducing the data that must be loaded from GPU memory at each step. However, this aggressive sharing can lead to a minor degradation in model quality compared to standard Multi-Head Attention.

  • Throughput: Enables much higher batch sizes during inference.
  • Quality: The capacity constraint from shared K and V can slightly reduce the model's ability to capture fine-grained syntactic or semantic nuances.
04

Relationship to Grouped-Query Attention (GQA)

MQA represents one extreme of the attention spectrum, while Grouped-Query Attention (GQA) is a direct interpolation between MQA and standard Multi-Head Attention. GQA partitions Query heads into G groups, with each group sharing a single Key and Value head.

  • MQA: G = 1 (all heads share one K/V).
  • GQA: 1 < G < h (balances speed and quality).
  • MHA: G = h (no sharing, maximum quality).

GQA was adopted by models like Llama 2 to recover the quality lost by MQA while retaining most of the speed benefits.

05

Adoption in Production Models

MQA was popularized by Google's PaLM model and the original Chinchilla paper, which demonstrated its effectiveness at scale. It is a critical technique for serving large language models in latency-sensitive applications.

  • PaLM: Used MQA to achieve state-of-the-art few-shot results with efficient serving.
  • Chinchilla: Showed MQA had minimal performance degradation compared to MHA when trained on an optimal compute budget.
  • Legacy: While foundational, many newer open-source models have migrated to GQA for a better quality-speed Pareto frontier.
06

Training and Kernel Implications

MQA requires custom attention kernels for maximum efficiency. Standard FlashAttention implementations must be adapted to handle the asymmetric shape where Query heads outnumber Key/Value heads.

  • Training: MQA trains faster than MHA due to reduced FLOPs in the attention calculation.
  • Kernel Design: The 'multi-query' pattern requires tiling strategies that broadcast the single K and V tensors across all Query heads to fully utilize GPU SRAM and avoid redundant HBM reads.
KV CACHE EFFICIENCY

MQA vs. MHA vs. GQA: Attention Variant Comparison

Comparison of memory bandwidth requirements and quality trade-offs between standard Multi-Head Attention, Multi-Query Attention, and Grouped-Query Attention during autoregressive decoding.

FeatureMulti-Head Attention (MHA)Multi-Query Attention (MQA)Grouped-Query Attention (GQA)

Key/Value Head Count

H (one per Query head)

1 (shared across all heads)

G (1 < G < H)

KV Cache Size (Relative)

100% (baseline)

1/H (e.g., 3.1% for H=32)

G/H (e.g., 12.5% for G=4, H=32)

Memory Bandwidth Bottleneck

High (bound by KV cache reads)

Low (significantly reduced reads)

Medium (tunable reduction)

Inference Speedup vs. MHA

1x (baseline)

3-5x faster decoding

1.5-3x faster decoding

Model Quality Impact

Highest (full expressivity)

Minor degradation

Negligible degradation (matches MHA)

Training Throughput

Baseline

Faster (reduced KV projection compute)

Slightly faster than MHA

Uptraining Compatibility

Adopted By

Original Transformer, BERT

PaLM, Falcon

Llama 2 70B, Llama 3, Mistral

MULTI-QUERY ATTENTION

Frequently Asked Questions

Clear, technical answers to the most common questions about Multi-Query Attention (MQA), its mechanisms, and its role in optimizing large language model inference.

Multi-Query Attention (MQA) is a variant of the multi-head attention mechanism where all attention heads share a single set of Key and Value projection weights, while retaining distinct Query projection weights for each head. In standard multi-head attention, each head has its own independent linear projections for Q, K, and V. MQA fundamentally alters this by collapsing the K and V projections into one shared pair. During computation, the distinct Query heads all interact with this single shared Key-Value pair. This architectural change directly targets the memory bottleneck during autoregressive inference. Because the KV cache stores only one set of Key and Value vectors per token instead of one per head, the cache size is reduced by a factor equal to the number of heads. This dramatically lowers memory bandwidth requirements, enabling faster token generation and larger batch sizes on memory-constrained hardware.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.