Skip to content

Report the reused cache prefix before prefill - #644

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/prefill-prefix
Oct 12, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/prefill-prefix

Conversation

@aleroot

@aleroot aleroot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

PrefillParameters.progress counts only the positions a prefill adds. A prompt that resumes on a reused prefix as ChatSession arranges 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 and GenerateCompletionInfo.cachedPromptTokenCount arrives.

Report the other half up front:

  • PrefillParameters.prefix, 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: TokenIterator, SpeculativeTokenIterator, MTPSpeculativeTokenIterator the main cache, once. A draft prefill reports nothing.

prefix + processed is what the cache holds while the prompt is prefilled, which is what a context gauge needs to stay correct mid-turn.

var parameters = GenerateParameters(maxTokens: 512)
parameters.prefill.prefix = { cached in gauge.tokens = cached }
parameters.prefill.progress = { processed, _ in gauge.tokens += processed }

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

`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.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thank you!

@davidkoski
davidkoski merged commit f3da868 into ml-explore:main Oct 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants