Merged
mock torch when compile pypto kernel #9115
mock torch when compile pypto kernel #9115
Merged
poorwillAdd on Jul 24
poorwill
poorwill
Jul 24

mock torch when compile pypto kernel

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for poorwill's contribution.)
poorwillpoorwill
Jul 24 Created pull request, commit 88308e15
atomgit-bot
atomgit-bot
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 常量中加入 mock torch 和 torch_npu 的代码片段,确保在执行 from pypto_pro.runtime.opc.pypto_compile import pypto_compile_op 之前已提供占位模块,防止无真实 torch/npu 环境下构建出错。
  • 构建时解耦运行时依赖:两处修改共同实现编译 pypto kernel 阶段对 torch 运行时的解耦,使代码生成和编译过程不再要求环境中存在完整的 torch/npu 安装。
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 24 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 24 Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
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 /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robotCollaborators
Jul 24 Set monologue815,jiang-lirui,Allan_Yu,wang-minbo,xtqh,liudan12,gaoxiang618,mabing1118 as approver
CANN-robotCANN-robotCollaborators
Jul 24 Set monologue815,jiang-lirui,Allan_Yu,wang-minbo,xtqh,liudan12,gaoxiang618,mabing1118 as approver
poorwill
poorwill
Jul 24 Comments:

compile

likedislike
poorwillpoorwill
Jul 24 Pre merge succeeded (commit_id: d1da03a345a77ed1f41ca168103ad7d4b286a40b)
CANN-robotCANN-robotCollaborators
Jul 24 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
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执行结束

likedislike
CANN-robot
CANN-robotCollaborators
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 >>>>>
likedislike
gaoxiang618
gaoxiang618Collaborators
Jul 24 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 24 Added label: approved
poorwill
poorwill
Jul 24 Comments:

compile

likedislike
poorwillpoorwill
Jul 24 Pre merge succeeded (commit_id: 73c1f1f316d97a67466a0c775019c81c5f652478)
CANN-robotCANN-robotCollaborators
Jul 24 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 24 Added label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
Jul 24 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 24 Added label: api-check-pass
CANN-robotCANN-robotCollaborators
Jul 24 Removed label: ci-pipeline-failedapi-check-pass
CANN-robot
CANN-robotCollaborators
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执行结束

likedislike
CANN-robot
CANN-robotCollaborators
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执行结束

likedislike
CANN-robotCANN-robotCollaborators
Jul 24 Added label: api-check-pass
CANN-robotCANN-robotCollaborators
Jul 24 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 24 Added label: ci-pipeline-passed
mabing
mabingCollaborators
Jul 24 Comments:

/lgtm

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