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

0/11

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

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.

  1. 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 in reports/.
  2. 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 in reports/.
  3. 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/.
  4. 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/.
  5. 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 in reports/.
  6. 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 in reports/, 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
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