Datasets:
|
Download articles/continuous-batching-explained.md from inference-engineering-academy/llm-inference-notes: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/datasets/inference-engineering-academy/llm-inference-notes/resolve/main/articles/continuous-batching-explained.md
- Command line
-
hf download hf://datasets/inference-engineering-academy/llm-inference-notes/articles/continuous-batching-explained.md
-
curl -L -o continuous-batching-explained.md https://huggingface.co/datasets/inference-engineering-academy/llm-inference-notes/resolve/main/articles/continuous-batching-explained.md
3.65 kB
| # Continuous batching explained with numbers | |
| Continuous batching is the single biggest reason modern LLM servers get high throughput. Here is why, with arithmetic you can check. | |
| ## Why batch at all? | |
| During decode, each step generates one token per sequence and must read all model weights from GPU memory. Take Llama 3.1 8B in BF16: about 16 GB of weights. On an H100 SXM with about 3.35 TB/s of HBM bandwidth, one weight read takes at least | |
| 16 GB / 3,350 GB/s = **~4.8 ms**. | |
| At batch size 1, that is at most ~210 tokens/s. With 32 sequences in the batch, the same weight read serves 32 tokens, so the ceiling becomes ~6,700 tokens/s, minus the extra cost of reading each sequence's KV cache. Batching amortizes the weight read. | |
| ## The problem with static batching | |
| Static batching groups requests and runs them until **all** are finished. Suppose a batch of 4 has output lengths of 50, 100, 200 and 1,000 tokens. | |
| - The batch runs 1,000 steps. | |
| - Useful token-steps: 50 + 100 + 200 + 1,000 = 1,350. | |
| - Slots available: 4 x 1,000 = 4,000. | |
| - Utilization: 1,350 / 4,000 = **34%**. | |
| Worse, new requests wait in the queue for up to 1,000 steps (about 5 seconds at 4.8 ms/step), which wrecks time to first token. | |
| ## Continuous (iteration-level) batching | |
| Introduced by Orca (OSDI 2022), continuous batching makes scheduling decisions at every decode step: | |
| 1. Sequences that emit an end token leave immediately. | |
| 2. Waiting requests are admitted into freed slots on the next step. | |
| 3. Their prefill is run (often in chunks) alongside ongoing decodes. | |
| In the example above, as soon as the 50-token request finishes, a new request takes its slot. With a steady queue, slots stay close to 100% occupied, so throughput approaches the full batch ceiling, and queueing delay drops from "until the longest request ends" to "until any slot frees up". | |
| ## What limits the batch size: KV cache | |
| Each running sequence holds a KV cache. For Llama 3.1 8B that is 128 KiB per token (2 x 32 layers x 8 KV heads x 128 dims x 2 bytes). On an 80 GB GPU at 90% utilization, after ~15 GiB of weights and ~2 GiB of overhead, about 50 GiB remains: | |
| 50 GiB / 128 KiB = ~410,000 tokens. | |
| At an average of 2,000 tokens per sequence (prompt + output), that is ~200 concurrent sequences; at 8,192 tokens, ~50. This is why PagedAttention (allocating KV in small blocks on demand instead of reserving max length) matters: it lets the scheduler pack in sequences based on what they actually use. | |
| ## Chunked prefill | |
| A new 8,000-token prompt can take tens of milliseconds to prefill. If it runs alone, every decoding sequence stalls for that time, which shows up as inter-token latency spikes. Chunked prefill splits the prompt into pieces (for example 512 or 2,048 tokens) and mixes each chunk with ongoing decode steps, smoothing latency. | |
| ## Tuning knobs (vLLM names) | |
| - `--max-num-seqs`: upper limit on concurrent sequences. | |
| - `--max-num-batched-tokens`: tokens processed per step, which bounds prefill chunk size. | |
| - `--gpu-memory-utilization`: fraction of memory vLLM may use (KV cache gets what is left after weights). | |
| - `--max-model-len`: maximum sequence length; lowering it does not shrink per-token KV but prevents one request from needing more cache than you can fit. | |
| ## Takeaway | |
| Batching works because weights are shared across sequences; continuous batching works because it keeps the batch full. The KV cache, not compute, usually decides how full it can get. | |
| --- | |
| *If you want to go deeper, the free Foundations course at [Inference Engineering Academy](https://www.inference-engineering.xyz/) covers these topics with browser labs. Disclosure: written by the academy team.* | |