Log inStart free
LESSON 29.17 · 6 STAGES · 2 CONTROLS · 2 CHECKSPRO · PREVIEW

Preemption: swap vs recompute

Watch a full KV pool force one request out, then bring it back two ways.


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.17 · KV POOLSTEP 3 · 32 OF 32 BLOCKS USED
01
Stage 1 of 6 · KV fills up
Each running request holds KV blocks. At step 3, requests 1, 2 and 4 hold all 32 blocks.
Explain it simply

Each row is one request, and time runs to the right. The grid on the right is the GPU's KV memory, cut into 32 blocks of 128 tokens. A long prompt fills many blocks, and every new token can need one more. Two long batch prompts take most of the grid.

Swimlanes for 13 requests up to step 3. The KV grid has 32 blocks: request 1 holds 16, request 2 holds 13, request 4 holds 3. No block is free.

request, prefill, KV blockoutput tokenslink, copieswait, work done again
step 30246810121416182022engine step (50 ms)1 batch2 batch3 batch4 chat5 chat6 chat7 chat8 chat9 chat10 chat11 chat12 chat13 chatlinkGPU KV memory, step 30 of 32 free11111111111111112222222222224442PCIe link · 25 GB/sCPU memoryemptythe KV pool fills16 + 13 + 3 = 32 of 32 blocks usedstep 30246810121416182022engine step (50 ms)1 batch2 batch3 batch4 chat5 chat6 chat7 chat8 chat9 chat10 chat11 chat12 chat13 chatlinkGPU KV memory, step 30 of 32 free11111111111111112222222222224442PCIe link · 25 GB/sCPU memoryemptythe KV pool fills16 + 13 + 3 = 32 of 32 blocks used
Toy: 13 requests, a 32-block KV pool, 0.5 MB of KV per token (a 7B model with 32 KV heads, fp16), 1 step = 50 ms
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
blocksi=⌈(pi+oi)/128⌉\sy{b}{data}{\text{blocks}_i} = \left\lceil (\sy{p}{data}{p_i} + \sy{o}{out}{o_i}) / 128 \right\rceil
16+13+3=32 of 32 blocks16 + 13 + 3 = 32\ \text{of}\ 32\ \text{blocks}

A request holds blocks for its prompt and the tokens it wrote.

Common mix-ups

MYTHSwapping is always cheaper than recomputing.

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

Predict, then reveal
2 CHECKS · PRO
Q01On the 25 GB/s link, does request 5 start sooner or later with recompute than with swap?

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.