Repository navigation
fix: limit logits processors to the sampled prefix - #2401
Open
YaochengTong wants to merge 2 commits into
Open
YaochengTong wants to merge 2 commits into
YaochengTong wants to merge 2 commits into
Conversation
4 of 5 tasks
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.
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 fullself._input_ids, unverified draft tokens included.sample()now stores the prefix length of the position being sampled (idx + 1, orn_tokenswhenidxisNone), and the sampler passes only that prefix.That is the same prefix
generate()already passes tostopping_criteria.In ordinary generation without a draft model or injected tokens, processors still receive the whole evaluated sequence.
Tests:
test_logits_processor_sees_only_accepted_tokens_with_draft_model, using the existingstories15M-q4_0.gguffixture, 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