fix: reject negative seq_seed before uint64 masking

A negative seq_seed was silently masked to a huge positive value by
seed & 0xFFFF_FFFF_FFFF_FFFF, so the seq_seed < 0 check in _verify_args
could never trigger. Validate the seed before masking, and give the
bare TypeError a message. Positive-seed masking behavior is unchanged.

Add unit tests for seq_seed handling in tests/engine/test_sampling_params.py.
This commit is contained in:
w00946819 2026-10-21 11:46:16 +08:00
parent 774ce244b8
commit d42acdde32
2 changed files with 41 additions and 3 deletions

View File

@ -86,9 +86,11 @@ class SamplingParams:
def _make_seed(seed: int | None) -> int:
if seed is None:
return random.getrandbits(64)
if isinstance(seed, int):
return seed & 0xFFFF_FFFF_FFFF_FFFF
raise TypeError()
if not isinstance(seed, int):
raise TypeError(f"seq_seed must be an int or None, got {type(seed).__name__}.")
if seed < 0:
raise ValueError(f"seq_seed must be in range of uint64, got {seed}.")
return seed & 0xFFFF_FFFF_FFFF_FFFF
def _verify_args(self) -> None:
if self.temperature < 0.0:

View File

@ -0,0 +1,36 @@
"""Unit tests for SamplingParams seq_seed handling."""
import pytest
import torch # noqa: F401 Load torch's shared libraries before deepinfer.
from deepinfer.sampling_params import SamplingParams
pytestmark = [pytest.mark.ci, pytest.mark.ci_npu]
def test_seq_seed_none_is_random_uint64() -> None:
params = SamplingParams()
assert 0 <= params.seq_seed < 2**64
def test_seq_seed_valid_is_preserved() -> None:
assert SamplingParams(seq_seed=42).seq_seed == 42
assert SamplingParams(seq_seed=2**64 - 1).seq_seed == 2**64 - 1
def test_seq_seed_wraps_to_uint64() -> None:
assert SamplingParams(seq_seed=2**64 + 5).seq_seed == 5
def test_seq_seed_negative_raises() -> None:
with pytest.raises(ValueError, match="seq_seed"):
SamplingParams(seq_seed=-1)
with pytest.raises(ValueError, match="seq_seed"):
SamplingParams(seq_seed=-(2**64) + 5)
def test_seq_seed_wrong_type_raises() -> None:
with pytest.raises(TypeError, match="seq_seed"):
SamplingParams(seq_seed="42") # type: ignore[arg-type]
with pytest.raises(TypeError, match="seq_seed"):
SamplingParams(seq_seed=1.5) # type: ignore[arg-type]