22/26
Part 4 of 4 · Prefill, decode and memoryLearn and do

Fewer key/value heads: multi-query and grouped-query attention

Once the cache works, interviewers ask about its size. There are three answers, each with its paper, and this is the first.

Llama 3 8B has 32 query heads. Pick how many key/value heads they share:

Query heads sharing key/value heads, in Llama 3 8B's shapeInteractive
0123
kv 0
4567
kv 1
891011
kv 2
12131415
kv 3
16171819
kv 4
20212223
kv 5
24252627
kv 6
28293031
kv 7
query head key/value head, the part that is cached
Cache per token
128 KiB
Per 8,192-token sequence
1 GiB
Sequences in 65 GiB
65
Query heads per kv head
4
grouped-query: each group of 4 query heads shares one key/value head. The cache is 2 × 32 layers × 8 × 128 × 2 bytes = 131,072 bytes per token.

Multi-query attention (Shazeer, 2019) shares one key head and one value head across all the query heads.

Predict

Llama 3 8B's shape needs 128 KiB a token with 8 key/value heads. Multi-query attention keeps 1. How much a token now?

Pick an answer above to see the rest