Merged
torch extension整改成分布式 #9712
torch extension整改成分布式 #9712
Merged
huangbolinAdd on Aug 6
huangbolin
huangbolinCollaborators
Aug 6

描述

ops-transformer仓支持各个算子独立编译torch_extention,并将torch_extension从集中式改成分布式(算子的文件分布到算子源码目录下新建的torch_extension文件夹)

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/4502

测试

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for huangbolin's contribution.)
huangbolinhuangbolinCollaborators
Aug 6 Created pull request, commit 85a49b29
atomgit-bot
atomgit-bot
Aug 6 Comments:

变更摘要

该 PR 将 cann_ops_transformer 的 torch extension 从集中式单目录结构重构为按算子类别分布的"分布式"目录结构。每个算子现在拥有独立的 torch_extension/ 子目录(如 attention/flash_attn/torch_extension/),内含 Python 算子文件、C++ 源码和 graph_convert 转换逻辑。同时引入延迟初始化模式:全局 AS_LIBRARY 宏被 get_as_library() 函数替代,每个 OpBuilder 子类实例化后需调用 _ensure_initialized() 完成 schema 注册和 meta 实现注册。setup.py 支持通过环境变量 TORCH_EXTENSION_OPS 和 TORCH_EXTENSION_VENDOR 选择性打包单个或多个算子,ops/__init__.py 改为自动发现机制。

主要改动

  • OpBuilder 基类引入 category 参数与 _ensure_initialized() 方法: OpBuilder.__init__ 新增 category 参数,用于推导 C++ 源文件路径(从 ops/csrc/X.cpp 变为 csrc/<category>/X.cpp);延迟初始化逻辑从 __init__ 移至 _ensure_initialized(),所有 30+ 个算子 builder 在实例化后立即调用该方法,避免在 torch.compile 追踪时触发 JIT 加载。

  • AS_LIBRARY 全局变量被 get_as_library() 函数取代: 原模块级立即创建的 Library("cann_ops_transformer", "DEF") 改为首次调用时惰性创建(builder.py),同时增加 RuntimeError 时的 FRAGMENT 回退逻辑,所有 @impl(AS_LIBRARY, ...) 装饰器统一替换为 @impl(get_as_library(), ...)。

  • 算子文件按类别分布到独立目录: 每个算子从集中式 ops/、ops/csrc/、ops/graph_convert/ 迁移至 attention/<op>/torch_extension/、mc2/<op>/torch_extension/ 等分层结构,每个目录新增 __init__.py 导出相应符号;跨模块引用(如 deep_ep.py 和 mega_moe.py 中的 CommContextManager)由相对导入改为绝对导入 cann_ops_transformer.ops.mc2.common。

  • ops/__init__.py 改为自动发现机制: 原先硬编码的 30+ 条 import 语句被替换为遍历目录和 entry_points 的自动发现加载逻辑,支持 importlib.metadata.entry_points 的 cann_ops_transformer.ops 组入口点,同时自动加载对应的 graph_convert_<op> 模块。

  • setup.py 支持选择性打包与分布式源码收集: 引入 BuildPyWithOps 自定义构建命令,通过 TORCH_EXTENSION_OPS 过滤仅打包指定算子;自动遍历 ops-transformer/ 根目录收集所有 torch_extension/ 中的 .py 和 .cpp 文件,并在构建时替换跨包引用路径;build.sh 新增 --torch_extension 选项支持单算子打包。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 6 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 6 Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Aug 6 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
attention ✅ 俞郑中, cc-z, 蒋礼锐 (3/2) ✅ 俞郑中, cc-z, 蒋礼锐 (3/1)
build.sh ✅ wang-minbo, 蒋礼锐, 俞郑中 (3/2) ✅ 蒋礼锐, 俞郑中, wang-minbo (3/1)
gmm ✅ 蒋礼锐, 俞郑中, cc-z, kknan (4/2) ✅ 蒋礼锐, 俞郑中, cc-z (3/1)
mc2 ✅ 俞郑中, wang-minbo, macech, 蒋礼锐 (4/2) ✅ 蒋礼锐, 俞郑中, wang-minbo, macech (4/1)
moe ✅ 俞郑中, cc-z, 宋恺, 蒋礼锐 (4/2) ✅ cc-z, 宋恺, 蒋礼锐, 俞郑中 (4/1)
posembedding ✅ 俞郑中, cc-z, 宋恺, 蒋礼锐 (4/2) ✅ 俞郑中, cc-z, 宋恺, 蒋礼锐 (4/1)
sig-ops-transformer ✅ 蒋礼锐, 俞郑中 (2/2) ✅ 蒋礼锐, 俞郑中 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
581 messages are collapsed here。 See more
游震游震Collaborators
Sep 11 Deleted associated issue: [Requirement|需求建议]: torch_extension目录拆分
游震游震Collaborators
Sep 11 Associated issue: [Requirement|需求建议]: ops_trans和ops_nn的torch_extension API为什么前者是集中呈现?后者是分散在各个算子目录下,正确的策略是什么?用户怎么调用该类API
游震游震Collaborators
Sep 11 Deleted associated issue: [Requirement|需求建议]: ops_trans和ops_nn的torch_extension API为什么前者是集中呈现?后者是分散在各个算子目录下,正确的策略是什么?用户怎么调用该类API
CANN-robotCANN-robotCollaborators
Sep 11 Removed label: ci-pipeline-passed
游震游震Collaborators
Sep 11 Associated issue: [Requirement|需求建议]: ops_trans和ops_nn的torch_extension API为什么前者是集中呈现?后者是分散在各个算子目录下,正确的策略是什么?用户怎么调用该类API