KV Memory and Prefix Reuse
KV state is both the main variable memory budget and a correctness-sensitive shared resource. Paging and prefix reuse must preserve ownership, positions, versions and tenant boundaries under churn.
Section 3 of 9 · 11 units · about 47 hours
By the end you can Pass long randomized allocate, append, fork, copy-on-write, evict and cancel histories, reconcile derived capacity with physical state, and show one workload where reuse helps and one where hit rate is misleading.
Units
- Per-layer KV layouts and the cache consequences of attention variants4h
- Logical tokens, paged storage, block size and block tables4h
- Fragmentation, page waste, capacity and KV compression4h
- Free lists, reserve policies, block tables and append4h
- Reference counts, copy-on-write, fork, beam and shared prefixes4h
- Eviction, recomputation, preemption and terminal cleanup4h
- Prefix hashing with collision checks and radix longest-prefix lookup6h
- Exact prefix identity, version scope and tenant privacy scope4h
- Cache-aware routing, KV offload and connector metadata3h
- Allocator and prefix-index state machines under randomized property tests5h
- Prefix reuse, memory pressure and fairness measured against outcomes5h
Build it
Mini project · about 12 to 16 hours
Paged KV allocator with prefix reuse
Extend engine/ with a paged KV allocator, a prefix index with strict identity and a labeled CPU simulation of routing and offload. Prove ownership under churn and show where reuse helps goodput and where high hits mislead, for a model that fits your GPU.
- 1Describe per-layer KV state with byte accounting for dense, latent and hybrid cases in
engine/, implement block-table translation with valid-length gathers, and prove permutation preserves positions checked against Nano-vLLM allocation views with ledgers kept inreports/. - 2Derive contiguous versus paged capacity with reserve and sharing in
engine/, implement a reserve-aware private allocator with multi-token append and failure atomicity, checked by a separate logical ledger with histories kept inreports/. - 3Implement fork with partial-tail copy-on-write and beam-pruning histories checked against unshared copies, then eviction, recompute, cancellation and late-event rejection proving single release with traces kept in
reports/. - 4Build block-hash lookup with exact verification under forced collisions and a radix longest-prefix index, checked with the TensorTonic trie problem plus a tenant, version and media identity matrix kept in
reports/. - 5Port the Mini-SGLang allocation regression contracts to
tests/and run randomized allocator and index histories against an independent oracle, shrinking failures and catching injected mutants with histories kept inreports/. - 6Simulate cache-aware routing and offload with versioned descriptors, stale-version rejection, cancellation and deferred free in
sim/using labeled CPU costs, then measure reuse off and on with ablations showing one benefit and one high-hit non-improvement with fairness evidence inreports/, with an optional measured pair on your GPU.
What you hand in
engine/paged allocator and prefix index with ownership, collision and scope tests.- Randomized state-machine suite in
tests/with shrunk failures and mutant catches. - Routing and reuse report in
reports/with benefit, high-hit negative and fairness evidence.
How to check it
- Self-grade with the rubric below
- TensorTonic Find Reusable Prefixes in KV Trie
Solve the trie cases by hand first, then extend with collision checks.
- Mini-SGLang allocator regression suite
Port the named contracts, then add randomized histories around them.
How it's graded
Ownership safety
Pass: Randomized fork, copy-on-write, eviction and cancellation histories match the unshared oracle with exact counts.
Great: Shrunk failures isolate the first bad transition and injected mutants are all caught.
Prefix correctness
Pass: Constant-hash histories never return foreign state and the identity matrix rejects incompatible reuse.
Great: Partial-edge splits and eviction keep references consistent with brute-force agreement.
Reuse honesty
Pass: Reuse off and on runs share arrivals and contracts with TTFT, goodput and fairness from traces.
Great: The high-hit negative names its overhead and the routing crossover is predicted before measurement.
Resources
- Efficient Memory Management for Large Language Model Serving with PagedAttentionvLLM Project
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
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)
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.
- Nano-vLLM - Block Manager and Sequence LifecycleGeeeekExplorer
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
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
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.
- AI Systems Performance Engineering - Chapter 18, tuning KV-cache utilizationChris Fregly (O'Reilly)
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.
- SGLang documentation - Radix cache eviction and HiCacheSGLang Project
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.
- 100 Days of LLM Inference - Days 5, 9, 10 and 16Elizabeth Thomas
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
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
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
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.
- AI Performance Engineering - KV Cache SystemsWafer
A curated index. Use Section 4 only to find a primary KV-cache source the other entries lack.
- vLLM 0.31.0 - cache specifications and connector lifecyclevLLM Project
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.