Open
fix: 修复 loss_compare 的 TorchAO-NPU worktree 源码加载 #1228
fix: 修复 loss_compare 的 TorchAO-NPU worktree 源码加载 #1228
Open
1Fire4Add on 25 days ago
1Fire4
1Fire4Collaborators
25 days ago

描述

loss_compare.py 为两个 Git ref 创建独立 worktree 时,原先遗漏每个 worktree 自带的 experiments/torchao-npu,可能导致 torchao_npu 从另一份 checkout 导入。本次在目录存在时按「插件仓、TorchAO-NPU、指定 TorchTitan、已有路径」顺序加入 PYTHONPATH,并用单测覆盖路径顺序和独立 upstream 配置;旧 ref 不含该目录时保持原行为。

类型

Checklist:

如何测试

  • tooling 单测:conda run --no-capture-output -n torchnpu-x86-build env PYTHONNOUSERSITE=1 TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -q --tb=short tests/unit_tests/tooling/test_loss_compare.py,结果 50 passed。
  • 干净 checkout 执行完整 pre-commit run --all-files --show-diff-on-failure,包括 Pyrefly,全部通过;开发 checkout 中未跟踪历史报告目录导致的检查错误未纳入提交。
  • NPU 对比使用对应 _a3.sh、8 卡、序列长度 4096、CANN 1021、eager(compile=null);loss_compare.py 使用 HEAD 与 HEAD~1、--mode bitwise。

精度和性能验证结果

用例 初始化 / 步数 loss grad_norm 判定
DeepSeek-V4.1 Flash 共享 FP32 seed DCP / 100 0 / 0 / 0 0 / 0 / 0 bitwise PASS
DeepSeek-V4 Flash 共享 FP32 seed DCP / 3 0 / 0 / 0 0 / 0 / 0 bitwise PASS(默认流程检查)
DeepSeek-V4 Flash 独立 seed 42 / 100 MEAN_AE 6.20e-5,MAX_RE 2.63e-5,MEAN_RE 6.61e-6 MEAN_AE 1.70e-3,MAX_RE 1.94e-3,MEAN_RE 3.41e-4 FAIL;第 29 步首次差异

失败用例两侧训练均完成 100 步,比较器返回 1;该差异根因未确认,本 PR 不调整阈值或模型配置。

其他信息

本次只修改比较工具的源码发现路径和对应单测,未修改模型公式、算子、训练配置或 checkpoint 格式;性能未测量。实际验证入口为 _a3.sh,原 _a5.sh 未在 A3 上验证,不据此声明 A5 结论。精度原始日志与 JSON 已在本地归档,未上传包含内部环境信息的文件。

likedislike
Merge blocked
1Fire41Fire4Collaborators
25 days ago Created pull request, commit abe4e3e1
atomgit-bot
atomgit-botCollaborators
25 days ago Comments:

变更摘要

本次改动聚焦 loss_compare.py 中源码路径发现逻辑的修复:为两个 Git ref 创建独立 worktree 后构建训练环境时,除插件仓根目录与指定 TorchTitan 源码目录外,新增把每个 worktree 自带的 experiments/torchao-npu 加入训练进程的 PYTHONPATH,避免 torchao_npu 导入失败或加载到其他 checkout 的包。同时调整单元测试以覆盖路径选取与顺序,并在两份 A5 runner 脚本中补充仅用于相邻提交验证的注释。

主要改动

  • PYTHONPATH 拼接逻辑重构: training_env 由原先直接拼接 repo_root 与 upstream,改为按 repo_root、仓内 experiments/torchao-npu、upstream 的顺序组装 source_roots,再统一 resolve() 并附加原 PYTHONPATH。
  • 目录存在性判断: 仅当 repo_root / "experiments" / "torchao-npu" / "torchao_npu" 实际存在为目录时才纳入搜索路径,旧 ref 无该目录时保持原有行为。
  • 单元测试覆盖路径与顺序: test_loss_compare_supports_independent_sequence_apis_and_upstreams 改为创建独立的 upstream 目录与 experiments/torchao-npu/torchao_npu 目录,并断言 training_env 返回的四段路径顺序为 repo_root、torchao-npu 根、upstream、已有 /independent/npu。
  • A5 runner 注释补充: deepseek_v4_flash_8p_cpt_4k_a5.sh 与 deepseek_v4_1_flash_8p_cpt_4k_a5.sh 各新增一行注释,说明其为 loss_compare 相邻 ref 探测用测试提交,运行参数未变化。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
25 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
25 days ago Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
25 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
25 days ago Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
25 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: lrwei0709, wanghui0, xuyujun, zhaowei1936, 钟林) ❌ (0/1)(You can also ask: xuyujun, hwcaifubi, zzyyjj012, 盛楠, 钟林)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
25 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
CANN-robotCANN-robotCollaborators
25 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
titan-npu-reviewer
titan-npu-reviewer25 days agoStart code review2
examples/deepseek_v4/debug/deepseek_v4_flash_8p_cpt_4k_a5.sh
Outdated
@@ -1,0 +1,1 @@
11#!/usr/bin/env bash
2+# loss_compare adjacent-ref probe: test commit only; runtime arguments remain unchanged.
titan-npu-reviewer
titan-npu-reviewer25 days agoComments:

一般(S2): 【文档同步】需要补充或更新文档。

本 PR 的文档同步要求:
属于条件性同步。scripts/numerical_alignment/README.md:182 现在写的是「解释器及依赖仍由各侧环境提供」,但改动后只要对应工作区里存在 experiments/torchao-npu/torchao_npu,工具就会把它加进训练进程的 PYTHONPATH,而且排在已安装的 torchao_npu 前面。也就是说,用户环境里装的 torchao_npu 可能被静默换成 worktree 里的源码,这恰好会改变量化路径实际跑的算子实现。对一个核心用途就是「控制到底跑哪份代码」的对比工具来说,这句需要补上,否则用户会误以为比较用的还是环境里的包。

  1. scripts/numerical_alignment/README.md:182:在「--baseline-upstream / --test-upstream 可分别指定 TorchTitan 源码目录,并加入对应进程的 PYTHONPATH;解释器及依赖仍由各侧环境提供。」之后补一句:「若对应工作区(当前仓库或两侧 worktree)存在 experiments/torchao-npu/torchao_npu,工具会自动把该目录加入训练进程的 PYTHONPATH,并优先于环境中已安装的 torchao_npu;目录不存在时沿用环境中的包。」

问题编号:R1228-001

审查版本:0944153830fe2258c4fa79b61bc49ecde0934d6c

likedislike
System
System MessageSystem
25 days ago Comments:

changed this line on 6fc4e575 view diff detail

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

使用模型:deepseek-v4.1-flash

🤖 AI 代码审查报告

审查结论

  • 合入建议: ⚠️ 建议修改后合入
  • 严重性: 建议(S3)
  • 代码质量: 修复逻辑正确,路径与顺序符合预期,没有发现影响数值或训练的缺陷
  • 安全性: 未发现
  • 测试数据: 有覆盖
  • 文档完整性: 部分

这次改动的核心,就是给每个 worktree 的训练进程补上它自己的 experiments/torchao-npu。代码路径和插入顺序都正确,旧 ref 没有这个目录时也确实回到原来的行为;我在当前快照实跑相关单测,50 个全部通过。剩下两件小事:两份 A5 示例脚本里残留的验证探针注释,以及 scripts/numerical_alignment/README.md 里一句关于依赖来源的描述现在不再完全准确。

代码质量检查

正确性与鲁棒性

未发现明确问题

专项规则

已读取并逐条核对 AGENTS.md、.agents/AGENTS.md、scripts/AGENTS.md、tests/AGENTS.md、tests/unit_tests/AGENTS.md、examples/AGENTS.md,命中一处问题:

  • 建议(S3):两份 A5 示例脚本第 2 行残留「test commit only」的验证探针注释,和本次修复目标无关,等于把测试脚手架留在了用户可见的示例入口里,也违反 .agents/AGENTS.md「保持改动最小,只改完成目标所需的文件」。这两行只是注释,删掉不影响任何运行参数。

涉及位置

  • examples/deepseek_v4/debug/deepseek_v4_flash_8p_cpt_4k_a5.sh:2
  • examples/deepseek_v4_1/debug/deepseek_v4_1_flash_8p_cpt_4k_a5.sh:2
# loss_compare adjacent-ref probe: test commit only; runtime arguments remain unchanged.

代码坏味道

未发现明确问题

设计与架构

未发现明确问题

上游交互

未发现明确问题

训练 infra 与 NPU 性能

未发现明确问题

改进建议

  • 合入前删掉两份 A5 脚本第 2 行的探针注释,并按「文档同步建议」给 README 补一句 torchao_npu 来源优先级说明即可,其余不用改。

文档同步建议

  • 结论:需要修改
  • 依据与建议:属于条件性同步。scripts/numerical_alignment/README.md:182 现在写的是「解释器及依赖仍由各侧环境提供」,但改动后只要对应工作区里存在 experiments/torchao-npu/torchao_npu,工具就会把它加进训练进程的 PYTHONPATH,而且排在已安装的 torchao_npu 前面。也就是说,用户环境里装的 torchao_npu 可能被静默换成 worktree 里的源码,这恰好会改变量化路径实际跑的算子实现。对一个核心用途就是「控制到底跑哪份代码」的对比工具来说,这句需要补上,否则用户会误以为比较用的还是环境里的包。
  1. scripts/numerical_alignment/README.md:182:在「--baseline-upstream / --test-upstream 可分别指定 TorchTitan 源码目录,并加入对应进程的 PYTHONPATH;解释器及依赖仍由各侧环境提供。」之后补一句:「若对应工作区(当前仓库或两侧 worktree)存在 experiments/torchao-npu/torchao_npu,工具会自动把该目录加入训练进程的 PYTHONPATH,并优先于环境中已安装的 torchao_npu;目录不存在时沿用环境中的包。」

没能验证的部分

  • 快照是完整 head 但没有 .git,无法独立核对 PR 描述里的 HEAD~1(db876e7)、A5 注释所属提交,以及它声称的 merge-tree 无冲突结果。
  • 没有 A3/A5 环境,未复跑 PR 描述的 8 卡训练;torchao_npu 优先级在真实量化训练里的效果只有 PYTHONPATH 层面的静态推断。
  • 环境里没装 torchao_npu / torch_npu,无法实机验证「in-tree 源码覆盖已安装包」的导入结果;结论依据的是 Python 的 sys.path 顺序(PYTHONPATH 先于 site-packages)。
  • 未运行 ruff / pyrefly 等全量 pre-commit,只跑了与改动直接相关的单测文件。
  • PR 自述 V4 100 步 bitwise 比较返回 FAIL 且根因未确认;这与本次源码加载改动没有直接关系,但意味着该配置的 bitwise 验收目前仍未达成。

测试审查

  • 结论:可以合入。新增逻辑有直接单测,且我在当前快照实跑 python -m pytest -q tests/unit_tests/tooling/test_loss_compare.py,结果 50 passed(32.78s)。
  • UT:无需补充。新增断言校验了 repo_root → experiments/torchao-npu → upstream → 已有 PYTHONPATH 的完整顺序与路径解析;现有 CLI 用例在临时仓库没有 experiments/ 时会走到「目录缺失保持原行为」的分支,只是没有单独断言,影响很小。
  • ST:无需补充。改动只影响 host 侧给训练子进程拼接 PYTHONPATH,不涉及算子、并行、checkpoint 或训练参数;两份 A5 runner 只是加注释,运行参数未变,不需要额外的 NPU 集成用例。

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

审查版本:0944153830fe2258c4fa79b61bc49ecde0934d6c

likedislike
1Fire41Fire4Collaborators
25 days ago Pre merge succeeded (commit_id: 840847042de7fff205b7fc380dd1374827ff9a03)
1Fire41Fire4Collaborators
25 days ago Pushed 1 commits:6fc4e575-style: remove temporary loss compare comments
1Fire41Fire4Collaborators
25 days ago Pre merge succeeded (commit_id: ac6cd82ed96c4cd34340f9942a81b9f4682de528)
CANN-robotCANN-robotCollaborators
25 days ago Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
25 days ago Added label: cann-cla/yes
1Fire41Fire4Collaborators
25 days ago Modify description of pull request
1Fire4
1Fire4Collaborators
25 days ago Comments:

compile

likedislike
1Fire41Fire4Collaborators
25 days ago Pre merge succeeded (commit_id: 044a3d038109d2e06c89c5c86c8d10f62eeab9d5)
CANN-robot
CANN-robotCollaborators
25 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
25 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
25 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
25 days ago Added label: ci-pipeline-passed