Grouped-query attention (GQA)
Grouped-query attention is a variant of transformer attention in which groups of query heads share one set of keys and values, shrinking the memory a model needs while it generates text with little loss of quality.
In standard multi-head attention, a transformer splits attention into several heads, and each head has its own queries, keys and values. When a model generates text, it stores the keys and values for every earlier token so it does not have to recompute them. This store, the KV cache, grows with the length of the conversation, the number of layers and the number of key-value heads. Reading it from memory at every step is a main bottleneck when serving large models.
In November 2019 Noam Shazeer proposed an extreme fix, multi-query attention, in which all heads share a single set of keys and values. That shrinks the cache dramatically and speeds up generation, with some loss of quality.
Grouped-query attention, published in May 2023 by Joshua Ainslie and colleagues and presented at EMNLP 2023, takes the middle path. Query heads are divided into groups, and each group shares one key-value head, so there are more key-value heads than in multi-query attention but fewer than in full multi-head attention. As an example, a model with 32 query heads and 8 key-value heads would keep a cache a quarter of the size of a full multi-head model's. The authors reported quality close to multi-head attention at speeds close to multi-query attention, and showed that an existing multi-head model could be converted using about 5% of its original pre-training compute.
It was soon adopted in open-weight models. Mistral 7B, released in September 2023, used grouped-query attention for faster inference, and Meta said in April 2024 that it adopted it across both the 8B and 70B sizes of Llama 3.
What it changes is cost, not knowledge. A smaller cache lets the same hardware handle longer inputs or more users at once, which lowers the price of serving a model. DeepSeek later pushed the same goal further with multi-head latent attention, which compresses the cached keys and values rather than sharing them.
In multi-head attention every query head has its own keys and values. In multi-query attention all query heads share one set. Grouped-query attention sits between the two: query heads are split into groups and each group shares one set of keys and values. During generation a model caches keys and values for every earlier token, and the cache size depends on how many key-value heads there are. Sharing each key-value head across a group of query heads cuts that number, so the cache shrinks in proportion and less data has to be read at each step. The paper that introduced it found quality close to full multi-head attention, while multi-query attention, which shares a single set of keys and values, lost more. It has since been adopted in widely used open models such as Mistral 7B and Llama 3.Common questions
What is the difference between multi-head, multi-query and grouped-query attention?
Why does grouped-query attention save memory?
Does grouped-query attention make models worse?
Search for "grouped-query attention", "GQA vs MQA vs MHA" or "KV cache size" to see how attention variants trade memory for quality. The original papers on arXiv, the architecture sections of open model technical reports and model configuration files on model hubs are reliable sources. Inference engine documentation explains how these choices affect serving cost in practice.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.