Open
[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置 #8888
xuxingleiAdd on Oct 28
[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置 #8888
Open
Total: 2 files changed+73-0
| @@ -49,6 +49,7 @@ class DeepseekV3ConfigConverter(ConfigConverter): | |||
| 49 | "moe_token_drop_policy": ("moe_token_drop_policy", get_drop_policy), | 49 | "moe_token_drop_policy": ("moe_token_drop_policy", get_drop_policy), |
| 50 | # MLA | 50 | # MLA |
| 51 | "rope_theta": "rotary_base", | 51 | "rope_theta": "rotary_base", |
| 52 | + "scaling_factor": "rotary_scaling_factor", | ||
| 52 | "qk_nope_head_dim": "qk_head_dim", | 53 | "qk_nope_head_dim": "qk_head_dim", |
| 53 | "qk_rope_head_dim": "qk_pos_emb_head_dim", | 54 | "qk_rope_head_dim": "qk_pos_emb_head_dim", |
| 54 | } | 55 | } |
| @@ -0,0 +1,72 @@ | |||
| 1 | +# Copyright 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# | ||
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | +# you may not use this file except in compliance with the License. | ||
| 5 | +# You may obtain a copy of the License at | ||
| 6 | +# | ||
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | +# | ||
| 9 | +# Unless required by applicable law or agreed to in writing, software | ||
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | +# See the License for the specific language governing permissions and | ||
| 13 | +# limitations under the License. | ||
| 14 | +# ============================================================================ | ||
| 15 | +"""DeepSeek-V3's scalar RoPE factor reaches the actual PyNative converter.""" | ||
| 16 | +from copy import deepcopy | ||
| 17 | + | ||
| 18 | +import pytest | ||
| 19 | + | ||
| 20 | +from mindformers.models.deepseek3.config_converter_deepseek_v3 import DeepseekV3ConfigConverter | ||
| 21 | +from mindformers.models.deepseek3.configuration_deepseek_v3 import DeepseekV3Config | ||
| 22 | + | ||
| 23 | +pytestmark = [pytest.mark.level0, pytest.mark.platform_x86_cpu, pytest.mark.env_onecard] | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +def _model_config(**overrides): | ||
| 27 | + config = {"num_hidden_layers": 2, "first_k_dense_replace": 1, | ||
| 28 | + "hidden_size": 128, "num_attention_heads": 8, | ||
| 29 | + "multi_latent_attention": True, "position_embedding_type": "rope"} | ||
| 30 | + config.update(overrides) | ||
| 31 | + return config | ||
| 32 | + | ||
| 33 | + | ||
| 34 | +def _convert(overrides): | ||
| 35 | + return DeepseekV3ConfigConverter.convert(deepcopy(_model_config(**overrides)), is_mla_model=True) | ||
| 36 | + | ||
| 37 | + | ||
| 38 | +@pytest.mark.parametrize("overrides, expected", [ | ||
| 39 | + ({}, 40.0), | ||
| 40 | + ({"scaling_factor": 1.0}, 1.0), | ||
| 41 | + ({"scaling_factor": 2.0}, 2.0), | ||
| 42 | + ({"scaling_factor": 40.0}, 40.0), | ||
| 43 | + ({"rotary_scaling_factor": 2.0}, 2.0), | ||
| 44 | + ({"scaling_factor": 1.0, "rotary_scaling_factor": 1.0}, 1.0), | ||
| 45 | +]) | ||
| 46 | +def test_scalar_factor_alias_and_existing_inputs(overrides, expected): | ||
| 47 | + """An explicit scalar reaches MLA while omission and the native field keep their behavior.""" | ||
| 48 | + assert _convert(overrides).rotary_scaling_factor == expected | ||
| 49 | + | ||
| 50 | + | ||
| 51 | +@pytest.mark.parametrize("reverse", [False, True]) | ||
| 52 | +def test_conflicting_top_level_factors_are_rejected(reverse): | ||
| 53 | + """The existing converter conflict rule also applies to the newly recognized alias.""" | ||
| 54 | + values = [("scaling_factor", 1.0), ("rotary_scaling_factor", 2.0)] | ||
| 55 | + if reverse: | ||
| 56 | + values.reverse() | ||
| 57 | + with pytest.raises(ValueError, match="Mapping Conflicts"): | ||
| 58 | + _convert(dict(values)) | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +def test_nested_rope_scaling_keeps_existing_precedence(): | ||
| 62 | + """The dataclass still resolves the supported nested factor after top-level conversion.""" | ||
| 63 | + config = _convert({"position_embedding_type": "yarn", "scaling_factor": 1.0, | ||
| 64 | + "rope_scaling": {"type": "yarn", "factor": 2.0}}) | ||
| 65 | + assert config.position_embedding_type == "yarn" | ||
| 66 | + assert config.rotary_scaling_factor == 2.0 | ||
🔵 Low Priority 改动行(tests/.../test_deepseek_v3_scaling_factor.py:63-66)断言:当同时给出顶层 证据链:
建议:明确 scaling_factor 与 rope_scaling.factor 并存时的契约:或扩展冲突检查在两者不相等时报错/告警,或在该用例 docstring 中写明顶层标量被有意丢弃,避免后续改动被这条用例锁死。 ![]() ![]() Incorrect? | |||
| 67 | + | ||
| 68 | + | ||
| 69 | +def test_model_config_uses_the_same_factor_alias(): | ||
| 70 | + """The model's configuration entry point preserves the scalar from a PyNative YAML.""" | ||
| 71 | + config = DeepseekV3Config(**_model_config(scaling_factor=2.0)) | ||
| 72 | + assert config.convert_to_transformer_config().rotary_scaling_factor == 2.0 | ||


🔵 Low Priority
改动行(tests/.../test_deepseek_v3_scaling_factor.py:42)为
({"scaling_factor": 40.0}, 40.0)。证据链:
({}, 40.0)已断言:完全省略scaling_factor时目标字段就是 40.0(解析后的默认值)。scaling_factor: 40,与默认值相同,无法暴露问题);它给后续读者一种“40 这条路径已被覆盖”的错觉。建议:把该参数化项替换为与默认值不同的取值(如 8.0),或改为断言显式 40.0 与原生字段 40.0 结果一致且不报冲突,使其在 scaling_factor 映射缺失时能够失败。