Repository navigation
[Fix] Validate user-supplied input_ids against the vocabulary #31908
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
v-code01
wants to merge
1
commit into
sgl-project:main
Choose a base branch
from
v-code01:fix/validate-user-input-ids
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.
+74
−12
Open
Changes from all commits
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
56 changes: 56 additions & 0 deletions
56
test/registered/unit/managers/test_tokenizer_manager_input_ids_validation.py
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,56 @@ | ||
| """Unit test for TokenizerManager._validate_input_ids_in_vocab. | ||
|
|
||
| Bug: user-supplied input_ids (native /generate and OpenAI token-id prompts) were | ||
| never validated -- _validate_input_ids_in_vocab existed but had zero callers, and | ||
| even it only checked id >= vocab_size (missing id < 0). An out-of-range id indexes | ||
| the model's embedding out of bounds (on CUDA a device-side assert that crashes the | ||
| whole server); a negative id silently wraps to the end of the table. | ||
| """ | ||
|
|
||
| import unittest | ||
| from types import SimpleNamespace | ||
|
|
||
| from sglang.srt.managers.tokenizer_manager import TokenizerManager | ||
| from sglang.test.ci.ci_register import register_cpu_ci | ||
| from sglang.test.test_utils import CustomTestCase | ||
|
|
||
| register_cpu_ci(est_time=1, suite="base-a-test-cpu") | ||
|
|
||
| VOCAB_SIZE = 32000 | ||
|
|
||
|
|
||
| class TestValidateInputIds(CustomTestCase): | ||
| def setUp(self): | ||
| self.tm = TokenizerManager.__new__(TokenizerManager) | ||
| self.tm.model_config = SimpleNamespace(vocab_size=VOCAB_SIZE) | ||
|
|
||
| def _validate(self, ids): | ||
| self.tm._validate_input_ids_in_vocab(ids, VOCAB_SIZE) | ||
|
|
||
| def test_above_vocab_raises(self): | ||
| with self.assertRaises(ValueError): | ||
| self._validate([1, 2, VOCAB_SIZE]) | ||
| with self.assertRaises(ValueError): | ||
| self._validate([1, 2, 2_000_000_000]) | ||
|
|
||
| def test_negative_raises(self): | ||
| # The previous check only guarded id >= vocab_size, so this used to pass. | ||
| with self.assertRaises(ValueError): | ||
| self._validate([1, -1, 2]) | ||
|
|
||
| def test_batch_out_of_range_raises(self): | ||
| with self.assertRaises(ValueError): | ||
| self._validate([[1, 2], [3, VOCAB_SIZE + 5]]) | ||
| with self.assertRaises(ValueError): | ||
| self._validate([[1, 2], [-4, 5]]) | ||
|
|
||
| def test_in_range_passes(self): | ||
| self._validate([0, 1, VOCAB_SIZE - 1]) | ||
| self._validate([[0, 1], [2, VOCAB_SIZE - 1]]) | ||
|
|
||
| def test_empty_passes(self): | ||
| self._validate([]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The vocabulary validation for user-supplied
input_idsis currently restricted toGenerateReqInputrequests. However,EmbeddingReqInputrequests can also accept user-suppliedinput_ids(e.g., via the embedding endpoints). If an out-of-bounds token ID is supplied to an embedding request, it will also trigger a CUDA device-side assert or index out-of-bounds error, crashing the server or causing silent corruption.Moving this validation outside of the
isinstance(obj, GenerateReqInput)block ensures that both generation and embedding requests are properly validated.