Inference Trade-offs Explained

Chapter 02 · KV memory

Paged KV and preemption

Reserving each request's whole KV cache up front, or handing out blocks as it grows and evicting a request when they run out. Below, the mechanism as the simulator ran it, step by step; then what it measured, why it behaves that way, and the papers.

Check yourself ↓ three questions and what to remember, at the end

Loading the KV memory animation…

Its row of the matrix

Loading the matrix…

Reserve up front, or hand out blocks

A request's KV cache grows by one token's keys and values per layer with every token it produces, and nobody knows in advance how long its output will be. Reserving (the baseline) books the prompt plus the whole output at admission: the request can never run out, but most of the booking sits empty for most of its life (the hatched cells), and a new request waits until a whole booking fits. In the animation the slowest first token takes 15,795 ms.

Paged KV (PagedAttention (vLLM)) hands out fixed-size blocks as tokens arrive, so memory holds tokens rather than promises and more requests run at once: the slowest first token takes 246.9 ms. The price is that the blocks can run out mid-flight. Then the scheduler preempts the latest arrival: it either frees its blocks and recomputes its prompt and tokens later (9 preemptions here), or copies them to host memory over PCIe and back (27). Only the last block of each request is partly empty (the light cells).

Why it matters only where memory binds

KV memory is HBM left over after the weights, and HBM is the scarce, fast level of the memory hierarchy. When the weights leave little room (OPT-13B on a 40 GB A100, the paper's setting) reservation wastes most of it: results.md records reserved KV holding tokens in 61.8% of what it allocated at six requests per second, paged in 97.3%, and the highest rate meeting the paper's latency target rises 2.16x (4.56 to 9.88 req/s).

Block tokensKV holding tokensRecompute: norm. latencySwap: norm. latencyswap seconds
199.1% 0.677 0.625 16.01
1697.3% 0.694 0.629 13.55
6492.0% 0.742 0.687 16.74
25676.1% 0.907 0.894 21.45
Block size and preemption mode, recorded in results.md section 20 (OPT-13B, one A100-40GB, six requests per second). Bigger blocks waste more of their last block; the swap path here is PCIe at five microseconds per block. ✓ / ✗ against 16-token blocks.

Why the sweep shows almost nothing

The sweep serves Llama-3-70B on four H100s per instance, which leaves room for hundreds of thousands of tokens of KV: no workload ever fills it at its reference load. Paged KV changes goodput by +0% on chat and lowers peak KV use by -17%; its one visible effect is offline batch's ITL p99 (-82%), from a saturated run where every request arrives at once; that tail is fragile (an FP8 KV cache moves it by -86%), so do not read it as a paging effect. Read the zeros as "memory does not bind here", not "paging does not help": the lever matters with smaller GPUs, longer contexts or larger batches, as above. Halving the KV format (chapter 9) and paging are the two ways to fit more requests in the same memory.

Against the paper

vLLM's measured fraction of KV memory holding tokens (KV memory holding token states: Orca Max 20.4%, Pow2 26.8%, Oracle 38.2%, vLLM 96.3% (Fig. 2)) against the simulator's 20.3% / 50.1% / 61.8% / 97.3%: Max and paged reproduce; Pow2 and Oracle do not (the paper's baselines also lose memory to a buddy allocator's fragmentation, which is not modelled here).

What the sweep measured, lever by lever

Paged KV, preempt by recompute

Change from the baseline on H100, at capacity for goodput, cost and energy, at the reference load for latency. Run it live

Workloadgoodput$/M tokJ/tokTTFT p99TPOT p99ITL p99
Chat+0% M+0% M$+0% M+0% M+0% M+0% M
Coding agent+0% M+0% M$+0% M+0% M+0% M+0% M
Offline batch+0% M+0% M$-0% M-6% +0% M-82%
Long-context RAG+0% M+0% M$+0% M+0% M+0% M+0% M
Real-time voice+0% M+0% M$+0% M+0% M+0% M+0% M

Paged KV, preempt by swap to host

Change from the baseline on H100, at capacity for goodput, cost and energy, at the reference load for latency. Run it live

Workloadgoodput$/M tokJ/tokTTFT p99TPOT p99ITL p99
Chat+0% M+0% M$+0% M+0% M+0% M+0% M
Coding agent+0% M+0% M$+0% M+0% M+0% M+0% M
Offline batch+0% M+0% M$-0% M-6% +0% M-82%
Long-context RAG+0% M+0% M$+0% M+0% M+0% M+0% M
Real-time voice+0% M+0% M$+0% M+0% M+0% M+0% M

Caveats on these numbers

  • MPaged KV shows +0% where memory does not bind. Llama-3-70B on four GPUs per instance never runs out of KV cache at these loads, so paged allocation has nothing to win and the sweep shows no change. Paged KV matters where memory binds: the simulator's results.md section 23 shows it on OPT-13B.
  • $Prices per GPU-hour are illustrative. Cost per million tokens uses round illustrative prices (H100 $3.00, H200 $3.50, B200 $5.00 per GPU-hour), not quotes. Cost scales linearly with them, so the ranking of levers on one device does not depend on them; comparisons across devices do.

✓ better than the baseline, ✗ worse, by more than ±2%. Every lever on every metric and device: the matrix; on any two metrics: the explorer.

Check yourself

Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.

Q1What does reserving (the baseline) book for each request at admission?
Reveal the answer

Answer: Its prompt plus the whole output it might produce

The request can never run out, but most of the booking sits empty for most of its life, and a new request waits until a whole booking fits.

Reserve up front, or hand out blocks →

Pick one, then reveal.

Q2Where memory binds (the vLLM paper's setting), how many times the reserving system's rate does paged KV sustain within the latency target?
Reveal the answer

Answer: 2.16× (counted right within 5%)

Reserved KV holds tokens in only 61.8% of what it allocates, paged in 97.3%, so far more requests run at once.

Why it matters only where memory binds →

Commit to a number, then reveal.

Q3In the sweep, paged KV changes chat's goodput hardly at all. Why?
Reveal the answer

Answer: Memory does not bind: four H100s per instance leave plenty of room for KV

Goodput moves by +0%: read the zeros as memory not binding. With smaller GPUs, longer contexts or larger batches the lever matters.

Why the sweep shows almost nothing →

Pick one, then reveal.

Papers and sources