Merged
torch cann_ops_nn export #7941
丛吉钰Add on Jul 25
torch cann_ops_nn export #7941
Merged
丛吉钰Add on Jul 25
丛吉钰
丛吉钰Collaborators
Jul 25

描述

将torch.ops.cann_ops_nn改为cann_ops_nn

关联的Issue

#3384

测试

本地torchair使用cann_ops_nn导入

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for 丛吉钰's contribution.)
丛吉钰丛吉钰Collaborators
Jul 25 Created pull request, commit e0fa1c4a
atomgit-bot
atomgit-bot
Jul 25 Comments:

变更摘要

该 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() 调用能反映完整的可用符号集合。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 25 Comments:

代码审查

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 自定义算子的目标。代码逻辑正确,仅有一处可选的健壮性改进。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

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

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Jul 25 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


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 /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robotCollaborators
Jul 25 Set zengjuan,crystalhu,yangyang016,fanqirui,chenqi317,liubo75,tangweiwei2 as approver
CANN-robotCANN-robotCollaborators
Jul 25 Set zengjuan,crystalhu,yangyang016,fanqirui,chenqi317,liubo75,tangweiwei2 as approver
丛吉钰
丛吉钰Collaborators
Jul 25 Comments:

compile

likedislike
丛吉钰丛吉钰Collaborators
Jul 25 Pre merge succeeded (commit_id: be077d373f01edc0690b16aae626e5df335f6db0)
CANN-robotCANN-robotCollaborators
Jul 25 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Jul 25 Comments:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Jul 25 Comments:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robotCANN-robotCollaborators
Jul 25 Added label: api-check-pass
CANN-robotCANN-robotCollaborators
Jul 25 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 25 Added label: ci-pipeline-passed
丛吉钰丛吉钰Collaborators
Jul 27 Modify description of pull request
范其瑞
范其瑞Collaborators
Jul 27 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 27 Added label: approved
chenqi317
chenqi317Collaborators
Jul 27 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 27 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 27 Merged pull request