Pull Request已成功合入, 合并人@ascend-robot
(感谢 sodachan 的贡献)变更摘要
本 PR 为 torch._dispatch.python.enable_python_dispatcher(关联 issue #2678)补充真实 NPU 环境下的验证用例,仅新增单个测试文件 test/npu/test__dispatch_python_enable_python_dispatcher.py(+87 行),不修改 PyTorch patch 或 TorchNPU 实现。测试类 TestEnablePythonDispatcher 按"上游 TorchTestCase 优先、保留 TorchNPU TestCase"的顺序继承;device_type 通过 torch.accelerator.current_accelerator() 获取当前加速器类型(无加速器时回退 "cpu"),并通过 is_python_dispatcher_enabled()(内部使用 torch._C._dispatch_tls_is_dispatch_key_included 查询 torch._C.DispatchKey.PythonDispatcher)校验上下文内线程分发状态。用例覆盖分发器启用、嵌套、正常/异常退出后的状态恢复契约、非法参数校验,以及真实 NPU 张量输入输出路径。
主要改动
- 新增 NPU 测试文件与测试入口: 新增
test/npu/test__dispatch_python_enable_python_dispatcher.py,定义TestEnablePythonDispatcher(TorchTestCase, TestCase)测试类,并以run_tests()作为__main__入口,保证基类校验兼容。 - 引入分发状态查询辅助函数: 定义
is_python_dispatcher_enabled(),通过torch._C.DispatchKey.PythonDispatcher与torch._C._dispatch_tls_is_dispatch_key_included读取 TLS 分发状态,供各用例断言上下文进入与退出状态。 - 验证启用与恢复契约:
test_context_enables_and_restores_dispatcher在enable_python_dispatcher()上下文内对 NPU 张量执行square() + 1运算,断言输入输出device.type == "npu"及数值正确,并验证退出后分发状态恢复为进入前状态。 - 覆盖嵌套与异常退出场景:
test_nested_context_restores_outer_state验证嵌套上下文退出后外层状态保持;test_context_restores_state_after_exception验证抛出RuntimeError("sentinel")后线程状态仍被正确恢复。 - 补充非法参数校验:
test_invalid_argument断言向enable_python_dispatcher("invalid")传入非法参数时抛出TypeError。


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


当前仓库存在以下 保护分支 :
| 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,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


ascend docs pipeline is running...


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


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/approve


/lgtm


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


compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


ascend docs pipeline is running...


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




【合入来源】
【修改方案】
关联 API:
torch._dispatch.python.enable_python_dispatcher。本 PR 面向
v2.11.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。已测提交:
a495a79b6be9417c9f599300fab870c358eb91ae。测试文件 SHA-256:02f4d514c2473229c33feddbdd6fa9b3c45dd07dc95f1e68abdb81852dbcc7db。python test/npu/test__dispatch_python_enable_python_dispatcher.py -vRan 4 tests、OKRan 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 脱敏完整日志
A5 / Ascend950PR 脱敏完整日志
【CheckList】