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

Reference counts and copy-on-writeFork, beam and shared-prefix ownership
1

Read

In this order, only the parts listed

0 of 2 done

  1. 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

  2. Nano-vLLM: immutable prefix ownershipNano-vLLM - Block Manager and Sequence Lifecycle

    allocate cached-block branch and deallocate; contrast with may_append

    This 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.

2

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.
3

Try it yourself

Closed-book, before checking anything

In a CPU model with block size 4, create the five-token sequence [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.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 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. 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