Merged
test: add NPU validation for enable_python_dispatcher (#2678) #45963
test: add NPU validation for enable_python_dispatcher (#2678) #45963
Merged
sodachanAdd on Sep 7
sodachan
Sep 7

【合入来源】

【修改方案】

关联 API:torch._dispatch.python.enable_python_dispatcher。

本 PR 面向 v2.12.0,新增 test/npu/test__dispatch_python_enable_python_dispatcher.py,不修改 API 声明、实现、注册或 test_upstream/。

适配方案: 1.2-2 PyTorch社区无用例或覆盖不完整,涉及硬件。 社区候选未完整覆盖 dispatcher 守卫的进入/退出、嵌套恢复、异常恢复、非法参数与守卫内真实 NPU 分发的组合契约。新增用例覆盖上述状态场景;首个用例在 NPU Tensor 上执行 square() + 1,断言输入输出设备、数值和退出后的状态恢复。

新增用例必要性: CPU、Meta/FakeTensor 或仅检查辅助算子结果不能同时证明 TLS 状态恢复和 NPU 输入输出在启用 dispatcher 时仍正确。

【资料变更】

不涉及。该接口位于私有 torch._dispatch 模块,native API 资料不适用。

【接口变更】

不涉及。未发现需要修改 API 实现的证据;本 PR 仅补齐测试覆盖。

【功能验证】

历史验证日期:2026-09-07。Python 3.10.12;A3 为 aarch64 / CANN 9.0.0,A5 为 x86_64 / CANN 9.1.0。已测提交:14c297360567098c7fe2eb01972b1a5f5e290ef2。测试文件 SHA-256:02f4d514c2473229c33feddbdd6fa9b3c45dd07dc95f1e68abdb81852dbcc7db。

python test/npu/test__dispatch_python_enable_python_dispatcher.py -v
硬件 运行包 结果
A3 / Ascend910B3 torch 2.12.0+cpu / torch_npu 2.12.0 4/4 通过,Ran 4 tests、OK
A5 / Ascend950PR torch 2.12.0+cpu / torch_npu 2.12.0 4/4 通过,Ran 4 tests、OK

完整脱敏日志和哈希索引见结果 #4682。该历史运行证据不包含 A2;CI、CLA 已通过,review 与 merge 待维护者确认。

run_tests 导入口径待确认: 当前测试从 torch.testing._internal.common_utils 导入 run_tests;目标 PyTorch 版本的顶层 torch.testing 未导出该符号。为避免引入 ImportError,未机械改写;请维护者确认该 testing 子模块入口是否符合规范。若要求修改,将对本版本的原四项 A3/A5 测试重跑验证。

A3 / Ascend910B3 脱敏完整日志
test_context_enables_and_restores_dispatcher (__main__.TestEnablePythonDispatcher) ... ok
test_context_restores_state_after_exception (__main__.TestEnablePythonDispatcher) ... ok
test_invalid_argument (__main__.TestEnablePythonDispatcher) ... ok
test_nested_context_restores_outer_state (__main__.TestEnablePythonDispatcher) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.915s

OK
A5 / Ascend950PR 脱敏完整日志
test_context_enables_and_restores_dispatcher (__main__.TestEnablePythonDispatcher) ... ok
test_context_restores_state_after_exception (__main__.TestEnablePythonDispatcher) ... ok
test_invalid_argument (__main__.TestEnablePythonDispatcher) ... ok
test_nested_context_restores_outer_state (__main__.TestEnablePythonDispatcher) ... ok

----------------------------------------------------------------------
Ran 4 tests in 2.146s

OK

【CheckList】

likedislike
Pull Request has been successfully merged by ascend-robot.
(Thank you for sodachan's contribution.)
sodachansodachan
Sep 7 Created pull request, commit 9dabab38
sodachansodachan
Sep 7 Associated issue: 【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(55)
atomgit-bot
atomgit-bot
Sep 7 Comments:

变更摘要

本 PR 为 torch._dispatch.python.enable_python_dispatcher 新增 NPU 侧验证用例,新增测试文件 test/npu/test__dispatch_python_enable_python_dispatcher.py,在真实 NPU 输入输出下覆盖 Python 分发器的上下文激活、嵌套、正常/异常退出后的状态恢复、参数校验等行为契约;纯测试改动,不涉及 API 实现或 patch 修改。

主要改动

  • 新增上下文状态验证测试类:TestEnablePythonDispatcher 继承上游 TorchTestCase 与 TorchNPU TestCase,并通过辅助函数 is_python_dispatcher_enabled()(调用 torch._C._dispatch_tls_is_dispatch_key_included 检查 DispatchKey.PythonDispatcher)断言上下文进入前后分发器状态的启用与恢复。
  • 验证真实 NPU 分发路径:张量经 torch.tensor([1.0, 2.0, 3.0]).to(device_type) 构造,断言 device.type == "npu",并在上下文内执行 input_tensor.square() + 1,校验输出设备为 NPU 且数值为 [2.0, 5.0, 10.0]。
  • 覆盖嵌套与异常退出场景:test_nested_context_restores_outer_state 验证嵌套上下文退出后外层仍保持启用状态;test_context_restores_state_after_exception 通过 assertRaisesRegex(RuntimeError, "sentinel") 验证抛出异常后上下文状态恢复到进入前值。
  • 补充非法参数用例:test_invalid_argument 对 enable_python_dispatcher("invalid") 断言抛出 TypeError。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 7 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
ascend-robotascend-robotCollaborators
Sep 7 Added label: ascend-cla/yes
ascend-robot
ascend-robotCollaborators
Sep 7 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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
test ✅ chenrayray, renyujin (2/2) ✅ chenrayray (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robot
ascend-robotCollaborators
Sep 7 Comments:

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

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

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

注意:

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

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
Sep 7 Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
Sep 7 Comments:

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

likedislike
ascend-robotascend-robotCollaborators
Sep 7 Removed label: docs-ci-pipeline-running
ascend-robotascend-robotCollaborators
Sep 7 Added label: docs-ci-pipeline-success
AtlasAccountAtlasAccountCollaborators
Sep 7 Removed label: ci-pipeline-running
AtlasAccountAtlasAccountCollaborators
Sep 7 Added label: ci-pipeline-passed
AtlasAccount
AtlasAccountCollaborators
Sep 7 Comments:
流水线 PR-pipeline_pytorch#66403 [ commitID:14c29736 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 ✅ COMPLETED >>>
Build_ARM ✅ COMPLETED >>>
Build_X86_torchair ⚪ IGNORED >>>
Build_ARM_torchair ⚪ IGNORED >>>
patch_test ⚪ IGNORED >>>
Build_X86_213 ⚪ IGNORED >>>
Build_ARM_213 ⚪ IGNORED >>>
恶意代码检查 Antipoison ✅ COMPLETED >>>
编码安全与规范检查 codecheck_pre-commit ✅ COMPLETED >>>
check_error ✅ COMPLETED >>>
lintrunner ✅ COMPLETED >>>
开源片段检查 SCA ✅ COMPLETED >>>
开发者测试 UT_ARM_A3_Part_01 ⚪ IGNORED >>>
UT_ARM_A3_Part_02 ⚪ IGNORED >>>
UT_ARM_A2_Part_01 ✅ COMPLETED >>>
UT_ARM_A2_Part_02 ✅ COMPLETED >>>
UT_ARM_A2_Part_03 ✅ COMPLETED >>>
UT_inductor_Part_01 ⚪ IGNORED >>>
UT_inductor_Part_02 ⚪ IGNORED >>>
UT_inductor_Part_03 ⚪ IGNORED >>>
UT_inductor_Part_04 ⚪ IGNORED >>>
UT_DIST_ARM_Part_01 ⚪ IGNORED >>>
UT_DIST_ARM_Part_02 ⚪ IGNORED >>>
UT_DIST_ARM_Part_03 ⚪ IGNORED >>>
UT_DIST_ARM_Part_04 ⚪ IGNORED >>>
UT_ARM_A2_Select_Part_01 ✅ COMPLETED >>>
UT_ARM_A2_Select_Part_02 ✅ COMPLETED >>>
UT_inductor_Part_213 ⚪ IGNORED >>>
流水线 PR-pipeline_pytorch ✅ COMPLETED >>>
此流水线已支持下列评论快捷指令,仅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, LiNuohang, taohuoquan, Jesse, WSs_321, SCh_zx]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
sodachansodachan
Sep 8 Modify description of pull request
sodachansodachan
Sep 9 Modify description of pull request
sodachansodachan
Sep 14 Modify description of pull request
sodachansodachan
Sep 14 Modify description of pull request
chenrayray
chenrayrayCollaborators
Sep 23 Comments:

/approve

likedislike
ascend-robotascend-robotCollaborators
Sep 23 Added label: approved
renyujin
renyujinCollaborators
Sep 28 Comments:

/lgtm

likedislike
ascend-robotascend-robotCollaborators
Sep 28 Added label: lgtm
ascend-robotascend-robotCollaborators
Sep 28 Removed label: ci-pipeline-passed
ascend-robot
ascend-robotCollaborators
Sep 28 Comments:

The following label is not ready.

ci-pipeline-passed: The ci-pipeline-passed label is expired. Please compile again.

likedislike
群青世界
群青世界Collaborators
Sep 29 Comments:

compile

likedislike
sodachansodachan
Sep 29 Pre merge succeeded (commit_id: 57023aeab3e7b42b91986f1f7cbd260fc9766815)
AtlasAccount
AtlasAccountCollaborators
Sep 29 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
AtlasAccountAtlasAccountCollaborators
Sep 29 Added label: ci-pipeline-running
ascend-robot
ascend-robotCollaborators
Sep 29 Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
Sep 29 Removed label: docs-ci-pipeline-success
ascend-robotascend-robotCollaborators
Sep 29 Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
Sep 29 Comments:

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

likedislike
ascend-robotascend-robotCollaborators
Sep 29 Removed label: docs-ci-pipeline-running
ascend-robotascend-robotCollaborators
Sep 29 Added label: docs-ci-pipeline-success
AtlasAccountAtlasAccountCollaborators
Sep 29 Removed label: ci-pipeline-running
AtlasAccountAtlasAccountCollaborators
Sep 29 Added label: ci-pipeline-passed
ascend-robotascend-robotCollaborators
Sep 29 Merged pull request
AtlasAccount
AtlasAccountCollaborators
Sep 29 Comments:
流水线 pytorch_gitcode_PR_multiVersion#15757 [ commitID:14c29736 ] 已完成
likedislike