Log inStart free
LESSON 29.04 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

KV-cache allocation and fragmentation

Watch six requests claim, waste and lose ranges of one GPU's KV-cache memory.


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

Go Pro · $8.25/mo
EXPLAINER · 29.04 · KV BAYSTEP 0 · R2 AT SLOT 24
01
Stage 1 of 6 · Cache layout
Each slot holds the keys and values of one token. R2's tokens fill one unbroken range from slot 24.
Explain it simply

Think of the GPU memory as one long shelf of 128 numbered slots. Each slot keeps the keys and values of one token. R2 gets one unbroken stretch that starts at slot 24. Its first 10 slots hold the system prompt, and the next 3 hold its question.

A grid of 128 slots in 4 rows. R2 holds slots 24 to 44: 10 system prompt tokens, 3 question tokens, then empty reserved slots.

cached tokensdashed: empty reserve or holefree slotwasted: never used or preempted; dashed: waitingtime cursor
kv-cache memory · 128 slots in blocks of 4requests over time (steps)0326496R1R2R3R4You24are25a26travel27guide28.29Keep30answers31short32.33Oslo34weather35?36012345678910111213141516171819R1R2R3R4R5R6step 0R2: slot(i) = 24 + i · slots 24–44kv-cache memory · 128 slots in blocks of 4requests over time (steps)0326496R1R2R3R4You24are25a26travel27guide28.29Keep30answers31short32.33Oslo34weather35?36012345678910111213141516171819R1R2R3R4R5R6step 0R2: slot(i) = 24 + i · slots 24–44
Toy: 29.C1's 6 requests, a 10-token system prompt, 128 slots
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
slot(i)=b+i\sy{slot}{data}{\text{slot}(i)} = \sy{b}{param}{b} + \sy{i}{data}{i}
slot(10)=24+10=34\text{slot}(10) = 24 + 10 = 34

Token i of a request sits i slots after its first slot.

Common mix-ups

MYTHIf enough slots are free, a new request fits.

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

Predict, then reveal
3 CHECKS · PRO
Q01With a reserve of max tokens, is any request preempted?

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