Pidoku

KV Cache Math

Module 12 Lesson 12.f 1h 30m Difficulty 3/5

Prerequisites 12.e

Pure arithmetic. Work every example with a calculator. These calculations are what you’ll be asked to do in an interview and what you’ll do weekly on the job.

In plain words: a model’s config file tells you how many bytes of KV cache each token costs. Multiply by how many tokens each request holds and how many requests run at once, and compare with the GPU memory left after the weights. Every example below is that one idea with different numbers. Sections 3–8 use standard GQA models to learn the method; section 9 applies it to the 2026 model designs (MLA, sliding windows, hybrid linear attention) explained in 12.g.


1. The formulas#

(1)  KV_per_token = 2 · L · n_kv_heads · head_dim · bytes_per_elem

(2)  KV_per_sequence = KV_per_token · context_length

(3)  KV_total = KV_per_sequence · batch_size

(4)  KV_budget = GPU_memory − weights − activation_headroom − framework_overhead

(5)  max_concurrency = KV_budget / KV_per_sequence

(6)  decode_bytes_per_step = weight_bytes + KV_per_token · context · batch

(7)  crossover: weight_bytes = KV_per_token · context · batch
                → context · batch = weight_bytes / KV_per_token

The 2 in (1) is for K and V. Everything else is read from config.json.

Diagram — Every term in the KV size formula#

flowchart LR
  TWO["2 - K and V"] --> X(("multiply"))
  L["layers"] --> X
  H["KV heads x head_dim"] --> X
  S["sequence length"] --> X
  B["concurrent sequences"] --> X
  D["bytes per element<br/>FP16 = 2"] --> X
  X --> R["KV cache bytes"]
  R --> C{"Fits in VRAM<br/>minus weights and overhead?"}
  C -->|"yes"| OK["Admit"]
  C -->|"no"| NO["Cut context or concurrency,<br/>quantize KV, or add GPUs"]

  class R,X memory
  class C queue
  class OK compute
  class NO warn
  class TWO,L,H,S,B,D neutral

2. Reading a config#

JSON
{
  "num_hidden_layers": 32,          →  L
  "hidden_size": 4096,              →  d
  "num_attention_heads": 32,        →  h
  "num_key_value_heads": 8,         →  n_kv_heads   (if absent, = num_attention_heads)
  "intermediate_size": 14336,       →  d_ff
  "vocab_size": 128256,             →  V
  "max_position_embeddings": 8192,  →  max context
  "torch_dtype": "bfloat16"         →  2 bytes
}

head_dim = hidden_size / num_attention_heads = 4096/32 = 128 (some configs specify head_dim explicitly — use it if present).


3. Worked example 1 — Llama-3-8B#

L=32, n_kv=8, head_dim=128, BF16

(1)  KV_per_token = 2 × 32 × 8 × 128 × 2
                  = 131,072 bytes = 128 KiB

(2)  at 8k context: 131,072 × 8,192 = 1.074 GB per sequence

Weights: 8.03e9 × 2 = 16.06 GB

On one 80 GB H100:
(4)  KV_budget = 80 − 16.06 − 2 (activations) − 2 (overhead) = 59.9 GB
(5)  max_concurrency at 8k = 59.9 / 1.074 = 55 sequences
                    at 4k  = 59.9 / 0.537 = 111
                    at 32k = 59.9 / 4.295 = 13
                    at 128k= 59.9 / 17.18 = 3

At 128k context you can serve three users on an H100. That is the entire economics of long-context serving in one line.


4. Worked example 2 — Llama-3-70B on 8×H100#

L=80, n_kv=8, head_dim=128, BF16, TP=8

(1)  KV_per_token = 2 × 80 × 8 × 128 × 2 = 327,680 bytes = 320 KiB
     Per GPU with TP=8: KV heads are split → 320/8 = 40 KiB per GPU per token

Weights: 70.6e9 × 2 = 141.2 GB total → 17.65 GB per GPU

Per GPU (80 GB):
(4)  KV_budget = 80 − 17.65 − 3 − 2 = 57.35 GB
(2)  per sequence at 8k, per GPU = 40 KiB × 8192 = 327.7 MB
(5)  max_concurrency = 57.35 GB / 0.328 GB = 175 sequences

     at 4k:   350
     at 32k:   43
     at 128k:  10

Note: with TP, both weights and KV are split across GPUs, so concurrency is computed per-GPU and the answer is the same for all of them (they hold the same sequences, different head slices).


5. Worked example 3 — the MHA penalty#

Same model shape but MHA (n_kv = 32) instead of GQA (n_kv = 8):

GQA-8:  KV_per_token = 2 × 32 × 8  × 128 × 2 = 131,072 B = 128 KiB
MHA:    KV_per_token = 2 × 32 × 32 × 128 × 2 = 524,288 B = 512 KiB

At 8k context, 80 GB H100, 16 GB weights, 60 GB budget:
  GQA-8: 60 GB / 1.074 GB = 55 sequences
  MHA:   60 GB / 4.295 GB = 13 sequences

→ GQA gives 4x the concurrency for this configuration.

For Llama-2-70B (MHA, L=80, h=64, head_dim=128):

KV_per_token = 2 × 80 × 64 × 128 × 2 = 2,621,440 B = 2.5 MiB per token
At 4k context: 10.2 GB PER SEQUENCE
On 8×A100 (640 GB) with 140 GB weights: 500 GB / 10.2 = 49 sequences

Compare to Llama-3-70B (GQA-8) in the same setup: ~390 sequences. 8x, from one architectural choice. This is why every serious model released after 2023 uses GQA or better.


6. Worked example 4 — the bandwidth crossover#

When does reading KV cost more than reading weights?

(7)  context × batch = weight_bytes / KV_per_token

Llama-3-8B:  16.06e9 / 131,072 = 122,543 token-slots
Llama-3-70B: 141.2e9 / 327,680 = 430,908 token-slots

For the 8B model:

batch 32  → crossover at context 3,830
batch 64  → crossover at context 1,915
batch 128 → crossover at context   957

At batch 128 and 4k context, KV reads are 4x the weight reads. Your decode step is dominated by KV bandwidth, and weight quantization gives you almost nothing while KV quantization gives you 2x.

Do this calculation for your workload. It tells you which optimization to invest in, and it’s frequently the opposite of what people assume.


7. Worked example 5 — decode step time#

Llama-3-8B, one H100 (3.35 TB/s), batch 32, context 4096:

(6)  bytes = 16.06 GB (weights) + 131,072 × 4,096 × 32 (KV)
           = 16.06 GB + 17.18 GB
           = 33.24 GB

     T_step = 33.24 / 3350 = 9.92 ms

     throughput = 32 / 0.00992 = 3,226 tokens/sec
     ITL per user = 9.92 ms

Real systems achieve 60-75% → expect ~2,000-2,400 tok/s and ITL ~13-16 ms.

Same model, batch 32, context 32768:

     bytes = 16.06 + 137.4 = 153.5 GB
     T_step = 45.8 ms
     throughput = 699 tok/s        ← 4.6x worse throughput at 8x context
     ITL = 45.8 ms                 ← 4.6x worse latency too

Long context hurts both throughput and latency, not just memory.


8. Worked example 6 — full capacity plan#

Requirement: serve Llama-3-70B, 2,000 concurrent users, average context 6k, p95 ITL < 60 ms.

Step 1 — KV per sequence
  KV_per_token = 320 KiB
  at 6k context = 1.966 GB per sequence

Step 2 — total KV needed
  2,000 × 1.966 GB = 3,932 GB

Step 3 — how many 8×H100 nodes?
  Per node: 640 GB − 141 GB (weights, TP=8) − 24 GB (overhead+activations) = 475 GB KV
  Nodes needed for KV: 3,932 / 475 = 8.3 → 9 nodes

Step 4 — check the latency constraint
  Per node, sequences = 475 / 1.966 = 241
  Decode bytes per step per GPU = 17.65 GB (weights) + 241 × 6144 × 40 KiB
                                = 17.65 + 59.2 = 76.9 GB
  T_step = 76.9 / 3350 = 23 ms theoretical
  With TP AllReduce overhead (~30%) and 70% efficiency: ~42 ms
  → ITL ≈ 42 ms  ✓ meets the 60 ms SLO

Step 5 — throughput check
  Per node: 241 sequences / 0.042 s = 5,738 tokens/sec
  9 nodes: 51,600 tokens/sec aggregate

Step 6 — headroom
  Add N+1 redundancy and 30% spike headroom: 9 × 1.3 + 1 = 13 nodes

ANSWER: 13 nodes of 8×H100 (104 GPUs).

Sanity check this against intuition: 2,000 users at ~24 tokens/sec each = 48,000 tokens/sec. Our 9-node capacity is 51,600. Consistent. Good.


9. Quick reference — current models#

Formula (1) covers standard attention. Current models also use three other designs (12.g explains them); each changes which bytes grow with context:

MLA (DeepSeek, Kimi):     per token = layers × (latent dim + rotary dim) × bytes   — no "2 ×", no heads
Sliding window (gpt-oss): windowed layers hold at most W tokens, whatever the context
Hybrid linear (Qwen3-Next): linear layers hold a FIXED state per sequence; only the few
                            full-attention layers grow with context

This program applies all four to real configurations (each model’s config.json on Hugging Face, checked on 11 November 2026), with a BF16 cache:

Go
// kvtable: KV cache bytes per token, and per sequence at 8k and 128k, for current open models.
// Configs from each model's config.json on Hugging Face (checked 11 November 2026). 2 bytes = BF16.
package main

import "fmt"

type model struct {
	name string
	// standard (GQA/MHA) layers that keep a full KV cache
	fullLayers, kvHeads, headDim int
	// MLA: one compressed latent (+ rotary key part) per layer instead of per-head K and V
	mlaLayers, mlaDim int
	// layers with a sliding window: they keep at most `window` tokens, whatever the context
	swLayers, window int
	// layers with a fixed-size recurrent state per sequence (linear attention), in bytes
	stateBytes float64
}

const bytesPer = 2.0 // BF16 cache

// perToken: bytes that grow with every token of context.
func (m model) perToken() float64 {
	full := 2 * float64(m.fullLayers*m.kvHeads*m.headDim) * bytesPer
	mla := float64(m.mlaLayers*m.mlaDim) * bytesPer
	return full + mla
}

// perSeq: total cache for one sequence of n tokens.
func (m model) perSeq(n int) float64 {
	sw := 0.0
	if m.swLayers > 0 {
		w := min(n, m.window)
		sw = 2 * float64(m.swLayers*m.kvHeads*m.headDim*w) * bytesPer
	}
	return m.perToken()*float64(n) + sw + m.stateBytes
}

func main() {
	models := []model{
		{name: "Llama-3.1-8B (GQA, 8 KV heads)", fullLayers: 32, kvHeads: 8, headDim: 128},
		{name: "Qwen3-8B (GQA, 8 KV heads)", fullLayers: 36, kvHeads: 8, headDim: 128},
		{name: "Llama-3.1-70B (GQA, 8 KV heads)", fullLayers: 80, kvHeads: 8, headDim: 128},
		{name: "Qwen3-235B-A22B (MoE, GQA 4)", fullLayers: 94, kvHeads: 4, headDim: 128},
		{name: "DeepSeek-V3 / Kimi-K2 (MLA)", mlaLayers: 61, mlaDim: 512 + 64},
		{name: "gpt-oss-120b (half sliding 128)", fullLayers: 18, kvHeads: 8, headDim: 64, swLayers: 18, window: 128},
		// 12 full-attention layers (every 4th of 48); 36 Gated DeltaNet layers with a
		// 32 heads x 128 x 128 state each, counted here at 2 bytes per element.
		{name: "Qwen3-Next-80B-A3B (hybrid)", fullLayers: 12, kvHeads: 2, headDim: 256, stateBytes: 36 * 32 * 128 * 128 * 2},
		{name: "Llama-2-70B (MHA, for contrast)", fullLayers: 80, kvHeads: 64, headDim: 128},
	}
	fmt.Printf("%-34s %10s %10s %10s\n", "model", "KiB/token", "8k seq", "128k seq")
	for _, m := range models {
		fmt.Printf("%-34s %10.1f %8.2fGB %8.2fGB\n", m.name, m.perToken()/1024,
			m.perSeq(8192)/1e9, m.perSeq(131072)/1e9)
	}
}

Output:

model                               KiB/token     8k seq   128k seq
Llama-3.1-8B (GQA, 8 KV heads)          128.0     1.07GB    17.18GB
Qwen3-8B (GQA, 8 KV heads)              144.0     1.21GB    19.33GB
Llama-3.1-70B (GQA, 8 KV heads)         320.0     2.68GB    42.95GB
Qwen3-235B-A22B (MoE, GQA 4)            188.0     1.58GB    25.23GB
DeepSeek-V3 / Kimi-K2 (MLA)              68.6     0.58GB     9.21GB
gpt-oss-120b (half sliding 128)          36.0     0.31GB     4.84GB
Qwen3-Next-80B-A3B (hybrid)              24.0     0.24GB     3.26GB
Llama-2-70B (MHA, for contrast)        2560.0    21.47GB   343.60GB

What to read from it:

  • Parameter count does not predict KV size. The 671B DeepSeek-V3 needs less KV per token than an 8B Llama; the 80B Qwen3-Next needs less than either. Architecture decides, not size.
  • A 100x spread separates the old MHA design from 2026 hybrids. That is why long-context serving became affordable between 2023 and 2026.
  • The hybrid’s fixed recurrent state (about 38 MB per sequence here) is paid even for a 10-token request, so for short requests a hybrid model’s cache is not smaller. It wins at long context.
  • Halve every number for an FP8 KV cache, quarter it for FP4 (13.h). DeepSeek-V3.2 adds a small per-token index cache for its sparse attention (12.g).

Treat these as estimates: engines round to blocks (12.l), and some keep the recurrent state at higher precision. Exercise B checks them against a real server.


10. Worked example 7 — reasoning traces#

Requirement: a reasoning model, Qwen3-8B, prompts of 2k tokens, answers of 12k tokens on average (mostly hidden thinking), one 80 GB H100.

Tokens per request at the end:    2k + 12k = 14k
KV at the end:                    144 KiB × 14,336 = 2.1 GB
Average over the request's life:  ~ (2k + 14k) / 2 = 8k tokens → 1.2 GB
KV budget:                        80 − 16.4 (weights) − 4 = ~59.6 GB
Concurrency (using the average):  59.6 / 1.2 ≈ 49 requests
Concurrency (worst case, all at their end): 59.6 / 2.1 ≈ 28 requests

Compare the same traffic with 500-token answers: ~2.5k tokens per request, ~160 concurrent. Turning on long reasoning cut concurrency by 3–6x, and each request now holds its slot for 24x more decode steps. Reasoning effort is a capacity setting, and 12.q builds it into a full plan.


11. Hands-on exercise#

A. Build a calculator. Write a script that takes a HuggingFace model id, downloads its config.json, and prints: parameters, weight bytes at each precision, KV per token, KV per sequence at several context lengths, and max concurrency on a GPU you specify. You will use this constantly.

B. Verify against reality. Run vLLM with --max-model-len set and read the startup log — it prints the number of KV blocks allocated. Convert to sequences and compare to your calculator’s prediction. Explain any discrepancy (hint: block granularity, and vLLM reserves some memory).

C. The crossover. For three models, compute and plot the (batch, context) crossover curve. For each, name the optimization you’d prioritize in three different traffic regimes.

D. Capacity plan. Do a full plan like section 8 for: Qwen3-32B (64 layers, 8 KV heads, head dimension 128 — check its config), 500 concurrent users, average context 16k, p95 ITL < 80 ms, on H200 (141 GB). Show every step.

E. KV quantization. Recompute example 5 with FP8 KV cache (1 byte instead of 2). What’s the throughput improvement at context 32768? At context 1024? Explain the difference.


12. Interview questions#

  1. Write the KV cache formula and explain every term.
  2. Compute KV per token for Llama-3-70B. Now for Llama-2-70B. Why the 8x difference?
  3. How many concurrent 8k-context sequences fit on one H100 running Llama-3-8B? Show your work.
  4. At what (batch, context) do KV reads exceed weight reads for an 8B model?
  5. A customer wants 128k context. What does that do to your capacity and cost?
  6. Size a cluster for 2,000 concurrent users on a 70B model. State your assumptions.
  7. Your ITL doubled when you enabled 32k context support, even at the same batch size. Why?

13. Further reading#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in