Repository navigation
[#22299][fix] Compute raw logprobs before penalties, embedding bias and token bans in TorchSampler - #22456
Closed
lori-ren wants to merge 2 commits into
Closed
[#22299][fix] Compute raw logprobs before penalties, embedding bias and token bans in TorchSampler#22456lori-ren wants to merge 2 commits into
lori-ren wants to merge 2 commits into
Conversation
…bias and token bans TorchSampler rewrote logits in place before reading raw logprobs from them; save the affected rows and restore them after sampling. Signed-off-by: Lori Ren <[email protected]>
Signed-off-by: Lori Ren <[email protected]>
Contributor
Author
1 task 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.
@coderabbitai summary
Description
Fixes #22299.
logprobs_mode="raw"(the default) is documented as log probabilities computed directly from the model's logits, but withTorchSamplerit included repetition/presence/frequency penalties, embedding bias and token bans (bad words,min_length,no_repeat_ngram_size)._process_requestsapplies those in place onlogits_cudabefore sampling, and_process_logprobslater reads raw logprobs from the same tensor. Temperature and top-k/top-p were not affected because they are applied out of place.The fix saves, before those in-place rewrites, the logits rows of single-beam requests that want raw logprobs and use one of the rewriting features (
_request_rewrites_logits), and copies them back once_sample_batched_by_strategyreturns. At that point the tokens and processed logprobs are already computed from the rewritten rows, and the raw-logprob path is the only remaining reader oflogits_cuda. Batches without such a request take no extra copy. Sampled tokens andlogprobs_mode="processed"are unchanged. TheLogprobMode.RAWdocstring now says which processing raw logprobs exclude; guided decoding and logits processors run before the sampler and stay included.Not covered: beam-search requests report logprobs from the beam history, which is scored on the rewritten logits; that is left for a separate change.
Reporter's script (Qwen2.5-0.5B-Instruct, greedy, first token
' mat'), raw logprob /log_softmax(generation_logits):Test Coverage
tests/unittest/_torch/sampler/test_logits_logprobs.py::test_logprobs_against_logits— now parametrized over repetition/presence/frequency penalty; raw logprobs must matchlog_softmax(generation_logits).tests/unittest/_torch/sampler/test_logits_logprobs.py::TestLogsprobsInBatchedSampling::test_logprobs— raw-logprob requests with embedding bias, bad words andmin_length, alone and mixed with other requests, with and without draft tokens; the expected values now come from a copy of the logits taken before sampling.tests/unittest/_torch/sampler/test_penalties_e2e.py— processed logprobs and tokens under penalties, unchanged.Without the
sampler.pychange, the new cases fail in the raw-logprob check.PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
To see a list of available CI bot commands, please comment
/bot help.