Inferensys

Glossary

Grouped-Query Attention (GQA)

Grouped-Query Attention (GQA) is an attention mechanism optimization that shares a single key-value head across multiple query heads, drastically reducing the KV-cache size and memory bandwidth requirements during large language model inference while maintaining quality close to multi-head attention.
ML engineer running AI model benchmarks, performance charts on multiple screens, late night home office setup.
KV-CACHE OPTIMIZATION

What is Grouped-Query Attention (GQA)?

Grouped-Query Attention (GQA) is an attention mechanism variant that reduces memory bandwidth requirements during inference by sharing a single key-value head across multiple query heads.

Grouped-Query Attention (GQA) is an interpolation between Multi-Head Attention (MHA) and Multi-Query Attention (MQA). In GQA, query heads are divided into groups, and each group shares a single key and value head. This architecture significantly shrinks the KV-Cache size, reducing the memory bottleneck during autoregressive decoding while preserving model quality closer to MHA than the more aggressive MQA approach.

GQA is a critical inference optimization for large language models. By reducing the number of unique key and value tensors that must be stored in memory, GQA decreases the memory bandwidth required per generated token. This allows for higher batch sizes and lower latency during serving, making it a standard component in models like Llama 2 and Mistral.

KV-CACHE EFFICIENCY TRADE-OFFS

GQA vs. MHA vs. MQA: Attention Mechanism Comparison

A technical comparison of Multi-Head Attention, Multi-Query Attention, and Grouped-Query Attention across memory footprint, quality retention, and inference speed metrics.

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

KV-Head Count

Equal to query heads (H)

1 shared head

G groups (1 < G < H)

KV-Cache Size

H × sequence_length × d_head

1 × sequence_length × d_head

G × sequence_length × d_head

Memory Bandwidth Load

High

Low

Medium

Quality Retention vs. MHA

Baseline (100%)

Moderate degradation (95-98%)

Near-identical (99-100%)

Inference Throughput

Memory-bandwidth bound

Compute-bound; highest speedup

Near-MQA speedup with MHA quality

Training Stability

Stable

Can be unstable; requires tuning

Stable; minimal adaptation needed

Uptraining from MHA Checkpoint

N/A (original checkpoint)

Requires full uptraining

Requires ~5% of original training budget

Adopted By

Original Transformer; GPT-3

PaLM; Falcon

LLaMA 2 (70B); LLaMA 3; Mistral

Architecture

Key Features of Grouped-Query Attention

Grouped-Query Attention (GQA) interpolates between Multi-Head Attention and Multi-Query Attention by sharing a single key-value head across a small group of query heads. This design preserves model quality while drastically reducing the memory footprint of the KV-Cache.

01

KV-Cache Memory Reduction

GQA reduces the size of the KV-Cache by a factor equal to the number of query heads per key-value head. In standard Multi-Head Attention, each query head has a dedicated key and value head, causing the cache to grow linearly with the number of heads. By sharing a single key-value pair across a group of query heads, GQA slashes the memory bandwidth required during autoregressive decoding. For a model with 32 query heads and 8 key-value groups, the KV-Cache size is reduced by 4x, directly lowering the hardware cost of serving long sequences.

02

Quality Interpolation Between MHA and MQA

GQA acts as a tunable trade-off between Multi-Head Attention (MHA) and Multi-Query Attention (MQA). MHA provides the highest model quality but the largest KV-Cache. MQA uses a single key-value head for all queries, minimizing memory but often degrading quality on tasks requiring fine-grained attention. GQA generalizes both: setting the number of key-value groups equal to the number of query heads recovers MHA; setting it to one recovers MQA. Intermediate group counts achieve near-MHA quality with near-MQA efficiency.

03

Uptraining from Existing Checkpoints

GQA models can be efficiently created from pre-trained MHA checkpoints through a process called uptraining. The original key and value head weight matrices are mean-pooled into the target number of groups, initializing a GQA model. This converted model is then fine-tuned for a small fraction (typically 5-10%) of the original training steps. This avoids the prohibitive cost of training a GQA model from scratch and preserves the knowledge already learned by the base model.

04

Adoption in Production LLMs

GQA has been widely adopted in state-of-the-art production models due to its favorable memory-quality trade-off:

  • Llama 2 70B uses GQA with 8 key-value heads
  • Llama 3 and Llama 3.1 use GQA across all model sizes
  • Mistral models leverage GQA for efficient inference
  • Gemma and other open-weight models incorporate GQA The technique is now a standard architectural choice for large-scale autoregressive decoders.
05

Impact on Inference Throughput

The primary bottleneck in LLM inference for long sequences is memory bandwidth, not compute. The KV-Cache must be read from GPU HBM for every new token generated. By shrinking the KV-Cache, GQA directly increases the number of tokens the system can generate per second. This improvement is most pronounced in batch inference scenarios where multiple sequences are processed simultaneously, as the aggregate KV-Cache size across the batch is the dominant memory consumer.

06

Relationship to Other Attention Optimizations

GQA is complementary to other attention efficiency techniques:

  • FlashAttention: Reduces HBM reads/writes via tiling; GQA reduces the total data that must be stored
  • Multi-Query Attention (MQA): The extreme case of GQA with one key-value group
  • Multi-Head Latent Attention (MLA): Used in DeepSeek-V2, compresses KV-Cache into a low-rank latent vector, achieving even greater compression than GQA
  • Sliding Window Attention: Limits attention to a local context; can be combined with GQA for further memory savings
GROUPED-QUERY ATTENTION EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about Grouped-Query Attention (GQA), its mechanisms, trade-offs, and role in optimizing large language model inference.

Grouped-Query Attention (GQA) is an attention mechanism variant that partitions query heads into groups, with each group sharing a single key-value head, rather than giving every query head its own dedicated key-value pair. In a standard Multi-Head Attention (MHA) configuration with H query heads, there are H corresponding key and value heads. GQA reduces this to G key-value heads, where G < H, and each key-value head is shared by H/G query heads within its group. During the attention computation, each query head within a group computes its attention scores against the group's shared key tensor and aggregates the shared value tensor. This design directly reduces the size of the KV-Cache by a factor of H/G, slashing memory bandwidth requirements during autoregressive decoding while preserving model quality significantly better than the extreme Multi-Query Attention (MQA) variant, which uses a single global key-value head for all queries.

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.