Log inStart free
LESSON 29.C1 · 6 STAGES · 2 CONTROLS · 2 CHECKSPRO · PREVIEW

KV-cache management end to end

Five cache managers serve six chats that share one system prompt.


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

Go Pro · $8.25/mo
EXPLAINER · 29.C1 · KV CACHESTEP 0 · 11 OF 32 BLOCKS IN USE · WASTE 6
01
Stage 1 of 6 · Allocate
R1 writes the system prompt into blocks 0–2. R2–R4 find blocks 0 and 1 by hash: 11 blocks in all.
Explain it simply

Four chats arrive at step 0. All of them start with the same 10 words, the system prompt. Look at blocks 0 and 1: four requests read them, so they are stored only once.

GPU memory at step 0 with the Offload manager. R1 writes the system prompt into blocks 0–2. R2–R4 find blocks 0 and 1 by hash: 11 blocks in all.

tokens (system prompt solid)booked empty, cached or freedwaste, evicted, computed; dashed: queuedcopied backstep
R1R2R3R4R5R60510152025303540step 0081624×4×4R1R1R2R2R3R3R3R4R4→ 0→ 1→ 4→ 5in use11cached, free0empty21evicted so far0preemptions so far0GPU memory · 32 blocks of 4 slots · Offload (29.08)CPU memory · 8 blocksR2's tokens in the cache at step 013 tokens ÷ 4, rounded up = 4 blockschats over time (step = one token per running request)GPU blocks at step 00 of 8 blocks hold evicted tokensR1R2R3R4R5R60510152025303540step 0081624×4×4R1R1R2R2R3R3R3R4R4→ 0→ 1→ 4→ 5in use11cached, free0empty21evicted so far0preemptions so far0GPU memory · 32 blocks of 4 slots · Offload (29.08)CPU memory · 8 blocksR2's tokens in the cache at step 013 tokens ÷ 4, rounded up = 4 blockschats over time (step = one token per running request)GPU blocks at step 00 of 8 blocks hold evicted tokens
Toy: R1–R6 with a 10-token system prompt, 4-slot blocks, one follow-up each
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
blocks=⌈tokens/4⌉\sy{blk}{param}{\text{blocks}} = \left\lceil \sy{tok}{data}{\text{tokens}} / 4 \right\rceil
R2: ⌈13/4⌉=4 blocks\text{R2: } \lceil 13 / 4 \rceil = 4 \text{ blocks}

A request takes one block for every 4 tokens.

Common mix-ups

MYTHA prefix cache always computes less than one shared prompt.

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

Predict, then reveal
2 CHECKS · PRO
Q01With 24 blocks, does paging alone stop preemptions?

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

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 2 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.