fix(frontend): bound stop strings and regex patterns (#37029)
This commit is contained in:
@@ -14,6 +14,9 @@ import msgspec
|
||||
|
||||
from sglang.srt.sampling.sampling_params import (
|
||||
MAX_LEN,
|
||||
MAX_STOP_COUNT,
|
||||
MAX_STOP_REGEX_COUNT,
|
||||
MAX_STOP_REGEX_LEN,
|
||||
TOP_K_ALL,
|
||||
SamplingParams,
|
||||
get_max_seq_length,
|
||||
@@ -344,6 +347,17 @@ class TestSamplingParamsNormalize(CustomTestCase):
|
||||
sp.normalize(tokenizer=tokenizer)
|
||||
self.assertEqual(sp.stop_strs, ["stop1", "stop2"])
|
||||
|
||||
def test_stop_count_limit(self):
|
||||
tokenizer = self._mock_tokenizer()
|
||||
SamplingParams(stop=["x"] * MAX_STOP_COUNT).normalize(tokenizer)
|
||||
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
SamplingParams(stop=["x"] * (MAX_STOP_COUNT + 1)).normalize(tokenizer)
|
||||
self.assertEqual(
|
||||
str(cm.exception),
|
||||
f"at most {MAX_STOP_COUNT} stop strings are allowed, got {MAX_STOP_COUNT + 1}",
|
||||
)
|
||||
|
||||
def test_stop_str_max_len_uses_encoded_length(self):
|
||||
"""Test that max_len is based on encoded token count, not character count."""
|
||||
# "ab" encodes to 1 token, "cdef" encodes to 2 tokens
|
||||
@@ -385,6 +399,33 @@ class TestSamplingParamsNormalize(CustomTestCase):
|
||||
sp.normalize(tokenizer=tokenizer)
|
||||
self.assertEqual(sp.stop_regex_max_len, 3)
|
||||
|
||||
def test_stop_regex_count_limit(self):
|
||||
tokenizer = self._mock_tokenizer()
|
||||
SamplingParams(stop_regex=["x"] * MAX_STOP_REGEX_COUNT).normalize(tokenizer)
|
||||
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
SamplingParams(stop_regex=["x"] * (MAX_STOP_REGEX_COUNT + 1)).normalize(
|
||||
tokenizer
|
||||
)
|
||||
self.assertEqual(
|
||||
str(cm.exception),
|
||||
f"at most {MAX_STOP_REGEX_COUNT} stop_regex patterns are allowed, "
|
||||
f"got {MAX_STOP_REGEX_COUNT + 1}",
|
||||
)
|
||||
|
||||
def test_stop_regex_byte_length_limit(self):
|
||||
tokenizer = self._mock_tokenizer()
|
||||
pattern = "é" * (MAX_STOP_REGEX_LEN // 2)
|
||||
SamplingParams(stop_regex=pattern).normalize(tokenizer)
|
||||
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
SamplingParams(stop_regex=pattern + "a").normalize(tokenizer)
|
||||
self.assertEqual(
|
||||
str(cm.exception),
|
||||
f"stop_regex is {MAX_STOP_REGEX_LEN + 1} bytes, over the "
|
||||
f"{MAX_STOP_REGEX_LEN}-byte limit",
|
||||
)
|
||||
|
||||
|
||||
class TestSamplingParamsMsgspecStruct(CustomTestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user