Repository navigation
Report the reused cache prefix before prefill - #644
Merged
Merged
Conversation
`PrefillParameters.progress` counts only the positions a prefill adds, so an application tracking how full a KV cache is cannot follow a prompt that resumes on a reused prefix -- as `ChatSession` arranges between turns, and as a template that re-renders the cached tail (Qwen 3 dropping the reasoning of past turns) forces it to rewind. The prefix is otherwise knowable only from the turn's `GenerateCompletionInfo.cachedPromptTokenCount`, once the turn has ended. Report it up front instead: - `PrefillParameters.prefix` is called once, before the first prefill forward, with the positions the cache already holds -- `0` when cold. It is the figure the same turn's completion info confirms. - Each token iterator reports it from its own cache progress, so the value is the cache's rather than a re-derivation: `TokenIterator`, `SpeculativeTokenIterator` and `MTPSpeculativeTokenIterator` each report the main cache once, and a draft prefill reports nothing. `prefix + processed` is what the cache holds while a prompt is prefilled, which is what a context gauge needs to stay correct mid-turn. Positions are the units `progress` already reports, so merged embedding positions on VLM turns count the same way in both. No model changes: models keep reporting through `progress`, and the new callback is optional, so existing call sites are untouched.
aleroot
force-pushed
the
alessio/prefill-prefix
branch
from
October 2, 2026 20:39
59fc776 to
52ad230
Compare
davidkoski
approved these changes
Oct 12, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Changes look good, thank you!
5 tasks done
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.
Proposed changes
PrefillParameters.progresscounts only the positions a prefill adds. A prompt that resumes on a reused prefix asChatSessionarranges between turns, and as a template that re-renders the cached tail forces it to rewind leaves an application unable to say how full the cache is until the turn ends andGenerateCompletionInfo.cachedPromptTokenCountarrives.Report the other half up front:
PrefillParameters.prefix, called once before the first prefill forward with the positions the cache already holds0when cold. It is the figure the same turn's completion info confirms.TokenIterator,SpeculativeTokenIterator,MTPSpeculativeTokenIteratorthe main cache, once. A draft prefill reports nothing.prefix + processedis what the cache holds while the prompt is prefilled, which is what a context gauge needs to stay correct mid-turn.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.