Module 4 · Lesson 5 of 5
Sharing safely; choosing an optimization
Preserve another request’s history while sharing storage, then choose optimizations from evidence.
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.
| Mechanism | Main change | Boundary |
|---|---|---|
| FlashAttention-style kernel | Intermediate workspace and IO | Persistent KV remains; dense pairwise work remains |
| KV paging | Allocation and block-aware access | Required context reads and tail slack remain |
| Prefix caching | Repeated valid-prefix computation | Cached payload and decode context reads remain |
| GQA/MQA | KV head count | Architecture/training compatibility required |
| Quantization | Storage precision | Accuracy, scales and kernel support matter |
| Windowed/sparse attention | Allowed query–key relationships | Attention 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.
- Long prefill with large temporary score/probability arrays: investigate workspace/IO-efficient kernels.
- Ragged growing requests wasting reserved capacity: investigate allocation/slack.
- Repeated compatible prefixes: investigate valid-prefix reuse.
- 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 / denominatorOptional 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