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 modification drastically reduces the size of the key-value (KV) cache that must be stored in memory during autoregressive generation, a primary bottleneck for long-sequence inference. By sharing keys and values across heads, MQA achieves significant memory savings with a minimal impact on model quality for many tasks, making it a cornerstone technique for deploying large language models in resource-constrained environments.
Glossary
Multi-Query Attention (MQA)

What is Multi-Query Attention (MQA)?
A transformer attention mechanism optimized for inference memory efficiency.
The standard Multi-Head Attention (MHA) mechanism uses separate, parallel key, value, and query projections for each attention head, leading to a KV cache size that scales linearly with the number of heads and sequence length. MQA breaks this coupling, decoupling the number of query heads from the key/value heads. This design directly targets the inference memory wall, allowing for larger batch sizes or longer context windows on the same hardware. It is a key enabler for the efficiency of models like Falcon and is often compared to its successor, Grouped-Query Attention (GQA), which offers a tunable trade-off between efficiency and quality.
Key Characteristics of MQA
Multi-Query Attention (MQA) is an attention mechanism designed to drastically reduce the memory footprint of the key-value cache during autoregressive inference, a critical bottleneck for deploying large models.
Core Mechanism: Shared Key-Value Heads
In standard Multi-Head Attention (MHA), each of the N query heads has its own dedicated key and value head, resulting in N distinct key and value projections. Multi-Query Attention (MQA) changes this by having all N query heads share a single key head and a single value head. This structural change is the source of its efficiency gains, particularly during the autoregressive decoding phase where the key-value cache must be stored in memory for all previous tokens in the sequence.
Primary Benefit: Reduced KV Cache Memory
The most significant advantage of MQA is the dramatic reduction in the size of the key-value (KV) cache. For a model with h attention heads, d_k key dimension, and a sequence length L:
- MHA KV Cache Size:
2 * L * h * d_k - MQA KV Cache Size:
2 * L * 1 * d_kThis represents a reduction by a factor ofh(e.g., 32x for a model with 32 heads). This is critical for long-context inference and high-throughput serving, where the KV cache can become the dominant memory consumer, often limiting batch sizes or sequence lengths.
Trade-off: Potential Quality Degradation
The efficiency of MQA comes with a trade-off. By sharing a single key/value representation across all query heads, the model loses the representational capacity and specialization that individual key/value heads provide in MHA. This can lead to a measurable drop in model quality or perplexity on some tasks compared to a full MHA baseline. The shared representation acts as a bottleneck, forcing all queries to attend to a common, compressed set of features. This trade-off makes MQA most suitable for inference-constrained scenarios where the memory savings outweigh a modest performance cost.
Inference Speed & Throughput Gains
Beyond memory savings, MQA accelerates inference. The smaller KV cache reduces memory bandwidth pressure when reading cached states for each new token generation. This leads to:
- Lower latency per generated token.
- Ability to support larger batch sizes within the same memory budget, increasing overall tokens/second throughput.
- More efficient use of GPU tensor cores as memory operations become less of a bottleneck. For large-scale deployment, these throughput gains directly translate to lower serving costs and improved user experience.
Architectural Context & Evolution
MQA exists on a spectrum of efficient attention variants:
- Multi-Head Attention (MHA): Full capacity, high memory cost.
- Multi-Query Attention (MQA): Minimal memory, shared KV heads.
- Grouped-Query Attention (GQA): A hybrid where
Nquery heads are divided intoGgroups, each sharing a key/value head. GQA (e.g., with 8 groups) often recovers most of MHA's quality while retaining significant memory savings over MHA, making it a popular compromise. MQA is frequently used in models where extreme inference efficiency is paramount, such as those deployed on edge devices or for real-time applications.
Practical Implementation & Use Cases
MQA is implemented by modifying the projection layers in a transformer block. Instead of h separate linear projections for keys and values, only one is used. Its outputs are then broadcast to all attention heads. Key use cases include:
- On-device large language models where DRAM is severely limited.
- High-throughput inference servers for chatbots and code generation.
- Long-context processing (e.g., document summarization) where the KV cache for a 128K context would be prohibitive with MHA. Models like Falcon and certain configurations of LLaMA 2 have employed MQA to achieve practical deployment scales.
MQA vs. Multi-Head vs. Grouped-Query Attention
A technical comparison of three key attention mechanisms, focusing on their architectural differences, memory footprint, and suitability for efficient inference, particularly in autoregressive decoding.
| Feature / Metric | Multi-Head Attention (MHA) | Multi-Query Attention (MQA) | Grouped-Query Attention (GQA) |
|---|---|---|---|
Core Architecture | N independent query, key, and value heads. | N query heads share a single key head and a single value head. | N query heads are grouped; each group shares a single key and value head. |
Key-Value Cache Size per Layer | Large (2 * N * d_head * seq_len) | Minimal (2 * 1 * d_head * seq_len) | Intermediate (2 * G * d_head * seq_len), where G = number of groups. |
Memory Reduction Factor (vs. MHA) | 1x (baseline) | ~Nx reduction | ~N/Gx reduction |
Inference Speed (Autoregressive) | Standard | Faster (reduced memory bandwidth) | Balanced (faster than MHA, often matches MQA) |
Model Quality / Flexibility | Highest (full independent heads) | Potential quality degradation for complex tasks | Approaches MHA quality with fewer groups (e.g., G=8) |
Primary Use Case | General-purpose training & inference | Memory-bound, high-throughput inference (e.g., LLM serving) | Efficient inference where MQA quality loss is unacceptable |
Adoption Examples | Original Transformer, BERT, GPT-2/3 | T5, some inference-optimized versions of large models | Llama 2 (70B), Llama 3, Gemini |
Training Compatibility | Native | Can degrade training stability; often applied post-training | Can be trained from scratch or converted from MHA checkpoints |
Models and Frameworks Using MQA
Multi-Query Attention (MQA) is a key efficiency feature in several prominent open-source and proprietary large language models, primarily to reduce the memory footprint of the key-value cache during autoregressive generation.
Frequently Asked Questions
Multi-Query Attention (MQA) is a critical optimization for transformer inference, particularly for deploying large language models on resource-constrained hardware. These questions address its core mechanism, trade-offs, and practical applications.
Multi-Query Attention (MQA) is an optimized transformer attention mechanism where multiple query heads share a single, common key head and a single, common value head. This architectural modification dramatically reduces the memory required to cache past key-value states during autoregressive inference, where a model generates tokens one at a time. In standard Multi-Head Attention (MHA), each of the n_heads query heads has its own corresponding key and value head, leading to a large, growing cache. MQA collapses this to one key and one value projection, which are then broadcast to and used by all query heads. The primary goal is not to improve model quality but to achieve a favorable trade-off, significantly lowering the memory bandwidth and storage footprint of the key-value (KV) cache—often by a factor of 8x or more—with a minimal impact on downstream task performance. It is a cornerstone technique for enabling the efficient deployment of large models in production and on edge devices.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Multi-Query Attention is a key component in a broader ecosystem of techniques designed to optimize transformer models for efficient inference. These related concepts address the trade-offs between computational cost, memory footprint, and model quality.
Grouped-Query Attention (GQA)
A hybrid attention mechanism that groups multiple query heads to share a single key head and a single value head. It is the direct architectural predecessor and successor to MQA.
- Key Innovation: Strikes a balance between the high quality of standard Multi-Head Attention (MHA) and the memory efficiency of Multi-Query Attention (MQA).
- Mechanism: For example, an 8-query-head model might use 4 groups (G=4), where each group of 2 query heads shares one key/value head.
- Trade-off: Reduces the size of the KV cache compared to MHA, but not as drastically as MQA. It often recovers most of the quality drop observed in pure MQA, making it a preferred choice in models like Llama 2 and Gemma.
Key-Value (KV) Cache
The critical memory bottleneck that MQA and GQA are designed to optimize. During autoregressive generation (token-by-token), the Key and Value tensors for all previous tokens in the sequence are stored to avoid recomputation.
- Memory Cost: In standard MHA, the cache size scales with:
batch_size * sequence_length * num_layers * num_heads * head_dim * 2(for K and V). - MQA/GQA Impact: By sharing key/value heads, MQA reduces this term from
num_headsto1, and GQA reduces it tonum_groups. This is a direct, linear reduction in memory for long sequences and large batches. - System Effect: A smaller KV cache enables longer context windows, larger batch sizes, and higher throughput on memory-constrained hardware.
FlashAttention
An I/O-aware, exact attention algorithm that optimizes the computation of attention, which works in tandem with MQA/GQA to accelerate inference.
- Core Problem: Standard attention implementations make frequent reads/writes between fast GPU SRAM and slow High Bandwidth Memory (HBM), creating a bottleneck.
- Solution: FlashAttention recomputes attention scores on-the-fly within SRAM, avoiding the materialization of the large
N x Nattention matrix in HBM. - Synergy with MQA: While MQA reduces the size of the KV cache, FlashAttention optimizes the speed of the attention operation itself. Using both techniques together yields the highest gains in end-to-end inference latency.
Multi-Head Attention (MHA)
The standard attention mechanism used in the original Transformer architecture, which MQA modifies for efficiency.
- Mechanism: Each attention "head" maintains its own independent set of Query (Q), Key (K), and Value (V) projection matrices. This allows the model to jointly attend to information from different representation subspaces.
- Computational Cost: The forward pass computation for MHA and MQA is similar. The primary difference is in the autoregressive inference memory footprint due to the KV cache.
- Quality Benchmark: MHA is the quality baseline. MQA and GQA are evaluated based on how closely they can approximate MHA's performance while using significantly less memory.
Sliding Window Attention
A complementary efficiency technique that reduces the computational complexity of attention, whereas MQA reduces its memory footprint.
- Principle: Each token only attends to a fixed-size window of preceding tokens (e.g., 4096), rather than the entire sequence history.
- Complexity: Reduces attention complexity from
O(n²)toO(n * window_size), enabling processing of extremely long sequences. - Combined Use: Models like Mistral 7B and Llama 3 often employ both Sliding Window Attention (to handle long contexts computationally) and GQA (to manage the memory of the KV cache for that window).
Mixture of Experts (MoE)
A different architectural paradigm for efficiency that scales model capacity without a proportional increase in computation per token.
- Contrast with MQA: While MQA is an attention-layer optimization, MoE is a model-wide architecture. MoE uses a routing network to activate only a small subset of neural network "experts" for each input.
- Efficiency Goal: MQA targets memory-bound inference latency. MoE targets compute-bound training and inference costs, enabling models with hundreds of billions of parameters that are cheaper to run.
- System Design: Advanced models may incorporate both techniques: using MoE for massive parameter counts and MQA/GQA to ensure the active experts run with minimal memory overhead.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us