Unit 10 of 11 · about 5 hours · implement
Allocator and prefix-index state machines under randomized property tests
Single-operation tests miss interactions between fork, collision, eviction and cancellation. State-machine tests must explain which earlier transition first broke ownership, not just report a later wrong answer.
You'll be able to
- Implement randomized allocator and prefix-index histories against an independent logical-state model.
- Check capacity conservation, exact references, token-position correctness and leak-free cancellation after every operation.
- Shrink a failing history and distinguish a stale-reference bug from an accounting bug.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Mini-SGLang: allocator regressionsMini-SGLang - Radix Cache and Cache Tests
TestAllocateEvictPageAlignment: all six tests, fromtest_allocate_after_evict_returns_page_alignedthroughtest_check_integrity_passes_after_evict_cycle - Mini-SGLang: what the integrity check provesMini-SGLang - Radix Cache and Cache Tests
check_integrity,_allocate,_freeand_page_to_token; compare withRadixPrefixCache.check_integrityin the radix file read earlierThe manager check counts free plus indexed pages and checks alignment; the radix integrity method is empty at this commit. These are not proofs of unique ownership or active-request state.
- Inference Systems Laboratory: KV evidence contractLLM Inference Systems Laboratory - KV State
Correctness requirements, KV physical/logical ownership; Failure-injection requirements, prefix fork/collision/eviction and request terminal phases
Use the KV requirements only. The rest of the track capstone is not this unit's assignment.
Focus on
- The independent oracle keeps request token lists, scope descriptors and expected ownership, not the implementation's tables or hashes. Recompute expected references from owners after each operation.
- Partition physical pages into disjoint free/reclaimable and pinned sets under one convention. Check unique allocation, nonnegative references, valid table generations, immutable shared prefixes and correct valid-length gathers.
- Generate successful and rejected allocate/append/fork/free/evict/cancel operations. Bias toward page boundaries, tiny pools, repeated terminal events, constant hashes, scope changes and reused physical IDs.
- Record the seed and full event history, then shrink while preserving operation preconditions. Inject deliberate double-free, missing decrement and stale-index mutants; an invariant suite that cannot detect them is incomplete.
Common mistakes · 3
- Checking only that total counts add up. One missing page and one double allocation can cancel numerically.
- Comparing the implementation with an oracle that reads its block tables. The same mapping bug then appears in both answers.
- Sampling only operations that succeed. Failure, duplicate cleanup and cancellation races are part of the state contract.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why is nonnegative refcount weaker than exact refcount?Answer
A positive extra count leaks capacity; a positive but insufficient count can free state while another owner still needs it. Compare counts with the complete owner ledger.
2.Can a stale table entry be dangerous even when its physical page ID is valid?Answer
Yes. The page may have been recycled for a new owner. A generation or equivalent lifecycle check must distinguish the old reference from the current allocation.
3.What makes a shrunk failure useful?Answer
It remains a valid operation history, still exposes the same violated contract, and is small enough to identify the first incorrect transition.
Practice
- Mini-SGLang:
TestAllocateEvictPageAlignmentPort the six named regression contracts to your allocator: post-eviction alignment, consecutive allocation non-overlap, aligned free slots, requested page count, page-to-token expansion and integrity after an eviction cycle
The section mini-project covers the broader
servelabrandomized state machines, independent token/owner oracle, collision/cancellation histories and mutation checks. These deterministic tests alone do not cover them.
You're done when
Your allocator passes the six regression contracts and replayable randomized histories, detects the injected lifecycle mutants and returns every physical page after explicit final cleanup.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 12 more in the section
- 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.
- 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.
Also in this 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.
- 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.
- 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.
- 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.