Model and Inference EngineeringKV cache and capacityStaff
How should beam search share KV cache safely when decoding branches diverge?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A beam-search decoder forks two continuations after three tokens in a four-token cache block. Both initially reference the same physical block. Explain which state may remain shared, how the first divergent write should work, and how to test reordering, pruning and cancellation against a fully private-cache reference.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
Beam search keeps several candidate continuations instead of committing immediately to one. Each candidate needs the attention keys and values computed from its own token history. That retained state is the key-value, or KV, cache. Candidates with the same history can reuse it, but divergent histories must not overwrite one another’s state.
The two branches are allowed to share the KV blocks for tokens they have already consumed in common. Those blocks are immutable as far as the branches are concerned. The first time they need different KV state, they need independent writable storage. If both block tables still point to the same writable physical block, a write for branch A can alter what branch B reads on its next attention step. The output then depends on the order in which the scheduler ran them. vLLM's PagedAttention design uses reference counts and copy-on-write to share blocks safely for parallel sampling and beam search.
Consider a hypothetical four-slot block containing the common tokens A, B and C. Both beams reference that block with length three. Beam one is about to process D, while beam two will process E. If both write slot four in the same storage, whichever write happens last can replace the other beam’s history. Copy the valid shared slots into private writable storage before an append that would modify a multiply referenced block. The scheduler must also order the copy before its dependent GPU write. Merely changing the host block table after launching the write leaves a race.
A block holds several tokens, so a fork can happen in the middle of its last block. It is not enough to allocate a new block only after the old one fills. If the next token writes into a shared, partially filled block, make a private copy first. Earlier complete blocks can still be shared. Each logical beam needs its own block table, sequence length, position and beam score. When beams are pruned or reordered, update the owners and references without freeing a block still reachable by another beam. This is exactly where a seemingly harmless scheduler optimization can break correctness.
A reference count tells the allocator how many live owners still depend on a block. Updating that count, changing beam mappings and reclaiming a block need a defined synchronization protocol. A pruned beam may no longer be scheduled while its earlier GPU read is still running. Reclamation must wait for those outstanding uses as well as logical ownership release. Check this explicitly in the selected engine rather than assuming a host-side counter proves device work has finished.
I would reproduce with a tiny fixed prompt that branches at a known token and forces different continuations. Compare each branch's logits and KV reads to a run that allocates fully separate caches. Then fork at every possible offset within a block, reorder beams, prune one, cancel another, and interleave requests from different customers. Validate block reference counts and copy events under all those transitions. Same outputs in a single fixed scheduling order are weak evidence because the bug may require the other order.
Could we copy the entire prefix for each branch? Yes, and that is a useful correctness baseline. But beam width and long prefixes make that costly. Copy-on-write preserves sharing until a real write needs isolation. Two requests share a cached prefix. Why did cancelling one corrupt the other? is about cancellation corrupting a shared prefix cache across requests. This page is about ownership at the moment a decode path forks within one search request. Both need an explicit answer to who may write each physical KV block.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux