已合并
feat: add aclnnReselectStaticKernelWithPath interface #40386
rich创建于 7月7日
feat: add aclnnReselectStaticKernelWithPath interface #40386
已合并
rich创建于 7月7日
rich
rich成员
7月7日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)
https://gitcode.com/Ascend/pytorch/issues/2634

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)
新增aclnnReselectStaticKernelWithPath 接口,在静态编译场景,支持指定的static_kernel生效路径。达成按进程隔离的目的。

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”
不涉及

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”
不涉及用户面资料,对用户透明。

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图
内部接口,在torchair中使用与看护

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 rich 的贡献)
richrich成员
7月7日 创建了 pull request,commit d5ed33a4
richrich成员
7月7日 关联了issue:[Feature]: 静态编译支持按进程隔离
atomgit-bot
atomgit-bot
7月7日 评论:

变更摘要

该 PR 新增了 aclnnReselectStaticKernelWithPath 接口,在静态编译场景下支持通过指定路径来重新选择静态 Kernel,实现按进程隔离的静态 Kernel 生效路径。变更涉及底层 C++ 动态加载封装、Python 绑定以及模块初始化层的暴露。

主要改动

  • 新增 ReselectStaticKernelWithPath 函数:在 OpInterface.cpp 中动态加载 aclnnReselectStaticKernelWithPath 符号并封装调用,接受 const std::string &path 参数,失败时报错 NOT_FOUND;对应的声明与注释添加在 OpInterface.h 中。
  • 新增 Python 绑定函数 THNPModule_aclnn_reselect_static_kernel_with_path:在 Module.cpp 中对传入路径进行严格校验(非空字符串、无空字节、路径存在且为目录),调用前后均执行 emptyAllNPUStream 清空 NPU 任务队列,并适配 task_queue_enable == 2 模式通过 OpCommand::RunOpApiV2 执行。
  • 注册 Python 方法 _aclnn_reselect_static_kernel_with_path:在 Module.cpp 的方法表 THNPModule_methods 中以 METH_O 形式注册,使 Python 层可调用该接口。
  • Python 层封装 _aclnn_reselect_static_kernel_with_path:在 __init__.py 中添加对 _lazy_init() 的调用,确保 NPU 初始化后调用底层 C 函数。
likedislike
atomgit-bot
atomgit-bot
7月7日 评论:

代码审查

现在我对所有四个文件都有了全面的了解。让我总结分析结果。

最终分析

经过对整个代码变更的详细分析,我得出以下结论:

OpInterface.cpp 和 OpInterface.h

  • 新增的 ReselectStaticKernelWithPath 函数完美遵循了现有 ReselectStaticKernel 的模式,包括函数加载、空指针检查和错误报告。
  • 头文件新增了正确的包含(<string>)和声明。

Module.cpp

  • 新增的 THNPModule_aclnn_reselect_static_kernel_with_path 函数严格遵循现有 THNPModule_aclnn_reselect_static_kernel 的模式。
  • 路径验证是最佳实践:字符串类型检查、空字节检查、存在性检查、目录检查。
  • 新增代码在 task_queue_enable == 2 lambda 中正确返回 aclnnStatus(而现有代码总是返回 0),改进了错误传播。
  • else 分支使用 NPU_CHECK_ERROR 正确检查返回码(现有代码完全忽略它)。
  • METH_O 用法与 PyObject* self, PyObject* arg 签名匹配。
  • 通过 HANDLE_TH_ERRORS / END_HANDLE_TH_ERRORS 正确处理异常。

init.py

  • Python 包装器严格遵循现有 _aclnn_reselect_static_kernel 的模式,在调用 C++ 函数之前调用 _lazy_init()

无发现

经过彻底审查(包括与所有相关现有模式的对比、边界值分析、异步任务队列生命周期检查和错误处理验证),此 diff 中没有引入任何真实问题。新增代码的所有错误路径都有适当保护,并且它遵循现有代码库的模式。


结束总结

  • 各文件发现数
    • torch_npu/csrc/core/npu/interface/OpInterface.cpp:无问题
    • torch_npu/csrc/core/npu/interface/OpInterface.h:无问题
    • torch_npu/csrc/npu/Module.cpp:无问题
    • torch_npu/npu/__init__.py:无问题
  • P0 发现:0
  • P1 发现:0
  • P2 发现:0
  • P3 发现:0
  • 整体风险评估:低 — 这是一个干净、结构良好的新增内容,正确复制了现有模式,并改进了现有代码忽略返回码的相关返回码处理方式。没有引入正确性、安全性、可靠性或破坏性变更问题。

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

likedislike
ascend-robotascend-robot成员
7月7日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
7月7日 评论:

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-Ascend/pytorch htchu, luochao60 (2/2) htchu (1/1)
torch_npu/npu htchu, luochao60 (2/2) htchu (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robot
ascend-robot成员
7月7日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.10.0
v2.7.1-26.1.0
v2.9.0-26.1.0
v2.10.0-26.1.0
v2.11.0-26.1.0
v2.12.0-26.1.0
v2.12.0
v2.11.0
v2.9.0
v2.7.1
ci-test

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robotascend-robot成员
7月7日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月7日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月7日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月7日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
7月7日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月7日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月7日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月7日 添加了label:ci-pipeline-failed
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 PR-pipeline_pytorch#42099 [ commitID:4fce5ee0 ] 运行失败
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 PR-pipeline_pytorch#42099 [ commitID:4fce5ee0 ] 运行失败
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
rich
rich成员
7月7日 评论:

compile

likedislike
ascend-robotascend-robot成员
7月7日 删除了label:ci-pipeline-failed
ascend-robotascend-robot成员
7月7日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月7日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月7日 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月7日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月7日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
7月7日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月7日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月7日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月7日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 PR-pipeline_pytorch#42195 [ commitID:4fce5ee0 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
htchu成员
7月8日 评论:

/approve

likedislike
ascend-robotascend-robot成员
7月8日 添加了label:approved
liujunzhu
liujunzhu成员7月8日进行代码检视2
torch_npu/csrc/core/npu/interface/OpInterface.h
@@ -18,1 +19,4 @@
1819 
20+/**
21+ This Api is used to reselect static kernel with a specified path,
22+ it need to be called once at process.
liujunzhu
liujunzhu7月8日评论:

如果调用多次会有什么问题,代码中最好添加校验处理

likedislike
rich
rich成员
7月8日 评论:

感谢回复,该接口是进程级全局操作,重复调用会覆盖之前的 kernel 选择路径。torchair 侧调用方已在 try/except 中处理异常,且实际场景中每次安装新的 static kernel 后需要重新调用 reselect 才能生效,属于预期行为。多次调用实际不会有问题。

luochao60成员
7月8日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
7月8日 添加了label:lgtm
ascend-robot
ascend-robot成员
7月8日 评论:

The MR can not be merged, because of CodeReview discussion not resolved

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

likedislike
liujunzhu
liujunzhu成员7月8日进行代码检视2
torch_npu/csrc/npu/Module.cpp
@@ -2082,0 +2084,4 @@
2084+ TORCH_CHECK(THPUtils_checkString(arg),
2085+ "path must be a string",
2086+ PTA_ERROR(ErrCode::PARAM));
2087+ std::string path = THPUtils_unpackString(arg);
liujunzhu
liujunzhu7月8日评论:

路径应该要做规范化处理

likedislike
rich
rich成员
7月8日 评论:

感谢回复,当前接口中对路径做了类型校验、null字节注入防护、类型以及存在性校验。在上游使用中path参数不是由用户指定的,而是在torchair传入规范化的path路径。

liujunzhu
liujunzhu成员7月8日进行代码检视2
torch_npu/csrc/core/npu/interface/OpInterface.cpp
@@ -44,0 +48,4 @@
48+ aclnnReselectStaticKernelWithPathFunc =
49+ (AclnnApiFunc)TORCH_NPU_GET_FUNC(aclnnReselectStaticKernelWithPath);
50+ }
51+ TORCH_CHECK(aclnnReselectStaticKernelWithPathFunc,
liujunzhu
liujunzhu7月8日评论:

这个check应该可以放到if (aclnnReselectStaticKernelWithPathFunc == nullptr)内部

likedislike
rich
rich成员
7月8日 评论:

这里是在编码风格上与现有的接口如ReselectStaticKernel保持了一致

richrich成员
7月8日 解决了最后一个问题
ascend-robotascend-robot成员
7月8日 合入了pull request
ascend-robot
ascend-robot成员
7月8日 评论:

Pull Request 已合并或已关闭。

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

likedislike
ascend-robot
ascend-robot成员
7月8日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12218 [ commitID:4fce5ee0 ] 已完成
likedislike