Open
[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置 #8888
[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置 #8888
Open
xuxingleiAdd on Oct 28
xuxinglei
xuxingleiCollaborators
Oct 28

https://gitcode.com/mindspore/mindformers/issues/2619

修改描述 (Description)

DeepSeekV3 PyNative YAML 中显式设置 scaling_factor: 1.0 或 2.0 时,现有模型配置转换器静默忽略该键,得到 rotary_scaling_factor=40.0。这会改变 MLA 使用的缩放配置。

在 DeepseekV3ConfigConverter 增加一条 scaling_factor → rotary_scaling_factor 映射,使显式值到达目标字段。省略时保持默认 40,原生目标字段、通用冲突检查和合法嵌套 rope_scaling 的既有优先级继续生效。修改不改变 RoPE/attention 算法、精度、算子或规约方式。

新增 CPU 测试覆盖实际模型配置入口、缺省与明确标量、既有40、原生字段、同值别名、双字段冲突以及嵌套配置优先级。

修改类型 (Type of Change)

测试结果 (Test Results)

基于官方 master 902de5f0aa5f16c77fe8f45c57550d333ac32634,独立 head e5fe163c59cdc473c7b34393292ee83a9b750a65,Python 3.12.14、MindSpore 2.12.0、NumPy 1.26.4。真实 MindSpore CPU 导入,私有 LF checkout、日志和输出目录隔离;测试前后核源码及原训练环境指纹。保留现有依赖路径,在导入 MindFormers 前选择 CPU,等价测试选择为:

import mindspore as ms
ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU")
import pytest
raise SystemExit(pytest.main([
    "-q", "-p", "no:cacheprovider",
    "tests/st/test_ut/test_parallel_core/test_config_converter/test_deepseek_v3_scaling_factor.py",
    "tests/st/test_ut/test_parallel_core/test_config_converter/test_config_converter.py",
]))

旧 902 上的新增测试:5 failed / 5 passed,模型入口也实际出现 40.0 != 2.0;候选:21 passed(10 个新增 + 11 个既有),用时 1.67 秒。环境中无 pylint,本轮未安装或运行。回归未执行模型前反向或训练,也不代表最终跨框架精度验收。

该修改只增加配置解析规则,不增加运行时张量操作;未进行无关的性能或显存微基准。真实模型语义及后续续训验证由关联总问题单继续记录。

检查清单 (Checklist)

补充测试结果:集成版本的真实 NPU 首微批

在 Atlas 910B3、MindSpore 2.12.0 / CANN 9.2.0 上,另以集成源码 6d8206686b116d397273b22971a6934d8accf47f 执行了三层(2 Dense + 1 MoE)、无 MTP 的真实首微批前向与反向。6d 基于已集成单卡重算/数据修复的 ce,配置转换器与新增测试文件的 blob 与本 PR 独立 head e5fe163c59cdc473c7b34393292ee83a9b750a65 一致;这是集成版本的 NPU 诊断,不是 e5fe 单独 exact-head 的 NPU 回归。

MF 从同一真实 MF0 核对 209 个状态,HP 从对应转换结果核对 144 项;两侧使用的首批 input_ids、labels、loss_mask、position_ids 摘要一致,批次形状为 4×4096。训练 seed 为 42,数据集 random_seed 默认 1234。每次反向沿单卡 128 微批累积口径除以 128,本次只执行其中 1 个微批,不做 optimizer 更新或训练循环。MF controller 10/10 检查通过,源码和环境前后指纹一致;69 个梯度为 FP32,三层均观察到 1 次原始前向与 1 次重算。

三层 live 配置的 rotary_scaling_factor 均为 1.0,attention softmax_scale 与 FlashAttention Cell 属性 scalar_value 均为 0.07216878364870323(约为 1/sqrt(192))。这是 Cell 实际属性观测,不是 Primitive 钩子;旧 ce 的相同位置此前观察为 0.1352337788608801。

MF 原始反向返回 0.0935668796300888,乘 128 的派生值为 11.976560592651367;同源 HP 首微批派生值 11.976579666137695,HP−MF 约 +1.91e-5。这些值不是独立观测的未缩放前向 loss;69→57 映射中仍只有 1/57 梯度 SHA 相同。单微批 loss 接近不证明梯度逐元素一致,也不是从真实第 500 步恢复或第 501–1000 步精度验收。

MF worker report SHA256 2c92d6094da5e5c9a4a56f7f8cdf2255fb1abd9385de5ae426d6c6662c3d3994,controller receipt SHA256 77e34fb931f4893cd0037252c7b3b67cc7d13340b7178f3e94575fc45ba21079;完整对照和限制由 HP/MF 单卡对齐 #304 跟踪。本地证据不是公开附件。

likedislike
Merge blocked
xuxingleixuxingleiCollaborators
Oct 28 Created pull request, commit d0cd521c
xuxingleixuxingleiCollaborators
Oct 28 Associated issue: [Bug]: DeepSeekV3 PyNative 静默忽略 scaling_factor 配置
atomgit-bot
atomgit-botCollaborators
Oct 28 Comments:

变更摘要

本 PR 修复 DeepSeekV3 PyNative 配置中 scaling_factor 被静默忽略的问题。此前用户在 YAML 中显式设置 scaling_factor: 1.0 或 2.0 时,配置转换器不会识别该键,导致 MLA 最终仍使用默认的 rotary_scaling_factor=40.0。改动在 DeepseekV3ConfigConverter 的字段映射表中新增一条 scaling_factor → rotary_scaling_factor 映射,使显式标量值能正确到达目标字段,同时保留省略时默认 40、原生 rotary_scaling_factor 字段、通用冲突检查以及合法嵌套 rope_scaling 的既有优先级。本改动不涉及 RoPE/attention 算法、精度、算子或规约方式的变化。

主要改动

  • 新增标量别名映射: 在 mindformers/models/deepseek3/config_converter_deepseek_v3.py 的 DeepseekV3ConfigConverter 映射表中加入 "scaling_factor": "rotary_scaling_factor",使显式设置的单值缩放因子透传到 MLA 使用的目标字段。
  • 新增标量别名与既有行为测试: 新文件 test_deepseek_v3_scaling_factor.py 通过参数化用例覆盖缺省值(40.0)、显式 1.0/2.0/40.0、原生 rotary_scaling_factor 输入以及双字段同值等场景。
  • 覆盖冲突检测: 新增用例验证 scaling_factor 与 rotary_scaling_factor 取值不一致时(两种顺序)仍抛出 ValueError,即既有映射冲突规则同样作用于新别名。
  • 覆盖嵌套配置优先级与模型入口: 验证 rope_scaling 嵌套 factor 在转换后仍优先于顶层标量,并通过 DeepseekV3Config.convert_to_transformer_config() 确认实际模型配置入口同样保留该标量值。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Oct 28 Comments:

代码审查

审查结论

逐文件核对

1. mindformers/models/deepseek3/config_converter_deepseek_v3.py(+1 行)— 未发现问题

  • 新增映射 "scaling_factor": "rotary_scaling_factor"(第 52 行)位于 # MLA 分组,与相邻 rope_theta → rotary_base、qk_nope_head_dim → qk_head_dim 形式一致(纯字符串映射,签名/契约未变)。
  • 目标字段消费点核对:configuration_deepseek_v3.py:257 通过 DeepseekV3ConfigConverter.convert(self.to_dict(), is_mla_model=...);三个实际调用点 modeling_deepseek_v3_train.py:34、modeling_deepseek_v3_pynative.py:36、modeling_deepseek_v3_infer.py:51 全部使用 is_mla_model=True,即一律产出 MLATransformerConfig,不存在“非 MLA 模型收到不存在的 rotary_scaling_factor 而 TypeError→RuntimeError”的路径。
  • 与未改动代码的一致性:DeepseekV3Config 未声明 scaling_factor 默认值(仅经 **kwargs 透传),因此省略时不产生该键,行为保持默认值;仓库示例配置写死的 scaling_factor: 40 与默认值相同,透传后数值不变,未引入数值回归。
  • 无拼写/死代码/异常处理/安全问题引入。

2. tests/st/test_ut/test_parallel_core/test_config_converter/test_deepseek_v3_scaling_factor.py(新增 72 行)— 2 个 P3 可选改进

  • 模块级 pytestmark 与既有 test_config_converter.py 的三个 marker 一致;该目录既有用例同样不引入 mindspore/set_context,约定一致,不构成缺陷。
  • _convert 的 convert(..., is_mla_model=True) 调用形式与既有测试一致;reverse 参数化通过改变 dict 插入顺序,对冲突检测方向确有覆盖意义。
  • 余下见下列 2 条 P3(均为可选改进,非必须修复)。

问题统计

  • P0:0
  • P1:0
  • P2:0
  • P3:2(均在新增测试文件中,指向测试判别力与契约固化,置信度 0.35,已标注为可选改进)

整体风险判断

生产改动仅一条纯配置映射,语义与 issue #2619 诉求一致,调用链上无 is_mla_model=False 分支,默认值路径不变,未发现正确性、安全、性能或破坏性变更风险。整体风险低;两条 P3 仅涉及新增测试的自证强度,可选择性采纳。

类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
Incorrect?
MindSpore-BotMindSpore-BotCollaborators
Oct 28 Added label: mindspore-cla/yes
MindSpore-Bot
MindSpore-BotCollaborators
Oct 28 Comments:

CLA Signature Pass

xuxinglei, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
昇思微(机器人)
昇思微(机器人)Collaborators
Oct 28 Comments:

@xuxinglei, 当前/check-pr未通过,原因如下:

以下Pull Request描述检查项未通过:
存在不符合模板的选项: Bug 修复 (Bug fix)
存在不符合模板的选项: 新特性 (New feature)
存在不符合模板的选项: 文档更新 (Documentation update)
存在不符合模板的选项: 代码重构 (Refactoring)
存在不符合模板的选项: 性能优化 (Performance improvement)
存在不符合模板的选项: 其他 (Other)
存在不符合模板的选项: 我已自验过该功能/修复 (Self-checked)
存在不符合模板的选项: 我已通过了本地的单元测试 (Passed local UTs)
存在不符合模板的选项: (如适用) 我已更新了对应文档 (Updated documentation)
存在不符合模板的选项: (如适用) 我已添加了新的测试用例 (Added new tests)
部分检查项缺失 请重新使用模板
模板中'Test Plan and Test Result' 信息为空,请补充对应信息。
/kind为空或类型无效, 请指定为 [ 'bug', 'task', 'feature', 'refactor', 'clean_code' ] 中的一种

请修改好上述检查错误后,重新使用/check-pr触发检查。

likedislike
昇思微(机器人)昇思微(机器人)Collaborators
Oct 28 Added label: pr-check-fail
atomgit-bot
atomgit-botCollaboratorsOct 28Start code review1
tests/st/test_ut/test_parallel_core/test_config_converter/test_deepseek_v3_scaling_factor.py
@@ -0,0 +39,4 @@
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
atomgit-botOct 28Comments:

🔵 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?
atomgit-bot
atomgit-botCollaboratorsOct 28Start code review1
tests/st/test_ut/test_parallel_core/test_config_converter/test_deepseek_v3_scaling_factor.py
@@ -0,0 +63,4 @@
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
atomgit-botOct 28Comments:

🔵 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?
xuxingleixuxingleiCollaborators
Oct 28 Modify description of pull request
xuxingleixuxingleiCollaborators
Oct 28 Change title to "[草稿] [Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置", originally titled "[Bug修复] 透传 DeepSeekV3 PyNative 的 scaling_factor 配置".
MindSpore-Bot
MindSpore-BotCollaborators
7 days ago Comments:

PR Merge Reminder from Scavenger:
This PR has been inactive for 30 days (as no new commits, comments, configurations) and will be closed as it becomes obsolete after 30 days of continued inactivity.
Please track the PR merging process in time, and respond according to the relevant prompts given by the robot to speed up the PR merging.
该PR已经闲置30天(因没有新的提交、评论、配置)且将因继续闲置30天后变得过时而被关闭。
请及时跟踪PR的合入流程并根据机器人给出的提示做出相应的响应以加速PR的合入。

likedislike