Grouped-Query Attention (GQA) is an attention mechanism that groups multiple query heads to share a single key head and a single value head. It is a hybrid approach positioned between Multi-Head Attention (MHA), which uses separate key and value projections per head, and Multi-Query Attention (MQA), where all query heads share a single key and value head. GQA's primary objective is to reduce the memory footprint of the key-value (KV) cache during autoregressive generation, a major bottleneck for long-sequence inference, while preserving much of the model quality associated with MHA.
Glossary
Grouped-Query Attention (GQA)

What is Grouped-Query Attention (GQA)?
Grouped-Query Attention (GQA) is a transformer attention mechanism designed to optimize the memory and computational efficiency of autoregressive inference, particularly for large language models.
The mechanism operates by partitioning the model's total query heads into G groups. Within each group, the queries share a single, jointly projected key head and value head. This grouping drastically shrinks the size of the KV cache compared to MHA, leading to significant reductions in memory bandwidth and latency during token generation. In practice, GQA offers a tunable trade-off: more groups (closer to MHA) yield higher quality but larger cache size, while fewer groups (closer to MQA) maximize memory efficiency, often with a minor quality penalty. It is a foundational technique for enabling performant inference in models like Llama 2 and Gemma.
Key Characteristics of GQA
Grouped-Query Attention (GQA) is an attention mechanism designed to balance the high-quality representation of Multi-Head Attention (MHA) with the memory efficiency of Multi-Query Attention (MQA). Its core innovation is grouping query heads to share key and value projections.
Hybrid Attention Design
GQA creates a middle ground between two established attention paradigms. It groups N query heads into G groups, where each group shares a single key head and a single value head. This contrasts with:
- Multi-Head Attention (MHA): Each of the N query heads has its own unique key and value head (N keys, N values).
- Multi-Query Attention (MQA): All N query heads share a single key head and a single value head (1 key, 1 value). GQA's design (G keys, G values, where 1 < G < N) allows it to retain much of MHA's expressive power while significantly reducing the memory footprint of the key-value (KV) cache during autoregressive generation.
KV Cache Memory Efficiency
The primary practical benefit of GQA is the dramatic reduction in memory required for the key-value cache during autoregressive inference (e.g., text generation).
- In a standard transformer, the KV cache stores past key and value vectors for all layers and heads to avoid recomputation.
- For a model with N heads, sequence length L, and hidden dimension d_k, the per-layer cache size is proportional to
2 * N * L * d_k. - With GQA using G groups, this becomes
2 * G * L * d_k. For example, moving from MHA (N=32) to GQA (G=8) reduces the KV cache memory by 75%, enabling longer context windows or batch sizes on the same hardware.
Performance-Quality Trade-off
GQA is engineered to deliver inference performance close to MQA while maintaining model quality close to MHA. Empirical results, such as those from the Llama 2 and Gemma model families, show:
- Inference Speed: GQA models achieve a significant latency reduction compared to equivalent MHA models, primarily due to reduced memory bandwidth pressure from the smaller KV cache.
- Quality Retention: On standard benchmarks, GQA models (e.g., Llama 2 70B with G=8) perform nearly on par with their hypothetical MHA counterparts, with minimal degradation in downstream task accuracy.
- The optimal group size G is a hyperparameter, often set to 8 for large models (e.g., 70B parameters), providing a favorable Pareto frontier.
Conversion from MHA Models
A key advantage of GQA is that it can be applied to existing pre-trained MHA models through upcycling. This process involves:
- Averaging Weights: The key and value projection matrices (
W_K,W_V) from the N heads in the original MHA model are averaged within each designated group to create the G new projection matrices for the GQA model. - Minimal Fine-Tuning: The converted model then undergoes a short period of continued pre-training or instruction tuning (often on just 1-5% of the original data) to recover any performance loss from the averaging operation. This allows organizations to efficiently retrofit older, larger MHA models for more cost-effective deployment without full retraining.
Implementation in Modern LLMs
GQA has been adopted as a standard efficiency feature in several prominent open and closed-source model families, validating its utility:
- Meta's Llama 2 & 3: The 34B and 70B parameter versions use GQA (with 8 groups).
- Google's Gemma: The 7B and 2B models implement GQA.
- Google's Gemini: The family of models utilizes a form of GQA.
- Mistral AI's Models: Several models from Mistral leverage GQA for efficiency. This widespread adoption highlights GQA's role as a critical component in the toolkit for building large-scale, deployable language models where inference cost and memory are primary constraints.
Relation to Other Efficient Attention
GQA exists within a spectrum of techniques designed to optimize the transformer's attention mechanism:
- Multi-Query Attention (MQA): The extreme efficiency point (G=1). Can lead to more noticeable quality degradation, especially in larger models.
- Multi-Head Attention (MHA): The quality baseline (G=N). Maximum expressivity but high memory cost.
- FlashAttention: An orthogonal, I/O-aware algorithm that speeds up attention computation. GQA's reduced KV cache size makes it even more beneficial when paired with FlashAttention.
- Sliding Window & Sparse Attention: These reduce the sequence length factor (L) in complexity. GQA reduces the head count factor (N/G). The techniques can be combined for compounded efficiency gains on long sequences.
GQA vs. MHA vs. MQA: Attention Mechanism Comparison
A technical comparison of three key attention mechanisms used in transformer models, focusing on their architectural trade-offs for memory efficiency and inference speed.
| Feature | Multi-Head Attention (MHA) | Multi-Query Attention (MQA) | Grouped-Query Attention (GQA) |
|---|---|---|---|
Core Architecture | Each query head has its own unique key and value head. | All query heads share a single key head and a single value head. | Query heads are grouped; each group shares a single key and value head. |
Key-Value (KV) Cache Size per Layer | Large: | Minimal: | Intermediate: |
Inference Memory Footprint | High | Very Low | Moderate to Low |
Inference Speed (Autoregressive) | Standard (Baseline) | Fastest | Fast (close to MQA) |
Model Quality / Expressiveness | Highest | Can degrade on complex tasks | Nearly matches MHA |
Primary Use Case | Training & high-quality inference where memory is not a bottleneck. | Extreme memory-constrained inference (e.g., very long contexts). | Production inference, balancing quality and efficiency (e.g., Llama 2 70B, Llama 3). |
Convertible from MHA Pre-trained Model? | N/A (Original) | Yes, but may require retraining/fine-tuning to recover quality. | Yes, often with minimal fine-tuning (groups can be formed by averaging existing heads). |
Number of Projection Matrices (per layer) | 3: | 3: | 3: |
Models and Frameworks Using GQA
Grouped-Query Attention (GQA) has been adopted by several leading open-source and proprietary models to balance quality and efficiency. This section details key implementations and their architectural specifics.
Frequently Asked Questions
Grouped-Query Attention (GQA) is a hybrid attention mechanism designed to balance the quality of Multi-Head Attention (MHA) with the memory efficiency of Multi-Query Attention (MQA). These questions address its core mechanics, trade-offs, and practical applications in efficient model architectures.
Grouped-Query Attention (GQA) is an attention mechanism that groups multiple query heads to share a single key head and a single value head. It operates by partitioning the standard Multi-Head Attention (MHA)'s N query heads into G groups. Within each group, the query heads share a single, common key head and value head. This reduces the total number of unique key-value heads from N (as in MHA) to G (where 1 < G < N), striking a balance between model expressiveness and the memory footprint of the key-value (KV) cache during autoregressive inference.
Mechanism: For a transformer layer with N=8 total attention heads, a GQA configuration with G=2 groups would create two unique key-value head pairs. Queries from heads 1-4 would attend to the first KV pair, and queries from heads 5-8 would attend to the second KV pair. This grouping is implemented via a linear projection that maps the N query vectors down to G key and value vectors before computing the attention scores.
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
Grouped-Query Attention (GQA) is a key component in the design of efficient transformer architectures. The following terms are fundamental to understanding the broader ecosystem of techniques for optimizing neural networks for performance and resource constraints.
Multi-Query Attention (MQA)
An attention mechanism where all query heads share a single key head and a single value head. This is the direct predecessor to GQA and represents the extreme of memory efficiency. It drastically reduces the size of the key-value (KV) cache during autoregressive inference, which is the primary bottleneck for long-sequence generation. However, this sharing can lead to a degradation in model quality compared to standard Multi-Head Attention (MHA).
- Primary Benefit: Minimal KV cache memory footprint.
- Trade-off: Potential quality loss due to excessive parameter sharing.
- Use Case: Found in models like PaLM where inference speed and memory were paramount.
Multi-Head Attention (MHA)
The standard attention mechanism in the original Transformer architecture. In MHA, each attention head maintains its own independent set of learned projection matrices for queries, keys, and values. This allows the model to jointly attend to information from different representation subspaces at different positions.
- Primary Benefit: High model capacity and representation power.
- Trade-off: High memory cost for the KV cache (
n_heads * sequence_length * head_dim). - Baseline: Serves as the quality benchmark against which efficient variants like MQA and GQA are measured.
Key-Value (KV) Cache
A critical optimization for autoregressive inference in decoder-only transformers (e.g., LLMs). To generate token n+1, the model reuses the key and value states computed for all previous tokens 1...n, storing them in a cache. This avoids recomputing these states for every new token, turning the complexity of a generation step from O(n^2) to O(n).
- Memory Bottleneck: The size of this cache scales with
batch_size * n_layers * n_kv_heads * sequence_length * head_dim. - GQA Impact: By reducing
n_kv_heads(e.g., from 32 to 8), GQA directly shrinks the KV cache size by a factor of 4, enabling longer context windows or larger batch sizes on the same hardware.
FlashAttention
An I/O-aware, exact attention algorithm that optimizes memory access patterns. It reduces the number of reads/writes between high-bandwidth memory (HBM) and fast on-chip SRAM by recomputing attention scores on-the-fly (a technique known as recomputation).
- Synergy with GQA: While GQA reduces the quantity of data in the KV cache, FlashAttention optimizes the speed and memory efficiency of the attention computation itself. Using both techniques together yields compounded performance gains.
- Key Innovation: Achieves significant speedup and reduces memory usage from
O(n^2)toO(n)for storing the attention matrix, without approximation.
Mixture of Experts (MoE)
A neural network architecture designed for conditional computation. An MoE layer consists of many sub-networks ("experts"), and for each input token, a router network activates only a small subset (e.g., 2 out of 64). This allows the model to have a very large parameter count while keeping the computational cost per token relatively low.
- Architectural Complement: GQA and MoE address efficiency at different levels. GQA optimizes the attention mechanism's memory footprint, while MoE optimizes the feed-forward network's computational cost. Modern efficient models like Gemini and Mixtral often combine both techniques.
- Target: Enables massive, sparse models that are impractical with dense architectures.
Sliding Window Attention
A sparse attention pattern used in efficient transformers where each token only attends to a fixed-size window of preceding tokens (e.g., the last 4096 tokens). This reduces the attention complexity from quadratic O(n^2) to linear O(n * w) where w is the window size.
- Context vs. Cache: While GQA optimizes the per-head cost of the KV cache, sliding window attention optimizes the sequence length factor. It is based on the observation that long-range dependencies often rely primarily on recent context.
- Combined Use: Models like Llama 2 and Mistral use sliding window attention to handle very long sequences efficiently, while GQA-type mechanisms manage the per-token memory cost within that window.

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