Skip to content

server : fix stale tokens in slot cache when generation stops inside an accepted draft - #32771

Open
philippesic wants to merge 1 commit into
ggml-org:masterfrom
philippesic:fix-spec-stop-token-cache
Open

philippesic wants to merge 1 commit into
ggml-org:masterfrom
philippesic:fix-spec-stop-token-cache

Conversation

@philippesic

Copy link
Copy Markdown

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:

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_accepted still counts tokens accepted after a stop.

Additional information

To reproduce with the existing test models (stories15M_MOE with the stories15M-q4_0 draft, temperature 0): send the prompt "I believe the meaning of life is" with stop: [" be"]. tokens_cached is 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

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes. Coding and testing was assisted by Claude Code. I reviewed every line and ran the tests and checks described above.

…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

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant