The Inference Engine
Serving at scale - 11 chapters
Explore the infrastructure layer that makes LLMs fast and efficient: batching, KV caching, PagedAttention, FlashAttention, speculative decoding, and production serving.
Prefill vs Decode
Inference has two phases. Prefill processes prompt tokens together; decode generates tokens autoregressively. Their bottlenecks depend on sequence length, batch size, model, and hardware: decode is often bandwidth-bound at small batches, while large prefills are compute-intensive.
In prefill, the model processes the prompt in one or more forward passes and builds the initial KV cache. In decode, each step computes the next token and reuses cached keys and values; the new query attends to the cached prefix. At low batch sizes, reading weights and KV state often limits decode throughput, while batching can improve weight reuse and shift the bottleneck. Some serving systems separate prefill and decode onto different worker pools, but transfer overhead and workload shape determine whether that helps. vLLM overview: vLLM online serving docs — disaggregated prefill (experimental): disaggregated prefill docs
The GPU is a supercomputer that spends most of decode waiting for memory reads. This is why memory bandwidth (GB/s) matters more than FLOPS for token generation, and why batching multiple requests together helps - it amortizes the memory reads.
Now try it yourself