[None][fix] Clamp small non-zero sampling temperature - #15716
Conversation
📝 WalkthroughWalkthroughAdds a Temperature Clamping
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tensorrt_llm/sampling_params.py`:
- Around line 376-381: Update the public documentation for SamplingParams to
reflect the new temperature normalization behavior. In the SamplingParams class
docstring (and any temperature field description used by the user-facing API),
add that values greater than 0 but below MIN_SAMPLING_TEMPERATURE are clamped to
MIN_SAMPLING_TEMPERATURE, while 0 still means greedy decoding. Keep the wording
aligned with the existing temperature description in sampling_params.py so the
contract matches the behavior in the temperature handling logic.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 68c9e8a0-1271-425b-884b-35a8a0e8f402
📒 Files selected for processing (2)
tensorrt_llm/sampling_params.pytests/unittest/llmapi/test_sampling_params.py
7f35e15 to
cac012d
Compare
Signed-off-by: Ethan Feng <ethan.fengch@gmail.com>
cac012d to
60468cc
Compare
|
@YihuiLu512 @lori-ren, could you please review #15716 before the merge decision and consider consolidating the work here, since this was the earlier implementation for #15715? |
|
Thanks for the fix! Before we settle on the approach, a few questions: The sampler already handles this case internally: GREEDY_TEMPERATURE_THRESHOLD = 1e-4 in _torch/pyexecutor/sampler/ops/vanilla.py treats any request with T <= 1e-4 as greedy (the temperature is never used as a divisor), so on the PyTorch sampler path a value like 1e-12 safely degrades to greedy rather than overflowing. Given that, I have some concerns about clamping up to MIN_SAMPLING_TEMPERATURE = 1e-2:
Could you share which path actually exhibited the inf/nan in #15715 (TRTLLMSampler? a path that bypasses the greedy threshold?) That would help decide whether the right fix is mapping tiny temperatures to greedy at the API level (reusing/aligning with GREEDY_TEMPERATURE_THRESHOLD), or adding the same guard to the affected path, rather than introducing a new clamp value. |
|
@zhaoyangwang-nvidia Thanks for raising these questions. I appreciate the concerns you raised. I’ll carefully review the current sampling paths, including the exact coverage of the existing threshold and which paths may still be affected, and then follow up with a detailed answer. |
Thanks for the context and for double-checking the current sampling paths. Just to add, we are also actively refactoring the Torch sampler recently, so some of the related logic may continue to evolve. If you find any bugs, corner cases, or have suggestions while reviewing the sampling behavior, please feel free to raise them or discuss with me directly. I’d be happy to go through them together. |
Summary by CodeRabbit
Description
Fix #15715 .This PR clamps very small non-zero
SamplingParams.temperaturevalues to1e-2before they are passed to the sampling backend.temperature=0continues to represent greedy decoding, while positive values below the stability threshold are normalized to avoid excessive logit scaling fromlogits / temperature.TensorRT-LLM currently validates that
temperature >= 0, but values such as1e-12can still enter the sampling path and may lead toinf,nan, or unstable sampling behavior in edge cases.This mirrors the numerical stability guard used by vLLM while preserving the existing greedy decoding semantics for
temperature=0.Changes
Added
MIN_SAMPLING_TEMPERATURE = 1e-2.Clamped
0 < temperature < MIN_SAMPLING_TEMPERATUREtoMIN_SAMPLING_TEMPERATUREinSamplingParams._validate().Added debug logging when a small non-zero temperature is clamped.
Added a unit test covering
temperature=0, very small positive temperatures, the threshold value, and normal temperatures.Test Coverage
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.