Datasets:
|
Download articles/prefill-vs-decode.md from inference-engineering-academy/llm-inference-notes: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/datasets/inference-engineering-academy/llm-inference-notes/resolve/main/articles/prefill-vs-decode.md
- Command line
-
hf download hf://datasets/inference-engineering-academy/llm-inference-notes/articles/prefill-vs-decode.md
-
curl -L -o prefill-vs-decode.md https://huggingface.co/datasets/inference-engineering-academy/llm-inference-notes/resolve/main/articles/prefill-vs-decode.md
4.19 kB
| title: "Prefill vs decode: why LLM inference is memory-bandwidth bound" | |
| tags: llm, inference, gpu, performance | |
| Ask why an LLM streams its answer at a steady ~100 tokens/s even on a GPU rated for nearly a petaFLOP, and you end up at one idea: generation has two phases with very different bottlenecks. | |
| ## Two phases | |
| **Prefill** runs the whole prompt through the model in one forward pass. Every prompt token is processed in parallel, so each layer does matrix × matrix multiplications. Prefill fills the KV cache and produces the first output token, so it drives **time to first token (TTFT)**. | |
| **Decode** then generates one token per step. Each step pushes a single new token (per sequence) through every layer and attends over the cached K/V. Decode drives **time per output token (TPOT)**, also called inter-token latency. | |
| ## Arithmetic intensity | |
| A GPU is limited either by how fast it can compute (FLOP/s) or by how fast it can read memory (bytes/s). The ratio that decides which one applies is **arithmetic intensity**: FLOPs done per byte moved. | |
| An H100 SXM offers roughly 989 TFLOP/s of dense BF16 and about 3.35 TB/s of HBM bandwidth. The break-even point (the "ridge" of the roofline) is about 989e12 / 3.35e12 ≈ **295 FLOPs per byte**. Below that, the work is memory-bound. | |
| For a matrix multiply against a weight matrix, each BF16 weight (2 bytes) gets used for 2 FLOPs (multiply + add) per token in the batch: | |
| - **Decode, batch 1:** ~2 FLOPs per 2 bytes ≈ **1 FLOP/byte**, far below 295. The GPU spends most of its time waiting for weights to arrive from HBM. | |
| - **Prefill, 2,000-token prompt:** each weight is reused for 2,000 tokens, so ≈ **2,000 FLOPs/byte**, well above the ridge. That's compute-bound. | |
| ## Back-of-envelope decode speed | |
| Llama 3.1 8B in BF16 is about 16 GB of weights. Every decode step has to read all of them at least once: | |
| ``` | |
| 16 GB / 3.35 TB/s ≈ 4.8 ms per step → ≈ 200 tokens/s upper bound (batch 1) | |
| ``` | |
| Real numbers come in lower because of KV-cache reads, kernel overheads and imperfect bandwidth use. Notice that FLOPs never showed up in that estimate. **For single-stream decode, memory bandwidth sets the speed.** | |
| ## Why batching is the main lever | |
| If 32 sequences decode together, the weights are read once per step and used 32 times. Intensity rises about 32×, so throughput rises almost linearly while per-step latency barely moves. That's why serving engines use **continuous batching**: they admit new requests into the running batch at every step instead of waiting for a whole batch to finish. | |
| There's a catch. Weights are shared across the batch, but **KV cache is not**: each sequence reads its own cache on every step. As batch size and context grow, cache reads take over and decode stays memory-bound. This is why KV-cache size (128 KiB/token for Llama 3.1 8B in BF16) matters for speed as well as capacity. | |
| ## What follows from this | |
| - **Quantizing weights speeds up decode** because there are fewer bytes to read. INT4/FP8 weights help decode more than they help prefill. | |
| - **Speculative decoding** uses spare compute: a small draft model proposes several tokens and the big model checks them in one pass, which is more work per byte read. | |
| - **Chunked prefill** splits long prompts into pieces so a big prefill doesn't stall everyone else's decode steps. | |
| - **Prefill/decode disaggregation** (as in NVIDIA Dynamo, and supported by vLLM and SGLang) runs the two phases on separate GPU pools, because they want different hardware and different batch sizes. | |
| - **Measure TTFT and TPOT separately.** A change that helps one often hurts the other. | |
| ## Summary | |
| Prefill is a big parallel matrix multiply and is compute-bound. Decode reads every weight and the whole KV cache to produce one token, so it is memory-bandwidth-bound. Almost every serving optimization (batching, quantization, paging, speculation, disaggregation) is a way of getting more useful tokens out of each byte read from memory. | |
| --- | |
| *If you'd like to work through these numbers yourself, the free Inference Engineering Foundations course at inference-engineering.xyz covers prefill, decode and the KV cache with runnable code.* | |