Closed
【API一致性任务】torch._dispatch.python.enable_python_dispatcher A3/A5 一致性验证结论 #4682
sodachanAdd on  Sep 8Closed at  Sep 29
sodachan
sodachan
Sep 8 Create

torch._dispatch.python.enable_python_dispatcher A3/A5 一致性验证结论

本 Issue 汇总四个活动分支的专项测试、社区覆盖取证和已完成运行证据;原任务和本结果 Issue 保持开启,等待维护者验收。

32/32 是 2026-09-07 已完成的历史运行记录:8 个“版本 × 硬件”单元各执行 4 个测试方法。日志已逐份复核实际方法标识、Ran 4 tests、OK 与 SHA-256;运行包、测试源码提交和当前 PR 状态分别记录。结果只覆盖 Ascend910B3 与 Ascend950PR,不代表 A2、其他型号或其他 API 的验证。

本任务保留 1.2-2 PyTorch社区无用例或覆盖不完整,涉及硬件。 的新增专项测试路线。当前实现从 torch.testing._internal.common_utils 导入 run_tests;各目标 PyTorch 引用的顶层 torch.testing 未导出该符号。请维护者确认该实际可用的 testing 子模块入口是否符合规范;在此之前不机械改为会导致 ImportError 的导入。若要求修改,仅对受影响版本重跑原四项 A3/A5 测试。CI、CLA 已通过;review、merge、A2 范围及该导入口径均待维护者确认。

原任务:Issue #2678。本结果不关闭原任务;测试源码提交、运行包与日志均对应 2026-09-07 已核验记录。

版本 PR 已测提交 A3 / Ascend910B3 A5 / Ascend950PR
v2.7.1 !45961 cceda7689b9d903f0ca89866d701fea42c37c300 4/4 通过 4/4 通过
v2.11.0 !45962 a495a79b6be9417c9f599300fab870c358eb91ae 4/4 通过 4/4 通过
v2.12.0 !45963 14c297360567098c7fe2eb01972b1a5f5e290ef2 4/4 通过 4/4 通过
master !45964 58b75d817412e66d8be71cd4871a33cc08952564 4/4 通过 4/4 通过

合计 8 个版本×硬件单元、32/32 项通过;每份日志均含四个实际测试标识、Ran 4 tests 与 OK。结果仅覆盖 Ascend910B3 和 Ascend950PR,不泛化为其他型号。

一、API 功能说明

当前状态: 有效。可见性: 私有模块接口,位于 torch._dispatch.python。

硬件相关性: 显式设备判断:上下文守卫没有 CPU/CUDA/NPU 白名单或拒绝条件,PythonDispatcher 是 dispatch key 而非设备类型。通用设备路径:守卫切换当前线程 dispatcher 状态;Tensor 设备仍由输入决定,TorchNPU 将 PrivateUse1 重命名并注册 NPU 模块,因此真实 NPU Tensor 可走通用分发路径。Tensor 计算:守卫自身不分配或计算 Tensor;本轮以 NPU Tensor 的平方、加法作为状态协作检查。纯 Python 判断:否,Python 入口绑定 C++ TLS 守卫。

硬件 支持结论 直接理由
CPU 支持该上下文路径 守卫没有设备限制,底层状态由 TLS 管理。
NPU 专项验证通过 四版本在 Ascend910B3 与 Ascend950PR 上均完成状态契约和真实 NPU 输入输出检查。

API 功能: 无参数上下文管理器;进入时启用 Python dispatcher,正常或异常退出恢复进入前状态;嵌套退出保留外层状态,非空构造参数应抛出 TypeError。

核心源码分析: torch/_dispatch/python.py 将入口绑定至 _EnablePythonDispatcher;torch/csrc/autograd/init.cpp 中构造函数保存旧 TLS 状态并设置解释器,析构函数恢复旧状态:

enable_python_dispatcher = torch._C._EnablePythonDispatcher
EnablePythonDispatcher() : old_(PythonDispatcherTLS::get_state()) {
  PythonDispatcherTLS::set_state(getPyInterpreter());
}
~EnablePythonDispatcher() { PythonDispatcherTLS::set_state(old_); }

二、PyTorch 社区测试用例情况

核验方式: 在各 PyTorch 引用的 test/ 下检索 enable_python_dispatcher 与 _EnablePythonDispatcher,再人工阅读命中调用上下文。

查找命令模板:

git grep -n -E 'enable_python_dispatcher|_EnablePythonDispatcher' refs/tags/版本号 -- test/
版本 执行结果
v2.7.1 退出 0;35 行、12 文件;底层绑定调用位于 test/test_python_dispatch.py:2473,2480。
v2.9.0 退出 0;35 行、12 文件;底层绑定调用位于 test/test_python_dispatch.py:2535,2542。
v2.10.0 退出 0;38 行、13 文件;底层绑定调用位于 test/test_python_dispatch.py:2538,2545。
v2.11.0 退出 0;38 行、13 文件;底层绑定调用位于 test/test_python_dispatch.py:2564,2571。
v2.12.0 退出 0;38 行、13 文件;底层绑定调用位于 test/test_python_dispatch.py:2589,2596。
master refs/heads/main;退出 0;42 行、13 文件;底层绑定调用位于 test/test_python_dispatch.py:2797,2804。
所属文件 测试用例 调用关系 验证内容 是否支持NPU环境
test/test_python_dispatch.py TestPythonDispatcher.test_basic、test_lstsq 直接构造底层绑定对象 基本加法及最小二乘形状;未断言上下文状态 不支持:原样固定创建 CPU 输入。
test/test_prims.py test_clone_complex、test_infinite_loop_from_py_dispatcher 直接调用完整入口 元张量复数操作及避免循环 不支持:CPU/元张量,并非真实 NPU。
test/test_decomp.py test_native_layer_norm_cpu_decomp、test_quick_core_backward、test_rnn_decomp_module 直接上下文或经梯度检查辅助路径 分解与反向结果对照 无法确认:依赖具体 device/OpInfo。
test/functorch/test_vmap.py test_decomposition_under_python_dispatcher 直接上下文 向量化平方分解结果 不支持:原样 CPU 输入。
test/test_functionalization.py test_instance_norm、test_batch_norm 经局部计算包装函数进入上下文 函数化结果对照 不支持:固定 CPU 输入。
test/test_fake_tensor.py test_split_return_self、test__upsample_bilinear2d_aa_backward_dynamic_shapes 直接上下文配合 FakeTensorMode 形状、步长与虚拟反向行为 不支持:虚拟/元张量。
test/test_meta.py test_index_select_out 直接上下文 元张量输出形状 不支持:目标为元张量。
test/test_proxy_tensor.py test_pre_dispatch_mode_stack、test_pre_dispatch_functionalization 直接上下文或跟踪辅助路径 预分发跟踪与模式交互 无法确认:需独立适配/执行。
test/export/opinfo_schema.py TestOpInfo.test_schema_check_op 直接上下文与 schema 模式 算子 schema 检查 无法确认:依赖设备参数与 OpInfo。

覆盖结论: 部分覆盖。 社区测试没有完整覆盖状态进入/退出、嵌套恢复、异常恢复、非法参数,以及同一状态下真实 NPU 输入输出的组合契约。

三、API 测试用例补齐及适配方案

用例完整性: 部分覆盖。是否需要 NPU 适配: 是,新增用例需实际创建 NPU Tensor 并验证输出仍为 NPU。是否纯 Python 且不涉及硬件: 否,入口对应 C++ TLS 守卫,测试需检查其与 NPU 分发协作。是否与硬件有关: 是,守卫不直接执行算子,但上下文中的算子由输入设备和后端决定。

适配方案: 1.2-2 PyTorch社区无用例或覆盖不完整,涉及硬件。 四个 PR 分别在目标分支新增同一专项测试 test/npu/test__dispatch_python_enable_python_dispatcher.py;不修改 test_upstream、API 声明或实现。

测试用例 验证场景
test_context_enables_and_restores_dispatcher 守卫启用、真实 NPU Tensor 平方加一、NPU 输出与退出恢复。
test_nested_context_restores_outer_state 嵌套退出后的外层状态与最终恢复。
test_context_restores_state_after_exception 异常退出后恢复,异常未被吞掉。
test_invalid_argument 非空构造参数抛出 TypeError。

新增用例必要性: 既有基本调用和分解结果不能排除线程状态泄漏;CPU、Meta/FakeTensor 也不能证明 NPU 协作。状态查询、平方和加法仅作为上述目标 API 契约的辅助检查,不扩展目标 API 范围。

四、资料变更

结论:不涉及。已在 TorchNPU master 的 docs/zh/api/native_api 实际检查如下四个已配置目录,目录均存在且目标 API 均为 0 命中;该接口属于私有 _dispatch 模块,按任务规则不补公开资料,零命中不是资料缺口。

API 版本 实际目录 检索结果 结论
v2.7.1 pytorch_2-7-1 目录检查退出 0;目标 API grep 退出 1,0 命中 不涉及
v2.11.0 pytorch_2-11-0 目录检查退出 0;目标 API grep 退出 1,0 命中 不涉及
v2.12.0 pytorch_2-12-0 目录检查退出 0;目标 API grep 退出 1,0 命中 不涉及
v2.13.0 pytorch_2-13-0 目录检查退出 0;目标 API grep 退出 1,0 命中 不涉及

五、接口变更

结论:不涉及。四个提交仅新增测试文件,未修改 API 声明、绑定、实现或客户可见接口;本轮没有归因至实现的缺陷。

六、功能验证

执行入口: 由 torch-npu-server-runner 在隔离 Python 3.10 环境、显式版本解释器、宿主机模式和 maxWorkers=1 下运行:

python test__dispatch_python_enable_python_dispatcher.py -v

测试源码 SHA-256:02f4d514c2473229c33feddbdd6fa9b3c45dd07dc95f1e68abdb81852dbcc7db。A3 为 aarch64、Ascend910B3、CANN 9.0.0;A5 为 x86_64、Ascend950PR、CANN 9.1.0。两侧均为 Python 3.10.12 的独立环境;稳定版本使用官方发布运行包。master 的历史运行包为 torch 2.15.0.dev20260902+cpu / torch_npu 2.15.0+git9f15aa3,运行包不等同于测试源码提交,也不据此声称由当前 PR head 构建。

运行版本 测试提交 torch / torch_npu A3 A5
v2.7.1 cceda7689b9d903f0ca89866d701fea42c37c300 2.7.1+cpu / 2.7.1.post8 4/4 4/4
v2.11.0 a495a79b6be9417c9f599300fab870c358eb91ae 2.11.0+cpu / 2.11.0 4/4 4/4
v2.12.0 14c297360567098c7fe2eb01972b1a5f5e290ef2 2.12.0+cpu / 2.12.0 4/4 4/4
master 58b75d817412e66d8be71cd4871a33cc08952564 2.15.0.dev20260902+cpu / 2.15.0+git9f15aa3 4/4 4/4

以下为八份已完成运行的完整脱敏日志展示;绝对路径和非失败运行时告警已删除。原件 SHA-256 见附件索引。

v2.7.1 — 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
v2.7.1 — 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
v2.11.0 — 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.903s

OK
v2.11.0 — 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.059s

OK
v2.12.0 — 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
v2.12.0 — 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
master — 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.946s

OK
master — 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 1.929s

OK

文末附件提供相同脱敏日志与原件 SHA-256 索引。CI、CLA 标签已通过;review 与 merge 尚未完成。

已完成运行的证据展示与附件

以下截图展示既有运行日志,未重新执行测试。

dispatcher 双硬件日志展示

下载完整脱敏日志与哈希索引(2678-logs.zip)

likedislike
TorchNPU-BotTorchNPU-BotCollaborators
Sep 8 Added label: help-wantedevent: api-consistencybot-triaged
TorchNPU-Bot
TorchNPU-BotCollaborators
Sep 8 Comments:

检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted

likedislike
sodachansodachan
Sep 8 Modify description of issue
sodachansodachan
Sep 8 Modify description of issue
sodachansodachan
Sep 9 Modify description of issue
sodachansodachan
Sep 14 Modify description of issue
群青世界群青世界Collaborators
Sep 29 changed custom state from TODO to DONE
群青世界群青世界Collaborators
Sep 29 Close issue
ascend-robotascend-robotCollaborators
Sep 29 Added label: resolved