Skip to content

[#22299][fix] Compute raw logprobs before penalties, embedding bias and token bans in TorchSampler - #22456

Closed
lori-ren wants to merge 2 commits into
NVIDIA:mainfrom
lori-ren:fix/raw-logprobs-before-penalties
Closed

lori-ren wants to merge 2 commits into
NVIDIA:mainfrom
lori-ren:fix/raw-logprobs-before-penalties

Conversation

@lori-ren

Copy link
Copy Markdown
Contributor

@coderabbitai summary

Description

Fixes #22299.

logprobs_mode="raw" (the default) is documented as log probabilities computed directly from the model's logits, but with TorchSampler it included repetition/presence/frequency penalties, embedding bias and token bans (bad words, min_length, no_repeat_ngram_size). _process_requests applies those in place on logits_cuda before sampling, and _process_logprobs later 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_strategy returns. 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 of logits_cuda. Batches without such a request take no extra copy. Sampled tokens and logprobs_mode="processed" are unchanged. The LogprobMode.RAW docstring 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):

config before after
no penalty -0.513 / -0.513 -0.513 / -0.513
repetition_penalty=1.3 not in top-5 / -0.510 -0.510 / -0.510
presence_penalty=1.0 -1.015 / -0.510 -0.510 / -0.510
frequency_penalty=1.0 -1.009 / -0.510 -0.510 / -0.510

Test Coverage

  • tests/unittest/_torch/sampler/test_logits_logprobs.py::test_logprobs_against_logits — now parametrized over repetition/presence/frequency penalty; raw logprobs must match log_softmax(generation_logits).
  • tests/unittest/_torch/sampler/test_logits_logprobs.py::TestLogsprobsInBatchedSampling::test_logprobs — raw-logprob requests with embedding bias, bad words and min_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.py change, 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-compatible or api-breaking. For api-breaking, include BREAKING in 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.

…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]>
@lori-ren

Copy link
Copy Markdown
Contributor Author

Closing in favor of #22303, which fixes #22299 the same way and was opened first.

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.

[Bug]: logprobs_mode="raw" (the default) returns logprobs after the repetition, presence and frequency penalties

1 participant