-
Notifications
You must be signed in to change notification settings - Fork 64
fix: resolve rope_theta from wherever the config stores it #166
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
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # Copyright (c) 2026 LightSeek Foundation | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| """Tests that a draft's rotary base is the one its config declares. | ||
|
|
||
| transformers 5.x moved `rope_theta` from a top-level config attribute into | ||
| `rope_parameters`, which silently reduced every affected draft to the library | ||
| default of 10000 while its config advertised something else. The sweep over | ||
| `configs/draft_models/` is the part that fails if a future release moves the | ||
| field again. | ||
| """ | ||
|
|
||
| import glob | ||
| import json | ||
| import os | ||
| import unittest | ||
|
|
||
| from transformers import LlamaConfig | ||
| from transformers.models.deepseek_v3.configuration_deepseek_v3 import DeepseekV3Config | ||
|
|
||
| from torchspec.config.utils import resolve_rope_theta | ||
| from torchspec.models.draft.auto import AutoDraftModelConfig | ||
| from torchspec.models.draft.deepseek_eagle import DeepSeekMLAAttention | ||
| from torchspec.models.draft.dflash import DFlashAttention, DFlashConfig | ||
| from torchspec.models.draft.llama3_eagle import LlamaAttention | ||
|
|
||
| REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| DRAFT_CONFIG_DIR = os.path.join(REPO_ROOT, "configs", "draft_models") | ||
|
|
||
| YARN = { | ||
| "rope_type": "yarn", | ||
| "factor": 32.0, | ||
| "original_max_position_embeddings": 64, | ||
| "beta_fast": 32, | ||
| "beta_slow": 1, | ||
| "mscale": 1.0, | ||
| "mscale_all_dim": 1.0, | ||
| } | ||
|
|
||
|
|
||
| def _declared_theta(raw): | ||
| """The base a config file asks for, from wherever the file happens to put it.""" | ||
| block = raw.get("rope_parameters") or raw.get("rope_scaling") or {} | ||
| nested = block.get("rope_theta") | ||
| return nested if nested is not None else raw.get("rope_theta") | ||
|
|
||
|
|
||
| class TestResolveRopeTheta(unittest.TestCase): | ||
| def test_prefers_top_level_attribute(self): | ||
| cfg = DFlashConfig(rope_theta=50000.0) | ||
| self.assertEqual(resolve_rope_theta(cfg), 50000.0) | ||
|
|
||
| def test_falls_back_to_rope_parameters(self): | ||
| # transformers 5.x absorbs a top-level rope_theta into rope_parameters, | ||
| # leaving no attribute for the naive read to find. | ||
| cfg = LlamaConfig(rope_theta=1000000.0) | ||
| self.assertIsNone(getattr(cfg, "rope_theta", None)) | ||
| self.assertEqual(resolve_rope_theta(cfg), 1000000.0) | ||
|
|
||
| def test_default_when_declared_nowhere(self): | ||
| cfg = DFlashConfig() | ||
| del cfg.rope_theta | ||
| self.assertEqual(resolve_rope_theta(cfg), 10000.0) | ||
| self.assertIsNone(resolve_rope_theta(cfg, default=None)) | ||
|
|
||
| def test_conflicting_values_are_refused(self): | ||
| cfg = LlamaConfig(rope_theta=1000000.0) | ||
| cfg.rope_theta = 10000.0 # stale legacy value left beside the block | ||
| with self.assertRaisesRegex(ValueError, "Conflicting rope_theta"): | ||
| resolve_rope_theta(cfg) | ||
|
|
||
|
|
||
| class TestShippedDraftConfigs(unittest.TestCase): | ||
| """Every config in configs/draft_models/ must resolve to the base it declares.""" | ||
|
|
||
| def test_declared_theta_is_what_resolves(self): | ||
| paths = sorted(glob.glob(os.path.join(DRAFT_CONFIG_DIR, "*.json"))) | ||
| self.assertGreater(len(paths), 0, "no draft configs found") | ||
| for path in paths: | ||
| with self.subTest(config=os.path.basename(path)): | ||
| declared = _declared_theta(json.load(open(path))) | ||
| if declared is None: | ||
| self.skipTest("config declares no rope_theta") | ||
| cfg = AutoDraftModelConfig.from_file(path) | ||
| self.assertEqual(resolve_rope_theta(cfg), float(declared)) | ||
|
|
||
|
|
||
| class TestAttentionUsesResolvedTheta(unittest.TestCase): | ||
| """Each rotary construction path must reach the resolved base, not the default.""" | ||
|
|
||
| def test_llama_yarn_path(self): | ||
| cfg = LlamaConfig( | ||
| hidden_size=64, | ||
| num_attention_heads=4, | ||
| num_key_value_heads=4, | ||
| max_position_embeddings=2048, | ||
| rope_theta=1000000.0, | ||
| rope_parameters=dict(YARN), | ||
| ) | ||
| cfg.target_hidden_size = 64 | ||
| self.assertEqual(LlamaAttention(cfg).rotary_emb.base, 1000000.0) | ||
|
|
||
| def test_deepseek_mla_yarn_path(self): | ||
| cfg = DeepseekV3Config( | ||
| hidden_size=64, | ||
| num_attention_heads=4, | ||
| q_lora_rank=32, | ||
| kv_lora_rank=16, | ||
| qk_nope_head_dim=16, | ||
| qk_rope_head_dim=8, | ||
| v_head_dim=16, | ||
| max_position_embeddings=2048, | ||
| rope_theta=50000.0, | ||
| rope_parameters=dict(YARN), | ||
| ) | ||
| self.assertEqual(DeepSeekMLAAttention(cfg).rotary_emb.base, 50000.0) | ||
|
|
||
| def test_dflash_path(self): | ||
| cfg = DFlashConfig( | ||
| hidden_size=64, | ||
| num_attention_heads=4, | ||
| num_key_value_heads=2, | ||
| max_position_embeddings=512, | ||
| rope_theta=1000000.0, | ||
| ) | ||
| self.assertEqual(DFlashAttention(cfg).rotary_emb.base, 1000000.0) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
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.
When a DFlash/DSpark draft config is written in the new
rope_parametersform without a top-levelrope_theta,DFlashConfigstill createsself.rope_theta = 10000.0from its constructor default before preserving the nested field from**kwargs. This new conflict branch then raises even though the file only declared one value, so loading such a v5-style draft config fails instead of resolving the nested theta. Consider ignoring the class default when the top-level value was not explicitly provided, or normalizing these config classes before calling this helper.Useful? React with 👍 / 👎.