Log inStart free
LESSON 29.03 · 5 STAGES · 3 CONTROLS · 3 CHECKS

KV-cache memory math

Watch the KV cache grow from one token to a full GPU.


EXPLAINER · 29.03 · MEMORY BAY
01
Stage 1 of 5 · One layer
For each token, a layer stores a key and a value per KV head. In bfloat16, 2 × 8 × 128 numbers take 4 KiB.
Explain it simply

Each token leaves a key and a value in every layer, so later tokens can look back at it. One KV head holds 128 numbers, and this model has 8 KV heads. So one layer keeps 2,048 numbers per token. At 2 bytes each, that's 4 KiB.

Layer 1 of 32. A key row and a value row of 8 head cells each. 4 KiB per token.

keys, values, requestsweights, model shapedoes not fitmemory totalsfree memoryroom for one request
Toy: an 8B-class decoder (Llama 3 8B shape) on one 80 GiB GPU · activations left out
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
32K
Tokens that each request holds.
4 requests
Requests that run together.
None
Change one factor of the cache.
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Mlayer=2⋅nkv⋅dhead⋅b\sy{lb}{out}{M_{\text{layer}}} = 2 \cdot \sy{kv}{param}{n_{kv}} \cdot \sy{hd}{param}{d_{\text{head}}} \cdot \sy{b}{param}{b}
Mlayer=2⋅8⋅128⋅2 B=4 KiBM_{\text{layer}} = 2 \cdot 8 \cdot 128 \cdot 2\,\text{B} = 4\,\text{KiB}

A key and a value per KV head, each of d numbers.

Show the code ▾Hide the code ▴ · Try it yourself · Python 3
# KV-cache memory of an 8B-class decoder (Llama 3 8B shape) on one 80 GiB GPU
GiB = 2**30
layers, kv_heads, head_dim = 32, 8, 128
bytes_per_number = 2  # bfloat16
context, batch = 32768, 4
weights = 8.03e9 * 2        # 8.03 billion bfloat16 weights
hbm = 80 * GiB

per_layer = 2 * kv_heads * head_dim * bytes_per_number   # a key and a value per KV head
per_token = layers * per_layer
per_request = per_token * context
kv_total = per_request * batch
free = hbm - weights

print(per_layer, "bytes per token per layer")        # 4096
print(per_token // 1024, "KiB per token")             # 128
print(per_request / GiB, "GiB per request")           # 4.0
print(kv_total / GiB, "GiB for the batch")            # 16.0
print(round((weights + kv_total) / GiB, 2), "GiB in use")  # 30.96
print(int(free // per_request), "requests fit")       # 16

Copy it into a file and run it.

Common mix-ups

MYTHThe KV cache is small next to the model weights.

FIXAt 128K tokens, one request needs 16 GiB, more than the 15 GiB of weights. Four requests need 64 GiB.

MYTHIf the weights fit on the GPU, the full context fits.

FIXThe weights take 15 GiB of 80 GiB. One 1M-token request needs 128 GiB of cache, so it does not fit.

Predict, then reveal
0 / 3 REVEALED
Q01In the shipped model, how does one request's cache change from 32K to 128K tokens?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02The model has 32 query heads. With 32 KV heads instead of 8, how many 32K requests fit?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03With an FP8 cache, how many 128K requests fit?
1 · GUESS2 · TRY IT IN THE DIAGRAM

Takeaways

  1. KV memory is 2 × layers × KV heads × head dimension × bytes × tokens × batch.
  2. The cache grows in step with context length and batch size.
  3. At long context, the KV cache, not the weights, fills the GPU.