Paged KV Cache Block Allocator with Copy-on-Write Forks
Last updated: November 29, 2026
Quick Overview
Implement a block allocator for a paged KV cache: sequences get fixed-size blocks as they grow, a sequence can be forked to share its blocks (for parallel sampling or beam search), and writing into a shared partial block triggers copy-on-write.
Together AI
November 29, 20260
0
0 solved
Implement a block allocator for a paged KV cache: sequences get fixed-size blocks as they grow, a sequence can be forked to share its blocks (for parallel sampling or beam search), and writing into a shared partial block triggers copy-on-write.
Inference engineers at Together AI work on engines that store attention keys and values in paged blocks so memory is not wasted on padding and prefixes can be shared. The onsite coding round checks that you can turn that idea into a correct data structure with reference counts, and reason about what happens at block boundaries.
What the Interviewer Expects
- Maps each sequence to a block table and allocates on block boundaries only
- Uses reference counts so forked sequences share blocks
- Performs copy-on-write only for a shared, partially filled last block
- Returns blocks to the free list exactly when the count hits zero
- Fails cleanly with an out-of-memory signal the scheduler can act on
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- When the allocator runs out, should the scheduler preempt by swapping to CPU or by recomputing? What decides it?
- Why do full shared blocks never need copying?
- How would you add prefix sharing across unrelated requests with the same system prompt?
- How does block size trade internal fragmentation against kernel efficiency?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Fixed pool of num_blocks blocks, each holding block_size tokens of K/V. Operations: add_sequence(id, prompt_len), append_token(id) returning the physical (block, slot) to write, `fork(parent, ...
High-Level Design
A free stack of block ids, a refcount per block, and per sequence a block table plus a token length. Forking copies the table and increments counts, so the child costs no memory. A new token goes to s...