User Agents

Web browsers and everyday web tech, explained

Reduce KV Cache Size with Grouped Query Attention?

· 6 min read · Cookies and Cache

In this article
  1. Background: The KV Cache in Transformer Models
  2. How Grouped Query Attention Works
  3. Comparison with MHA and MQA
  4. How to Choose GQA Configuration
  5. Common Problems and Limitations
  6. Alternatives to GQA
  7. Frequently Asked Questions

Yes, grouped query attention (GQA) can reduce KV cache size in transformer models by grouping multiple query heads to share a single key and value head, decreasing memory use without a drastic drop in model quality. This technique strikes a balance between the memory-heavy multi-query attention (MHA) and the less expressive multi-query attention (MQA), making it effective for long-context inference in large language models.

Background: The KV Cache in Transformer Models

Transformer-based language models generate text autoregressively, predicting one token at a time. To improve efficiency during inference, these models store past key (K) and value (V) vectors in a cache—known as the KV cache—so they don’t need to recompute attention for previously processed tokens. As sequence length grows, so does the KV cache, becoming a primary bottleneck for memory usage, especially in models handling long contexts (e.g., 32k or 128k tokens).

Each attention head maintains its own K and V projections. In standard multi-head attention (MHA), if a model has 32 attention heads, it stores 32 sets of K and V matrices. This leads to substantial memory consumption, particularly in decoder-only models like Llama or GPT, where the KV cache scales linearly with sequence length and number of layers.

How Grouped Query Attention Works

Grouped query attention reduces KV cache size by grouping multiple query heads to share a subset of key-value heads. Instead of each query head having its own dedicated key and value head (as in MHA), GQA divides the query heads into groups, each sharing a single key and value head.

For example:

  • A model with 32 query heads and 8 key-value heads uses a GQA ratio of 4 (32 ÷ 8), meaning every group of 4 query heads shares one key and one value head.
  • This reduces the number of stored K and V vectors by a factor of 4 compared to full MHA.
  • The attention computation still allows each query group to attend to its shared keys and values, preserving much of the representational power.

The attention mechanism remains largely unchanged, but the projection layers for K and V are duplicated across groups, reducing the total number of parameters and cached activations.

Comparison with MHA and MQA

Understanding GQA requires comparing it to two extremes: Multi-Head Attention (MHA) and Multi-Query Attention (MQA).

  1. Multi-Head Attention (MHA): Each of the h query heads has its own key and value head. Highest quality, but largest KV cache. Ideal for tasks needing high fidelity, but impractical for long sequences on limited hardware.
  2. Multi-Query Attention (MQA): All query heads share a single key and value head. Maximally reduces KV cache but often leads to significant quality degradation, especially in complex reasoning or long-form generation.
  3. Grouped Query Attention (GQA): Balances between MHA and MQA. By grouping queries (e.g., 4:1 or 8:1), it reduces KV cache size substantially while maintaining most of MHA’s performance.

Studies and real-world deployments (e.g., in models like Llama-2-70B and PaLM) show that GQA can achieve over 80% reduction in KV cache size with less than 5% drop in benchmark accuracy compared to MHA, whereas MQA can drop by 10–15% or more.

How to Choose GQA Configuration

Selecting the right GQA setup involves balancing memory savings, model performance, and hardware constraints.

  • Group size ratio: Common ratios are 2:1, 4:1, or 8:1. A 4:1 ratio is often optimal—significant memory savings without major quality loss.
  • Model size: Larger models (e.g., 70B+ parameters) benefit more from GQA due to their inherently larger KV caches.
  • Target sequence length: For applications requiring long context (e.g., document summarization, code generation), higher compression via GQA is more valuable.
  • Hardware memory: On GPUs with limited VRAM (e.g., consumer cards like RTX 3090/4090), GQA enables longer sequences or batched inference.

For instance, a 70B model with MHA might require ~14 GB of KV cache for a 32k context window, but with 4:1 GQA, that drops to ~3.5 GB—making it feasible to run on 2–4 consumer GPUs instead of requiring data center-scale hardware.

Common Problems and Limitations

While GQA improves memory efficiency, it introduces trade-offs:

  • Slight performance degradation: In tasks requiring fine-grained attention (e.g., long-range dependency resolution), sharing KV heads can limit expressiveness. This is usually minor but measurable.
  • Implementation complexity: GQA requires modifications to attention layers and caching logic. Frameworks like Hugging Face Transformers and vLLM now support it, but custom implementations may need careful tuning.
  • Not universally applicable: Some architectures or fine-tuning regimes may not benefit equally. Models trained with MHA may need retraining or distillation to fully adapt to GQA.
  • Diminishing returns at high ratios: Beyond 8:1, quality drops accelerate. A 16:1 ratio may halve the KV cache again but often at unacceptable cost to output quality.

Additionally, GQA does not reduce the model’s parameter count—only the activation memory during inference. Training still requires full MHA or equivalent, unless GQA is baked into the architecture from the start.

Alternatives to GQA

Several other methods aim to reduce KV cache or improve inference efficiency:

  • Key-Value Caching Quantization: Storing K and V vectors in lower precision (e.g., FP16, INT8, or even INT4) reduces memory footprint. However, this risks numerical instability or loss of precision.
  • Sliding Window Attention: Only caches recent tokens (e.g., last 4k), limiting context but reducing memory. Used in models like Mistral-7B.
  • StreamingLLM and Ring Attention: Techniques that recycle cache slots or use circular buffers to support infinite context with fixed memory. Still experimental and not widely deployed.
  • Speculative Decoding: Uses a smaller model to draft tokens, verified by the larger model. Reduces effective decoding steps but doesn’t shrink KV cache directly.
  • Attention Offloading: Moves parts of the KV cache to CPU or disk. Increases latency but allows longer contexts on low-memory systems.

GQA is often preferred because it maintains full context length and offers predictable quality trade-offs, unlike quantization or windowing, which may introduce artifacts or context loss.

Frequently Asked Questions

Does GQA reduce training memory?

No, GQA primarily reduces memory during inference. Training still requires computing and storing gradients for all attention heads. If GQA is used in the model architecture, training memory may be slightly lower than full MHA, but the main savings are in inference caching.

Can GQA be used with any transformer model?

Only if the model architecture supports it. Models like Llama-2 and PaLM were designed with GQA. Existing MHA models can be converted, but this usually requires retraining or fine-tuning to recover performance, as the attention dynamics change.

How much faster is inference with GQA?

GQA doesn’t directly speed up computation, but by reducing KV cache size, it allows longer sequences to fit in memory, reducing the need for recomputation or CPU offloading. This leads to higher effective throughput, especially on memory-constrained devices.

Is GQA the same as multi-query attention?

No. Multi-query attention (MQA) shares a single key-value head across all query heads, while GQA groups queries and assigns each group a shared key-value head. GQA is more expressive and typically performs better than MQA.

Does GQA affect model accuracy?

Slightly. In most benchmarks, GQA models show minor degradation (1–5%) compared to MHA, but significantly outperform MQA. The trade-off is considered acceptable given the memory savings, especially for deployment.

Which models use grouped query attention?

Llama-2-70B, Google’s PaLM, and some versions of Mistral use GQA. It’s increasingly common in large open-weight models designed for long-context inference on consumer or edge hardware.