Inferensys

Glossary

Multi-Query Attention (MQA)

Multi-Query Attention (MQA) is a transformer attention variant where multiple query heads share a single key head and a single value head, drastically reducing the memory footprint and I/O cost of the KV cache.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
KV CACHE MANAGEMENT

What is Multi-Query Attention (MQA)?

A transformer attention variant designed to drastically reduce the memory and bandwidth cost of the key-value cache during inference.

Multi-Query Attention (MQA) is a transformer attention mechanism where multiple query heads share a single, common key head and a single, common value head. This architectural change, introduced to optimize inference, reduces the size of the KV cache by a factor equal to the number of attention heads, significantly lowering memory footprint and the I/O cost of reading cached states during autoregressive decoding. The primary trade-off is a potential reduction in model capacity compared to standard Multi-Head Attention (MHA).

MQA achieves its efficiency gains by projecting the decoder hidden state into many query vectors but only one key and one value vector per layer. This contrasts with MHA, which projects separate keys and values for each head. The technique is particularly impactful for long-context and high-throughput serving, as the KV cache is often the memory-bound bottleneck. Grouped-Query Attention (GQA) is a popular generalization that provides a tunable middle ground between MQA's efficiency and MHA's expressiveness.

KV CACHE MANAGEMENT

Key Characteristics of Multi-Query Attention

Multi-Query Attention (MQA) is a transformer attention variant designed to drastically reduce the memory and I/O cost of the key-value (KV) cache. Its core innovation is having multiple query heads share a single key head and a single value head.

01

Shared Key and Value Heads

The defining architectural feature of MQA. Unlike standard Multi-Head Attention (MHA), where each of the N query heads has its own dedicated key and value head, MQA uses a single, shared key head and a single, shared value head for all query heads.

  • Mechanism: During the attention computation, the N distinct query vectors all attend to the same, single set of key and value vectors.
  • Impact: This reduces the number of key and value tensors that must be stored in the KV cache from 2 * N per layer to just 2 per layer (one key, one value).
02

Drastic KV Cache Memory Reduction

MQA's primary benefit is a linear reduction in the memory footprint of the KV cache, which is the dominant memory consumer during autoregressive decoding.

  • Quantitative Reduction: For a model with h attention heads, MQA reduces the size of the KV cache by a factor of approximately h. For a 32-head model, this is a ~32x reduction in the number of cached key/value vectors.
  • Consequence: This allows for either longer context windows within the same GPU memory budget or higher batch sizes to increase throughput, directly addressing inference cost and latency.
03

Reduced Memory Bandwidth Pressure

By caching fewer tensors, MQA significantly decreases the amount of data that must be read from GPU memory during each decoding step, moving the computation from a memory-bound regime towards a compute-bound one.

  • The Bottleneck: In standard MHA, loading the massive KV cache for attention is often the performance limiter.
  • MQA's Effect: With a smaller cache, the memory I/O volume per token is drastically lower. This reduces latency and increases the theoretical maximum tokens-per-second throughput, especially on memory-bandwidth-constrained hardware.
04

Trade-off: Potential Quality Degradation

The memory efficiency of MQA comes with a trade-off. Sharing keys/values across all queries can reduce the model's representational capacity and expressiveness.

  • The Risk: The shared key/value projection may act as an information bottleneck, potentially leading to a drop in output quality or factual accuracy compared to the full MHA baseline, particularly on complex reasoning tasks.
  • Empirical Observation: The degradation is often model and task-dependent. Some large models (e.g., certain versions of Falcon) use MQA effectively, while others require careful tuning or prefer the hybrid Grouped-Query Attention (GQA) approach.
05

Architectural Simplicity and Efficiency

MQA simplifies the attention computation graph and parameter count, leading to cleaner and faster kernel implementations.

  • Fewer Parameters: The model has fewer linear projection matrices for keys and values, slightly reducing the total parameter count.
  • Kernel Optimization: The simpler data access pattern (many queries to one key/value set) can be optimized into highly efficient GPU kernels. This contributes to lower latency beyond just the cache savings.
  • Decoding Step: The autoregressive step becomes: Attn = softmax(Q_all @ K_shared.T) @ V_shared, where Q_all is the concatenated query tensor.
06

Relationship to Grouped-Query Attention (GQA)

Grouped-Query Attention (GQA) is a generalization and intermediate point between MHA and MQA, created to mitigate MQA's quality trade-off.

  • How it Works: Instead of N query heads sharing one key/value head (MQA) or each having its own (MHA), GQA groups queries into G groups. Each group shares a single key head and value head.
  • The Spectrum:
    • G = N -> Multi-Head Attention (largest cache, highest quality).
    • 1 < G < N -> Grouped-Query Attention (balanced trade-off).
    • G = 1 -> Multi-Query Attention (smallest cache, highest efficiency).
  • Practical Use: GQA allows practitioners to tune the KV cache size versus model quality based on their specific deployment constraints.
KV CACHE ARCHITECTURE COMPARISON

MQA vs. Multi-Head Attention vs. Grouped-Query Attention

A technical comparison of three transformer attention variants, focusing on their KV cache memory footprint, computational characteristics, and typical use cases.

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

Core Architecture

Each query head has a unique, dedicated key head and value head.

All query heads share a single, common key head and a single, common value head.

Query heads are partitioned into G groups; each group shares a single key head and value head.

KV Cache Size per Layer

2 * Batch Size * Sequence Length * Num_Heads * Head_Dim

2 * Batch Size * Sequence Length * 1 * Head_Dim

2 * Batch Size * Sequence Length * Num_Groups * Head_Dim

Memory Reduction vs. MHA

0% (Baseline)

~90-95% (for models with 8-32 heads)

Tunable. ~50-90% (e.g., 8 groups for a 32-head model yields 75% reduction).

Primary Optimization Goal

Model quality and representational capacity.

Maximum memory and I/O bandwidth reduction for the KV cache.

A tunable trade-off between MQA's memory savings and MHA's model quality.

Inference Throughput Impact

Standard baseline. KV cache I/O can become a bottleneck at scale.

Highest potential throughput for memory-bound, long-context, or high-batch scenarios.

High throughput, often closer to MQA than to MHA, while preserving more quality.

Typical Model Quality

Highest, due to full parameterization for each attention head.

Can incur a measurable quality drop, especially on tasks requiring nuanced multi-head differentiation.

Minimal quality degradation when using a sufficient number of groups (e.g., 4-8).

Adoption Examples

Original Transformer, BERT, T5, early GPT models.

PaLM, StarCoder, Falcon, many inference-optimized models.

Llama 2 (70B), Llama 3, Gemma, Command R, modern general-purpose models.

Best For

Training and research where quality is the sole priority.

High-throughput production inference where latency/cost dominate and some quality loss is acceptable.

General-purpose models requiring a balanced cost-quality trade-off for production deployment.

IMPLEMENTATION LANDSCAPE

Models and Frameworks Using MQA

Multi-Query Attention (MQA) has been adopted by several prominent open-source and proprietary models to achieve significant reductions in memory and I/O costs. This section details key implementations and the frameworks that support them.

KV CACHE MANAGEMENT

Frequently Asked Questions

Multi-Query Attention (MQA) is a key technique for optimizing transformer inference by drastically reducing the memory and bandwidth cost of the KV cache. These questions address its core mechanisms, trade-offs, and practical applications.

Multi-Query Attention (MQA) is a transformer attention variant where multiple query heads share a single, common key head and a single, common value head. In standard Multi-Head Attention (MHA), each of the N query heads has its own corresponding key and value head, resulting in N unique KV pairs per layer. MQA collapses this to just one shared KV pair for all query heads. During the attention operation, the shared key and value tensors are broadcast and used by all query heads in parallel. This architectural change directly reduces the size of the KV cache by a factor of N, where N is the number of attention heads, leading to significant memory and I/O savings during the autoregressive decode phase.

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.