Draft
[WIP][refactor] 复用上游 DeepSeek-V4 GraphTrainer 配置转换 #1287
[WIP][refactor] 复用上游 DeepSeek-V4 GraphTrainer 配置转换 #1287
Draft
hanboyouAdd on 19 days ago
hanboyou
hanboyouCollaborators
19 days ago

描述

维护中的图训练配方原先通过模型层包装函数投影 Trainer 配置。删除该包装文件,在 recipe 中直接使用上游 derive(base, Trainer.Config) 和 to_graph_trainer_config,保留 parallelism、seq_len、hf_assets_path 及嵌套 NPU 组件配置。

功能归属:models/deepseek_v4/graph_trainer.py;两个 recipe 使用方、GraphTrainer 指南和 loader 行为测试一并调整,删除旧的源码形状断言。

类型

Checklist:

如何测试

基线:6e3c9320453c0e36875a1fb7f9af59abb57ed3b8;本分支:ead44d680db0070ea423421896bc37ce594aba4a,目标为 dev_0.4.0。

TORCH_DEVICE_BACKEND_AUTOLOAD=0 OMP_NUM_THREADS=1 python -m pytest -v --tb=short tests/unit_tests/recipes/test_deepseek_v4_examples.py tests/unit_tests/test_dsv4_muon.py

CPU 结果:22 项通过。新增测试通过真实 ConfigLoader 选择图训练 recipe,并检查模型、loss、训练组件、Muon profile、override 和文档对齐设置;它没有执行图捕获或编译训练。

完整本地 pre-commit --all-files 已在 CI 对应的 Torch 2.14 类型环境通过;仅在独立配置中将 Pyrefly 查找路径指向固定 TorchTitan 0.4.0,保留全部 15 个 hooks,并使用实际 checkpoint/grain 依赖别名。训练 CPU 测试使用 Torch 2.15;其全项目类型检查与目标分支有相同 8 条既有诊断、无新增。OAT 另对本 PR 的实际变更文件完成实扫。

完整 CPU 套件使用 TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -v --tb=short tests/unit_tests(去掉 -x,执行全部收集用例):1247 passed / 75 skipped / 1 xfailed / 2 failed,原始 exit 1。另一项 tooling 临时路径失败在物理可执行短路径下重跑通过。剩余可复现失败均为 test_virtual_optimizer_dcp_roundtrip_preserves_two_rank_dtensor_shards:原生 DCP 在 fork 子进程中初始化 NPU,未修改的目标分支同样失败;未发现新增可复现失败,完整套件仍未全绿。

本地原始日志、JUnit、复现环境与基线比较;保留全量运行与失败项重跑的独立结果,未修改测试或增加跳过。

远端 CI #2696 已通过,UT 为 1241 passed / 82 skipped / 1 xfailed。已核对实际检出的合并提交父节点,分别为上述目标基线与本分支;CI 的 workflow head 与被测代码 head 分开核对。该结果补充 compile 入口的验证,NPU ST 和训练对齐状态见下文。

精度和性能验证结果

当前分支的 NPU smoke/ST、100 步训练对齐、完整 GraphTrainer 训练及性能测试均未执行;原有 NPU GraphTrainer 限制保持,保留草稿。

历史整组记录见 #1257,其基线为 c84d8b76、整组版本为 7c4f16f0;该记录不是本拆分分支的独立验证,也不用于声明本分支精度或性能通过。

其他信息

旧 torchtitan_npu.models.deepseek_v4.graph_trainer 导入路径有意退役。只简化配置转换,不将 CPU 配置加载解释为整步 GraphTrainer 可用。 本 PR 是 #1257 的独立功能拆分,以草稿提交,等待对应分支的后续验证。

likedislike
Merge blocked
hanboyouhanboyouCollaborators
19 days ago Created pull request, commit de03ee68
CANN-robotCANN-robotCollaborators
19 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
19 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ❌ (0/2)(You can also ask: wanghui0, zhanghz1, 盛楠, zhuofanshen, 钟林) ❌ (0/1)(You can also ask: xuyujun, 盛楠, panchao-gitcode, zzyyjj012, zhanghz1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
CANN-robotCANN-robotCollaborators
19 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
hanboyouhanboyouCollaborators
19 days ago Modify description of pull request
hanboyou
hanboyouCollaborators
19 days ago Comments:

compile

likedislike
hanboyouhanboyouCollaborators
19 days ago Pre merge succeeded (commit_id: e9c97b1ebb0d64f5c6cfe88c1533db0c227868f3)
CANN-robot
CANN-robotCollaborators
19 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
19 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
19 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
19 days ago Added label: ci-pipeline-passed
hanboyouhanboyouCollaborators
19 days ago Modify description of pull request
titan-npu-reviewer
titan-npu-reviewer16 days agoStart code review1
docs/feature_guides/deepseek_v4_graph_trainer.md
@@ -3,7 +3,7 @@
33GraphTrainer 是 torchtitan 上游 `experiments/graph_trainer/` 中的编译式分布式训练实验:用 `minimal_fx_tracer` 把 forward、loss、backward(可选 optimizer.step)整体捕获为单个 FX 图,所有优化都以 graph pass 的形式作用于同一张图。本仓保留 DeepSeek-V4(DSV4)的 NPU 图训练实现;TorchTitan 0.4.0 的当前验收范围为文本 eager,图训练仍待迁移和回归。图训练配置位于独立 recipe 模块。
44 
55## 上游 GraphTrainer 特性
66 
titan-npu-reviewer
titan-npu-reviewer16 days agoComments:

缺少精度验证结果

本 PR 修改了可能影响数值结果的代码路径,但 PR 描述中没有看到精度验证结果及精度是否正常的说明。

请参考 精度对齐开发指南 完成精度验证,
并将验证结果(对齐目标、指标数值、是否满足阈值)写入 PR 描述。

问题编号:R1287-001

审查版本:ead44d680db0070ea423421896bc37ce594aba4a

likedislike
titan-npu-reviewer
titan-npu-reviewer
16 days ago Comments:

使用模型:deepseek-v4.1-flash[1m]

🤖 AI 代码审查报告

审查结论

  • 合入建议: ✅ 可以合入
  • 严重性: 建议(S3)
  • 代码质量: 良好(转换路径与旧实现等价,已按 0.4.0 源码逐字段核对)
  • 安全性: 未发现影响
  • 测试数据: 有覆盖(CPU UT,本次未复跑)
  • 文档完整性: 完整

该 PR 把模型层的配置投影包装换成上游 derive(base, Trainer.Config) + 上游 to_graph_trainer_config。按 requirements.txt 固定的 torchtitan 0.4.0(取自 PyPI wheel)核对:derive 只拷贝目标类的 init 字段并构造 Trainer.Config,而 Trainer.Config / TrainingEngine.Config 中没有 init=False 字段,与旧包装「遍历 fields(Trainer.Config) 逐个取值」得到的字段集合完全相同,所以顶层 extension/sdc/anticipatory 仍被丢弃、嵌套组件仍按引用保留;五个 recipe 和 _graph_example 的调用点语义不变。没有阻塞项,剩余两条建议级问题;改动的实际训练路径在 0.4.0 下仍被既有 guard 拦住,不涉及数值变化。

代码质量检查

正确性与鲁棒性

未发现明确问题

专项规则

已按根、torchtitan_npu/、torchtitan_npu/models/、tests/、tests/unit_tests/、docs/ 与 .agents/ 的 AGENTS.md 核对:删除模型层文件没有改变目录职责和导入顺序,python scripts/check_architecture_boundaries.py 实跑通过。未发现规则违规。

代码坏味道

未发现明确问题

设计与架构

  • 建议(S3): 被删除的投影包装并没有彻底退役——torchtitan_npu/models/deepseek_v4/config_registry.py:568 仍保留逐字相同的 to_graph_trainer_config(含 isinstance(TrainerEx.Config) 投影)和同一组 graph_trainer_deepseek_v4_* 工厂。这不是本 PR 引入的问题,但本次删除后仓内同时存在两套图训练配置转换实现,后续只改 torchtitan_npu_recipes/graph_trainer/deepseek_v4.py 时,config_registry 这个入口会继续沿用旧写法,逐渐与新路径不一致。建议确认该入口是否仍需保留:不再使用就一并删除,仍在使用就在后续 PR 改为上游转换。

涉及位置

  • torchtitan_npu/models/deepseek_v4/config_registry.py:568
def to_graph_trainer_config(base_config, model_config_cls):
    if isinstance(base_config, TrainerEx.Config):
        base_config = Trainer.Config(**{f.name: getattr(base_config, f.name) for f in fields(Trainer.Config)})
    from torchtitan.experiments.graph_trainer.configs import to_graph_trainer_config as _to_graph_trainer_config
    return _to_graph_trainer_config(base_config, model_config_cls)

上游交互

未发现明确问题

训练 infra 与 NPU 性能

未发现明确问题

文档同步建议

  • 结论:无需修改
  • 依据与建议:docs/feature_guides/deepseek_v4_graph_trainer.md:21 与 :31 已改成 derive + 上游转换器的描述,与 torchtitan_npu_recipes/graph_trainer/deepseek_v4.py 的实际实现一致;全仓(README、docs、.agents/notes、测试、CI)检索 models/deepseek_v4/graph_trainer 已无残留引用;docs/architecture.md:35 关于图训练组合归属的说明仍然成立。本 PR 不改变公开入口名和配置键,不需要额外文档改动。

改进建议

  • 可选:把新增的 loader 测试参数化到五个 graph recipe,一次覆盖被删测试原先逐名核对过的五个入口。最小断言可以是 isinstance(config, GraphTrainer.Config)、config.training 仍是 NPU TrainingConfig、config.optim.optimizer._muon_profile 非空。
  • 可选:确认 torchtitan_npu/models/deepseek_v4/config_registry.py 中重复的图训练工厂是否仍在用,按其结论删除或迁移,避免两套转换实现漂移。
  • PR 描述:数值验证一节目前只写「未执行」。按 docs/developer_guides/numerical_alignment.md §3,纯配置构造重构可以在 PR 中说明后跳过;建议补一句跳过依据(本次只改配置对象构造,不进入计算路径),不要留成未完成项。

没能验证的部分

  • 未运行作者给出的 pytest 命令,也未复跑完整 CPU 套件:本机缺 tokenizers 和 torch_npu,import torchtitan_npu 直接失败。所以「22 passed」「1247 passed / 2 failed」「CI #2696 通过」都只来自 PR 描述,新测试的运行时行为(尤其 ConfigLoader 转换链和 not hasattr(config, "extension"))没有实际确认。本次实际执行过的只有:ruff check 与 ruff format --check(0.14.14,与 .pre-commit-config.yaml 一致,四个改动文件均通过),以及 python scripts/check_architecture_boundaries.py(通过)。
  • 上游语义取自 PyPI torchtitan==0.4.0 wheel;未核对 .ci/setup_torchtitan.sh 从 git tag v0.4.0 取到的源码与 wheel 是否逐字一致。若二者有差异,derive / to_graph_trainer_config 的等价性结论需要按 tag 重新核对。
  • torchtitan_npu_recipes/examples/deepseek_v4.py:479 的新调用在 0.4.0 下不可达:前面的 GraphTrainerEx 导入会先抛 TypeError 并转成 NotImplementedError,这一行只有静态等价性判断,没有运行时验证。

测试审查

  • 结论:可以合入。新增测试从真实 ConfigLoader 入口选择图训练 recipe,覆盖了被删源码形状断言保护的核心结果:顶层 NPU 字段被移除、嵌套组件与 Muon profile 保留。
  • UT:无需补充。依据:新测试覆盖 graph_trainer_deepseek_v4_debugmodel;tests/unit_tests/models/deepseek_v4/test_graph_trainer_parallelize.py:22 与 tests/unit_tests/patches/torchtitan/experiments/graph_trainer/test_chunked_loss.py:66 分别构造 debugmodel 与 flash_43layers recipe;其余三个 recipe 与已覆盖的只差所调用的模型工厂,转换体相同。(参数化建议见改进建议。)
  • ST:无需补充。改动只构造 GraphTrainer.Config,不改变训练计算;NPU GraphTrainer 在 0.4.0 仍被既有 layout guard 拦截,tests/integration_tests 没有图训练 case,本 PR 也没有新增可运行的 NPU 路径。

必须解决项以成功发布的代码行内评论为准;总结中的其他内容仅供参考。

审查版本:ead44d680db0070ea423421896bc37ce594aba4a

likedislike