Figure: Grouped Query Attention explained, with the head diagrams drawn illustratively at 4 query heads. Multi-Head Attention gives every query head its own key and value head, 4 KV heads for 4 Q heads; Grouped Query Attention shares one KV head per group, 2 KV heads in a 2 to 1 grouping; Multi-Query Attention shares a single KV head across all query heads. In the worked flow, W_Q, W_K and W_V project the input hidden states into Q0 to Q3 with n_heads = 4 and K0, K1 and V0, V1 with n_kv = 2, so group 0 is Q0 and Q1 attending to K0 and V0 and group 1 is Q2 and Q3 attending to K1 and V1, before the heads are concatenated. Cache size is 2 x n_layers x n_kv_heads x seq_len x head_dim x dtype_size. For the canonical Llama-2-70B, MHA with 64 KV heads costs 32 KB per layer per token, GQA with 8 KV heads costs 4 KB — an 8 times reduction and what the model actually uses — and MQA with 1 KV head costs 512 B; 2 x 80 layers x 8 KV heads x 128 x 2 B = 327,680 B = 320 KiB per token, times 131,072 = 40 GiB = 43 GB at 128K context. All values are an analytical model, not measured.
Grouped Query Attention
Balancing KV-cache memory efficiency with model quality — diagram is illustrative (4 Q heads shown)
n_heads = 4
n_kv = 2
n_kv = 2
Group 1: Q₂,Q₃ → K₁,V₁
2 × n_layers × n_kv_heads × seq_len × head_dim × dtype_size
327,680 B = 320 KiB per token
→ × 131,072 = 40 GiB = 43 GB at 128 K context
Analytical model — cache sizes are derived from the model configuration, not measured. Canonical numbers v4.0.