All courses

Batching, continuous batching and paged KV

Explain how batching many requests amortizes weight reads during decode. Explain how continuous batching and paged KV memory raise GPU utilization in serving.

Builds on: Prefill vs decode: compute- vs memory-bound

▶ Watch this lesson Start the whole course

What the animation says

One token, one huge read

Every new token, the GPU streams all sixteen gigabytes of an eight-billion-parameter model out of memory. For each weight it does only about two arithmetic operations, then moves on. At three point three five terabytes per second, that read takes about five milliseconds: roughly two hundred tokens per second. Meanwhile the arithmetic units could handle hundreds of times more work. They sit nearly idle. Now stack thirty-two chats, each with a thousand tokens of context, into one step. What happens to the step time?

Batching shares the read

About ten milliseconds. Here's why. The current token of all thirty-two chats is stacked into one input. Each weight is read once and used for every row. But each chat's key-value cache is its own: half a gigabyte each, read separately. Sixteen gigabytes of weights plus sixteen of caches: under ten milliseconds. Throughput jumps sixteenfold, to about thirty-three hundred tokens a second. Sharing keeps paying until the arithmetic catches up, near three hundred requests on this GPU. Going from one chat to thirty-two: tap the two things that grow thirty-two times.

Keep every slot busy

Batching only pays if the slots stay full. Here are eight slots and sixty-four requests of random lengths. Static batching runs a group of eight until its longest request finishes. Short ones end early, and their slots sit empty. Continuous batching checks after every step: a finished request leaves, and a waiting one takes its slot. This loop is the whole idea: always refill the slot that frees up first. Simulated, static keeps slots busy about sixty percent of the time. Continuous reaches about ninety. So why not one hundred percent?

Paged KV: memory on demand

It's the ending: the queue is empty, so slots drain one by one. The other limit is memory. Eighty gigabytes, minus sixteen for weights, leaves sixty-four for caches. Reserve room for four thousand tokens per request, two gigabytes each, and only thirty-two requests fit. But most chats use about a thousand tokens. The rest of each reservation stays empty. Paged KV hands out small blocks only as a cache grows. At half a gigabyte each, about a hundred twenty-eight requests fit.

Remember

Batching shares each step's weight read, so throughput grows almost with the batch size. That stops when the arithmetic catches up, or when every chat's own cache reads dominate. Continuous batching keeps every slot busy, step by step. Paged KV stops reserving memory for tokens that never arrive, so more requests fit in the batch.

Why can one GPU serve 100 users at once?

In decode, each new token forces the GPU to read every weight from memory but do only about 2 FLOPs per parameter, so its arithmetic units sit mostly idle. Real servers handle hundreds of chats at once on the same hardware. **How do they turn that idle compute into tokens, and what stops them from simply adding more requests?**

Share the weight read; stop wasting slots

Stack the current token of $B$ requests into one `[B, 1, d]` input. Each weight is read once and used for all $B$ rows, so weight traffic is shared while FLOPs grow $B$-fold. Each request's KV cache is its own, so it is read separately. **Continuous batching:** after every decode step, drop finished requests and admit waiting ones, so no slot idles. **Paged KV:** store caches in small blocks, allocated only as they grow. Nothing is reserved up front for the maximum length.

Decode step time for a batch

While memory-bound, one decode step for $B$ requests takes $$t_{\text{step}} \approx \frac{2N + \sum_{b=1}^{B}\text{KV}_b}{\text{bandwidth}}$$ $2N$: weight bytes (2 bytes/parameter; $N$ ≈ all parameters here), read once per step. $\text{KV}_b$: request $b$'s cache, $2\cdot L\cdot d\cdot 2$ bytes per token (keys and values, every layer; assumes $h_{kv}=h$). Throughput $=B/t_{\text{step}}$ tokens/s. Weight FLOPs per weight byte $=2NB/2N=B$. So the weight matmuls stay memory-bound until $B\approx$ peak FLOP/s ÷ bandwidth (≈1000 TFLOP/s ÷ 3.35 TB/s ≈ 300). KV reads are never shared, so batching does not amortize them.

8B model on an 80 GB GPU

$N=8\text{B}$, $L=32$, $d=4096$, bandwidth 3.35 TB/s. Weights: 16 GB. KV per token: $2\cdot32\cdot4096\cdot2\approx0.5$ MB. - $B=1$, 1000-token context: $(16+0.5)/3350$ s ≈ 4.9 ms → ~200 tok/s. - $B=32$, each with a 1000-token context: $(16+16)/3350$ ≈ 9.6 ms → ~3300 tok/s (16×). Memory left for KV: 64 GB. Reserving 4096 tokens (2 GB) per request fits 32. Paging blocks for the ~1000 tokens actually used (0.5 GB) fits ~128.

Static vs continuous batching, simulated

64 requests with random output lengths, 8 slots. Static batching runs each group of 8 until its longest request ends. Continuous batching refills a slot the moment it frees; the newcomer's prefill joins that step, ignored here. The last two numbers are slot utilization. Expect static near 0.6 and continuous around 0.9. Continuous falls short of 1 only because slots drain at the end, when no requests are left to admit.
import numpy as np
rng = np.random.default_rng(0)
lens = rng.integers(10, 500, size=64)   # tokens each request generates
B = 8                                    # decode slots
static = sum(lens[i:i+B].max() for i in range(0, 64, B))
slots = np.zeros(B)                      # step when each slot frees up
for n in lens:                           # refill earliest-free slot
    i = slots.argmin(); slots[i] += n
cont = slots.max()
useful = lens.sum()
print(static, cont, useful/(B*static), useful/(B*cont))

Remember

Batching shares each decode step's weight read across $B$ requests, so throughput grows almost $B$-fold until compute or the per-request KV reads take over. Continuous batching keeps every slot busy step by step. Paged KV stops memory from being reserved for tokens that never arrive, so more requests fit in a batch.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.