Module 7 · AI infrastructureLesson 76 of 886 minAdvanced
Batching
A GPU can generate the next token for many requests in the same pass for almost the same cost as one. Batching shares that pass.
Compare "One at a time" with "Static batch" and watch throughput. Then notice the idle slots in a static batch, where short answers wait for the longest one.
Recall from the GPU lesson: to generate one token, the GPU reads all the model's weights from memory. For one user, the arithmetic units sit mostly idle while waiting on memory.
Batching puts several requests into the same forward pass. The weights are read once and used for every request in the batch. Up to a point, a batch of 8 costs barely more time than a batch of 1, producing about 8 times the tokens.
Static batching
The simplest approach:
- Wait until you have N requests (or a timeout passes).
- Run them together until every one has finished.
- Start the next batch.
Two problems:
- Waiting to fill: early requests wait for the batch to form.
- Padding waste: answers have different lengths. A 10-token answer finishes early, but its slot sits idle until the 500-token answer next to it finishes. New requests can't join mid-batch.
That works fine for offline jobs (summarising a million documents overnight), where throughput is all that matters.
The trade-off
- Bigger batches: higher throughput, lower cost per token.
- But: more memory for KV caches, and potentially more waiting for each user.
Interactive services need a smarter scheduler, which is continuous batching, next.