Skip to content

Commit 2c14393

Browse files
authored
[#22232][fix] Reject an empty stop string in SamplingParams (#22237)
Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
1 parent 885a043 commit 2c14393

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎tensorrt_llm/sampling_params.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ def _validate(self):
439439
f"under the greedy decoding."
440440
)
441441

442+
# An empty stop string tokenizes to no tokens, and the sampler rejects a
443+
# stop word without tokens while setting up the request, which fails
444+
# every request in flight with it instead of just this one.
445+
if self.stop is not None:
446+
stop_strings = [self.stop] if isinstance(self.stop, str) else self.stop
447+
if any(stop_string == "" for stop_string in stop_strings):
448+
raise ValueError("stop must not contain an empty string")
449+
442450
self.logprobs_mode = LogprobMode(self.logprobs_mode)
443451

444452
if self.truncate_prompt_tokens is not None and self.truncate_prompt_tokens < 1:

‎tests/unittest/llmapi/test_sampling_params.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,32 @@ def test_sampling_params_accepts_in_range_values(field, value):
305305
assert getattr(SamplingParams(**{field: value}), field) == value
306306

307307

308+
@pytest.mark.parametrize("stop", ["", [""], ["a", ""]])
309+
def test_sampling_params_rejects_empty_stop_string(stop):
310+
with pytest.raises(ValueError, match="stop must not contain an empty string"):
311+
SamplingParams(stop=stop)
312+
313+
314+
@pytest.mark.parametrize("stop", [None, [], "a", ["a"], ["a", "b"], [" "]])
315+
def test_sampling_params_accepts_non_empty_stop_strings(stop):
316+
assert SamplingParams(stop=stop).stop == stop
317+
318+
319+
@pytest.mark.parametrize(
320+
"request_cls, base",
321+
[
322+
(CompletionRequest, {"prompt": "hi"}),
323+
(ChatCompletionRequest, {"messages": [{"role": "user", "content": "hi"}]}),
324+
],
325+
)
326+
@pytest.mark.parametrize("stop", ["", [""]])
327+
def test_serve_requests_reject_empty_stop_string(request_cls, base, stop):
328+
request = request_cls(model="test", stop=stop, **base)
329+
330+
with pytest.raises(ValueError, match="stop must not contain an empty string"):
331+
request.to_sampling_params(backend="pytorch")
332+
333+
308334
@pytest.mark.parametrize(
309335
"round_trip",
310336
[lambda params: pickle.loads(pickle.dumps(params)), copy.copy, copy.deepcopy],

0 commit comments

Comments
 (0)