Skip to content

perf(cache): avoid extra copies when saving and restoring model state - #2474

Open
hellomiku831 wants to merge 3 commits into
abetlen:mainfrom
hellomiku831:perf/kv-snapshot-copy
Open

hellomiku831 wants to merge 3 commits into
abetlen:mainfrom
hellomiku831:perf/kv-snapshot-copy

Conversation

@hellomiku831

@hellomiku831 hellomiku831 commented Dec 2, 2026 •

Copy link
Copy Markdown

Saving a completion to the cache copies the serialized state into a second ctypes array just to turn it into bytes. On a cache hit, restoring scores also makes a temporary copy before NumPy writes them into the context. This PR reads the written prefix of the state buffer with ctypes.string_at and copies the saved scores straight into the existing destination. A caller can still reuse the same snapshot after generating more tokens.

Saving. I timed non-streaming create_completion calls on Qwen2.5-3B-Instruct Q4_K_M with an RTX 5090, CUDA 12.8, all layers offloaded, n_ctx=8448, n_batch=512, and LlamaRAMCache. Each request had a document prefix, an 11-token suffix, and 16 greedy output tokens. These medians compare the save change alone against main, with the order alternating after warm-up.

Prefix and starting state Pairs Before After
4,096 tokens, prefilled in the live context 7 368 ms 278 ms
8,192 tokens, prefilled in the live context 7 623 ms 433 ms
4,096 tokens, restored from the RAM cache during the request 7 598 ms 506 ms
4,096 tokens, full prefill during the request 5 604 ms 524 ms

For the 8K live-context case, peak Python-tracked allocations fell from 1,164 to 875 MiB. Both versions wrote identical snapshots. Generation with caching disabled skips this save path.

Restoring. With the save change already on both sides, I timed requests that hit the same cached prefix. The context was reset before each request. Times include the lookup, restore, generation, and save. On Qwen2.5-3B Q4_K_M with eight CPU threads, five alternating pairs gave 933 → 760 ms for a 1,024-token prefix and 8 output tokens, and 1,793 → 1,546 ms for 4,096 and 16.

On an RTX 5060 Laptop with Qwen2.5-1.5B, the 1K case took 209 → 165 ms in the first seven pairs and 216 → 167 ms in a second run of 15 pairs. The 4K result was noisier: 424 → 455 ms in the first run, then 444 → 378 ms in the second. Timing the restore separately in that second run gave 149 → 102 ms at 4K.

Completion text, token IDs, and token counts matched in every pair. The full test suite passed (27 tests), along with both Ruff checks. Tests cover a partially filled state buffer with an embedded NUL and check that restored scores equal the saved values and have independent storage.

Related to #1293.

@hellomiku831 hellomiku831 changed the title perf(cache): avoid an extra copy when saving model state perf(cache): avoid extra copies when saving and restoring model state Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant