Unit 5 of 11 · about 4 hours · implement
Reference counts, copy-on-write, fork, beam and shared prefixes
Beyond one cache introduced sharing vocabulary. This unit makes it safe: one request's append or cancellation must not corrupt or release another request's prefix.
You'll be able to
- Implement fork and release with reference counts derived from actual owners.
- Implement copy-on-write before modifying a shared partial block.
- Diagnose beam-pruning and fork bugs using an independent unshared token oracle.
Covers
Read
In this order, only the parts listed
0 of 2 done
- PagedAttention: fork and copy-on-writeEfficient Memory Management for Large Language Model Serving with PagedAttention
arXiv:2309.06180v1, Section 4.4, Parallel sampling, Beam search and Shared prefix, with Figures 8 to 10
- Nano-vLLM: immutable prefix ownershipNano-vLLM - Block Manager and Sequence Lifecycle
allocatecached-block branch anddeallocate; contrast withmay_appendThis revision shares cached full blocks but has no general fork or copy-on-write operation. Use the paper for that extension, not an invented Nano-vLLM API.
Focus on
- A fork copies the logical table and increments references to its physical blocks. Count all owners under your chosen convention, including any cache or pending-transfer pins; do not count a duplicated table as duplicated payload.
- Before writing a multiply owned tail, allocate a private block, copy valid state across every layer, redirect only the writer and release its old reference. Publish after the copy is ready.
- A full immutable prefix block needs no copy when append starts a fresh page. A shared partial tail does need protection, even if both branches will write the same token.
- Beam selection can create multiple descendants of one parent and prune other parents. Acquire survivor references before releasing old owners, so a transient zero count cannot recycle a still-needed page.
Common mistakes · 3
- Copying only keys, one layer or the token-ID metadata. Copy-on-write protects the complete stored state used by future attention.
- Assuming an unused slot in a shared page has no owner. Different branches can disagree on the contents and validity of that slot.
- Releasing all old beams before acquiring new beam references. This can recycle a prefix that a survivor still needs.
Try it yourself
Closed-book, before checking anything
[10, 11, 12, 13, 14] and fork it once. Both pages initially have two request references. Append 20 to the child: copy its partial tail, leaving the full prefix shared. Require three physical pages, prefix refcount 2 and tail refcounts 1 each; the parent remains length 5. Cancel the child, then the parent, and require all pages free when caching is disabled. Compare both token histories with separately copied Python lists.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.A fork occurs at exactly eight committed tokens with block size 4. Does the next append copy an old page?Answer
No. Both old pages are full and immutable; each writing branch allocates a fresh tail page.
2.Does cancelling a child free the shared prefix immediately?Answer
No. It releases the child's references. The parent's references still protect the prefix, as would any other pin.
3.Why is one reference count insufficient to prove correct data?Answer
It proves ownership only if maintained correctly. A page can have a plausible count yet contain the wrong sequence, generation, layer or position; the token/state oracle checks that separately.
You're done when
Forks at partial and full boundaries preserve parent state, reference counts match all owners, and pruning or cancellation restores capacity without invalidating survivors.
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.