# Grouped-Query Attention (GQA)

- **Canonical URL:** https://getaimath.com/dictionary/grouped-query-attention-gqa/
- **Topics:** [Transformer Architecture](https://getaimath.com/dictionary/category/transformer-architecture/)
- **Last updated:** 2026-11-25

## What is Grouped-Query Attention (GQA)?

**In one sentence:** Grouped-query attention is a middle ground where groups of heads share keys and values, balancing speed and quality.

Instead of every head having its own keys and values, or all heads sharing one set, heads are split into groups that each share a set. This keeps most of the quality of full attention while saving a lot of memory.

![Transformers & Attention: The Smart Highlighter — Simple cartoon diagram showing how a Transformer reads a whole sentence at once and shines a bright highlighter beam from the word "IT" back to "trophy" so it knows what "IT" means.](https://getaimath.com/diagrams/transformer-architecture.png)
*Napkin Diagram (Transformer Architecture): Older AI read word-by-word and often lost track of earlier words; a Transformer highlights which words connect to which.*

## What is an example of Grouped-Query Attention (GQA)?

A model with 32 query heads might use 8 key-value groups, so every 4 heads share one set.

## Why does Grouped-Query Attention (GQA) matter?

GQA is used in many modern models like Llama to run faster and handle longer context.

## Grouped-Query Attention (GQA) vs. Multi-Head Attention (MHA)

MHA gives every query head its own key and value heads; GQA lets a group of query heads share one key/value pair, which shrinks the KV cache a lot at long context.

| Dimension | Grouped-Query Attention (GQA) | [Multi-Head Attention (MHA)](https://getaimath.com/dictionary/multi-head-attention-mha/) |
| --- | --- | --- |
| KV heads | 1 KV pair shared per group of Query heads | 1 KV pair per Query head |
| KV cache memory | Several times smaller (e.g., 8× with 8 query heads per group), usually with little quality loss | Large—a bottleneck for long-context serving |
| Adoption | Common in modern open models such as Llama 3, Qwen and Mistral | Original Transformer and many earlier LLMs |

## Related Concepts

- [Multi-Query Attention (MQA)](https://getaimath.com/dictionary/multi-query-attention-mqa/): Multi-query attention is a faster type of attention where all heads share one set of keys and values but keep separate queries.
- [FlashAttention](https://getaimath.com/dictionary/flashattention/): FlashAttention is a clever way to compute attention on GPUs that is much faster and uses much less memory, while giving the same results.
- [Cross-Attention](https://getaimath.com/dictionary/cross-attention/): Cross-attention lets one part of a model pay attention to a different input, like a decoder looking at an encoder's output.
- [Attention Mask](https://getaimath.com/dictionary/attention-mask/): An attention mask is a pattern that tells the model which positions it may or may not attend to.
- [Masked Attention](https://getaimath.com/dictionary/masked-attention/): Masked attention is attention where some positions are hidden, so certain words cannot look at certain other words.
- [Positional Encoding](https://getaimath.com/dictionary/positional-encoding/): Positional encoding adds information about word order, so the model knows which word came first, second, and so on.

---
Source: The AI Math Dictionary (https://getaimath.com/dictionary/grouped-query-attention-gqa/)
