Grouped-query attention
Lesson: Multi-Head AttentionCheat sheetTrack: Transformers from scratchTrack: Papers to code
Problem
Grouped-query attention (GQA) lets several query heads share one key head and one value head. The model then stores fewer keys and values in its cache. With one group, GQA is multi-query attention. With one group for each query head, GQA is standard multi-head attention. Large open models such as Llama 3 use GQA.
Task
Write three functions.
repeat_kv(kv, n_heads)repeats each key or value head so that every query head has a matching head.grouped_query_attention(x, w_q, w_k, w_v, w_o, n_heads, n_kv_heads)returns the output and the attention weights of GQA self-attention.mean_pool_kv_heads(w, n_heads, n_kv_heads)turns the key or value projection of a multi-head checkpoint into a GQA projection.
Let H be n_heads, G be n_kv_heads, r = H / G and d_{head} = D / H. Head h of a projected array holds the columns h \cdot d_{head} to (h + 1) \cdot d_{head} - 1, in the same order. Query head h uses key and value group \lfloor h / r \rfloor. So the first r query heads use group 0, the next r use group 1, and so on.
The steps of grouped_query_attention are:
- Q = x W_q, K = x W_k, V = x W_v. There are no bias terms.
- Split Q into H heads and split K and V into G heads each. Repeat K and V to H heads.
- In each head, compute \mathrm{softmax}(Q_h K_h^T / \sqrt{d_{head}})\, V_h. The softmax is over the key axis. There is no mask.
- Join the H head outputs in head order, so that the result has shape
(B, T, D), and multiply by W_o.
mean_pool_kv_heads takes a projection with H heads. Group g of the result is the mean of the heads g r to g r + r - 1.
All three functions raise ValueError when H is not a multiple of G.
Input and output
| Name | Type | Shape | Meaning |
|---|---|---|---|
kv | float array | (B, G, T, d_head) | keys or values split into G heads |
x | float array | (B, T, D) | a batch of B sequences of T tokens with D features |
w_q | float array | (D, D) | the query projection |
w_k, w_v | float array | (D, G * d_head) | the key and value projections |
w_o | float array | (D, D) | the output projection |
n_heads | int | the number of query heads H | |
n_kv_heads | int | the number of key and value groups G | |
w | float array | (D, H * d_head) | a key or value projection with H heads |
repeat_kvreturns a float array of shape(B, H, T, d_head).grouped_query_attentionreturns a tuple(output, weights).outputhas shape(B, T, D).weightshas shape(B, H, T, T), andweights[b, h, i, j]is the weight that queryigives to keyjin headh.mean_pool_kv_headsreturns a float array of shape(D, G * d_head).
Constraints
1 <= B <= 4,1 <= T <= 32,1 <= G <= H <= 16andDis a multiple ofH.- Divide the scores by \sqrt{d_{head}}, not by \sqrt{D}.
- Do not change the input arrays.
- Use numpy only.
Examples
pythonkv = np.arange(8.0).reshape(1, 2, 2, 2)
repeat_kv(kv, 4)[0]text[[[0. 1.]
[2. 3.]]
[[0. 1.]
[2. 3.]]
[[4. 5.]
[6. 7.]]
[[4. 5.]
[6. 7.]]]pythonx = np.array([[[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0], [1.0, 1.0, 0.0, 0.0]]])
w_k = np.array([[1.0, 0.0], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0]])
w_v = np.array([[1.0, 0.0], [0.0, 2.0], [0.0, 0.0], [1.0, 0.0]])
output, weights = grouped_query_attention(x, np.eye(4), w_k, w_v, np.eye(4), 2, 1)textoutput
[[[1.20333628 1.19777581 0.79666372 1.60444837]
[0.79666372 1.60444837 1.20333628 1.19777581]
[1. 1.50348984 1. 1.33333333]]]
weights
[[[[0.40111209 0.19777581 0.40111209]
[0.19777581 0.40111209 0.40111209]
[0.24825508 0.24825508 0.50348984]]
[[0.19777581 0.40111209 0.40111209]
[0.40111209 0.19777581 0.40111209]
[0.33333333 0.33333333 0.33333333]]]]pythonmean_pool_kv_heads(np.arange(16.0).reshape(2, 8), 4, 2)text[[ 1. 2. 5. 6.]
[ 9. 10. 13. 14.]]Source paper GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints, Ainslie et al., 2023.
Hints
4 hintsSolution and notes
LockedPass every check with Submit to open the reference solution and the notes.
Run it on your computer
Clone the repository, install numpy and run the same checks in a terminal:
python3 scripts/practice/run.py grouped-query-attention my_answer.py
python3 scripts/practice/run.py grouped-query-attention my_answer.py --examplesStart my_answer.py from starter.py.
Your code is saved in this browser as you type. On a phone you can read and edit the code, but the checks are easier to use on a desktop.
Keyboard shortcuts
- Ctrl Enter
- Run the examples
- Ctrl Shift Enter
- Submit
- Tab / Shift Tab
- Indent or outdent the lines
- Esc then Tab
- Move the focus out of the editor
- Alt H
- Show the next hint
On a Mac, use Cmd in place of Ctrl.
Select Run to check the examples, or Submit to run every check.