Grouped-query attention (GQA): sharing keys and values in groups
One shared set of keys and values saved a lot of memory. Do all the heads have to share the same set?
In multi-query attention, every head used the same keys and values. That saved memory, but left only one way to represent them.
What if a few heads shared one set, and another group had its own?
Share within a group
Start with the separate heads below. Press Share within groups.
K and V move outside the heads again. This time, each group keeps its own pair of projections and its own cache.
This is grouped-query attention, or GQA.
Each head still has its own query. Heads in one group ask their different questions of the same keys, then take their own weighted sums of the same values.
Another group can learn different keys and values.
How much should we share?
Consider 32 query heads.
With one K/V pair for each head, we keep 32 sets of keys and values. MQA brought that down to one.
Keeping eight sets instead gives us eight groups of four query heads. The cache is four times smaller than keeping a separate set for every head.
We choose that group count when designing the model. Its query, key and value projections learn together during training.
What do the extra groups buy us?
They give the heads more ways to represent the tokens than a single shared K/V pair allows.
The GQA experiments found quality close to multi-head attention, with decoding speeds comparable to MQA.
We have saved memory by storing fewer K/V sets. Could we shrink what goes into each cache entry too?
Next: learn a compact representation to cache.
Sources
- GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints, Joshua Ainslie and colleagues (2023).