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

【合入来源】

【修改方案】

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

本 PR 面向 v2.7.1,新增 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。已测提交:cceda7689b9d903f0ca89866d701fea42c37c300。测试文件 SHA-256:02f4d514c2473229c33feddbdd6fa9b3c45dd07dc95f1e68abdb81852dbcc7db。

python test/npu/test__dispatch_python_enable_python_dispatcher.py -v
硬件 运行包 结果
A3 / Ascend910B3 torch 2.7.1+cpu / torch_npu 2.7.1.post8 4/4 通过,Ran 4 tests、OK
A5 / Ascend950PR torch 2.7.1+cpu / torch_npu 2.7.1.post8 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.978s

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.026s

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 c22d75a5
sodachansodachan
Sep 7 Associated issue: 【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(55)
atomgit-bot
atomgit-bot
Sep 7 Comments:

变更摘要

本 PR 针对 API torch._dispatch.python.enable_python_dispatcher 新增 NPU 侧验证:新增单文件测试 test/npu/test__dispatch_python_enable_python_dispatcher.py(+87 行),不修改 PyTorch patch 或 TorchNPU 实现。测试通过 torch._C._dispatch_tls_is_dispatch_key_included 与 torch._C.DispatchKey.PythonDispatcher 查询分发器线程状态,使用 torch.accelerator.current_accelerator() 解析目标设备并断言输入/输出张量设备为 npu,覆盖上下文启用/恢复、嵌套、异常退出恢复与非法参数等场景。

主要改动

  • 新增 NPU 测试文件:新建 test/npu/test__dispatch_python_enable_python_dispatcher.py,导入 enable_python_dispatcher 并以真实 NPU 张量(torch.tensor(...).to(device_type))执行 square() + 1 等运算,断言结果设备与数值正确性。
  • 调度器状态辅助函数:定义 is_python_dispatcher_enabled(),通过 torch._C._dispatch_tls_is_dispatch_key_included(torch._C.DispatchKey.PythonDispatcher) 检测线程局部状态中的 PythonDispatcher 分发键是否被包含。
  • 上下文启停与恢复测试:test_context_enables_and_restores_dispatcher 验证进入上下文后分发器启用、退出后恢复至进入前的 before 状态;test_nested_context_restores_outer_state 验证嵌套上下文退出后外层状态保持启用;test_context_restores_state_after_exception 通过抛出 RuntimeError("sentinel") 验证异常路径下状态同样恢复。
  • 非法参数校验测试:test_invalid_argument 断言向 enable_python_dispatcher 传入非上下文参数 "invalid" 时抛出 TypeError。
  • 测试基类与设备选择约定:测试类 TestEnablePythonDispatcher 采用上游 TorchTestCase 优先、保留 TorchNPU TestCase 的继承顺序以兼容 run_tests 基类校验;device_type 依据当前加速器(torch.accelerator.current_accelerator())解析,并断言张量设备类型为 "npu"。
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#66401 [ commitID:cceda768 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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 ✅ COMPLETED >>>
UT_ARM_A3_Part_02 ✅ COMPLETED >>>
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: 7b4a71e0e44abf79c57a7a70bcdd7792a89de64c)
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