Pull Request has been successfully merged by CANN-robot.
(Thank you for huangbolin's contribution.)变更摘要
该 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选项支持单算子打包。


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


描述
ops-transformer仓支持各个算子独立编译torch_extention,并将torch_extension从集中式改成分布式(算子的文件分布到算子源码目录下新建的torch_extension文件夹)
关联的Issue
https://gitcode.com/cann/ops-transformer/issues/4502
测试
文档更新
类型标签