Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Llama-2 70B has 80 layers, 64 attention heads, d_head = 128. Assume MHA (not GQA, use original Vaswani style). Context length = 4096. Compute the KV cache memory in FP16 (2 bytes per value). Show the formula.
Llama-2 70B MHA at 4k context in fp16 needs ~10 GB of KV cache per request; GQA-8 collapses that to ~1.3 GB by cutting kv_heads from 64 to 8.
Think of every word the model reads as leaving two sticky notes on each shelf of a tall library, one note labeled K, one labeled V. For a Llama sized library with 80 shelves and 64 reading stations per shelf, a 4096-word document fills enough notes to fill a small filing cabinet (about 10 GB of GPU memory). Pile up a few visitors at once and the cabinet overflows. Newer designs let every 8 reading stations share one set of notes, shrinking the cabinet 8x without losing what the readers can do.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
State the formula, plug in numbers, then layer in GQA savings (8x), int8/fp8 quantization (2x), MLA's latent compression, and the implications for GPU memory budgeting at production batch sizes and context lengths.
def kv_cache_bytes(num_layers, num_kv_heads, d_head, bytes_per_value, seq_len):
"""Per-request KV cache memory in bytes."""
return 2 * num_layers * num_kv_heads * d_head * bytes_per_value * seq_len
# Llama-2 70B as MHA (textbook), 4k context, fp16
print(kv_cache_bytes(80, 64, 128, 2, 4096) / 1e9, 'GB') # ~10.74 GB
# Llama-2 70B as it actually ships (GQA, 8 KV heads), 4k context, fp16
print(kv_cache_bytes(80, 8, 128, 2, 4096) / 1e9, 'GB') # ~1.34 GB
# Same model at 32k context, GQA, fp16
print(kv_cache_bytes(80, 8, 128, 2, 32768) / 1e9, 'GB') # ~10.7 GB| Setting | Cache per token | Cache @ 4k context | Cache @ 32k context |
|---|---|---|---|
| Llama-2 70B MHA (textbook), fp16 | 2.5 MB | 10.74 GB | 85.9 GB |
| Llama-2 70B real (GQA-8), fp16 | 320 KB | 1.34 GB | 10.74 GB |
| Llama-2 70B GQA-8, int8 KV | 160 KB | 0.67 GB | 5.37 GB |
| Llama-2 70B MQA (hypothetical), fp16 | 40 KB | 0.17 GB | 1.34 GB |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Forgetting the factor of 2 for K + V, using num_query_heads instead of num_kv_heads (matters under GQA/MQA), or using fp32 bytes (4) instead of fp16/bf16 (2).
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.