Open
[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置 #8888
[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置 #8888
Open
xuxingleiAdd on Oct 28
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 # MLA50 # 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),
atomgit-bot

🔵 Low Priority

改动行(tests/.../test_deepseek_v3_scaling_factor.py:42)为 ({"scaling_factor": 40.0}, 40.0)。

证据链:

  1. 同文件 39 行的 ({}, 40.0) 已断言:完全省略 scaling_factor 时目标字段就是 40.0(解析后的默认值)。
  2. 因此 42 行“显式 40.0 → 40.0”与 39 行“省略 → 40.0”在断言层面完全等价:即使映射被删除(即本 issue 修复被回退),该参数化用例依然通过。
  3. 失败模式:该用例对“别名是否真正透传”没有判别力,恰好复刻了 issue #2619 能长期潜伏的原因(仓库 YAML 用的就是 scaling_factor: 40,与默认值相同,无法暴露问题);它给后续读者一种“40 这条路径已被覆盖”的错觉。

建议:把该参数化项替换为与默认值不同的取值(如 8.0),或改为断言显式 40.0 与原生字段 40.0 结果一致且不报冲突,使其在 scaling_factor 映射缺失时能够失败。

likedislike
Incorrect?
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
atomgit-bot

🔵 Low Priority

改动行(tests/.../test_deepseek_v3_scaling_factor.py:63-66)断言:当同时给出顶层 scaling_factor: 1.0 与 rope_scaling: {"type": "yarn", "factor": 2.0} 时,最终 rotary_scaling_factor == 2.0。也就是说,本 PR 新增的 scaling_factor → rotary_scaling_factor 别名在这一组合下仍被静默丢弃。

证据链:

  1. 同一文件的 51-58 行断言 scaling_factor=1.0 与 rotary_scaling_factor=2.0 并存时抛 ValueError("Mapping Conflicts"),即“同一目标字段出现两个不同来源 → 硬报错”。
  2. 而 63-66 行显示,rope_scaling.factor 与 scaling_factor 落到同一个目标字段时并不报冲突,而是后者被前者覆盖且无任何告警。
  3. 触发条件:PyNative/HF 导出的 DeepSeek 配置中同时携带 scaling_factor 与 rope_scaling(HF 侧导出配置常见形态)。
  4. 失败模式:同一类“显式输入被静默忽略”的语义出现两套行为——一种报错、一种无声覆盖;用户写 scaling_factor: 1.0(期望 MLA softmax scale ≈ 1/sqrt(d))却得到 factor=2.0 的 mscale,且没有任何日志,正是本 issue 想消除的静默失效形态。

建议:明确 scaling_factor 与 rope_scaling.factor 并存时的契约:或扩展冲突检查在两者不相等时报错/告警,或在该用例 docstring 中写明顶层标量被有意丢弃,避免后续改动被这条用例锁死。

likedislike
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