Zenaique

Compute the KV cache memory for a single request at 4096 context on Llama-2 70B (MHA) in FP16.

Predict output·Hard·4.0 · 0·~2 min·Asked atSiemensTogether AiVoyage Ai·Relevant atAnthropicNVIDIA
Attempt it
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.
TL;DR

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.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

If you only memorize one piece of arithmetic in LLM inference, make it the KV cache memory formula. It governs how many concurrent requests a GPU can hold, how far you can push context length, and why every flagship model since 2023 has shipped GQA, MQA, or MLA. The arithmetic is brutally simple, six factors multiplied together, but the consequences cascade across architectural design, kernel implementation, and serving economics.

This deep dive walks the six factors, plugs in concrete numbers for Llama-2 70B under both the textbook MHA assumption and the GQA-8 reality, traces the scaling behavior across context and batch, then connects the formula to every downstream optimization the field has invented since 2022: paged allocation, KV quantization, prefix sharing, MLA's latent reshape, and the FlashAttention family of kernels.

The goal is that after reading you can take any config.json, ballpark per-request KV memory at arbitrary context, predict batch capacity on a given GPU class, and explain why a particular architectural choice was made. That diagnostic ability is what separates engineers who can size a serving fleet from those who guess.

The six factors and what each one controls

bytes=2LHkvdhTb\text{bytes} = 2 \cdot L \cdot H_{kv} \cdot d_h \cdot T \cdot b

Drop any one factor and the estimate is off by an integer ratio that's almost always wrong by an order of magnitude.

The leading 2 counts K and V as separate tensors of identical shape. Some learners try to fold it into other factors; don't. Keep it explicit so the formula stays mechanical.

L is transformer depth. Every block stores its own K/V tensors because each attention operation needs its own representations. This is the most commonly forgotten factor in interview answers. Drop it on a 70B model and you're off by 80x.

H_kv is the number of distinct K/V projections. Under MHA it equals query head count. Under MQA it collapses to 1. Under GQA with group size G, it becomes num_query_heads / G. Under MLA (DeepSeek-V2, 2024) the formula breaks entirely.

d_h is per-head dimension, typically 64, 96, or 128. Set during architecture design and rarely changed.

T is the cached sequence length, growing one token per decode step.

b is bytes per value: 4 for fp32, 2 for fp16/bf16, 1 for int8 or fp8 (E4M3 on Hopper+), roughly 0.5 for int4 packed.

The factors compound. Halving H_kv (GQA) and halving b (int8 KV) together gives 4x reduction, not 2x. Every modern optimization stacks orthogonally on this formula.

Worked example: Llama-2 70B as textbook MHA
Reality: Llama-2 70B ships GQA-8
Scaling in context and batch: where the bottleneck binds
Cross-checking other 2026 flagships
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
python
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
SettingCache per tokenCache @ 4k contextCache @ 32k context
Llama-2 70B MHA (textbook), fp162.5 MB10.74 GB85.9 GB
Llama-2 70B real (GQA-8), fp16320 KB1.34 GB10.74 GB
Llama-2 70B GQA-8, int8 KV160 KB0.67 GB5.37 GB
Llama-2 70B MQA (hypothetical), fp1640 KB0.17 GB1.34 GB

Real products, models, and research that use this idea.

  • Llama 4 Maverick uses GQA with 8 KV heads on 64 query heads; per-request cache at 32k context drops 8x vs textbook MHA.
  • Mistral Large 3 with 8 KV heads at 64k context in fp16 produces a per-request cache larger than the model weights for batch sizes above 4.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QIf your GPU has 80 GB and the model occupies 70 GB after sharding, how many simultaneous requests can you serve at 4k context with Llama-2 70B GQA?
A

Remaining memory ≈ 10 GB. Per-request cache ≈ 1.34 GB. Naive division: ~7 requests. In practice, paged attention can squeeze more by avoiding over reservation; also prefix sharing helps if requests share a system prompt.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

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).

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • The six factors in the KV cache memory formula

  • Llama 2 70B MHA at 4k context, order of magnitude

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium