已合并
[test][v2.7.1] add NPU validation cases for torch.Tensor.map_ and torch.Tensor.module_load (issues #2720 #2722) #41661
[test][v2.7.1] add NPU validation cases for torch.Tensor.map_ and torch.Tensor.module_load (issues #2720 #2722) #41661
已合并
lele130创建于 7月14日
lele130
7月14日

【合入来源】

如有社区 issue,请关联 issue 链接
请勿携带内部流程信息(需求链接、问题单、内部 issue 等)

关联 issue:

  • #2720torch.Tensor.map_(API 一致性验证)
  • #2722torch.Tensor.module_load(API 一致性验证)

【修改方案】

本 PR 为 torch.Tensor.map_torch.Tensor.module_load 两个 API 在 NPU 场景下补充一致性验证用例。PyTorch 社区缺少针对这两个 API 的直接、集中测试,按规范 1.3(自写用例)新增测试文件到 test/ 目录。

修改文件:

  • test/test_tensor_map_.py(新增):覆盖 torch.Tensor.map_(3 个用例)
  • test/test_tensor_module_load.py(新增):覆盖 torch.Tensor.module_load(5 个用例)

合计 8 个用例。不涉及 torch_npu 任何 C++/Python 代码改动。

【API 功能介绍】

1. torch.Tensor.map_(tensor, callable) → Tensor

self 和给定 tensor 的每个元素应用 callable,结果存回 self tensor。两个 tensor 必须可广播。

  • callable 签名def callable(a, b) -> number
  • 实现位置torch/_tensor.py,底层调用 at::map kernel
  • NPU 支持情况:NPU 上不可用(TypeError: map_ is only implemented on CPU tensors),社区 CUDA 同样不支持
  • 测试覆盖:CPU 正常路径(2 用例)+ NPU 异常断言(1 用例)

2. torch.Tensor.module_load(other, assign=False) → Tensor

定义如何将 other 转换后加载到 self 中,用于 load_state_dict() 的 swap_tensors 路径。当 get_swap_module_params_on_conversion() 为 True 时使用。

  • 实现self.copy_(other).detach()(assign=False)或 other.detach()(assign=True)
  • 实现位置torch/_tensor.py
  • NPU 支持情况:非计算类 API,基于 copy_detach 实现,NPU 上直接可用,与数据类型无关
  • 测试覆盖:NPU tensor 上 5 个用例(返回值、dtype 保持、assign=True、destination 更新、detach 语义)

【上游社区用例情况】

torch.Tensor.map_

PyTorch 社区 test/test_torch.py::test_broadcast 包含 "map",但显式跳过 CUDA 设备。NPU 行为与 CUDA 一致。

torch.Tensor.module_load

社区无直接测试用例,仅通过 load_state_dict 间接覆盖。

【测试环境】

  • 操作系统:Linux 4.19.90-2102.2.0.0068.3.ctl2.aarch64
  • 昇腾硬件:Ascend NPU(Ascend910B2)
  • 安装版本:torch 2.7.1 + torch_npu 2.7.1.post8
  • 测试分支:v2.7.1

【测试日志】

$ python -u test/test_tensor_map_.py -v
test_map_applies_callable ... ok
test_map_raises_on_npu_tensor ... ok
test_map_uses_destination_values ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.925s OK

$ python -u test/test_tensor_module_load.py -v
test_module_load_assign_true ... ok
test_module_load_keeps_dest_dtype ... ok
test_module_load_returns_detached_tensor ... ok
test_module_load_returns_source_values ... ok
test_module_load_updates_destination ... ok
----------------------------------------------------------------------
Ran 5 tests in 0.944s OK

【资料补齐检查结论】

  • torch.Tensor.map_docs/zh/native_apis 中已有该 API 记录。map_ 在 NPU 上不可用,社区 CUDA 同样不支持,无需补充资料。
  • torch.Tensor.module_loaddocs/zh/native_apis 中已有该 API 记录。非计算类 API,与数据类型无关,无需补充资料。

Fixes #2720, Fixes #2722

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

变更摘要

本 PR 针对 Ascend for PyTorch API 一致性任务(#2720、#2722),新增了两个独立的 NPU 验证测试文件,分别覆盖 torch.Tensor.map_torch.Tensor.module_load 两个 API。这两个 API 在 PyTorch 社区中缺少直接、聚焦的测试用例,新增的用例以自包含方式验证其在 NPU 加速器环境下的基本行为一致性。

主要改动

  • 新增 test/test_tensor_map_.py:包含 TestTensorMap 测试类,共 2 个用例——test_map_applies_callable 验证 map_ 的 callable 应用逻辑(源值 ×2),test_map_uses_destination_values 验证目标张量值在 callable 中的复用(目标值 + 源值)。由于 map_ 为 CPU-only API,测试张量均在 CPU 上运行,并标注了 NPU-EXEMPT
  • 新增 test/test_tensor_module_load.py:包含 TestTensorModuleLoad 测试类,共 2 个用例——test_module_load_returns_source_values 验证 module_load 返回源张量的值,test_module_load_keeps_dest_dtype 验证输出张量保持目标张量的 dtype。张量通过 DEVICE 模式运行在 NPU 加速器上。
  • 统一的设备检测模式:两个文件均使用 torch.accelerator.current_accelerator() 进行加速器检测,未检测到时回退为 "cpu",确保测试在不同环境下的兼容性。
likedislike
atomgit-bot
atomgit-bot
7月14日 评论:

代码审查

Now let me do the final boundary-value and co-located-defect sweep over the diff.

test_tensor_map_.py — re-reading each line:

  • Line 13: DEVICE — already reported as unused.
  • Lines 19-21: torch.zeros(3), torch.tensor([1.0, 2.0, 3.0]), dst.map_(src, lambda d, s: s * 2) — the lambda ignores d, but that's correct for "test_map_applies_callable" (it tests callable application, not destination-value usage). No issue.
  • Line 22: torch.tensor([2.0, 4.0, 6.0]) — correct expected values (1*2, 2*2, 3*2).
  • Lines 25-28: dst.map_(src, lambda d, s: d + s)[11.0, 22.0] — correct (10+1, 20+2).

test_tensor_module_load.py — re-reading each line:

  • Line 13: DEVICE — used correctly on lines 18, 19, 24, 25.
  • Line 18: torch.zeros(3)3 > 0, no zero-length tensor edge case.
  • Line 21: out.cpu() vs src.cpu() — both .cpu() calls are safe on any device.
  • Line 24: torch.zeros(2, dtype=torch.float32)2 > 0, fine.
  • Line 27: out.dtype == dest.dtype — comparison of dtype objects is well-defined.

No boundary issues, no secondary defects at already-reported locations.


Closing Summary

Priority Count
P0 0
P1 0
P2 0
P3 1

Reviewed files:

  • test/test_tensor_map_.py — 1 finding (P3: unused DEVICE variable)
  • test/test_tensor_module_load.py — no issues

Overall risk judgment: 低风险。本次变更新增两个测试文件,逻辑正确、无安全/可靠性问题。唯一的 P3 发现是 test_tensor_map_.py 中定义了但未使用的 DEVICE 变量(该文件为 NPU-EXEMPT,所有测试均在 CPU 上运行),属于轻微的可维护性问题。

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

⛔ 需要修改

likedislike
此处折叠了300条消息 查看更多
Jingwei Huang
Jingwei Huang成员
3 天前 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
3 天前 添加了label:lgtm
ascend-robotascend-robot成员
3 天前 合入了pull request
ascend-robot
ascend-robot成员
3 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike
ascend-robot
ascend-robot成员
3 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14158 [ commitID:43fe55ed ] 已完成
likedislike