Merged
mock torch when compile pypto kernel #9115
poorwillAdd on Jul 24
mock torch when compile pypto kernel #9115
Merged
Merged fromwangbing110/ops-transformermasterintocann/ops-transformermaster
Pull Request has been successfully merged by CANN-robot.
(Thank you for poorwill's contribution.)atomgit-bot
Jul 24 Comments:
Jul 24 Comments:
变更摘要
此 PR 主要解决在编译 pypto kernel 时因缺少 torch 和 torch_npu 运行时模块而导致构建失败的问题。通过在代码生成和编译导入阶段使用 unittest.mock.MagicMock 对 torch 与 torch_npu 进行 mock,消除对真实运行时环境的依赖,确保仅需构建的流程可以正常执行。
主要改动
- 新增
_install_build_only_torch_stub函数:在pypto_codegen.py中定义该函数,使用MagicMock将sys.modules中的torch和torch_npu替换为 mock 对象,并在_load_kernels中调用,避免加载内核模块时触发对真实 torch 运行时的依赖。 - 扩展
PYPTO_IMPORT_HEADER导入头:在ascendc_impl_build.py的PYPTO_IMPORT_HEADER常量中加入 mocktorch和torch_npu的代码片段,确保在执行from pypto_pro.runtime.opc.pypto_compile import pypto_compile_op之前已提供占位模块,防止无真实 torch/npu 环境下构建出错。 - 构建时解耦运行时依赖:两处修改共同实现编译 pypto kernel 阶段对 torch 运行时的解耦,使代码生成和编译过程不再要求环境中存在完整的 torch/npu 安装。


Incorrect?
Jul 24 Added label: cann-cla/yes
CANN-robot
Jul 24 Comments:
Jul 24 Comments:
CANN-robot
Jul 24 Comments:
Jul 24 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 |
|---|---|---|
| cmake | ✅ gaoxiang618, 马兵 (2/2) | ✅ gaoxiang618 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


Jul 24 Set monologue815,jiang-lirui,Allan_Yu,wang-minbo,xtqh,liudan12,gaoxiang618,mabing1118 as approver
Jul 24 Set monologue815,jiang-lirui,Allan_Yu,wang-minbo,xtqh,liudan12,gaoxiang618,mabing1118 as approver
Jul 24 Set monologue815,jiang-lirui,Allan_Yu,wang-minbo,xtqh,liudan12,gaoxiang618,mabing1118 as approver
Jul 24 Set monologue815,jiang-lirui,Allan_Yu,wang-minbo,xtqh,liudan12,gaoxiang618,mabing1118 as approver
poorwill
Jul 24 Comments:
Jul 24 Comments:
compile


Jul 24 Pre merge succeeded (commit_id: d1da03a345a77ed1f41ca168103ad7d4b286a40b)
Jul 24 Added label: ci-pipeline-running
CANN-robot
Jul 24 Comments:
Jul 24 Comments:
流水线任务触发成功
任务链接 [b257c3a154644725aea4d13556b55d1c][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_classify | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_9030_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> |
[2026-07-24 15:31:49] CI执行结束


CANN-robot
Jul 24 Comments:
Jul 24 Comments:
流水线任务触发成功
任务链接 [ff92bddc2a4544bda1e68a4b33503d9b][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ❌ FAILED | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_style | ✅ SUCCESS | >>>>> | |
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_checkpr | ✅ SUCCESS | ||
| codecheck_precommit | ✅ SUCCESS | >>>>> |


gaoxiang618
Jul 24 Comments:
Jul 24 Comments:
/lgtm
/approve


Jul 24 Added label: approved
poorwill
Jul 24 Comments:
Jul 24 Comments:
compile


Jul 24 Pre merge succeeded (commit_id: 73c1f1f316d97a67466a0c775019c81c5f652478)
Jul 24 Removed label: ci-pipeline-running
Jul 24 Added label: ci-pipeline-failed
Jul 24 Added label: ci-pipeline-running
Jul 24 Added label: api-check-pass
Jul 24 Removed label: ci-pipeline-failedapi-check-pass
CANN-robot
Jul 24 Comments:
Jul 24 Comments:
流水线任务触发成功
任务链接 [5084b41c42b74df7a8cebe464811b56f][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_classify | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_9030_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> | |
| UT_Test_harmony-infer-chs-transformer | ✅ SUCCESS | ||
| UT_Test_report_lcov | ⚠️ WARNING | >>>>> |
[2026-07-24 15:52:05] CI执行结束


CANN-robot
Jul 24 Comments:
Jul 24 Comments:
流水线任务触发成功
任务链接 [d3be975f23f54b74afd62a31e8e82789][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_style | ✅ SUCCESS | >>>>> | |
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_checkpr | ✅ SUCCESS | ||
| codecheck_precommit | ✅ SUCCESS | >>>>> |
[2026-07-24 15:35:19] CI执行结束


Jul 24 Added label: api-check-pass
Jul 24 Removed label: ci-pipeline-running
Jul 24 Added label: ci-pipeline-passed
mabing
Jul 24 Comments:
Jul 24 Comments:
/lgtm


Jul 24 Added label: lgtm
Jul 24 Merged pull request
mock torch when compile pypto kernel