Loading diagram…

Reference

← All guides

Grouped-Query Attention

Grouped-Query Attention (GQA) lets several query heads share one key/value head. This reduces the KV cache while keeping separate query projections.

Overview

How it works

A model saves keys and values from earlier tokens during generation. Each query head uses those cached vectors to compute attention weights. GQA reduces how many separate key/value heads must be stored and read.

Details

The KV cache

Without a cache, the model would recompute earlier keys and values at each generation step. Caching avoids that work, but uses memory in proportion to the sequence length, layer count, KV head count and head dimensions.

Details

MHA, GQA and MQA

Multi-head attention (MHA) has one key/value head per query head. Multi-query attention (MQA) shares one key/value head across all query heads. GQA uses several groups, each with its own shared key/value head.

Details

What is shared

Query heads in one group use the same key and value projections. Their query projections remain separate, so they can produce different attention weights over the same cached vectors. The semantic roles shown in the demo are illustrative, not fixed jobs assigned to real heads.

Details

Memory savings and quality

With equal head dimensions and precision, cache size scales with the number of KV heads. For example, reducing 32 KV heads to eight cuts that storage by four. The effect on quality depends on how the model is trained. Fewer heads are not a free change to an arbitrary checkpoint.

Sources