已合并
test: add unflatten_dense_tensors NPU coverage #41632
yulin520创建于 7月14日
test: add unflatten_dense_tensors NPU coverage #41632
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 yulin520 的贡献)7月14日 关联了issue:[Usage]: 【API一致性】torch._C._nn.unflatten_dense_tensors NPU 测试适配
atomgit-bot
7月14日 评论:
7月14日 评论:
变更摘要
本 PR 针对 v2.7.1 分支新增 NPU 设备上 torch._C._nn.unflatten_dense_tensors API 的测试覆盖文件 test/nn/test_unflatten_dense_tensors.py。该测试文件验证了将扁平化 dense tensor 按原始形状拆分恢复为多个 NPU tensor 时的行为一致性,包括形状、dtype、设备类型、数值正确性、存储共享及偏移量等维度,并覆盖了包含空 tensor 的边界场景。此变更仅新增测试文件,未涉及 API 实现或核心框架代码的修改。
主要改动
- 新增测试文件
test/nn/test_unflatten_dense_tensors.py:添加了继承自torch_npu.testing.testcase.TestCase的测试类TestUnflattenDenseTensors,包含 NPU 设备检查逻辑与两个测试用例。 - 新增辅助方法
_get_npu_device_type:通过torch.accelerator.current_accelerator()获取当前加速设备,并显式断言设备类型为npu,确保测试在 NPU 环境下执行。 - 新增基础测试
test_unflatten_dense_tensors_basic:构造两个不同形状的 NPU dense tensor((2,3)和(3,2)),验证torch._C._nn.unflatten_dense_tensors恢复结果的形状、dtype、设备、数值、存储数据指针及storage_offset均与预期一致。 - 新增空 tensor 边界测试
test_unflatten_dense_tensors_with_empty_tensor:在 tensor 列表中引入一个空形状(0,)的 tensor,验证恢复后空 tensor 的numel()为 0、非空 tensor 的数值和存储指针正确性。


ascend-robot
7月14日 评论:
7月14日 评论:
atomgit-bot
7月14日 评论:
7月14日 评论:
此处折叠了271条消息 查看更多
chenrayray
12 天前 评论:
12 天前 评论:
/lgtm


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


ascend-robot
12 天前 评论:
12 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14043 [ commitID:d0397e37 ] 已完成


〖合入来源〗
Issue:
Issue #2862
〖修改方案〗
本 PR 针对
v2.7.1分支新增以下 NPU 测试文件:涉及 API:
torch._C._nn.unflatten_dense_tensors位于 PyTorch C++ 扩展绑定模块torch._C._nn中,用于根据原始 tensor 列表的信息,将扁平化后的 dense tensor 拆分并恢复为多个 dense tensor。经验证,该 API 在
v2.7.1分支中已经存在,torch-npu 无需新增对应的 API 实现代码。本 PR 仅补充 Python 层 NPU 测试,用于验证该接口在 NPU Tensor 输入下的行为一致性。测试中通过统一方式获取当前设备类型:
device_type = ( acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" )本次共新增 4 个测试用例,主要覆盖:
基本功能测试
torch._utils._flatten_dense_tensors进行 flatten;torch._C._nn.unflatten_dense_tensors恢复 tensor;Empty Tensor 测试
Autograd 测试
dtype 保持测试
float16和float32;本 PR:
〖资料变更〗
不涉及。
torch._C._nn.unflatten_dense_tensors属于 PyTorch 内部 C++ 绑定接口。本 PR 未新增公开 API,也未修改用户可见接口,因此不涉及公共 API 资料变更。
〖接口变更〗
不涉及。
本 PR 仅新增 NPU 测试文件,不涉及 API 实现及公开接口变更。
〖功能验证〗
验证环境:
最终验证 Commit:
已完成以下检查:
float16/float32dtype 保持验证;transfer_to_npu未引入检查;Runtime 验证结果:
最终结果:
远程分支与提交信息:
〖CheckList〗
验证截图