Advanced

PagedAttention & continuous batching

Paged KV allocation and iteration-level scheduling decide how many concurrent Qwen3.8-27B conversations one GPU can actually hold.

Updated

01 · Concept

Concept

You have one 80 GB accelerator and you want to serve Qwen3.8-27B to as many concurrent users as possible. The weights occupy 54 GB in bf16 (lesson 9.3 walks the full budget), and lesson 7.2 established the model’s KV bill: only the 16 full-attention layers cache keys and values, at 64 KiB per token, reaching 16 GiB per sequence at the full 262,144-token native context. The serving question is where that growing, unpredictable state should live.

The classic wrong turn is contiguous reservation at the maximum. A request could in principle grow to native context, so reserve 16 GiB up front. Lesson 9.3 owns the complete device budget: after unit conversion, weights, and runtime reserve, its bf16 example leaves a 23.3 GiB state pool. One maximum reservation fits. Your 80 GB GPU serves exactly one user, and since a typical chat uses a few thousand tokens, almost all of that reservation sits empty. Reserving the current length instead forces buffers to grow, and growth means relocation, copies, and fragmentation across dozens of differently sized allocations.

PagedAttention, introduced with vLLM in 2023, borrows the operating-system idea of virtual memory. A sequence’s logical KV positions are divided into fixed-size blocks — say 16 tokens, which at Qwen’s 64 KiB per token makes a tidy 1 MiB physical block. Physical blocks live anywhere in the cache pool; a per-sequence block table maps logical order to physical addresses, and the attention kernel follows the mapping while reading keys and values. When a sequence outgrows its last block, the allocator hands it another free block without moving anything. Waste is bounded to the unused tail of one block per sequence, not an entire maximum reservation.

Now redo the capacity example with paging. Suppose your users run long-document sessions around 32,768 tokens of context. Per sequence, KV costs 32,768×64 KiB=2 GiB32{,}768 \times 64\ \text{KiB} = 2\ \text{GiB}. Add the 144 MiB reference float32 DeltaNet matrix state from lesson 7.2 and each sequence costs 2,192 MiB. The 23,859 MiB pool from lesson 9.3 therefore holds ten such sessions, with that lesson’s runtime reserve already removed. Against the single user of the reservation scheme, that is an order of magnitude from allocation policy alone.

The hybrid architecture adds a wrinkle worth naming. Paging governs the 16 attention layers’ growing KV; the 48 DeltaNet layers’ state is fixed-size and per-sequence, so a hybrid-aware server manages two pools with different lifecycles. How vLLM handles this concretely is lesson 8.4’s territory.

Indirection also enables sharing. Parallel samples, beam candidates, or requests with an identical prompt prefix can point at the same read-only blocks; when a branch diverges, copy-on-write allocates a fresh block for the changed positions, and reference counts decide when a shared block returns to the pool.

Allocation solves where state lives; continuous batching solves when each request runs. A static batch groups requests and runs until every member finishes, so one long response leaves the other lanes idle. Continuous batching revisits membership at every iteration boundary: finished or canceled sequences leave, waiting sequences enter, active ones advance one token. The scheduler juggles free blocks, maximum batch tokens, priorities, and the prefill/decode mix — often chunking long prefills so a new arrival’s 30,000-token prompt does not stall everyone else’s next token. Because capacity is visible as countable blocks rather than vague free memory, admission control can reason in blocks and reject early with a clear error instead of failing deep into generation.

Watch the operational metrics that make this machinery legible: free and used blocks, allocation failures, tail waste, prefix hit rate, preemptions, queue delay, time to first token, and inter-token latency, sliced by request class so throughput does not hide starvation.

The durable mental model has two layers. Paged allocation turns a growing, irregular cache into fixed blocks plus a logical directory, so concurrency is set by real usage rather than pessimistic reservations. Continuous batching turns the batch into a living population that refills at every step. Together they are why the difference between one and ten concurrent Qwen sessions on the same silicon is software.

02 · Analogy

Analogy

A hotel that demands one contiguous floor for every group wastes rooms when groups grow unpredictably. A paged hotel assigns standard room blocks anywhere in the building and gives each group a directory listing its rooms in order. Continuous batching is the front desk filling newly freed rooms immediately rather than waiting for every group that arrived together to depart. The directory adds lookup work, but occupancy improves dramatically.

03 · Teach it back

Teach it back

Explain why reserving maximum-context KV buffers collapses concurrency for Qwen3.8-27B on an 80 GB GPU, how paged allocation changes the arithmetic, and what continuous batching adds.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

Reserving Qwen's full native context per request means 16 GiB of KV cache per sequence. Lesson 9.3's complete bf16 budget leaves a 23.3 GiB state pool on an 80 GB GPU, so only one maximum-context user fits. Paged allocation stores KV in fixed-size blocks assigned on demand and mapped through a per-sequence block table, so each request consumes only what it has generated: at 32,768 tokens, 2 GiB of KV plus the reference path's 144 MiB fixed DeltaNet state totals 2,192 MiB, and ten such sequences fit. Continuous batching then reschedules at token-step boundaries, admitting waiting requests as others finish instead of holding the original batch until its slowest member completes.

04 · Check your understanding

Check your understanding

01What does a sequence block table map?
Answer and explanation

Logical KV-cache blocks to physical memory blocks — The indirection lets a logical sequence remain ordered even when its cache blocks are scattered physically.

02Using lesson 7.2's 64 KiB-per-token figure, roughly how much KV cache does one 32,768-token Qwen3.8-27B sequence occupy in bf16?
Answer and explanation

About 2 GiB — 32,768 tokens times 64 KiB per token is 2,097,152 KiB, which is 2 GiB; 16 GiB corresponds to the full 262,144-token native context.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Woosuk Kwon et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention.
  2. Qwen Team (2026). Qwen3.8-27B Model Card.