Repository navigation
server : fix stale tokens in slot cache when generation stops inside an accepted draft - #32771
Open
philippesic wants to merge 1 commit into
Open
philippesic wants to merge 1 commit into
philippesic wants to merge 1 commit into
Conversation
…an accepted draft Accepted draft tokens were added to the slot cache before process_token ran, so a stop at one of them left the stop token and the tokens after it in the cache. Remove them and trim the memory once, at the final position.
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.
Overview
When multiple draft tokens are accepted at once, the server adds all of them to the slot's cache before checking for a stop condition. If a stop triggers partway through a draft, all tokens from the draft remain in cache.
Without a drafter, tokens are processed one at a time, where tokens after a stop condition aren't cached. This leads to different cache states on identical request depending on whether speculative decoding was used or not.
This causes two main problems:
tokens_cachedin the response is higher when a draft is used.This can happen with both a stop string and when an accepted EOG is the last draft token. #29638 stopped accepting an EOG in the middle of a draft, but end-of-draft EOG and rejection sampling accepting mid-draft EOG are still unaddressed. This PR covers all three of these cases.
The fix: when generation stops partway through the accepted tokens, the stop token and everything after it are removed from the cache before the slot is released, restoring cache parity between speculative decoding and absent requests.
To do this memory is now trimmed once per round at the final position after the stop check On recurrent models a partial rollback can only be done once before the next decode, so trimming twice aborts.
Acceptance statistics are unchanged and
draft_n_acceptedstill counts tokens accepted after a stop.Additional information
To reproduce with the existing test models (
stories15M_MOEwith thestories15M-q4_0draft, temperature 0): send the prompt"I believe the meaning of life is"withstop: [" be"].tokens_cachedis 10 with the draft and 9 without.I added a test that sends identical requests with and without a draft, using each word of the output as a stop string, and checks that the cache states match. On master, 9 of the 20 words give different cache states. With this change all 20 match.
Because testing uses plain attention models, I also checked a hybrid Qwen3.5-4B with its built-in MTP draft. On master, about half of the stop words and 3 of 10 normal chat endings left extra tokens in the cache, and in one case a follow-up request gave different output than the same request with prompt caching turned off. With this change, extra tokens and follow-ups are identical between speculative decoding on/off.
In some setups the target can't undo part of a batch and has to restore a checkpoint instead (for example recurrent models with a long draft), which keep the current behaviour. Fixing them needs a larger change.
Testing ran on a CPU with a single slot with a draft model and MTP.
Related: #28049, #29638. #28232 is an open PR that handles the EOG case differently: it truncates the accepted tokens at the EOG, changes the acceptance counts, and handles checkpoint rounds, but doesn't cover stop strings. I've asked its author about combining the two.
Requirements