Continuous Batching & Speculative Decoding
In cloud AI serving, static request batching introduces catastrophic latency overheads: fast 10-token queries sit stranded in memory waiting for slow 1,000-token queries to finish, creating massive "inference bubbles" where GPUs burn energy transferring weights for half-empty batches.
This chapter details the two core algorithmic breakthroughs that solve this bottleneck: Continuous Batching (iteration-level scheduling) and Speculative Decoding (draft-verify acceleration).
Imagine a restaurant where the kitchen refuses to serve anyone at a table until every single person has finished dessert. If one guest orders a 7-course meal while others just wanted a coffee, everyone is trapped sitting at the table for two hours. That is Static Batching.
Continuous Batching is a restaurant that brings the bill to the coffee drinker immediately, cleans the chair in 10 seconds, and seats a new guest right away, keeping every seat occupied at every minute.
Speculative Decoding is a clever waiter who knows you always order coffee after dinner: they bring the cup out before you even ask. If you wanted coffee, you save 5 minutes. If you say no, they take it back and bring tea, with zero harm done.
1. The Bubble Problem in Static Batching
In traditional deep learning serving (e.g. TorchServe, Triton dynamic batching), sequences are batched at the Request Level:
Because sequence lengths follow a long-tailed Pareto distribution, padding tokens consume up to 70% of execution time.
Continuous Batching (Iteration-Level Scheduling)
Introduced in the Orca paper (OSDI 2022) and popularized by vLLM, Continuous Batching operates at the granularity of individual forward pass iterations:
Whenever a sequence emits an <EOS> (end of sentence) token, its physical memory is reclaimed, and a new waiting request's prefill or decode step is immediately scheduled in the very next iteration.
2. Speculative Decoding: Overcoming the Memory Bandwidth Bottleneck
During autoregressive decoding, generating each token requires streaming every single model parameter from high-bandwidth memory (HBM) into on-chip cache. For a 70B parameter model in FP16, this means loading 140 GB across the memory bus for every single token.
Speculative Decoding (Leviathan et al., Chen et al.) overcomes this by converting memory-bandwidth bound generation into compute-bound verification:
3. Mathematical Proof: Exact Distributional Match
A common misconception is that Speculative Decoding is an approximation technique that degrades model quality. It does not. The output probability distribution is mathematically identical to sampling directly from the target model.
The Modified Rejection Sampling Algorithm
Let be the probability distribution of the large target model, and be the probability distribution of the small draft model.
For each candidate token proposed by the draft model:
- Sample .
- If , ACCEPT .
- If , REJECT and halt verification.
- Resample the replacement token from the normalized positive residual distribution:
4. Tree Attention & Medusa Heads
While standard Speculative Decoding drafts a linear sequence of tokens, modern production architectures expand the draft space into a tree structure:
Using custom Tree Attention Masks, the target model verifies multiple candidate paths simultaneously in a single forward pass, raising the average accepted tokens per step from to over .
5. Python Simulation: Speculative Decoding Speedup Calculator
Run this live in-browser Python implementation to compute the expected speedup for any draft model and acceptance rate:
Pair architectures with matching tokenizers. Speculative decoding requires the draft model and target model to share the exact same vocabulary and tokenizer byte-mapping. If tokens do not align, candidate tokens cannot be evaluated directly in target embeddings.
Explore numerical precision, scale factors, and FP8/INT4 kernels in Quantization & Model Compression.