Continuous batching for LLM inference

in LLM inference & batching

Serve an LLM one call at a time and ten calls take ten times as long, while the GPU spends most of each step just reading weights.

  1. What do the terms mean?
  2. Why does one call at a time waste the GPU?
  3. What does static batching fix, and what does it miss?
  4. How does continuous batching fill the gaps?
  5. What limits how many calls fit?
  6. What does batching cost?
  7. Which mistakes are common?
  8. Summary

You serve a model behind an API, one call at a time. The GPU shows as busy, and ten calls take ten times as long. Yet most of each step goes to reading the model's weights from memory. The maths after the read uses a small part of the chip.

The running example is four calls: A needs 3 tokens, B 6, C 2 and D 4. Calls E to H arrive while they run. All numbers are my arithmetic for a 7B model in 16-bit weights on an 80 GB GPU with 2 TB/s memory (an A100-class card). I haven't measured them, and real servers add overhead.

What do the terms mean?

Term Meaning
Token A word piece. The model writes one per step
Prefill The first pass over the whole prompt
Decode step Makes one token for every call in the batch
KV cache Key-value cache: earlier tokens' keys and values, kept so they aren't recomputed
PagedAttention Stores the KV cache in small pages
vLLM An open-source LLM server

Why does one call at a time waste the GPU?

Each new token needs a read of every weight, and the read takes far longer than the maths. Reading 14 GB at 2 TB/s takes about 7 ms. The maths for one call is 2 operations per weight, 14 billion in all, and an A100 does about 312 trillion a second, so it takes under 0.1 ms.

So one read can serve many calls in the same step:

1 call:   read 14 GB + maths ×1
          ~7 ms → 1 token
32 calls: read 14 GB + maths ×32
          ~8 ms → 32 tokens

The 32-call line is an estimate. Its maths is about 1.4 ms, mostly hidden behind the read. In the figure, the accent arrow is what lets the next call in: memory freed by a finished one.

new callswait in a queue schedulerevery token step KV cache0.5 MB per token running batch1 token per call answersstream out weights14 GB, read once freed pages
Figure 1. One weight read per step serves the whole batch.

What does static batching fix, and what does it miss?

Static batching starts a group of calls together, but the group ends only when its longest call does.

One call at a time first. █ is a step where the call makes a token:

step  1   5    10   15
A     ███
B        ██████
C              ██
D                ████
→ 15 steps, 1 call per step

The same calls as one static batch. · is a slot holding a finished call:

step:  1 2 3 4 5 6
A      █ █ █ · · ·
B      █ █ █ █ █ █
C      █ █ · · · ·
D      █ █ █ █ · ·

15 steps drop to 6. But 9 of the 24 slot-steps sit idle, and E and F wait for B to finish, though a slot opened at step 3.

How does continuous batching fill the gaps?

Continuous batching picks the batch again at every step, so a freed slot goes to the next call in the queue at once. vLLM does this, as do most current inference servers.

step:  1 2 3 4 5 6
slot1  A A A E E E
slot2  B B B B B B
slot3  C C F F F G
slot4  D D D D H H

The same 6 steps carry 8 calls, with no idle slot. F takes C's slot at step 3, E takes A's at step 4, H takes D's at step 5. Each new call runs its prefill, then makes one token per step with the rest.

Scheduler GPU call arrivesprompt + max tokens wait in queuefirst come, first in ✕ free slot and KV room? join the batchat the next step yes no: keep waiting prefillread the whole prompt decode step1 token, every call ✕ call done? free KV pagesthe slot opens answer sent no: next step yes new call slot and pages free 1 2 3 3 4 5
  1. A call arrives and waits in the queue.
  2. At each token step the scheduler checks for a free slot and enough KV cache room. If there's none, the call keeps waiting.
  3. The call joins the batch. Its prompt is read in one prefill pass.
  4. Each decode step makes one token for every call in the batch.
  5. A finished call frees its KV pages, its answer is sent, and the next call in the queue can join.
Figure 2. Continuous batching between the scheduler and the GPU.

Dashed arrows are messages between the scheduler and the GPU. A call joins only if there's a free slot and memory for its KV cache. Memory is the real limit.

What limits how many calls fit?

KV cache memory. It grows with every token a call has seen. For a Llama 2 7B-sized model it's about 0.5 MB per token (2 tensors × 32 layers × 4,096 values × 2 bytes), so a 4,096-token call holds 2 GB.

GPU memory, 80 GB
┌──────────┬───────────────────────┐
│ weights  │ KV cache, all calls   │
│ 14 GB    │ about 66 GB           │
└──────────┴───────────────────────┘

If each call reserves its full 4,096 tokens up front, 66 GB fits about 33 calls. Most calls stop far short, so most of that room sits empty. The vLLM team found earlier systems used only 20% to 40% of KV cache memory for real tokens.

PagedAttention hands out KV memory in small pages as a call grows and frees them when it ends. At an average of 1,000 tokens per call, 66 GB then holds about 125 calls (my arithmetic). The vLLM paper reports 2 to 4 times the throughput of earlier systems at the same latency.

What does batching cost?

Batching gives far more tokens per second in total, for a little time per call:

  • Each token comes a bit later. A 32-call step takes about 8 ms, not 7 (estimate).
  • A new call waits up to one step to join, plus its prefill. A long prompt's prefill slows that step for the whole batch.
  • The gain flattens. Each extra call adds maths but no extra read. On this GPU the maths catches up with the read near 150 calls per step (312 trillion operations ÷ 2 TB/s; rough, ignoring KV reads). Past that, a bigger batch only makes steps longer.

For one user who needs the fastest reply, a batch of 1 is fine. For many users, I'd batch and let memory set the size.

Which mistakes are common?

Symptom Cause Fix
Throughput is flat as users grow, GPU shows "100% busy" Calls go one at a time; "busy" counts the weight reads A server with continuous batching, such as vLLM
Short calls wait behind long ones Static batching ends with its longest call Continuous batching
Out of memory at a modest batch size Each call reserves KV room for the maximum length Paged KV memory, or a lower maximum length
Each token slows once the batch is large The maths, not the read, now sets the step time Cap the batch there, or add a GPU

Summary

  • A GPU reads 14 GB of weights every step, for 1 call or 32.
  • Static batching shares that read but idles until the longest call ends.
  • Continuous batching refills each slot at once.
  • Paged KV memory fits many more calls.
  1. Measure your own model. Run vLLM's throughput benchmark at batch sizes 1, 8, 32 and 128, and plot tokens per second against time per token. This post's numbers are arithmetic; your curve is the one that counts.
  2. Read the vLLM paper, "Efficient Memory Management for Large Language Model Serving with PagedAttention" (Kwon et al., 2023).
  3. Read the Orca paper (Yu et al., OSDI 2022), which first picked the batch at every step.