已合并
test: add NPU validation for enable_python_dispatcher (#2678) #45964
test: add NPU validation for enable_python_dispatcher (#2678) #45964
已合并
sodachan创建于 9月7日
sodachan
sodachan
9月7日

【合入来源】

【修改方案】

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

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

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

master 的历史运行包使用官方 nightly PyTorch 与目标源码构建的 torch_npu,不等同于当前上游 master。完整脱敏日志和哈希索引见结果 #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.946s

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 1.929s

OK

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 sodachan 的贡献)
sodachansodachan
9月7日 创建了 pull request,commit 82afd2e7
sodachansodachan
9月7日 关联了issue:【社区任务】7月社区任务第一期-Ascend for PyTorch API 一致性开发(55)
atomgit-bot
atomgit-bot
9月7日 评论:

变更摘要

本 PR 针对社区对 torch._dispatch.python.enable_python_dispatcher 覆盖不完整的问题,新增 NPU 侧测试文件 test/npu/test__dispatch_python_enable_python_dispatcher.py(关联 issue #2678,不关闭),用于在真实 NPU 输入输出上验证 Python dispatcher 上下文的启用、嵌套与状态恢复契约。测试未修改 PyTorch patch 或 TorchNPU 实现,仅新增用例。

主要改动

  • 新增 NPU 测试文件: 创建 test/npu/test__dispatch_python_enable_python_dispatcher.py,定义测试类 TestEnablePythonDispatcher,按上游 TorchTestCase 优先、TorchNPU TestCase 其次的顺序继承,并通过 run_tests 执行。
  • 增加真实 NPU 分发路径用例: test_context_enables_and_restores_dispatcher 使用 torch.accelerator.current_accelerator() 获取 device_type 并以 .to(device_type) 构造输入,断言输入/输出张量的 device.type 为 "npu",同时校验 input_tensor.square() + 1 的数值结果。
  • 增加上下文启用/恢复与嵌套用例: 新增 test_context_enables_and_restores_dispatcher 与 test_nested_context_restores_outer_state,通过辅助函数 is_python_dispatcher_enabled()(基于 torch._C._dispatch_tls_is_dispatch_key_included 查询 DispatchKey.PythonDispatcher)验证进入上下文后 dispatcher 启用、退出后恢复到进入前状态。
  • 增加异常退出恢复与参数校验用例: 新增 test_context_restores_state_after_exception,验证上下文内抛出 RuntimeError 后线程状态仍恢复;新增 test_invalid_argument,验证向 enable_python_dispatcher("invalid") 传入非法参数时抛出 TypeError。
likedislike
不准确?
atomgit-bot
atomgit-bot
9月7日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
ascend-robotascend-robot成员
9月7日 添加了label:ascend-cla/yes
此处折叠了55条消息 查看更多
ascend-robotascend-robot成员
9月29日 添加了label:docs-ci-pipeline-success
AtlasAccountAtlasAccount成员
9月29日 删除了label:ci-pipeline-running
AtlasAccountAtlasAccount成员
9月29日 添加了label:ci-pipeline-passed
ascend-robotascend-robot成员
9月29日 合入了pull request
AtlasAccount
AtlasAccount成员
9月29日 评论:
流水线 pytorch_gitcode_PR_multiVersion-onetrack#236 [ commitID:58b75d81 ] 已完成
likedislike