Inferensys

Glossary

Grouped-Query Attention (GQA)

Grouped-Query Attention (GQA) is an attention mechanism that uses a single set of key-value heads for multiple query heads, balancing the speed of multi-query attention with the quality of multi-head attention.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
ATTENTION MECHANISM

What is Grouped-Query Attention (GQA)?

Grouped-Query Attention is a transformer attention variant that balances computational efficiency and model quality by sharing key-value heads among groups of query heads.

Grouped-Query Attention (GQA) is an attention mechanism that partitions query heads into groups, with each group sharing a single set of key and value heads, while maintaining separate query heads per group. This design interpolates between the speed of Multi-Query Attention (MQA)—which uses a single KV head for all queries—and the expressive power of standard Multi-Head Attention (MHA).

GQA significantly reduces the memory bandwidth overhead of loading the KV cache during autoregressive decoding, as fewer key-value tensors must be stored and fetched. This makes it a critical optimization for self-hosted large language model inference, enabling larger batch sizes and lower latency on on-premises GPU clusters without the quality degradation often observed with pure MQA.

MECHANISM

Key Characteristics of GQA

Grouped-Query Attention (GQA) is a hybrid attention mechanism that interpolates between the speed of Multi-Query Attention (MQA) and the quality of standard Multi-Head Attention (MHA). It achieves this by dividing query heads into groups that share a single key-value head.

01

The Interpolation Spectrum

GQA exists on a spectrum between two extremes:

  • MHA (G=H): Every query head has its own unique key-value head. Highest quality, highest memory bandwidth.
  • MQA (G=1): All query heads share a single key-value head. Fastest inference, potential quality degradation.
  • GQA (1 < G < H): Query heads are partitioned into G groups, with each group sharing one key-value head. This balances the trade-off.
02

KV Cache Memory Reduction

The primary motivation for GQA is reducing the size of the KV cache during autoregressive decoding. The memory footprint scales inversely with the number of groups:

  • MHA stores H key-value heads per layer.
  • MQA stores 1 key-value head per layer.
  • GQA stores G key-value heads per layer, reducing memory by a factor of H/G compared to MHA. This allows for larger batch sizes and longer context lengths under the same memory budget.
H/G
Memory Reduction Factor
03

Quality Parity with MHA

Empirical results from the original paper demonstrate that GQA achieves quality nearly indistinguishable from MHA while approaching the speed of MQA. Key findings include:

  • Using G=8 for a model with H=32 (4 query heads per KV head) performs comparably to the MHA baseline on summarization and translation tasks.
  • The performance gap between MQA and MHA widens with model size, but GQA closes this gap effectively.
  • The shared KV heads learn to generalize, acting as a form of regularization.
04

Uptraining from MHA Checkpoints

GQA can be efficiently bootstrapped from existing MHA models through a process called uptraining:

  • Mean Pooling: Initialize the single key-value head for a group by mean-pooling the key-value heads of all query heads in that group.
  • Continued Training: Fine-tune the converted model on a small fraction (typically <10%) of the original pre-training data.
  • This allows rapid conversion of existing MHA models to the GQA architecture without training from scratch, preserving the original knowledge.
05

Adoption in Major Models

GQA has become the standard attention mechanism for production-grade open-weight models due to its inference efficiency:

  • Llama 2 70B and Llama 3 use GQA with 8 key-value heads.
  • Mistral models leverage GQA alongside sliding window attention.
  • Gemma and other Google-derived architectures implement GQA.
  • The mechanism is a critical enabler for serving large models on commodity hardware with constrained memory bandwidth.
06

Computational Trade-off

While GQA reduces memory reads, the total FLOPs remain largely unchanged from MHA. The speedup comes from memory-bound operations:

  • During autoregressive generation, loading the KV cache from HBM is the bottleneck.
  • GQA reduces the bytes transferred per token, shifting the operation from memory-bound toward compute-bound.
  • The effective throughput gain is most pronounced on hardware with limited memory bandwidth relative to compute, such as consumer GPUs and edge devices.
ATTENTION MECHANISM COMPARISON

MHA vs. MQA vs. GQA

Structural and performance comparison of Multi-Head, Multi-Query, and Grouped-Query Attention mechanisms in transformer models.

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

KV Head Count

Equal to query heads (H)

1 shared across all query heads

G groups (1 < G < H)

Memory Bandwidth (KV Cache)

High (H × d_k per token)

Low (1 × d_k per token)

Medium (G × d_k per token)

Inference Speed

Baseline

Fastest

Comparable to MQA

Model Quality

Highest

Minor degradation

Near-identical to MHA

Training Throughput

Baseline

Faster (reduced KV computation)

Faster (reduced KV computation)

Uptraining Required

Used In

Original Transformer, BERT

PaLM, Falcon

LLaMA 2 (70B), LLaMA 3, Mistral

GROUPED-QUERY ATTENTION

Frequently Asked Questions

Clear, technical answers to the most common questions about the GQA mechanism, its trade-offs, and its role in modern self-hosted 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. It works by interpolating between Multi-Head Attention (MHA), where every query head has its own key-value pair, and Multi-Query Attention (MQA), where all query heads share one key-value pair. In GQA, if you have H query heads, you partition them into G groups. Each group of H/G query heads shares one key head and one value head. During the attention computation, queries within the same group attend to the same key and value projections, drastically reducing the size of the KV cache during autoregressive decoding while preserving most of the model's representational quality.

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.