Repository navigation
perf(cache): avoid extra copies when saving and restoring model state - #2474
Open
hellomiku831 wants to merge 3 commits into
Open
hellomiku831 wants to merge 3 commits into
hellomiku831 wants to merge 3 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_atand 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_completioncalls on Qwen2.5-3B-Instruct Q4_K_M with an RTX 5090, CUDA 12.8, all layers offloaded,n_ctx=8448,n_batch=512, andLlamaRAMCache. 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.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.