Inferensys

Glossary

Grouped-Query Attention (GQA)

Grouped-Query Attention (GQA) is a transformer attention mechanism that groups multiple query heads to share a single key and value head, optimizing the trade-off between model quality and inference memory efficiency.
ML engineer running AI model benchmarks, performance charts on multiple screens, late night home office setup.
EFFICIENT MODEL ARCHITECTURES

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.

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.

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.

EFFICIENT MODEL ARCHITECTURES

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.

01

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.
02

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.
03

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.
04

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:

  1. 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.
  2. 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.
05

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.
06

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.
EFFICIENT ATTENTION ARCHITECTURES

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.

FeatureMulti-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: (batch * seq_len * num_heads * head_dim)

Minimal: (batch * seq_len * 1 * head_dim)

Intermediate: (batch * seq_len * num_groups * head_dim)

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: W_Q, W_K, W_V (each shape: [d_model, num_heads * head_dim])

3: W_Q (full), W_K, W_V (single head)

3: W_Q (full), W_K, W_V (for num_groups)

IMPLEMENTATION EXAMPLES

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.

GROUPED-QUERY ATTENTION (GQA)

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.

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.