Unit 2 of 11 · about 4 hours · explain, derive
Logical tokens, paged storage, block size and block tables
Variable-length, paged and decode attention supplied the kernel mapping. Here the same mapping becomes an allocator contract that must survive growth, reuse and physical-page permutation.
You'll be able to
- Translate logical token positions through a sequence block table into physical KV slots.
- Explain how a contiguous physical pool supports noncontiguous per-request storage.
- Defend block size using tail waste, table overhead and reuse granularity.
Covers
Read
In this order, only the parts listed
0 of 2 done
- PagedAttention: storage and translationEfficient Memory Management for Large Language Model Serving with PagedAttention
arXiv:2309.06180v1, Sections 4.1 to 4.3, Figures 5 to 7; Section 7.2, Impact of Block Size
Trace storage and address translation. Use your existing attention oracle rather than the displayed block-softmax equation; retain normalization over valid tokens. Treat the block-size experiments as the paper's workloads, not a universal optimum.
- Nano-vLLM: from tables to write locationsNano-vLLM - Block Manager and Sequence Lifecycle
prepare_block_tables,prepare_prefillandprepare_decode: positions, context lengths andslot_mappingRead the metadata construction only; no engine launch or kernel rewrite is needed.
Focus on
- For zero-based token position and block size , logical block is and offset is . The table entry, not the logical block number, selects physical storage.
- The pool itself can be contiguous while each request's blocks are scattered. Paging removes the requirement for a contiguous request-sized allocation, not all contiguous memory.
- Valid length is separate from allocated capacity. A full last page has occupancy equal to the block size, not zero; an empty request needs an explicit convention.
- Smaller pages reduce tail waste and make block-based reuse finer. They also increase table entries and management work. Measure the trade-off rather than assuming the smallest page wins.
Common mistakes · 3
- Using physical slot numbers as RoPE positions. Relocation must not change logical positions.
- Reading the whole final page. Its unused slots can contain stale values from an earlier owner.
- Expecting bit-identical floating-point attention after changing kernels. Storage relocation preserves the mathematical inputs; reduction-order differences still need the earlier numerical contract.
Try it yourself
Closed-book, before checking anything
[5, 1, 7]. List all token locations: token 9 is block 7, offset 1, flat slot 29. Gather integer token labels on CPU, permute physical blocks while updating the table, and require identical gathered labels. Keep absolute positions unchanged and verify that unused tail slots never appear in the gather.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.With block size 4 and table
[5, 1, 7], where is token 4?AnswerLogical block 1 maps to physical block 1, offset 0, flat slot 4.
2.Does moving a physical block require recomputing positional keys?Answer
No, if the values and logical positions are preserved. Moving storage is not moving tokens within the sequence.
3.Why can free space in separate pages admit a request when a contiguous allocator cannot?Answer
The page table joins disjoint fixed-size pages into one logical sequence. A contiguous allocator needs one sufficiently large free interval.
You're done when
Your CPU gather preserves every valid token under physical-page permutation and excludes tail slots, and you can translate an unseen table without source code.
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.
- 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.
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.
- 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: 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.
- 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.