Search

Search papers and lessons

GQA: Training Generalized Multi-Query Transformer Models

Joshua Ainslie, James Lee-Thorp, Michiel de Jong et al. · 2023

GQA shows that a multi-head language model can be uptrained into a grouped-query model with about 5% of its pretraining compute, keeping quality near multi-head at speed near multi-query.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Architectures
  • Neural network basics
  • Linear algebra
  • Efficiency
  • Sign in to see your progress

TL;DR

Decoder inference in a large language model is slow mainly because each new token must read a large key-value cache from memory. Multi-query attention fixes that with one shared key and value head, but it can cost quality and it needs a separate model. This paper shows that an existing multi-head checkpoint can be converted and uptrained into a multi-query model using about 5% of its pretraining compute, and it introduces grouped-query attention, which sits between the two. The best grouped setting, with eight groups, comes close to multi-head quality at close to multi-query speed.

Where you'll see it: in the design of inference-heavy language models, where the choice between multi-head, grouped-query and multi-query attention sets how much memory the cache needs per token.

The problem

Generating text with a transformer is a loop. At every step the model computes attention for one new token against every earlier token. Each earlier token's keys and values sit in the KV Cache, and that cache must be read from GPU memory at every step. For long outputs the cost is dominated by moving this data, not by arithmetic, so the paper frames the bottleneck as memory bandwidth.

Multi-query attention (MQA) reduces the cache by using many query heads but a single key head and a single value head. The paper notes two problems. First, MQA can lower quality and make training less stable. Second, it may not be feasible to train one model for quality and another for fast inference. Many widely used public checkpoints, such as T5 and LLaMA, use multi-head attention and do not have an MQA variant, so a conversion path matters as much as the architecture.

Key idea

Two ideas, one trade-off dial.

First, a multi-head checkpoint already contains key and value heads that can be merged. Averaging them into one head gives a better starting point than keeping one head or starting from random values, which is what lets a short additional training run adapt the model. The paper calls this uptraining.

Second, the number of key and value heads does not have to be 1 or HH. Group the HH query heads into GG groups, and give each group its own key and value head. One group is MQA, and one group per query head is MHA. Anything in between is grouped-query attention.

flowchart LR
  MHA["Multi-head: H key and value heads"] --> GQA["Grouped-query: G key and value heads"]
  GQA --> MQA["Multi-query: 1 key and value head"]
  MHA -. "more memory, best quality" .-> GQA
  GQA -. "less memory, faster" .-> MQA

The method

Notation

Let the layer have HH query heads, each of dimension dkd_k. A head computes attention for its own query q\mathbf{q} against keys KK and values VV:

Attn⁡(Q,K,V)=softmax⁡ ⁣(QK⊤dk)V.\operatorname{Attn}(Q, K, V) = \operatorname{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V.

In MHA each query head hh has its own KhK_h and VhV_h. In MQA all heads read the same KK and VV. In GQA with GG groups, the H/GH/G query heads in group gg read the same KgK_g and VgV_g, where GG divides HH.

Cache size

Per token and per layer, the cache stores one key and one value for each key-value head. It therefore holds 2⋅nkv⋅dk2 \cdot n_{kv} \cdot d_k numbers, where nkvn_{kv} is the number of key-value heads. That gives

nkv=H  (MHA),nkv=G  (GQA),nkv=1  (MQA).n_{kv} = H \;\text{(MHA)}, \qquad n_{kv} = G \;\text{(GQA)}, \qquad n_{kv} = 1 \;\text{(MQA)}.

Moving from MHA to GQA shrinks the cache by the factor H/GH/G. Moving to MQA shrinks it by HH. The paper also notes that the same proportional saving holds as models grow, because the cache scales with the model dimension while model parameters scale faster. Standard model sharding replicates the single shared head once per partition, and GQA removes that waste as well.

Uptraining

Conversion takes two steps. First, the key and value projection matrices are mean-pooled. For GQA, each group's key projection is the average of the key projections of the heads in that group, and likewise for values. Second, the converted model is pretrained further for a fraction α\alpha of its original training steps, using the original recipe and data. The paper uses α=0.05\alpha = 0.05 for its main results.

The paper compares mean pooling with keeping only the first head and with random initialisation. Mean pooling works best, and the ordering follows how much of the pretrained information each method keeps.

As a toy illustration, not a number from the paper, suppose a group has two heads whose one-dimensional key projections are w1=1w_1 = 1 and w2=3w_2 = 3. Keeping the first head gives w=1w = 1, which discards the second head entirely. Mean pooling gives w=(1+3)/2=2w = (1 + 3) / 2 = 2, which sits between the two and keeps a share of both. The uptraining run then lets the model adjust to the averaged projection.

Results

The main experiments use the T5.1.1 encoder-decoder architecture. The reference points are T5 Large and XXL with multi-head attention. The uptrained models are T5 XXL with MQA and with GQA using eight groups, both trained with α=0.05\alpha = 0.05. Attention is converted in the decoder self-attention and cross-attention, not in the encoder. Table 1 reports average inference time per sample and average dev-set score across seven datasets (five summarisation, one translation, one question answering).

ModelTime per sample (s)Average score
MHA-Large0.3746.0
MHA-XXL1.5147.2
MQA-XXL (uptrained, 5%)0.2446.6
GQA-8-XXL (uptrained, 5%)0.2847.1

Read against the table, uptrained GQA is about 5.4 times faster than MHA-XXL (1.51 divided by 0.28) and scores 0.1 points lower on average. MQA is about 6.3 times faster (1.51 divided by 0.24) and scores 0.6 points lower. The paper's uptrained MQA model is also faster and higher quality than MHA-Large, which takes 0.37 seconds. The uptraining run is reported at about 600 TPUv3 chip-days for the 5% setting.

The ablations add three points. Mean pooling beats the other conversions. Uptrained GQA already performs reasonably right after conversion, while MQA needs uptraining to be useful. Both gain from the 5% run, with diminishing returns by 10%. Going from one group to eight adds only modest inference time, with the cost rising as the number of groups approaches the number of heads. The paper chooses eight groups as a middle ground.

Appendix A reports a separate finding. MQA models trained from scratch on T5 Large showed frequent loss spikes, and their fine-tuned versions diverged on long-input tasks. Uptrained MQA was more stable but still noisy, and uptrained GQA appeared stable, though the authors did not investigate the cause.

Why it mattered

The paper gives a concrete recipe. A team with an existing multi-head checkpoint can convert it to a faster variant for a small share of the original compute, rather than training a new model. It also gives a dial, the number of groups, that trades speed against quality, and it reports settings where the quality loss is small.

The paper states its own limits. The summarisation scores rely on ROUGE, which the authors say is a flawed measure. They did not train an XXL GQA model from scratch, so the comparison between uptraining and full training is open. They tested only encoder-decoder models, and they expect a larger advantage over MQA for decoder-only models, which they say are the more popular design. Claims about how widely GQA was adopted afterwards are not in the paper, so this lesson does not make them.

Reading guide

Read the paper in this order. The prerequisites are listed for each part.

  • Abstract and 1 Introduction. Needs Multi-Head Attention and Multi-Query Attention, to see what is being reduced, and KV Cache, to see why the reduction matters during generation.
  • 2.1 Uptraining. Needs Transfer Learning and Fine-Tuning, since the converted model starts from a pretrained checkpoint and keeps training. The mean-pooling step needs only linear algebra on the projection matrices.
  • 2.2 Grouped-query attention. Needs Grouped-Query Attention for the group structure, and Attention Mechanism for the query, key and value roles. Work through the cache-size argument in this section slowly.
  • 3.1 Experimental setup. Needs Transformer, because the models are T5 encoder-decoders, and Autoregressive Model, because decoding is one token at a time. Table 1 is the main result.
  • 3.3 Ablations. Read after the method, since the conversion and group-count results only make sense with it in hand.
  • Appendix A. Read with Multi-Query Attention in mind, since the instability finding concerns MQA specifically.

Check yourself

QuestionWhat does grouped-query attention reduce to when there is one group, and when there is one group per query head?Show answer

With one group, every query head shares one key head and one value head, which is multi-query attention. With one group per query head, each query head has its own key and value head, which is multi-head attention. GQA is the family that interpolates between the two.

QuestionA layer has H=32H = 32 query heads of dimension dk=128d_k = 128. Compare the number of cached key and value numbers per token per layer for MHA and for GQA with G=8G = 8 groups.Show answer

MHA stores 2⋅32⋅128=81922 \cdot 32 \cdot 128 = 8192 numbers. GQA with 8 groups stores 2⋅8⋅128=20482 \cdot 8 \cdot 128 = 2048 numbers. The ratio is 32/8=432 / 8 = 4, so the cache is a quarter of the MHA size. These numbers are for this illustrative configuration, not for the paper's models.

QuestionWhy does the paper mean-pool the key and value projections when converting a checkpoint, rather than keeping one head or starting new heads at random?Show answer

Mean pooling keeps the most information from the pretrained weights, because it averages over every head in the group. The paper's ablation finds it works best, followed by keeping the first head and then random initialisation. Keeping more of the pretrained information gives the uptraining run a better starting point, so it needs less compute to recover quality.

QuestionA team serves long outputs on memory-limited GPUs and has an existing multi-head checkpoint. Which option does this paper support, and what should they check before relying on it?Show answer

The paper supports converting the checkpoint to GQA with 8 groups and uptraining for about 5% of the original pretraining compute. Before relying on it, the team should measure quality on their own tasks, since the paper's summarisation scores rely on ROUGE and the paper tested only encoder-decoder models. They should also note that the paper did not compare against a GQA model trained from scratch.

Sources

Read the full paper

Joshua Ainslie, James Lee-Thorp, Michiel de Jong et al., 2023. arXiv:2305.13245