Multi-Head Attention

Multi-head attention runs attention lookups in parallel on the same input. Each lookup is a head with its own queries, keys and values. Head has projections and , usually with . For input , with biases left out,

The head outputs are placed side by side and mixed by one more linear map :

Heads play the role that channels play in a CNN. One head returns one weighted average per token, and several heads can fetch several different things.

A decoder that generates text stores the keys and values of all earlier tokens in a KV cache. Two variants make the cache smaller by sharing keys and values across heads:

  • Multi-query attention (MQA): every head keeps its own query projection. All heads share one key projection and one value projection.
  • Grouped-query attention (GQA): the query heads form groups, and each group shares one key and one value projection. gives standard multi-head attention, and gives MQA.

Intuition

A single head gives each token one convex combination of the values. A token often needs several facts at once. The Spring 2023 lecture 17 notes use verb agreement as the example. To conjugate a verb, the decoder may need the number and the gender of the subject. These facts can sit in different places. One head would have to blend these lookups into one average. With several heads, each lookup gets its own query and its own weights.

The channels of a CNN give the same picture. Different channels detect different patterns, and a max pool over each channel keeps its own strongest response. Different heads score the keys differently, so each head can attend to different tokens. Prince also notes that several heads seem to make training more robust to bad initializations.

Splitting into heads of size costs almost nothing extra. The projections have the same total size as one head of size . The score computation also costs the same in total. Each of the heads works with vectors of length .

Properties

(Parameter Count) With , a multi-head attention layer has weights, whatever the number of heads.

The query projections of all heads together form one matrix. The key projections and the value projections do too. That gives weights. The output map is , which adds . The total is , and biases add more. In practice one matrix computes all queries, keys and values at once. The result is then split into blocks of columns each.

(Concatenation Restores the Width) The head outputs must add up to width , so that the block’s output can be added to its input.

A transformer block adds the attention output to its input through a residual connection. Each head returns numbers per token, and maps numbers back to . The Spring 2023 lecture 17 notes make the same point. Each head’s values should be the size of the input, so that .

(Why the Keys and Values Can Be Cached) In causal attention, a token’s key and value never change after they are computed, so a decoder stores them.

To generate token , the decoder needs the query of token . It also needs the keys and values of tokens to . The causal mask means that token ‘s keys and values depend only on tokens to . They are the same at every later step. Without a cache, each step would recompute all earlier keys and values. With a cache, each step computes one new key and value per head and layer and appends them. The cache holds numbers per token, for layers and key-value heads. Standard multi-head attention has .

(Sharing Keys and Values) MQA and GQA shrink the KV cache by the factor , while every head keeps its own query.

In GQA, number the heads and the groups . Head uses the keys and values of group . Its scores are

Heads in one group still produce different attention matrices, because their queries differ. They mix the same values, though. The lecture states the motivation as saving memory. During generation, reading the cache from memory takes much of the time per token. A smaller cache also lets more sequences fit on the GPU at once.

(Converting a Trained Model) A trained multi-head model can be converted to GQA by averaging its key and value projections within each group.

Not Taught in Lecture

Uptraining follows Ainslie et al. (2023), who introduced GQA. MQA was introduced by Shazeer (2019).

The averaged projections give a GQA model that starts close to the original. It then trains for a small fraction of the original compute to recover quality. Ainslie et al. call this uptraining. Llama 2 70B and Mistral 7B are examples of models that use GQA with key-value heads.

Examples

(Easy) Shapes and Parameters

A multi-head attention layer has and heads, and it reads tokens.

(Medium) Size of the KV Cache

A decoder has layers, and heads of size . It holds a context of tokens in 16-bit floats, which take bytes each.

(Hard) What GQA Saves in Parameters

Take the decoder from the Medium example, with and heads of size . Compare MHA with GQA using .

See Also