Pull Request has been successfully merged by CANN-robot.
(Thank you for 丛吉钰's contribution.)变更摘要
该 PR 重构了 cann_ops_nn 包的模块导出机制:将原先的 from .ops import * 星号导入改为更精细的显式属性注入方式,同时引入对 torch.ops.cann_ops_nn 命名空间的延迟属性查找支持,使得模块既能从本地 ops 子模块导出符号,也能回退到 PyTorch 的自定义算子命名空间中查找,从而将 torch.ops.cann_ops_nn 的访问路径统一为 cann_ops_nn。
主要改动
- 导入方式变更:将
from .ops import *替换为from . import ops,通过显式循环将ops模块的非私有属性注入到当前模块的globals()中,避免星号导入带来的不可控符号污染。 - 新增
torch.ops.cann_ops_nn命名空间绑定:引入_op_namespace = torch.ops.cann_ops_nn变量,作为 PyTorch 自定义算子注册表的引用,为后续动态属性查找提供回退目标。 - 新增
__getattr__延迟查找机制:当模块中找不到某个属性时,自动从_op_namespace(即torch.ops.cann_ops_nn)中查找并缓存到globals()中,实现按需加载和访问路径统一。 - 新增
__dir__方法:返回globals()、_op_namespace以及ops子模块三者属性的并集,确保 IDE 自动补全和dir()调用能反映完整的可用符号集合。


代码审查
Now let me do my final sweep over the diff for frequently missed issues:
Boundary checks: the for loop uses dir(ops) which is never empty; hasattr(_op_namespace, _name) is validated by existing code at builder.py:55; del _name is safe since the loop always binds _name. No boundary issues.
Co-located secondary defects: I re-examined lines 25–28 (the for loop) and lines 40–42 (__dir__) — no transposed indices, reversed comparisons, or wrong field references. The filtering logic (_name.startswith("_") and hasattr) is semantically correct.
Other: The noqa: F401 annotation on line 18 is technically unnecessary since ops IS referenced in the for loop and __dir__, but this is a harmless lint-suppression annotation — not a defect.
No additional findings.
关闭摘要
- 变更文件数:1(
torch_extension/cann_ops_nn/__init__.py),已完整审查。 - 发现问题:1 个 P3(
__getattr__未回退到ops模块,可选健壮性改进)。 - 整体风险评估:低风险。变更设计合理,将
from .ops import *替换为显式的名称复制 +__getattr__/__dir__懒加载机制,实现了cann_ops_nn模块同时兼容torch.ops.cann_ops_nn注册算子和ops自定义算子的目标。代码逻辑正确,仅有一处可选的健壮性改进。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-cann/ops-nn | ✅ 范其瑞, 陈琦 (2/2) | ✅ 范其瑞, 陈琦 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


compile


流水线任务触发成功
任务链接 [509ea560e06b4309951015cfe748212c][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_classify | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_mobile_station_9030_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test_ophost | ✅ SUCCESS | ||
| UT_Test_opapi | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| UT_Test_opgraph | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> | |
| UT_Test_harmony-infer-chs-nn | ✅ SUCCESS | ||
| UT_Test_harmony-infer-chs-nn-1 | ✅ SUCCESS | ||
| UT_Test_report_lcov | ⚠️ WARNING | >>>>> |
[2026-07-25 11:17:24] CI执行结束


流水线任务触发成功
任务链接 [8b8894e4beba4e0f8461c1e0faef1278][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_checkpr | ✅ SUCCESS | ||
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_precommit | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> |
[2026-07-25 10:59:03] CI执行结束


/lgtm
/approve


/lgtm
/approve


描述
将torch.ops.cann_ops_nn改为cann_ops_nn
关联的Issue
#3384
测试
本地torchair使用cann_ops_nn导入
文档更新
类型标签
AI/Agent生成声明