Multi-query and grouped-query attention
Share key/value heads across groups of query heads (h_kv < h). Compute the KV-cache reduction and explain why the quality cost is small.
Builds on: The KV cache
What the animation says
The KV cache is huge
Every token a model processes leaves a key and a value behind, in every layer and every head. That stash is the KV cache. Per token, multiply: two, for keys and values, times thirty-two layers, times thirty-two heads, times one hundred twenty-eight numbers per head, times two bytes. That's five hundred twelve kibibytes for a single token. After eight thousand tokens, one sequence holds four gibibytes. Every new token reads all of it. To shrink it, what could heads share while each still gets its own attention pattern?
Share keys and values
Right: keys and values. Here are eight query heads, each with its own query weights. Now just two key-value heads. Each one serves a group of four query heads. Every query head still asks its own question, so it still gets its own attention pattern. Only what each past token offers, its key and its value, is shared. Tap the key-value head that query head five reads from.
GQA, MQA, and the cache formula
Choose how many key-value heads to keep. Each one serves the total heads divided by that number. Keep all thirty-two, and it's ordinary multi-head attention. Keep eight: grouped-query attention. Keep just one: multi-query attention. Query head i uses group i, times key-value heads, divided by total heads, rounded down. Only the shared keys and values are cached. So in the cache formula, the head count becomes the key-value head count. For our thirty-two-layer model with eight key-value heads, how big is each token's cache?
What it buys, what it costs
Answer: one hundred twenty-eight kibibytes per token. One gibibyte per sequence, down from four. With one key-value head, multi-query: sixteen kibibytes per token, one hundred twenty-eight mebibytes per sequence. Key and value weights shrink too. Query and output weights don't, and all thirty-two attention patterns remain. Quality holds because separate key-value heads were largely redundant; trained with sharing, they learn to serve the group. About eight key-value heads: close to full quality. Just one: a small, noticeable drop.
GQA in numpy
In code, compute just the few key-value heads, then copy each one across its group. This line projects keys into two heads. The next line repeats each head twice, giving four, one per query head. From there it's ordinary attention: scores, causal mask, softmax, weighted values. Four attention patterns, five by five, and an output of five by sixteen, ready for the output projection. One of these lines produces exactly what the cache stores. Tap it.
Share keys and values, keep the queries
GQA and MQA precisely
Cache sizes: MHA vs GQA vs MQA
GQA in numpy
import numpy as np
T, d, h, h_kv, dh = 5, 16, 4, 2, 4
X = np.random.randn(T, d)
WQ, WK, WV = (np.random.randn(d, n * dh) for n in (h, h_kv, h_kv))
Q = (X @ WQ).reshape(T, h, dh)
K = (X @ WK).reshape(T, h_kv, dh).repeat(h // h_kv, axis=1) # [T,h,dh]
V = (X @ WV).reshape(T, h_kv, dh).repeat(h // h_kv, axis=1)
S = np.einsum('qhd,khd->hqk', Q, K) / np.sqrt(dh) + np.triu(np.full((T, T), -np.inf), 1)
A = np.exp(S - S.max(-1, keepdims=True)); A /= A.sum(-1, keepdims=True)
out = np.einsum('hqk,khd->qhd', A, V).reshape(T, h * dh) # then @ W_O
print(A.shape, out.shape) # (4, 5, 5) (5, 16)
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses