已合并
refactor: reorganize package layout and add agent guidance #688
refactor: reorganize package layout and add agent guidance #688
已合并
zzyyjj012创建于 9月2日
zzyyjj012
zzyyjj012成员
9月2日

描述

本 PR 对 torchtitan-npu 的目录结构和仓库开发规则和PR 代码结构规范刷新和重构进行阶段性整理,使代码归属、自动注入边界和第三方适配层更加明确,方便后续开发者及 Agent 理解和维护代码。

本次修改以目录迁移、导入路径调整和文档补充为主,不改变训练流程、配置默认值、量化 recipe、算子实现或数值逻辑。

规则与架构说明

  • 建立根目录、模块目录和测试目录的分层 AGENTS.md,明确目录职责、依赖方向、注入顺序和测试分层。
  • 新增 Agent Note,记录当前布局、目标布局、迁移边界和后续债务。
  • 新增 docs/architecture.md,统一说明 patches → extensions 的目标注入关系。
  • 明确 TORCHTITAN_NPU_DISABLE_EXTENSIONS 与 TORCHTITAN_NPU_DISABLE_COMPONENTS 目前只是目标接口,尚未实现,不能作为运行时配置使用。

目录迁移

  • 删除 torchtitan_npu/distributed/:HF32 配置 helper 原样迁入 extensions/torchtitan/trainer.py。
  • 将 compile/sdc_checksum.py 迁入 extensions/compile/。
  • 将 interfaces/torchao_converter.py 迁入 extensions/adaptor/torchao/converter.py;adaptor/ 统一纳入 extensions 层。
  • 将 package 内 checkpoint 转换工具由 scripts/ 迁入 tools/,删除 patches/torchtitan/scripts/ 的无调用转发层。
  • 清理 patches/workaround/:Torch NPU workaround 迁入 patches/torch_npu/,eager_flex.py 迁入 extensions/torchtitan/。
  • 建立 extensions/torchtitan/ 镜像目录,将有明确上游 owner 的 Trainer、components、FSDP、EP process group、graph trainer、TorchFT、profiler 和工具扩展迁入对应路径。
  • 同步迁移 TorchAO、graph trainer、TorchFT、Trainer、profiler、checkpoint 等单测目录和引用,删除迁移后的空壳目录。

兼容性

  • 保留现有 extension 激活顺序,只更新其模块路径。
  • extensions/adaptor/ 保持按需导入,不加入 extensions 包入口的自动导入链。
  • 未实现 loader/禁用开关,也未改变 override、模型或算子的调用语义。

类型

Checklist:

如何测试

完整 UT 过程中发现的两个 test_trainer.py 模块名冲突已经通过将 TorchFT 测试重命名为 test_torchft_trainer.py 修复。

其他信息

  • 本 PR 不修改业务逻辑和训练数值行为。
  • 文件迁移尽量保留原始内容,仅同步必要的 import、模块路径、测试路径和文档引用。
  • 本 PR 是目录治理的阶段性修改。TorchAO adaptor 对部分 model、override 和 patch 私有实现的依赖仍作为后续架构债务记录在 Agent Note 中,本阶段不做业务重构。
  • 后续阶段再处理 extension loader、禁用开关、组件注册表和跨层依赖收敛。
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 zzyyjj012 的贡献)
zzyyjj012zzyyjj012成员
9月2日 创建了 pull request,commit 6ec2e87c
atomgit-bot
atomgit-bot成员
9月2日 评论:

变更摘要

本 PR 基于「代码结构规范刷新和重构」建立 torchtitan-npu 的目标 Agent 规则体系与目标架构说明(新增根目录及各目标目录的 AGENTS.md、目标架构文档和架构提案,并将各 Agent 入口统一到根规则),同时配套新增 pre-commit 检查以防止多个 extensions/torchtitan 模块对同一上游完整目标直接做 monkey-patch。本 PR 只定义目标布局和开发守则,不迁移历史生产代码,不改变训练、checkpoint 或数值语义;TORCHTITAN_NPU_DISABLE_EXTENSIONS 与 TORCHTITAN_NPU_DISABLE_COMPONENTS 被明确标注为待迁移时实现的目标接口,当前不可用。

主要改动

  • 新增 pre-commit 钩子 extension-patch-ownership:在 .pre-commit-config.yaml 的本地 hooks 中新增该检查项,以 python3 scripts/check_extension_patch_ownership.py 作为入口,并在 pre-commit 阶段执行(pass_filenames: false)。
  • 新增检查脚本 scripts/check_extension_patch_ownership.py:通过 AST 解析 extensions 根目录下所有 Python 文件,识别对上游目标(torch、torch_npu、torchtitan)的 monkey-patch 赋值;仅允许 extensions/torchtitan 下的模块进行 patch 且每个上游目标只能有一个所有者,拒绝组件层(components)的 patch、同一目标的重复所有者以及经 import alias 赋值的替换方式。
  • 新增单元测试 tests/unit_tests/tooling/test_extension_patch_ownership.py:覆盖三种场景——单个 torchtitan 所有者通过检查、重复上游所有者被拒绝、组件层 monkey-patch 与 import alias 用法均被拒绝。
likedislike
不准确?
atomgit-bot
atomgit-bot成员
9月2日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
9月2日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
9月2日 评论:

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 ✅ lrwei0709, xuyujun (2/2) ✅ lrwei0709, xuyujun (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
此处折叠了186条消息 查看更多
CANN-robotCANN-robot成员
10月13日 添加了label:lgtm
xuyujun
xuyujun成员
10月13日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
10月13日 添加了label:approved
CANN-robotCANN-robot成员
10月13日 合入了pull request
CANN-robot
CANN-robot成员
10月13日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike