Log inStart free
LESSON 29.09 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

KV-cache compression and eviction

Watch a full KV cache drop tokens or bits, and see what each costs.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 29.09 · MEMORY BAY128K TOKENS · 16 GIB A REQUEST · 4 FIT
01
Stage 1 of 6 · Full cache
Each cached token keeps its keys and values. A 128K-token prompt needs 16 GiB, so 4 requests fit.
Explain it simply

Each square in the strip is one token of the prompt. For every token, the model keeps its keys and values in all 32 layers. That's 128 KiB a token, and 16 GiB for a long prompt. Only 4 such requests fit next to the weights.

A strip of 64 token cells. Below it, a GPU memory bar: weights, then 4 requests of 16 GiB each.

tokens, attentionevicted tokenlost attention, errorweights
one request's KV cachetoken 0 · sinktopic tokenstoken 28 · needle64 tokens cachedone 80 GiB GPU, 128K-token requestsweights 14.96 GiB4 requests × 16 GiB fit80 GiB2 × 32 layers × 8 heads × 128 × 2 bytes = 128 KiB a token · × 131,072 = 16 GiBone request's KV cachetoken 0 · sinktopic tokenstoken 28 · needle64 tokens cachedone 80 GiB GPU, 128K-token requestsweights 14.96 GiB4 requests × 16 GiB fit80 GiB2 × 32 layers × 8 heads × 128 × 2 bytes = 128 KiB a token · × 131,072 = 16 GiB
Toy: one attention head, 64 + 8 tokens; memory from the 29.C5 decoder
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
M=2⋅L⋅nkv⋅d⋅b⋅T\sy{M}{data}{M} = 2 \cdot L \cdot n_{kv} \cdot d \cdot b \cdot \sy{T}{data}{T}
M=2⋅32⋅8⋅128⋅2⋅131,072=16 GiBM = 2 \cdot 32 \cdot 8 \cdot 128 \cdot 2 \cdot 131{,}072 = 16\ \text{GiB}

Cache memory grows with every cached token.

Common mix-ups

MYTHA token with little attention so far is safe to evict.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With sinks + recent, does a budget of 32 tokens lose more or less attention at step 1?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.