Simulate contiguous versus paged KV allocation
CPU · your Macscript120 minrequiredGoal · Write a discrete-event simulator that admits requests with random prompt and output lengths under two allocators and reports utilization and throughput.
- 1Define a GPU with a fixed KV budget in tokens (e.g. 65,536) and a request generator with prompt lengths from a lognormal and output lengths capped at max_tokens=1024.
- 2Implement a contiguous allocator that reserves prompt_len + max_tokens per request up front and frees on completion.
- 3Implement a paged allocator with 16-token blocks, a free list and per-request block tables that grows one block at a time.
- 4Run both for 10,000 simulated decode steps, admitting new requests whenever memory allows; log used tokens, reserved tokens and completed requests.
- 5Compute internal fragmentation (reserved minus used) and the number of concurrent requests over time for both.
- 6Add prefix caching by giving 50 percent of requests a shared 512-token system prompt and reference-counting those blocks; re-run.
- 7Plot concurrency and utilization over time for the three configurations.
- Deliverable
- The simulator script and a plot comparing contiguous, paged and paged+prefix-cache runs.
- Check
- Paged allocation sustains at least 2x the concurrent requests of contiguous allocation; prefix caching raises it further and the internal waste of paged is under one block per request.