From b250bea994b84d8e30a8fcc66fb244f7426630c9 Mon Sep 17 00:00:00 2001 From: Ting SUN Date: Sun, 14 Jun 2026 14:41:36 +0800 Subject: [PATCH] fix(sampling): reject non-finite temperature in SamplingParams.verify (#28153) Signed-off-by: Ting Sun --- python/sglang/srt/sampling/sampling_params.py | 5 +++-- .../registered/unit/sampling/test_sampling_params.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/python/sglang/srt/sampling/sampling_params.py b/python/sglang/srt/sampling/sampling_params.py index 0744a57fd..38a950596 100644 --- a/python/sglang/srt/sampling/sampling_params.py +++ b/python/sglang/srt/sampling/sampling_params.py @@ -14,6 +14,7 @@ """Sampling parameters for text generation.""" import logging +import math from typing import Any, Dict, List, Optional, Union # sre_parse is deprecated in Python 3.11+, use re._parser instead @@ -118,9 +119,9 @@ class SamplingParams: self.top_k = TOP_K_ALL # whole vocabulary def verify(self, vocab_size): - if self.temperature < 0.0: + if not math.isfinite(self.temperature) or self.temperature < 0.0: raise ValueError( - f"temperature must be non-negative, got {self.temperature}." + f"temperature must be a non-negative finite number, got {self.temperature}." ) if not 0.0 < self.top_p <= 1.0: raise ValueError(f"top_p must be in (0, 1], got {self.top_p}.") diff --git a/test/registered/unit/sampling/test_sampling_params.py b/test/registered/unit/sampling/test_sampling_params.py index c98222b2f..caf94c449 100644 --- a/test/registered/unit/sampling/test_sampling_params.py +++ b/test/registered/unit/sampling/test_sampling_params.py @@ -92,6 +92,18 @@ class TestSamplingParamsVerify(CustomTestCase): with self.assertRaises(ValueError): sp.verify(self.VOCAB_SIZE) + def test_nan_temperature_raises(self): + """verify() must reject NaN temperature; the bare < 0.0 check alone lets it through.""" + sp = self._make(temperature=float("nan")) + with self.assertRaises(ValueError): + sp.verify(self.VOCAB_SIZE) + + def test_inf_temperature_raises(self): + """verify() must reject non-finite (inf) temperature.""" + sp = self._make(temperature=float("inf")) + with self.assertRaises(ValueError): + sp.verify(self.VOCAB_SIZE) + # --- top_p --- def test_top_p_negative_raises(self): """Test that verify() rejects negative top_p (valid range is (0, 1])."""