Inferensys

Glossary

Grouped-Query Attention (GQA)

An interpolation between multi-head and multi-query attention that partitions Query heads into groups, with each group sharing a single set of Key and Value heads, balancing inference speed and model quality.
ML engineer working on model compression and quantization, laptop showing performance benchmarks, technical workspace.
ATTENTION MECHANISM

What is Grouped-Query Attention (GQA)?

Grouped-Query Attention (GQA) is an interpolation between multi-head and multi-query attention that partitions query heads into groups, with each group sharing a single set of key and value heads, balancing inference speed and model quality.

Grouped-Query Attention (GQA) is an attention mechanism that divides query heads into a smaller number of groups, where each group shares a single Key and Value head while maintaining separate Query projections. This architecture directly addresses the memory bandwidth bottleneck of the Key-Value (KV) cache during autoregressive decoding by reducing the number of stored KV heads, achieving inference speeds close to Multi-Query Attention (MQA) while preserving model quality comparable to standard Multi-Head Attention (MHA).

GQA generalizes the trade-off between MHA and MQA by introducing a configurable number of KV head groups. When the number of groups equals the number of query heads, GQA is equivalent to MHA; when there is a single group, it reduces to MQA. Intermediate group counts, such as 8 query heads sharing 4 KV heads, provide a practical sweet spot that significantly decreases KV cache memory requirements without the severe representational capacity loss observed in pure MQA, making it a critical optimization for deploying large language models like Llama 2.

MECHANISM

Key Features of GQA

Grouped-Query Attention (GQA) interpolates between Multi-Head and Multi-Query Attention by partitioning query heads into groups that share a single key-value head, optimizing the trade-off between inference speed and model quality.

01

The Interpolation Spectrum

GQA sits precisely between Multi-Head Attention (MHA) and Multi-Query Attention (MQA) on the speed-quality curve.

  • MHA (G = H): Each query head has its own unique key-value head. Highest quality, largest KV cache.
  • 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. Each group shares one key-value head.

This provides a tunable knob to balance memory bandwidth and representational capacity.

02

KV Cache Reduction

The primary engineering motivation for GQA is reducing the size of the Key-Value (KV) Cache during autoregressive decoding.

  • The KV cache size scales with the number of distinct key-value heads.
  • For a model with H query heads and G groups, the cache is reduced by a factor of H/G compared to standard MHA.
  • This directly reduces the memory bandwidth bottleneck that plagues large-batch inference, allowing for higher throughput and lower latency on memory-constrained hardware.
03

Uptraining from MHA Checkpoints

GQA models can be efficiently bootstrapped from existing Multi-Head Attention checkpoints through a process called uptraining.

  • Mean Pooling: The key and value projection matrices for a group are initialized by mean-pooling the weight matrices of the original heads assigned to that group.
  • Continued Pre-training: The converted model is trained for a small fraction (typically <10%) of the original pre-training steps.
  • This allows practitioners to convert a high-quality MHA model to GQA without training from scratch, drastically reducing compute cost.
04

Adoption in Frontier Models

GQA has become the standard attention mechanism for large-scale production models due to its favorable inference characteristics.

  • Meta Llama 2 70B: Used GQA with 8 key-value heads for 64 query heads.
  • Google PaLM 2: Employed multi-query attention, the extreme endpoint of the GQA spectrum.
  • Mistral 7B: Adopted GQA alongside Sliding Window Attention for efficient long-context processing.
  • The technique is now a default architectural choice for models targeting deployment at scale.
05

Quality Retention

Empirical evaluations demonstrate that GQA maintains model quality remarkably close to MHA while approaching the speed of MQA.

  • On benchmarks like MMLU and HellaSwag, GQA models with a moderate number of groups (e.g., G=8) show negligible perplexity degradation compared to MHA baselines.
  • The quality gap between MHA and MQA widens on tasks requiring fine-grained attention to multiple distinct contexts.
  • GQA effectively closes this gap, recovering most of the lost representational power while retaining the memory savings.
06

Batch Inference Optimization

GQA's benefits are most pronounced during high-throughput, batched inference rather than single-stream generation.

  • In single-sequence decoding, the compute is dominated by matrix multiplications, and KV cache size is less critical.
  • In large-batch serving (e.g., 256+ concurrent requests), memory bandwidth for reading the KV cache becomes the primary bottleneck.
  • GQA reduces the total bytes read from memory per decoding step, directly translating to higher throughput and lower cost-per-token in production serving environments.
ATTENTION MECHANISM COMPARISON

MHA vs. MQA vs. GQA

Structural comparison of Multi-Head, Multi-Query, and Grouped-Query Attention mechanisms, highlighting the trade-offs between model quality, inference speed, and memory bandwidth.

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

Key-Value Head Count

Equal to Query head count (H)

1 (shared across all Query heads)

G (1 < G < H)

Query Head Count

H

H

H

KV Cache Size

2 × H × d_k × L

2 × 1 × d_k × L

2 × G × d_k × L

Memory Bandwidth Load

High

Low

Medium

Inference Throughput

Baseline

Highest

High

Model Quality

Highest

Degraded

Near-MHA quality

Training Throughput

Baseline

Slightly faster

Comparable to MHA

Upscaling from MQA Checkpoint

GROUPED-QUERY ATTENTION

Frequently Asked Questions

Clear, technical answers to the most common questions about Grouped-Query Attention (GQA), an interpolation between Multi-Head and Multi-Query Attention that balances inference speed and model quality.

Grouped-Query Attention (GQA) is an attention mechanism that partitions the Query heads into groups, with each group sharing a single set of Key and Value projection weights. It works by dividing the total number of Query heads (H) into G groups, where each group contains H/G Query heads. All Query heads within a single group attend to the same Key and Value vectors, while different groups use distinct Key and Value projections. This design interpolates between Multi-Head Attention (MHA), where every Query head has its own unique Key and Value heads (G = H), and Multi-Query Attention (MQA), where all Query heads share a single Key and Value head (G = 1). By choosing an intermediate number of groups, GQA achieves a balance: it significantly reduces the size of the KV cache compared to MHA, cutting memory bandwidth requirements during inference, while retaining more representational capacity than MQA, which can suffer from quality degradation. The shared Key and Value heads are computed once per group, and the attention output for each Query head within the group is computed using the standard scaled dot-product attention formula against the shared K and V.

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.