Merged
test(tensor): add Tensor.__setstate__ NPU coverage #42446
test(tensor): add Tensor.__setstate__ NPU coverage #42446
Merged
coconutAdd on Jul 22
coconut
coconut
Jul 22

【合入来源】

关联 issue

【修改方案】

修改说明

本 PR 在 v2.11.0 分支新增 test/test_tensor_setstate_api_completion.py,直接验证 torch.Tensor.__setstate__ 的核心行为:

  • 叶子 Tensor 调用 __setstate__ 后可以恢复 requires_grad 状态;
  • 非叶子 Tensor 调用 __setstate__ 时抛出与原生 PyTorch 一致的异常;
  • 测试通过 torch.accelerator.current_accelerator() 选择当前设备,NPU 环境实际使用 NPU。

修改文件:

test/test_tensor_setstate_api_completion.py

涉及 API

torch.Tensor.__setstate__

社区用例覆盖情况

PyTorch 社区用例主要通过序列化、JIT 或 pickle 等间接路径触发 __setstate__,没有面向该接口的直接行为测试。因此本 PR 增加直接 API 调用覆盖,验证 Ascend NPU 上的行为与原生 PyTorch 保持一致。

NPU 适配

测试不使用 transfer_to_npu,也不包含 tensor.cuda()、torch.cuda.* 或 device="cuda" 等 CUDA 迁移路径。

device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"

无加速器环境回退到 CPU 仅用于保证测试文件可导入;NPU 环境通过 accelerator=npu 和 NPU_AVAILABLE=True 确认实际走 NPU 路径。

测试核心逻辑

tensor = torch.ones(2, device=device_type)
self.assertFalse(tensor.requires_grad)

tensor.__setstate__((True, None, None))
self.assertTrue(tensor.requires_grad)

with self.assertRaisesRegex(
    RuntimeError, "__setstate__ can be only called on leaf Tensors"
):
    (tensor + 1).__setstate__((False, None, None))

【资料变更】

已通过独立 doc PR 在 docs/zh/api/native_api/private_apis.md 中补充 API 记录。

【接口变更】

不涉及 API 签名、实现或行为变更,本 PR 仅补充测试覆盖。

【功能验证】

910B3 测试结果

COMMAND: /workspace/venvs/torch2110/bin/python /workspace/tensor_setstate_910B3_tests_20260908/test_tensor_setstate_api_completion_v2110.py -v
PWD: /tmp
DATE: 2026-09-08 14:12:11 CST
test_tensor_setstate (__main__.TestTensorSetstateApiCompletion.test_tensor_setstate) ... ok

----------------------------------------------------------------------
Ran 1 test in 1.422s

OK
EXIT_CODE: 0

Ascend950PR 测试结果

COMMAND: /tmp/benchmark-api-venvs/torch211/bin/python /tmp/tensor_setstate_950pr_tests_20260908/test_tensor_setstate_api_completion.py -v
PWD: /tmp
DATE: 2026-09-08 14:22:23 UTC
test_tensor_setstate (__main__.TestTensorSetstateApiCompletion.test_tensor_setstate) ... [W908 14:22:27.764007073 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) ok

----------------------------------------------------------------------
Ran 1 test in 1.964s

OK
EXIT_CODE: 0

950PR 硬件为 Ascend950PR,npu-smi 25.7.rc1,Health 为 OK。

【CheckList】

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

变更摘要

本 PR 新增测试文件 test/test_tensor_setstate_api_completion.py,为 torch.Tensor.__setstate__ API 补充 NPU 上的直接测试覆盖。由于社区官方没有针对该 API 的直接用例(仅存在序列化/JIT 等间接路径),本次新增自包含的测试用例,验证在 NPU 设备上 leaf Tensor 通过 __setstate__ 恢复 requires_grad 状态,以及 non-leaf Tensor 调用时保持原生报错语义。

主要改动

  • 新增测试文件 test/test_tensor_setstate_api_completion.py:添加 31 行代码,使用 torch.accelerator.current_accelerator().type 动态获取 NPU 设备类型,并引入 torch_npu 模块。
  • test_tensor_setstate 测试用例 — leaf Tensor 状态恢复:在 NPU 设备上创建 requires_grad=False 的 leaf Tensor,调用 __setstate__((True, None, None)) 后断言 requires_grad 变为 True。
  • test_tensor_setstate 测试用例 — non-leaf Tensor 报错语义:对 non-leaf Tensor(tensor + 1)调用 __setstate__,通过 assertRaisesRegex 验证抛出 RuntimeError: "__setstate__ can be only called on leaf Tensors",确保与原生 PyTorch 行为一致。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 22 Comments:

代码审查

关闭总结

已审查文件:

  • test/test_tensor_setstate_api_completion.py:发现 2 个问题

按优先级统计:

  • P1:1 个 — 第 25 行 assertRaisesRegex 错误消息词序错误("can be only called" 应为 "can only be called"),导致测试在匹配 PyTorch 原生 RuntimeError 时必然失败
  • P2:1 个 — 第 13 行 device_type 获取缺少加速器可用性守卫,与项目中其他 20+ 个测试文件的安全模式不一致,在 NPU 未正确初始化时会导致模块导入阶段崩溃

整体风险判断:中高风险。P1 问题为确定性失败(测试无法通过),P2 问题为边界条件下的模块导入崩溃。两个问题均应在合入前修复,同时建议在实际 NPU 环境中执行一次测试以确认第 25 行的错误消息是否完全匹配(本审查因无法访问 PyTorch C++ 源码,conf 为 0.85)。

类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
Incorrect?
111 messages are collapsed here。 See more
群群青世界Collaborators
Sep 28 Resolved final issue
ascend-robotascend-robotCollaborators
Sep 28 Closed associated issue
ascend-robotascend-robotCollaborators
Sep 28 Merged pull request
ascend-robot
ascend-robotCollaborators
Sep 28 Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
AtlasAccount
AtlasAccountCollaborators
Sep 28 Comments:
流水线 pytorch_gitcode_PR_multiVersion#15687 [ commitID:67bb2819 ] 已完成
likedislike