Lecțiile acestui traseu sunt publicate deocamdată doar în engleză, așa că lecția de mai jos apare în engleză. Restul — module, quiz și materiale — funcționează la fel.
Serving at Scale: Continuous Batching and Paged Attention
Vei putea să:
You can explain why batching raises throughput almost for free during decode, what continuous (iteration-level) batching fixes compared with static batching, how PagedAttention stops KV-cache memory being wasted, and which knobs in vLLM, SGLang or TensorRT-LLM trade latency against throughput.
Throughput is the product you sell; latency is the product users feel
Module 1 established that a single decode step reads all the weights whether it serves one request or sixty. A server that handles requests one at a time is therefore wasting almost all of its hardware. Serving systems exist to fill that idle capacity without making individual users wait too long. The three ideas that matter are batching, scheduling at the granularity of a single token, and managing KV-cache memory like an operating system manages RAM.
Static batching and its failure mode
The obvious design groups incoming requests into a batch, runs prefill for all of them, then decodes step by step until every request in the batch has finished. The problem is that generation lengths vary wildly: one user asks for a word, another for an essay. With static batching the slot held by the short request sits idle until the longest request finishes, and new arrivals wait for the entire batch to drain. Measured GPU utilisation under static batching is often poor exactly because of this length variance.
Continuous (iteration-level) batching
The Orca paper from Seoul National University proposed scheduling at the level of iterations rather than requests: after each decode step, the scheduler checks which requests have finished, removes them, and admits waiting requests into the freed slots immediately. A newly admitted request needs a prefill pass; Orca handles the mix with selective batching — the parts of the model that do not care about sequence length (the linear layers) are batched across all requests, while attention is run per request. The Anyscale write-up "How Continuous Batching Enables 23x Throughput" shows the measured effect on an open model: throughput jumps because slots never idle, and tail latency improves too because requests stop waiting behind unrelated long generations.
Today every serious server (vLLM, NVIDIA TensorRT-LLM, SGLang, and Hugging Face TGI, which entered maintenance mode and was archived in March 2026) does continuous batching by default. The remaining differences are in how they schedule prefill against decode: a big incoming prompt can stall decode for everyone in the batch, so servers chunk prefill into pieces interleaved with decode steps, and some deployments separate prefill and decode onto different machines entirely.
PagedAttention: stop reserving memory you will not use
Continuous batching exposes a second problem. A request's KV cache grows as it generates, and nobody knows in advance how long the output will be. Early systems reserved a contiguous block for the maximum possible length, so most of the reserved memory was never used, and fragmentation between differently sized reservations wasted more. Fewer requests fit in memory, so the batch was smaller than the hardware could handle.
The vLLM paper by Kwon et al. borrowed virtual memory from operating systems. PagedAttention stores each request's KV cache in fixed-size blocks (say 16 tokens each) that can live anywhere in GPU memory, with a per-request block table mapping logical positions to physical blocks. Blocks are allocated on demand as tokens are generated, so waste is bounded by one partial block per request. Because blocks are referenced through a table, two requests that share a prefix (the same system prompt, or parallel samples of one prompt) can point at the same physical blocks with copy-on-write — the foundation of prefix caching in module 7. The paper reports large throughput gains at equal latency compared with the systems of the time, almost entirely from fitting more requests in memory.
The knobs you will actually turn
A worked example of the trade-off. Suppose a decode step costs 25 ms at batch size 1 and 40 ms at batch size 32 on a given GPU (the step gets slower as arithmetic starts to matter, but far less than 32×).
- At batch 1: 40 tokens/s per user, 40 tokens/s total.
- At batch 32: 25 tokens/s per user, 800 tokens/s total.
- Per-user speed dropped 37%; total throughput rose 20×. Cost per token fell by the same factor.
Move the batch size and watch both numbers, then compare static and continuous batching on a queue with one long answer in it:
Laborator
Batching: viteza unui utilizator față de producția serverului
- Un pas de decodare
- 40 ms
- Fiecare utilizator vede
- 25 tokeni/s
- Serverul produce
- 800 tokeni/s
- Fluxul unui utilizator este mai lent cu
- 37.5%
- Costul pe token scade de
- 20×
O coadă cu lungimi amestecate
Opt cereri sosesc împreună. Fiecare număr arată câți tokeni cere răspunsul. 40, 8, 200, 12, 60, 16, 24, 120
Batching static
- Așteptarea medie
- 4.23 s
- Ultimul răspuns gata
- 8.46 s
- Pași de slot ținuți de o cerere terminată
- 800
Batching continuu
- Așteptarea medie
- 1.85 s
- Ultimul răspuns gata
- 5.14 s
- Pași de slot ținuți de o cerere terminată
- 0
Încearcă o întrebare
Patru cereri sosesc împreună și au nevoie de 2, 5, 10 și 10 tokeni. Serverul are două sloturi și fiecare pas de decodare durează 25 ms. Cu batching static, luate în această ordine, care este timpul mediu de finalizare în milisecunde?
Numerele se schimbă de fiecare dată. Răspunsul este calculat de laborator.
Rulează în browserul tău. Nimic nu este trimis sau salvat.
Whether that trade is right depends on the product. The main knobs are the maximum number of concurrent sequences (vLLM's max-num-seqs, SGLang's max-running-requests), the token budget per scheduler step, the prefill chunk size, and the fraction of GPU memory handed to the KV cache. More concurrency lowers cost and raises per-user latency; a bigger KV-cache allocation allows more concurrency but leaves less headroom for activations. Tensor parallelism across GPUs reduces per-step time by splitting the weight reads, at the price of interconnect traffic every layer.
What is still unsettled
How best to co-schedule prefill and decode is an open design question: chunked prefill, priority queues and disaggregated prefill/decode each win in different traffic mixes. Fairness between users under overload, and preemption policies when memory runs out (swap the KV cache to CPU, or recompute it), are handled differently by each server and are rarely benchmarked honestly. Expect the defaults to be tuned for throughput benchmarks rather than your latency goals.
What to remember
- Static batching wastes slots on finished requests; continuous batching re-fills slots after every token step and raises throughput and improves tail latency together.
- PagedAttention allocates KV cache in small blocks via a block table, cutting waste to one partial block per request and enabling shared prefixes.
- Bigger batches cut cost per token dramatically while slowing each user modestly; the concurrency limit is the main dial between the two.
- Prefill can starve decode; chunking or separating the phases is how servers keep inter-token latency stable.
Materiale selectate
1.9hMunca de profunzime se întâmplă aici — lecția este briefingul tău.
- Orca: A Distributed Serving System for Transformer-Based Generative Models (Yu et al., OSDI 2022)Lucrare· 45 min
- Efficient Memory Management for Large Language Model Serving with PagedAttention (Kwon et al., the vLLM paper)Lucrare· 45 min
- How Continuous Batching Enables 23x Throughput in LLM Inference (Anyscale)Tutorial· 25 min
În direct pe aiqbench
Subiectul acestei lecții în restul site-ului, astăzi.
În știri, ultimele 30 de zile
- GPU decoder keeps pace with a superconducting qubit clock on a continuous stream, with NVIDIA NVQLink interconnectAlice & Bob ·
- DeepSeek Opens Source Production-Level Ascend Inference Code36Kr ·
- NVIDIA and Cerebras Partner to Accelerate AI Agent Inference with Two-Stage Compute Architecture36Kr ·
Mergi mai departe
- Care model merită?Preț, viteză și hardware alături de capabilitate.
- Întreabă AI despre această lecțieDeschide Întreabă AI cu o întrebare despre această lecție, cu răspuns din arhiva de știri, cu surse.
- Skill-uri de agent pentru această muncăSkill-uri gata făcute pentru agenți AI de programare, verificate și evaluate.
Testează-ți cunoștințele
5 întrebări în 100 de secunde, pe o pagină separată. Citește mai întâi tot ce este mai sus.
Testează-ți cunoștințele →Misiunea modulului
35–45 minSimulate a serving queue on paper or in a 40-line script: eight requests arrive at once with output lengths 10, 20, 30, …, 80 tokens, each decode step takes 20 ms regardless of batch size (up to 8), and the server holds at most 4 requests at a time. Compute total wall-clock time and average per-request completion time under (a) static batching in groups of 4 and (b) continuous batching that admits a new request the moment one finishes. Report both tables and the speed-up.
Se încarcă lucrarea ta…