Unit 3 of 11 · about 4 hours · derive
Fragmentation, page waste, capacity and KV compression
The KV bytes per token result is already known. The new question is how many physical blocks remain usable after tails, retained prefixes and allocator reserve consume the pool.
You'll be able to
- Derive logical payload, allocated slots and page waste for a length distribution.
- Account for reserve and shared physical blocks when predicting admission capacity.
- Distinguish exact storage management from lossy KV quantization or token removal.
Covers
Read
In this order, only the parts listed
0 of 2 done
- PagedAttention: the three kinds of wasteEfficient Memory Management for Large Language Model Serving with PagedAttention
arXiv:2309.06180v1, Section 3.1 with Figure 3; Sections 4.2 to 4.3 for on-demand blocks; Section 7.2 for block-size sensitivity
- Module 06: KV compression and qualityLLM Inference: From scratch, one token at a time - Modules 02 and 06
KV cache quantization, and why K and V want different treatment, then the next concept, Measuring quality honestly
Read for orientation only. Skip the Code lab and its expected concurrency figures: its paged append mutates length before capacity failure, and the driver ignores that failure. Do not use its utilization as evidence. Treat outlier patterns and quality costs as workload-dependent.
Focus on
- For independent dense sequences of lengths , page size and payload bytes per token, allocated bytes are ; tail waste is . This excludes metadata and padding.
- A nonempty private sequence wastes at most slots in its tail. Mean waste is only if length residues are uniform; block-aligned lengths waste none.
- With sharing, count the union of physical blocks, not the sum of request-table lengths. Partition the pool into pinned, reclaimable cached and uncached free blocks; reserve reduces admission headroom.
- Fixed-size pages avoid external fragmentation within one homogeneous pool. Quantization adds scales, packing and possible padding; dropping attended tokens changes the computation even at unchanged dtype.
Common mistakes · 3
- Calling cached, evictable memory either permanently unavailable or simultaneously free and cached. Use disjoint accounting categories.
- Assuming half a page of waste for every distribution. The length residues determine the mean.
- Equating nominal bit width with allocated bytes or safe quality. Metadata and alignment affect capacity; attention and task checks govern quality.
Try it yourself
Closed-book, before checking anything
[1, 4, 5, 8], block size 4, 64 payload bytes per token, and an eight-block pool with one block reserved. The requests use six blocks, 24 slots and 1536 bytes for 18 tokens of payload; tail waste is six slots or 384 bytes. One additional block is admissible after reserve. Then explain how sharing would change the physical-block count, not the per-token payload formula.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.What tail waste does an 8-token sequence have with block size 4?Answer
None. Both pages are full; a full final page is not an extra empty page.
2.Two requests reference the same two prefix pages. How many physical pages do those references consume?Answer
Two, not four. The references still need separate table entries and ownership accounting.
3.Does exact paging justify removing middle-context tokens to fit memory?Answer
No. Paging changes addresses without dropping valid keys or values. Removing attended tokens changes attention and needs a separate quality contract.
You're done when
Your derived physical-byte and waste totals match the allocator ledger for aligned, partial and shared cases, with reserve and metadata stated separately.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 12 more in the section
- Efficient Memory Management for Large Language Model Serving with PagedAttentionvLLM Project · primary
Read the memory-waste motivation, block-table design, sharing and copy-on-write sections, then reproduce the logical-to-physical translation on paper. Separate the paper's reported workloads from results on your engine and hardware.
- LLM Inference: From scratch, one token at a time - Modules 02 and 06Tushar Amrit · support
Use The KV Cache for fragmentation and reuse and Memory Optimization for paging, prefix sharing and KV compression. Work the checkpoints before the matching units.
Also in this section
- SGLang: Efficient Execution of Structured Language Model ProgramsSGLang Project · required
Study RadixAttention, longest-prefix matching, eviction and cache-aware scheduling. Compare its sequence-level radix index with vLLM's block-level paging instead of treating them as competing names for one mechanism.
- Hands-On LLM Serving and Optimization - Prefix Caching and Advanced KV CachingChi Wang and Peiheng Hu (O'Reilly) · required
Read "Prefix Caching" in Chapter 6 (RadixAttention, use cases, best practices, scaling the prefix cache) and "Advanced KV Caching" in Chapter 7 (long context, cost and latency, LMCache). Separate exact-reuse mechanisms from capacity extensions such as offload.
- AI Systems Performance Engineering - Chapter 18, tuning KV-cache utilizationChris Fregly (O'Reilly) · support
Read "Tuning KV Cache Utilization and Management" in Chapter 18 and "Prefix Caching" in Chapter 16. Leave the disaggregated pool and transfer sections for INF-7.
- Nano-vLLM - Block Manager and Sequence LifecycleGeeeekExplorer · required
Trace nanovllm/engine/block_manager.py, sequence.py, scheduler.py and attention.py. Draw allocate, append, share, preempt and free transitions, then implement randomized allocator tests in your own project before touching production-engine code.
- Mini-SGLang - Radix Cache and Cache TestsSGLang Project · required
Read docs/structures.md, the cache allocation code and tests/core/test_cache_allocate.py. Compare exact prefix identity and lifecycle with Nano-vLLM, then add tenant and version fields to your own prefix index.
- vLLM Automatic Prefix Caching DesignvLLM Project · required
Check block hashing, extra hash keys (multimodal inputs, LoRA, cache salt), isolation and eviction against v0.31.0, the version pinned here; cache-key fields change between releases.
- SGLang documentation - Radix cache eviction and HiCacheSGLang Project · support
Use the radix eviction policy and session radix cache pages to compare eviction choices with vLLM, and the HiCache design page for a GPU, host and storage hierarchy.
- AI Performance Engineering - KV Cache SystemsWafer · reference
A curated index. Use Section 4 only to find a primary KV-cache source the other entries lack.
- 100 Days of LLM Inference - Days 5, 9, 10 and 16Elizabeth Thomas · support
Use the four notebooks for KV arithmetic, paged allocation, radix reuse and prompt-layout experiments. Re-run conclusions against your own property tests and workload because these notebooks are a supplementary learning journal.
- TensorTonic Language Modeling from Scratch - Find Reusable Prefixes in a KV TrieTensorTonic · support
Solve after the prefix-index unit and add the cases its public tests leave out to your own property tests.
- LLM Inference Systems Laboratory - KV StateRoadmap-Final · support
Implement the paged allocator and prefix index with differential, randomized, cancellation, collision, fork and pressure tests. Retain one reuse workload that improves hit rate but not goodput.
- vLLM 0.31.0 - cache specifications and connector lifecyclevLLM Project · reference
Read the per-layer attention, MLA and Mamba specifications for hybrid state contracts; read the V1 connector metadata, load synchronization and deferred-free methods for transfer ownership. These fill the pool's hybrid-state and connector-contract gaps, without requiring distributed execution.