已合并
[test][v2.7.1] add NPU validation cases for torch.Tensor.map_ and torch.Tensor.module_load (issues #2720 #2722) #41661
lele130创建于 7月14日
[test][v2.7.1] add NPU validation cases for torch.Tensor.map_ and torch.Tensor.module_load (issues #2720 #2722) #41661
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 lele130 的贡献)atomgit-bot
7月14日 评论:
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",确保测试在不同环境下的兼容性。


ascend-robot
7月14日 评论:
7月14日 评论:
atomgit-bot
7月14日 评论:
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 ignoresd, 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()vssrc.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 ofdtypeobjects 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: unusedDEVICEvariable)test/test_tensor_module_load.py— no issues
Overall risk judgment: 低风险。本次变更新增两个测试文件,逻辑正确、无安全/可靠性问题。唯一的 P3 发现是 test_tensor_map_.py 中定义了但未使用的 DEVICE 变量(该文件为 NPU-EXEMPT,所有测试均在 CPU 上运行),属于轻微的可维护性问题。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 2 |
| 🟡 建议 | 0 |
⛔ 需要修改


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


3 天前 添加了label:lgtm
3 天前 合入了pull request
ascend-robot
3 天前 评论:
3 天前 评论:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


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


【合入来源】
关联 issue:
torch.Tensor.map_(API 一致性验证)torch.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,结果存回selftensor。两个 tensor 必须可广播。def callable(a, b) -> numbertorch/_tensor.py,底层调用at::mapkernelTypeError: map_ is only implemented on CPU tensors),社区 CUDA 同样不支持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.pycopy_和detach实现,NPU 上直接可用,与数据类型无关【上游社区用例情况】
torch.Tensor.map_
PyTorch 社区
test/test_torch.py::test_broadcast包含"map",但显式跳过 CUDA 设备。NPU 行为与 CUDA 一致。torch.Tensor.module_load
社区无直接测试用例,仅通过
load_state_dict间接覆盖。【测试环境】
【测试日志】
【资料补齐检查结论】
torch.Tensor.map_:docs/zh/native_apis中已有该 API 记录。map_在 NPU 上不可用,社区 CUDA 同样不支持,无需补充资料。torch.Tensor.module_load:docs/zh/native_apis中已有该 API 记录。非计算类 API,与数据类型无关,无需补充资料。Fixes #2720, Fixes #2722