-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Fix added tokens config with sensible filter #17905
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
Merged
Fridge003
merged 3 commits into
sgl-project:main
from
sbeurnier:fix-lora-validation-and-added-tokens
Apr 1, 2026
Merged
Changes from 2 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
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
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,138 @@ | ||
| # Copyright 2023-2024 SGLang Team | ||
|
Fridge003 marked this conversation as resolved.
Outdated
|
||
| # 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. | ||
| # ============================================================================== | ||
| """Unit tests for LoRAConfig functionality.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from sglang.srt.lora.lora_config import LoRAConfig | ||
| from sglang.test.ci.ci_register import register_cpu_ci | ||
|
|
||
| register_cpu_ci(est_time=10, suite="stage-a-cpu-only") | ||
|
|
||
|
|
||
| class TestLoRAConfigFilterAddedTokens(unittest.TestCase): | ||
| """Test cases for LoRAConfig.filter_added_tokens method.""" | ||
|
|
||
| def _create_config_with_added_tokens(self, added_tokens: dict) -> LoRAConfig: | ||
| """Helper to create a LoRAConfig with specified added_tokens.""" | ||
| config_dict = { | ||
| "target_modules": ["q_proj", "k_proj", "v_proj"], | ||
| "r": 8, | ||
| "lora_alpha": 16, | ||
| } | ||
| return LoRAConfig.from_dict(config_dict, added_tokens_config=added_tokens) | ||
|
|
||
| def test_filter_no_added_tokens(self): | ||
| """Test filtering when there are no added tokens.""" | ||
| config = self._create_config_with_added_tokens(None) | ||
| self.assertEqual(config.lora_added_tokens_size, 0) | ||
|
|
||
| config.filter_added_tokens(base_vocab_size=32000) | ||
| self.assertEqual(config.lora_added_tokens_size, 0) | ||
| self.assertIsNone(config.added_tokens_config) | ||
|
|
||
| def test_filter_empty_added_tokens(self): | ||
| """Test filtering when added_tokens is empty dict.""" | ||
| config = self._create_config_with_added_tokens({}) | ||
| self.assertEqual(config.lora_added_tokens_size, 0) | ||
|
|
||
| config.filter_added_tokens(base_vocab_size=32000) | ||
| self.assertEqual(config.lora_added_tokens_size, 0) | ||
|
|
||
| def test_filter_all_fake_tokens(self): | ||
| """Test filtering when all tokens are fake (ID < base_vocab_size).""" | ||
| # These tokens have IDs less than base_vocab_size (32000) | ||
| added_tokens = { | ||
| "<pad>": 0, | ||
| "<eos>": 2, | ||
| "<bos>": 1, | ||
| } | ||
| config = self._create_config_with_added_tokens(added_tokens) | ||
| self.assertEqual(config.lora_added_tokens_size, 3) | ||
|
|
||
| config.filter_added_tokens(base_vocab_size=32000) | ||
| self.assertEqual(config.lora_added_tokens_size, 0) | ||
| self.assertEqual(config.added_tokens_config, {}) | ||
|
|
||
| def test_filter_all_real_tokens(self): | ||
| """Test filtering when all tokens are real (ID >= base_vocab_size).""" | ||
| base_vocab_size = 32000 | ||
| # These tokens have IDs >= base_vocab_size | ||
| added_tokens = { | ||
| "<new_token_1>": 32000, | ||
| "<new_token_2>": 32001, | ||
| "<new_token_3>": 32002, | ||
| } | ||
| config = self._create_config_with_added_tokens(added_tokens) | ||
| self.assertEqual(config.lora_added_tokens_size, 3) | ||
|
|
||
| config.filter_added_tokens(base_vocab_size=base_vocab_size) | ||
| self.assertEqual(config.lora_added_tokens_size, 3) | ||
| self.assertEqual(config.added_tokens_config, added_tokens) | ||
|
|
||
| def test_filter_mixed_tokens(self): | ||
| """Test filtering with both fake and real tokens.""" | ||
| base_vocab_size = 32000 | ||
| added_tokens = { | ||
| # Fake tokens (ID < base_vocab_size) | ||
| "<pad>": 0, | ||
| "<eos>": 2, | ||
| # Real tokens (ID >= base_vocab_size) | ||
| "<new_token_1>": 32000, | ||
| "<new_token_2>": 32001, | ||
| } | ||
| config = self._create_config_with_added_tokens(added_tokens) | ||
| self.assertEqual(config.lora_added_tokens_size, 4) | ||
|
|
||
| config.filter_added_tokens(base_vocab_size=base_vocab_size) | ||
| self.assertEqual(config.lora_added_tokens_size, 2) | ||
| self.assertEqual( | ||
| config.added_tokens_config, | ||
| {"<new_token_1>": 32000, "<new_token_2>": 32001}, | ||
| ) | ||
|
|
||
| def test_filter_boundary_token(self): | ||
| """Test token exactly at base_vocab_size boundary.""" | ||
| base_vocab_size = 32000 | ||
| added_tokens = { | ||
| "<at_boundary>": 32000, # Exactly at boundary, should be kept | ||
| "<below_boundary>": 31999, # Just below, should be filtered | ||
| } | ||
| config = self._create_config_with_added_tokens(added_tokens) | ||
|
|
||
| config.filter_added_tokens(base_vocab_size=base_vocab_size) | ||
| self.assertEqual(config.lora_added_tokens_size, 1) | ||
| self.assertEqual(config.added_tokens_config, {"<at_boundary>": 32000}) | ||
|
|
||
| def test_filter_idempotent(self): | ||
| """Test that calling filter_added_tokens multiple times is safe.""" | ||
| base_vocab_size = 32000 | ||
| added_tokens = { | ||
| "<fake>": 100, | ||
| "<real>": 32000, | ||
| } | ||
| config = self._create_config_with_added_tokens(added_tokens) | ||
|
|
||
| # First call | ||
| config.filter_added_tokens(base_vocab_size=base_vocab_size) | ||
| self.assertEqual(config.lora_added_tokens_size, 1) | ||
|
|
||
| # Second call should not change anything | ||
| config.filter_added_tokens(base_vocab_size=base_vocab_size) | ||
| self.assertEqual(config.lora_added_tokens_size, 1) | ||
| self.assertEqual(config.added_tokens_config, {"<real>": 32000}) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main(warnings="ignore") | ||
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.