Repository navigation
fix: save the prompt cache at the turn boundary so early exits keep it (#245) - #253
Merged
Merged
Conversation
#245) The save moved to the producer's `.info` event to avoid racing the decode step, but a request that ends on a stop sequence or a client disconnect leaves the stream before `.info`, so its prompt was never saved and the next turn re-prefilled the whole history. For the full-attention path (no hybrid/rotating layers, draft model, MTP or TurboKV), prefill the prompt but its last token synchronously, save, then let generation feed the last token, as the hybrid and rotating paths already do. No decode step has started when the cache is read, so there is no race, and the save no longer depends on how the request ends. The entry is one token short of the prompt; restore replays that token anyway. Contract test 16 covers a stop-sequence turn and a client-disconnect turn; test 15 now allows the one-token difference. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.
Fixes the remaining cases in #245 (a tool-call-only turn was fixed by #250).
Problem
Since the mlx-swift-lm b620ae8 bump the prompt cache is saved when the producer emits
.info. A request that ends on a stop sequence or a client disconnect leaves the stream before.info, so nothing is saved and the next turn re-prefills the whole history.Reproduced on current main with
Qwen2.5-0.5B-Instruct-4bit(greedy, ~690-token system prompt, follow-up repeats turn 1 plus a short exchange). Tokens reused by the follow-up:max_tokensThe built-in stop strings (
<|im_end|>etc.) do not cause it: the EOS token arrives first, so ordinary turns were fine. Only requests that setstopor leave early were affected.Fix
For the full-attention path (not hybrid, rotating, draft model, MTP or TurboKV), prefill the prompt except its last token synchronously, save, then let generation feed the last token. This is what the hybrid and rotating paths already do. No decode step has started when the cache is read, so the race that moved the save to
.infocannot happen, and the save no longer depends on how the request ends.The saved entry is one token shorter than the prompt;
restorealready replays the last token on a full match. Turn-1 output text is identical to main for all four scenarios.Tests
tests/test-contract.shagainst this build,Qwen2.5-0.5B-Instruct-4bit: 26 passed, 0 failed, 2 skipped. New Test 16 covers a stop-sequence turn (511 of 530 tokens reused) and a client-disconnect turn (509 of 528).startGeneration, which has no unit test) and the other integration legs; CI covers them.Not covered
.infoand keep the old early-exit behaviour./v1/completionsdoes not use the prompt cache at all, so it is unaffected.🤖 Generated with Claude Code