Inferensys

Glossary

Grouped Query Attention (GQA)

An attention mechanism that uses a smaller number of key-value head groups than query heads, interpolating between multi-head and multi-query attention to reduce the KV cache size while preserving quality.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
KV CACHE OPTIMIZATION

What is Grouped Query Attention (GQA)?

Grouped Query Attention is an interpolation between Multi-Head and Multi-Query Attention that balances inference speed and model quality by sharing key-value heads across multiple query heads.

Grouped Query Attention (GQA) is an attention mechanism that partitions query heads into groups, with each group sharing a single key-value head. This reduces the size of the KV cache during autoregressive decoding by a factor equal to the number of query heads per group, directly lowering memory bandwidth requirements and accelerating inference while preserving more representational capacity than Multi-Query Attention (MQA).

GQA addresses the memory bottleneck in large language model serving by trading off between the quality of standard Multi-Head Attention (MHA) and the speed of MQA. By using an intermediate number of key-value heads—typically 4 to 8—GQA maintains near-MHA quality on benchmarks while achieving inference throughput close to MQA, making it a standard choice in production models like Llama 2 and Mistral.

KV CACHE EFFICIENCY

MHA vs MQA vs GQA: Attention Mechanism Comparison

Structural comparison of Multi-Head, Multi-Query, and Grouped Query Attention mechanisms regarding key-value head configuration and memory footprint during autoregressive decoding.

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

Number of KV heads

Equals number of query heads (H)

1 shared KV head

G groups (1 < G < H)

KV cache size relative to MHA

1x (baseline)

1/H (maximum reduction)

G/H (interpolated reduction)

Query head grouping

One-to-one with KV heads

All query heads share one KV head

Query heads partitioned into G groups

Quality degradation vs MHA

None (reference)

Noticeable on large models

Negligible to minimal

Training overhead vs MHA

None (reference)

Minimal

Minimal; often converted from MHA checkpoint

Uptraining required

Used in

Original Transformer, BERT

PaLM, Falcon

LLaMA 2, LLaMA 3, Mistral

KV CACHE OPTIMIZATION

Key Properties of Grouped Query Attention

Grouped Query Attention (GQA) is a critical architectural innovation that balances the quality of Multi-Head Attention with the memory efficiency of Multi-Query Attention, making it essential for scaling large language model inference.

01

The Interpolation Spectrum

GQA occupies a precise middle ground between Multi-Head Attention (MHA) and Multi-Query Attention (MQA). In MHA, the number of key-value heads equals the number of query heads (H=K=V). In MQA, all query heads share a single key-value head (K=V=1). GQA generalizes this by dividing query heads into G groups, where each group shares one key-value head. This creates a tunable knob: setting G=1 yields MQA, while G=H yields MHA. Typical configurations use G=4 or G=8, reducing the KV cache size by a factor of H/G compared to MHA while preserving most of the quality.

02

KV Cache Memory Reduction

The primary motivation for GQA is reducing the memory footprint of the KV cache during autoregressive decoding. For a model with H query heads, the KV cache stores 2 tensors (keys and values) per head per token. GQA with G groups reduces this to 2G tensors per token. This yields a memory reduction factor of H/G. For a 70B parameter model with 64 heads and G=8, the KV cache shrinks by 8x. This directly translates to larger batch sizes, longer context windows, and lower per-user serving costs without requiring architectural retraining from scratch.

8x
Typical KV Cache Reduction
G=4-8
Common Group Count
03

Uptraining from Multi-Head Checkpoints

GQA models are not trained from scratch. The standard procedure is uptraining: starting from a pre-trained MHA checkpoint and converting it to GQA. This is done by mean-pooling the key and value projection weights of the heads within each group. The model then undergoes a small fraction (typically 5-10%) of the original training steps to adapt. This uptraining is remarkably sample-efficient; the model quickly recovers quality because the underlying representations are already well-formed. This allows teams to retrofit existing large models with GQA for inference savings without the cost of full pre-training.

04

Quality Parity with Multi-Head Attention

Empirical evaluations demonstrate that GQA achieves near-identical performance to MHA across standard benchmarks while approaching the speed of MQA. Key findings include:

  • Perplexity: GQA with G=8 matches MHA perplexity on language modeling tasks.
  • Downstream tasks: Negligible degradation on MMLU, HellaSwag, and reading comprehension.
  • Long-context: GQA maintains quality better than MQA on sequences exceeding 10k tokens. The slight grouping constraint acts as a mild regularizer, and the shared key-value heads learn more robust representations that generalize well across the query heads within their group.
05

Adoption in Production Models

GQA has become the de facto standard for large-scale production language models due to its inference efficiency. Notable implementations include:

  • Llama 2 70B: Uses GQA with 8 key-value heads (G=8) across 64 query heads.
  • Llama 3: Continues the GQA architecture across all model sizes.
  • Mistral: Employs GQA with sliding window attention for efficient long-context processing.
  • Gemma: Google's open models use GQA to balance quality and serving costs. The widespread adoption validates GQA as the optimal trade-off point for models deployed at scale.
06

Mechanistic Implications for Attention Circuits

From a mechanistic interpretability perspective, GQA constrains the OV circuit formation. In MHA, each head can develop a specialized copy operation. With GQA, the shared value projection forces heads within a group to read from the same source representation. This encourages complementary query specialization: heads in a group learn distinct attention patterns (QK circuits) while operating on identical value information. Analysis shows this leads to more interpretable head clusters, where a group collectively represents a concept while individual heads route information to different token positions based on context.

GROUPED QUERY ATTENTION

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the GQA mechanism, its trade-offs, and its role in optimizing large language model inference.

Grouped Query Attention (GQA) is an attention mechanism that divides query heads into groups, with each group sharing a single key-value head, interpolating between Multi-Head Attention (MHA) and Multi-Query Attention (MQA) to reduce the KV cache memory footprint while preserving model quality.

In standard MHA, each of H query heads has its own dedicated key and value head, requiring 2 * H key-value tensors to be cached per token during autoregressive generation. In MQA, all query heads share a single key-value head, slashing the cache size by a factor of H but often degrading quality. GQA strikes a balance: query heads are partitioned into G groups, and each group shares one key-value head. This reduces the KV cache size by a factor of H/G compared to MHA. For example, with H=32 query heads and G=4 groups, only 8 key-value heads are stored instead of 32, achieving a 4x memory reduction while retaining most of the quality benefits of MHA. The shared key-value heads are typically derived by mean-pooling the original MHA key-value heads during uptraining from an MHA checkpoint, allowing a smooth quality-speed trade-off.

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.