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.
Give the shapes inside each head and of the layer output. Count the parameters. . Its queries, keys and values are . Its score matrix and attention matrix are , and its output is . The eight outputs side by side form a matrix, and maps it to . The layer has weights and biases, so parameters.
Each head has
(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.
Compute the cache size for one sequence with MHA, GQA with and MQA. How large is the MHA cache for a batch of sequences? The cache holds numbers. With MHA, : GQA with stores a quarter of that, numbers or GB. MQA with stores numbers or GB, which is times less than MHA. A batch of sequences with MHA needs GB for the cache alone. That fills a large part of a GPU before any weights are loaded. The same batch fits in GB with MQA.
(Hard) What GQA Saves in Parameters
Take the decoder from the Medium example, with and heads of size . Compare MHA with GQA using .
Count the attention weights per layer in both models. Show that two heads in the same GQA group can still attend to different tokens. weights per layer. In GQA, the query projection is still , with weights, and so is . The key projection has only heads of size , so it is , with weights. The value projection has the same size. The total is which is fewer than MHA.
MHA has
Head belongs to group , so heads to form group . They share the keys and the values . Head in this group computes . Two heads with different query projections give different score rows for the same token. One head can put its weight on the previous token while another puts it on the first token. Both outputs are then averages of the same value vectors , with different weights.
See Also
- 7.1. Attention, the single lookup that each head performs.
- 7.1.2. Efficient Attention, for methods that cut the cost of each head.
- 7.2. Transformer Architecture, where multi-head attention forms half of each block.
- 7.5. Encoder and Decoder Models, for decoders that use the KV cache during generation.
- 6.1.2. Graph Convolutional Networks, whose graph attention also uses several heads.