Skip to content

fix: limit logits processors to the sampled prefix - #2401

Open
YaochengTong wants to merge 2 commits into
abetlen:mainfrom
YaochengTong:fix/logits-processor-sampled-prefix
Open

YaochengTong wants to merge 2 commits into
abetlen:mainfrom
YaochengTong:fix/logits-processor-sampled-prefix

Conversation

@YaochengTong

@YaochengTong YaochengTong commented Nov 4, 2026 •

Copy link
Copy Markdown

Fixes #2393.
Related: #2399 addresses the same issue.
This PR implements the fix I proposed in #2393 and adds a regression test.
If the maintainers accept this PR, #2399 can be closed in favor of it.

With a draft model, generate() evaluates the draft block before verifying it, so the custom sampler passed every logits processor the full self._input_ids, unverified draft tokens included.
sample() now stores the prefix length of the position being sampled (idx + 1, or n_tokens when idx is None), and the sampler passes only that prefix.
That is the same prefix generate() already passes to stopping_criteria.
In ordinary generation without a draft model or injected tokens, processors still receive the whole evaluated sequence.

Tests:

  • Added test_logits_processor_sees_only_accepted_tokens_with_draft_model, using the existing stories15M-q4_0.gguf fixture, prompt lookup, and greedy sampling.
    It asserts exactly 32 processor calls, each receiving the prompt plus previously accepted tokens, and confirms that an evaluated draft suffix was encountered.
    In the pre-fix run, the first prefix mismatch occurred on the fourth processor call.
  • pytest tests: 27 passed.
  • ruff check llama_cpp tests: passed.
  • ruff format --check llama_cpp tests: passed.

Environment: macOS 27.0 arm64 (Apple M1 Pro), Python 3.11.15, editable installation with Metal enabled, llama.cpp submodule at 0c1e57098bba43ac29e6e3b677cdceebdd22334f.

AI assistance: I used Claude and Codex to prepare and review the change.

cc @abetlen

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logits processors receive unverified draft tokens in input_ids when a draft_model (prompt lookup) is used

1 participant