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
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.
Share the weight read; stop wasting slots
Decode step time for a batch
8B model on an 80 GB GPU
Static vs continuous batching, simulated
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
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.
Bytewise
All courses