Merge d42acdde324375a4c460a30423251c54b4b2336d into 553741557a57df44a46fb3ab72b6aafeaf945337

This commit is contained in:
world-executed 2026-10-28 21:54:34 -07:00 committed by GitHub
commit 2a6e44a999
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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]