Continuous Batching & Iteration Scheduling
Traditional deep learning batching schemes were designed for fixed-size vision or classification inputs: a batch of $B$ requests enters together, executes synchronously through all layers, and exits together.
For autoregressive large language models, this static approach creates severe system inefficiency:
- Heterogeneous Input Lengths: One prompt may contain 20 tokens; another may contain 2,000.
- Dynamic Generation Lengths: One request finishes after 10 output tokens; another generates 800 tokens.
If requests are bundled into a static batch, the fast requests remain trapped, waiting idle and consuming GPU memory while the longest request completes.
1. Static Batching vs. Iteration-Level Continuous Batching
The Orca architecture (Yu et al., OSDI 2022) replaced request-level batching with Iteration-Level Scheduling (commonly called Continuous Batching or In-Flight Batching):
text
The Iteration Cycle
At every single token generation step:
- Check if any running sequences generated an
EOS(end of sequence) token or hit maximum token limits. If so, eject them and release their KV-cache blocks. - Check the waiting queue for newly arrived requests.
- If sufficient free KV-cache blocks exist, admit new requests into the batch.
- Execute one forward pass across all active sequences simultaneously.
2. Prefill vs. Decode Contention & Chunked Prefill
While continuous batching dramatically improves throughput, mixing newly arrived prompts (Prefill) with active token generation (Decode) introduces a new problem: Latency Spikes.
Remember from the Roofline model:
- Prefill processes thousands of tokens and saturates the GPU's compute cores for hundreds of milliseconds.
- Decode needs quick, predictable steps (e.g., 20–30 ms) to preserve low Inter-Token Latency (ITL) for streaming users.
If a new 4,000-token prompt is admitted, all ongoing decode requests in that batch experience a sudden 200 ms stall!
The Solution: Chunked Prefill (Sarathi-Serve)
Rather than executing an entire prompt in one massive pass, Chunked Prefill splits long prompts into fixed token chunks (e.g., 512 tokens):
text
By co-scheduling compute-heavy prefill chunks with bandwidth-heavy decode tokens:
- Decode tokens "piggyback" on the compute-bound prefill step without degrading user-perceived ITL.
- GPU compute and memory bandwidth are saturated concurrently.
3. Memory Pressure & Preemption Strategies
What happens during a sudden surge in traffic when GPU memory is 100% full, but ongoing sequences need more KV-cache blocks to keep generating?
The scheduler must execute a Preemption Policy:
- Swapping (Offloading to CPU RAM):
- The scheduler transfers the KV blocks of the lowest-priority sequence across PCIe into host CPU RAM.
- When memory frees up, the blocks are transferred back to GPU HBM, and generation resumes.
- Recompute (Drop and Re-evaluate):
- The engine drops the sequence's KV cache entirely.
- When memory becomes available, the sequence is treated as a prompt re-evaluation up to its current position, which is fast thanks to compute-bound parallel prefill.
In modern servers with high-speed PCIe Gen 5 and fast GPU cores, Recompute is often preferred over swapping because recomputing a sequence can be faster than waiting on host memory bus transfers!