Loading…
Continuous batching from first principles
Hugging FaceRémi Ouazan Reboul, Arthur Zucker, Luc Georges
Summary
Large language model serving requires running expensive next-token generation across multiple concurrent user requests. Traditional batching approaches introduce severe padding inefficiencies when mixing variable-length prompts and different generation phases, especially under static shape constraints like CUDA graphs. Continuous batching resolves these inefficiencies by combining key-value caching, chunked prefill, ragged batching, and dynamic request scheduling. Ragged batching eliminates the traditional batch axis by concatenating token sequences into a single tensor and using boolean attention masks to isolate independent sequences. By dynamically removing completed prompts and packing decoding tokens alongside chunked prefill tokens up to a hardware memory budget, serving systems maintain high hardware utilization and throughput.
Context
Serving large language models concurrently is computationally expensive because generation predicts one token at a time across billions of parameters. Traditional sequence batching introduces severe padding waste when combining varying prompt lengths or mixing decoding and prefill phases, with padding overhead scaling quadratically with batch size and prompt length.
Approach / What changed
The author explains continuous batching by combining key-value caching, chunked prefill, ragged batching, and dynamic scheduling. Ragged batching removes the batch dimension by concatenating sequences and enforcing boundaries via boolean attention masks, while dynamic scheduling immediately replaces completed sequences with chunked prefill tokens to hit GPU token budgets.
Takeaways
- Ragged batching removes the batch dimension by concatenating sequences into a single tensor, using boolean attention masks to prevent cross-sequence token interactions and eliminate padding waste.
- Introducing an n-token prompt into a batch of B decoding sequences requires (n-1)(B-1) padding tokens under standard batching, causing padding overhead to scale quadratically with batch size and prompt length.
- Continuous batching maximizes GPU throughput by allocating decoding tokens first, filling remaining batch token capacity with chunked prefill inputs, and dynamically swapping out finished prompts.