-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[#11625][fix] handle list-typed eos_token_id in SamplingParams._setup #13178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edenfunf
wants to merge
2
commits into
NVIDIA:main
Choose a base branch
from
edenfunf:fix/sampling-params-list-eos-token-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−1
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright 2025 NVIDIA CORPORATION & AFFILIATES | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Unit tests for SamplingParams that do not require GPU or model weights.""" | ||
|
|
||
| from tensorrt_llm.sampling_params import SamplingParams | ||
|
|
||
|
|
||
| class _MockTokenizer: | ||
| """Minimal tokenizer stub for _setup() without real models.""" | ||
|
|
||
| def __init__(self, eos_token_id, pad_token_id=None): | ||
| self.eos_token_id = eos_token_id | ||
| self.pad_token_id = pad_token_id | ||
|
|
||
|
|
||
| def test_setup_scalar_eos_token_id(): | ||
| """_setup with a scalar eos_token_id sets end_id correctly.""" | ||
| tokenizer = _MockTokenizer(eos_token_id=2, pad_token_id=0) | ||
| params = SamplingParams() | ||
| params._setup(tokenizer, hf_model_config=None, generation_config=None) | ||
|
|
||
| assert params.end_id == 2 | ||
| assert params.pad_id == 0 | ||
| assert not params.stop_token_ids | ||
|
|
||
|
|
||
| def test_setup_list_eos_token_id_single_element(): | ||
| """_setup with a single-element list behaves like a scalar.""" | ||
| tokenizer = _MockTokenizer(eos_token_id=[128001], pad_token_id=0) | ||
| params = SamplingParams() | ||
| params._setup(tokenizer, hf_model_config=None, generation_config=None) | ||
|
|
||
| assert params.end_id == 128001 | ||
| assert params.pad_id == 0 | ||
| assert not params.stop_token_ids | ||
|
|
||
|
|
||
| def test_setup_list_eos_token_id_multiple_elements(): | ||
| """Regression for GitHub issue #11625. | ||
|
|
||
| _setup with a multi-element eos_token_id list (e.g. Llama 3.1) must set | ||
| end_id to a scalar int — not a list — to avoid std::bad_cast in the C++ | ||
| binding. Extra EOS tokens go into stop_token_ids so generation stops on | ||
| all of them. | ||
| """ | ||
| tokenizer = _MockTokenizer(eos_token_id=[128001, 128009], pad_token_id=0) | ||
| params = SamplingParams() | ||
| params._setup(tokenizer, hf_model_config=None, generation_config=None) | ||
|
|
||
| assert isinstance(params.end_id, int), ( | ||
| "end_id must be int, not list, to avoid std::bad_cast in C++ binding" | ||
| ) | ||
| assert params.end_id == 128001 | ||
| assert params.stop_token_ids is not None | ||
| assert 128009 in params.stop_token_ids | ||
| assert 128001 not in params.stop_token_ids | ||
|
|
||
|
|
||
| def test_setup_list_eos_token_id_no_duplicate_stop_tokens(): | ||
| """Extra EOS tokens are not added to stop_token_ids twice. | ||
|
|
||
| When the user already specified them explicitly, no duplicate should appear. | ||
| """ | ||
| tokenizer = _MockTokenizer(eos_token_id=[128001, 128009], pad_token_id=0) | ||
| params = SamplingParams(stop_token_ids=[128009]) | ||
| params._setup(tokenizer, hf_model_config=None, generation_config=None) | ||
|
|
||
| assert params.end_id == 128001 | ||
| assert params.stop_token_ids.count(128009) == 1 | ||
|
|
||
|
|
||
| def test_setup_end_id_already_set_ignores_tokenizer_eos(): | ||
| """When end_id is explicitly provided, _setup must not overwrite it.""" | ||
| tokenizer = _MockTokenizer(eos_token_id=[128001, 128009], pad_token_id=0) | ||
| params = SamplingParams(end_id=99) | ||
| params._setup(tokenizer, hf_model_config=None, generation_config=None) | ||
|
|
||
| assert params.end_id == 99 | ||
| assert not params.stop_token_ids | ||
|
|
||
|
|
||
| def test_setup_list_eos_token_id_pad_fallback(): | ||
| """When pad_token_id is None, pad_id falls back to end_id (an int).""" | ||
| tokenizer = _MockTokenizer(eos_token_id=[128001, 128009], pad_token_id=None) | ||
| params = SamplingParams() | ||
| params._setup(tokenizer, hf_model_config=None, generation_config=None) | ||
|
|
||
| assert params.end_id == 128001 | ||
| assert params.pad_id == 128001 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.