Module 4 · Lesson 5 of 5

Sharing safely; choosing an optimization

Preserve another request’s history while sharing storage, then choose optimizations from evidence.

A shared prefix can have two owners

Two continuations can begin with the same processed prefix. Their logical histories can reference the same physical K/V until a write requires private storage.

What you will be able to explain

Follow ownership through sharing, copy-on-write and release; distinguish prefix caching policy; choose an optimization from workload evidence.

Reference count
Number of sequence mappings owning this physical block in the teaching allocator.
Copy-on-write
Copy shared storage only when a request needs to modify it.
Private tail
A block with one owner; that request can append without changing another sequence.

Fixture: A has six processed positions; C = 4. Fork B shares both blocks. A’s next processed token would write the shared partial tail, so reserve a destination, copy its two valid rows, remap A, then append.

Fork → detect shared tail → reserve → copy valid rows → remap → append → release. The first full block stays shared.

Allocator operations commit transactionally; the scene exposes conceptual stages inside that change. Reserved destinations are shown separately before remapping. GPU synchronization and instruction atomicity are not simulated.

Fork sharing and prefix caching are different policies

Fork sharing starts from a known shared processed history. Cross-request prefix caching needs a way to identify reusable computation and decide what stays resident.

Reusing a later word after a different earlier prefix is unsafe: K/V depends on model, position and preceding context. Check exact token prefix and compatible model/weights, positional settings, adapters and other conditioning. Do not assume equal strings alone establish reusable tensors.

Current vLLM prefix-cache documentation describes full-block reuse with prefix-dependent identity. A shared partial tail in the paper’s fork/COW example is not permission to reuse arbitrary partial blocks across requests.

A zero-reference block is reclaimed immediately in this module’s allocator. A prefix-cache policy may keep an unreferenced valid block eligible for reuse until eviction. “Unused by active requests” and “contains no cached data” are different states.

Predict: A finishes while B still references their first block. Free that block?

Current vLLM prefix-cache design · Historical kernel-design warning. Verify a pinned engine version before making current implementation claims.

Name the resource before choosing the optimization

Attention kernels and serving cache managers cooperate. They address different costs, even when an engine combines both.

What changes; what remains
MechanismMain changeBoundary
FlashAttention-style kernelIntermediate workspace and IOPersistent KV remains; dense pairwise work remains
KV pagingAllocation and block-aware accessRequired context reads and tail slack remain
Prefix cachingRepeated valid-prefix computationCached payload and decode context reads remain
GQA/MQAKV head countArchitecture/training compatibility required
QuantizationStorage precisionAccuracy, scales and kernel support matter
Windowed/sparse attentionAllowed query–key relationshipsAttention semantics change

PagedAttention includes block-aware attention access; the cache manager controls allocation and sharing. More admitted requests can improve throughput opportunity but also alter latency. Neither phase names nor byte ratios establish measured speedup.

  1. Long prefill with large temporary score/probability arrays: investigate workspace/IO-efficient kernels.
  2. Ragged growing requests wasting reserved capacity: investigate allocation/slack.
  3. Repeated compatible prefixes: investigate valid-prefix reuse.
  4. One long decode request: measure context-read traffic and kernel behavior; paging alone does not remove it.

Predict: Sharing halves physical KV storage. Must decode read traffic halve?

Takeaway: Name the resource first (temporary workspace, allocation, a repeated prefix, KV size or context reads), then choose the mechanism that changes that resource.

CPU verification, then hardware evidence

Use tiny deterministic examples to establish correctness before timing an engine. This browser already runs the same pure models used by the tests.

Optional Python handoff

Implement dense stable attention and the streaming recurrence. Replay the four-position fixture; then seeded random causal, rectangular and ragged cases. Print maximum absolute error, shape, mask, tile size and seed. Compare against an independent dense reference, not the same merge called twice.

m, denominator = -inf, 0
numerator = zeros(value_width)
for scores, values in tiles:
    new_m = max(m, max(scores))
    alpha = exp(m - new_m) if denominator else 0
    p = exp(scores - new_m)
    denominator = alpha * denominator + sum(p)
    numerator = alpha * numerator + p @ values
    m = new_m
output = numerator / denominator

Optional Go handoff

Build a fixed KV block pool with Allocate, Resolve, Fork, EnsureWritableTail and Release. Test exact-boundary growth, copy isolation, no negative references, pool exhaustion rollback and no stale data after reuse. Export deterministic allocation/copy counters. No HTTP server required.

Separate real-engine experiment

Pin model, engine commit, backend, hardware, precision, shapes, masks, warmup and synchronization. Compare repeated latency and peak allocated memory under identical workloads. Mac CPU/Metal observations do not establish CUDA/vLLM/NVIDIA-kernel performance. Record throughput and latency separately; do not infer one from allocation counts.

Official implementation/backend requirements · Kernel/code study