Merged
[fix] Fix test case failures related to `anticipatory_routing` that occurred after the 0.4.0 migration. #1467
[fix] Fix test case failures related to `anticipatory_routing` that occurred after the 0.4.0 migration. #1467
Merged
zhangguoguangAdd on 2 days ago
zhangguoguang
zhangguoguang
2 days ago

描述

修复 #238 中 anticipatory routing 测试在 TorchTitan 0.4.0 迁移后的接口不兼容问题。

  • CPU 单测适配 TrainingMicrobatch 的 loss_token_counts、routing_token_counts,以及前向录制所需的 global_routing_token_counts;更新模型预处理返回值和 TrainingEngine、Optim fixture。
  • NPU smoke 适配 TrainingEngine 的执行与状态管理接口,保留真实 DCP 保存恢复、loss 异常触发回滚、WARMUP → ACTIVE → DRAIN → NORMAL 状态转换,以及录制与重放时 microbatch 数据和专家 ID 的一致性校验。
  • 新增非均匀 token counts 用例,验证 step 级计数汇总、DP 归约边界参数传递,以及录制和重放使用相同的计数。
  • 移除 anticipatory routing 对应的 18 个 CPU 用例和 1 个 smoke 用例的临时跳过。未修改生产实现。

类型

Checklist:

如何测试

  1. CPU 单测:

    TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -q --tb=short \
      tests/unit_tests/extensions/experiment/anticipatory_routing
    

    结果:49 passed,0 skipped。

  2. NPU smoke:

    ASCEND_RT_VISIBLE_DEVICES=8 python -m pytest -q \
      tests/smoke_tests/anticipatory_routing/test_anticipatory_routing_smoke.py
    

    结果:1 passed。已验证移除临时 skip 后,默认入口能够完成异常 loss 检测、真实 checkpoint 回滚、路由录制和重放。

精度和性能验证结果

不涉及。

其他信息

在这里可以添加任何与这个 Pull Request 相关的其他说明。

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zhangguoguang's contribution.)
zhangguoguangzhangguoguang
2 days ago Created pull request, commit 6df8e402
CANN-robotCANN-robotCollaborators
2 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
2 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

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ✅ xuyujun, hwcaifubi (2/2) ✅ xuyujun, hwcaifubi (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
2 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
CANN-robotCANN-robotCollaborators
2 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
zhangguoguang
zhangguoguang
2 days ago Comments:

compile

likedislike
zhangguoguangzhangguoguang
2 days ago Pre merge succeeded (commit_id: 2db679c3d608b9759fdc1acd0d9ae14905f41159)
CANN-robot
CANN-robotCollaborators
2 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-running
zhangguoguangzhangguoguang
2 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-passed
titan-npu-reviewer
titan-npu-reviewer
2 days ago Comments:

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

🤖 AI 代码审查报告

审查结论

  • 合入建议: ⚠️ 建议修改后合入
  • 严重性: 一般(S2)
  • 代码质量: 测试迁移与上游 0.4.0 接口一致,未发现代码级缺陷
  • 安全性: 不涉及
  • 测试数据: 未说明
  • 文档完整性: 完整

本 PR 只改测试:把 anticipatory routing 的 CPU UT 和 NPU smoke 从 defer 列表中释放,并按 0.4.0 的 TrainingMicrobatch/TrainingEngine/Optim 接口重写 fixture,另加一条 DP 路由计数的 CPU 用例。我按已安装的 torchtitan 0.4.0 源码逐条核对了断言与调用链,未发现会让这些用例失败的代码错误;主要问题是 PR 完全没有测试执行记录,而这恰恰是一个“修复测试”的 PR,无法仅凭静态核对确认取消 skip 后真的全绿。

代码质量检查

正确性与鲁棒性

未发现明确问题

专项规则

已按 AGENTS.md、.agents/AGENTS.md、tests/AGENTS.md、tests/unit_tests/AGENTS.md、tests/smoke_tests/AGENTS.md 以及 .agents/skills/developer-tests-review/ 的 UT/ST/format 参考核对本次改动。

  • 一般(S2): 这是测试修复 PR,但 如何测试 为空、我已经自己测试过我的代码 未勾选,没有任何实际执行命令或结果。.agents/AGENTS.md:128 要求“如何测试 写实际命令或未执行原因”,developer-tests-review 的更新流程也要求“UT 修改至少执行修改的 unit test……不得把未执行写成通过”。取消 tests/unit_tests/conftest.py 的 skip 后,这 10 条用例会直接进入 .ci/unit_test.sh 的常规 UT;同样,smoke 用例从 _DEFERRED_SMOKE_TESTS 移除后会进入 .ci/smoke_test.sh。没有执行记录时,评审无法确认这些迁移后的用例真的通过,也无法确认新增 DP 用例的期望值在真实环境成立。建议在 PR 描述补上实际命令与结果,例如 TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -v --tb=short tests/unit_tests/extensions/experiment/anticipatory_routing/test_anticipatory_routing.py,以及一次目标 smoke(本机/CI 的 NPU 环境)。

涉及位置

  • PR 描述“如何测试”章节;规则依据 .agents/AGENTS.md:128

代码坏味道

未发现明确问题

设计与架构

未发现明确问题

上游交互

未发现明确问题

训练 infra 与 NPU 性能

未发现明确问题

文档同步建议

  • 结论:无需修改
  • 依据与建议:本 PR 只修改 tests/ 下的测试和 pytest defer 列表,未改公开配置字段、函数签名、入口或训练行为。已核对 docs/feature_guides/anticipatory_routing.md:195-222 对同名 UT/smoke 覆盖场景的描述,与新用例内容一致,无需同步文档。

改进建议

  • 在 PR 描述中补齐上面 S2 要求的 UT 与 NPU smoke 实际执行命令和结果;如某项因环境未执行,请写清原因。
  • 顺带把 tests/unit_tests/extensions/experiment/anticipatory_routing/test_anticipatory_routing.py:342 的 loss_kwargs={"global_valid_tokens": ...} 改为 0.4.0 的 global_loss_token_counts(或直接传 {})。当前被 monkeypatch 的 _non_pp_forward_backward_microbatch 忽略 loss_kwargs,所以不影响结果,但旧名会误导数读者,日后去掉 stub 时会起作用。该项为建议级,不阻塞合入。

没能验证的部分

  • 本机缺少 torch_npu,import torchtitan_npu 直接报 ModuleNotFoundError: No module named 'torch_npu',因此无法执行任何迁移后的 UT;我没有把“静态一致”写成“测试通过”。
  • 无 Ascend NPU 设备,tests/smoke_tests/anticipatory_routing/test_anticipatory_routing_smoke.py 未执行,无法确认 10 step + DCP 回滚流程在真实设备上通过。
  • PR 只有目标分支名 master、没有 base SHA,未做合并树验证;本次改动仅测试路径,合并冲突风险低但未核实。

测试审查

  • 结论:补充测试后合入。迁移后的 fixture 与 0.4.0 契约在静态层面一致,但没有执行记录,无法确认取消 skip 后这些用例全部通过。
  • UT:无需补充。已静态核对 make_trainer 的生产调用链;新增 test_step_routing_counts_reach_recording_and_replay 用 local/dp-reduced 两态覆盖 step 级 routing_token_counts 汇总、preprocess 透传、缓存未被原地修改以及 loss 使用全局分母,期望值独立手算;仍需作者实际跑一次该文件。
  • ST:无需补充(用例已在 tests/smoke_tests/anticipatory_routing/ 且本次被重新启用,是唯一真实 NPU 路径,覆盖单卡 DCP 回滚与 WARMUP→ACTIVE→DRAIN→NORMAL)。但需要实际 NPU 执行证据;另注意 tests/smoke_tests/AGENTS.md:7 把 checkpoint resume 和多 step 训练划给 tests/integration_tests/,请确认保留在 smoke 的理由或调整归属。

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

审查版本:6b2637c896c2158da160bd83cf1d8ebde326c883

likedislike
zhangguoguangzhangguoguang
1 day ago Modify description of pull request
zhangguoguangzhangguoguang
1 day ago Modify description of pull request
zhangguoguangzhangguoguang
1 day ago Modify description of pull request
zhangguoguangzhangguoguang
1 day ago Modify description of pull request
xuyujun
xuyujunCollaborators
1 day ago Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
1 day ago Added label: approved
hwcaifubi
hwcaifubiCollaborators
1 day ago Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
1 day ago Added label: lgtm
CANN-robotCANN-robotCollaborators
1 day ago Merged pull request