Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions tensorrt_llm/sampling_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,18 @@ def _validate(self):
For instance, while the greedy decoding with n > 1 is capable in the
Executor class of C++ runtime, the LLM API disallows such combination.
"""
if self.top_p is not None and (self.top_p < 0 or self.top_p > 1):
# These bounds are written as negated range checks rather than as
# `value < low or value > high`, so that NaN is rejected too: every
# comparison against NaN is False, which lets it slip through the
# positive form. The top_p_decay / top_p_min checks below already use
# this form.
if self.top_p is not None and not 0 <= self.top_p <= 1:
raise ValueError(f"require 0 <= top_p <= 1, got top_p={self.top_p}")
if self.top_k is not None and self.top_k < 0:
raise ValueError(f"require top_k >= 0, got top_k={self.top_k}")
if self.min_p is not None and (self.min_p < 0 or self.min_p > 1):
if self.min_p is not None and not 0 <= self.min_p <= 1:
raise ValueError(f"require 0 <= min_p <= 1, got min_p={self.min_p}")
if self.temperature is not None and self.temperature < 0:
if self.temperature is not None and not self.temperature >= 0:
raise ValueError(f"require temperature >= 0, got temperature={self.temperature}")

# Top-p decay param ranges mirror the hard checks in the
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_lists/test-db/l0_cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ l0_cpu:
- unittest/llmapi/test_mpi_session.py ISOLATION
- unittest/llmapi/test_reasoning_parser.py
- unittest/llmapi/test_request_priority.py
- unittest/llmapi/test_sampling_params.py
- unittest/llmapi/test_serialization.py
- unittest/llmapi/test_utils.py
- unittest/others/test_http_utils_fail_fast.py
38 changes: 38 additions & 0 deletions tests/unittest/llmapi/test_sampling_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,44 @@ def test_completion_logprobs_assignment_revalidates():
request.to_sampling_params(backend="pytorch")


@pytest.mark.parametrize("field", ["top_p", "min_p", "temperature"])
def test_sampling_params_rejects_nan(field):
with pytest.raises(ValueError, match=field):
SamplingParams(**{field: float("nan")})
Comment thread
coderabbitai[bot] marked this conversation as resolved.


@pytest.mark.parametrize(
("field", "value"),
[
("top_p", -0.1),
("top_p", 1.1),
("min_p", -0.1),
("min_p", 1.1),
("temperature", -1.0),
],
)
def test_sampling_params_rejects_out_of_range(field, value):
with pytest.raises(ValueError, match=field):
SamplingParams(**{field: value})


@pytest.mark.parametrize(
("field", "value"),
[
("top_p", 0.0),
("top_p", 0.9),
("top_p", 1.0),
("min_p", 0.0),
("min_p", 0.5),
("min_p", 1.0),
("temperature", 0.0),
("temperature", 1.0),
],
)
def test_sampling_params_accepts_in_range_values(field, value):
assert getattr(SamplingParams(**{field: value}), field) == value


@pytest.mark.parametrize("value", [None, -1])
def test_thinking_token_budget_unlimited_values(value):
assert SamplingParams(thinking_token_budget=value).thinking_token_budget is None
Expand Down
Loading