已合并
test: add unflatten_dense_tensors NPU coverage #41632
yulin520创建于 7月14日
test: add unflatten_dense_tensors NPU coverage #41632
已合并
yulin520
7月14日
7月14日
# 〖合入来源〗
- [ ] 需求
- [ ] 问题单
- [x] issue/工单:#2862
- [ ] 重构优化
- [ ] 资料更新
Issue:
[Issue #2862](https://gitcode.com/Ascend/pytorch/issues/2862)
# 〖修改方案〗
本 PR 针对 `v2.7.1` 分支新增以下 NPU 测试文件:
```text
test/nn/test_unflatten_dense_tensors.py
```
涉及 API:
```python
torch._C._nn.unflatten_dense_tensors
```
`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 输入下的行为一致性。
测试中通过统一方式获取当前设备类型:
```python
device_type = (
acc.type
if (acc := torch.accelerator.current_accelerator())
else "cpu"
)
```
本次共新增 4 个测试用例,主要覆盖:
1. **基本功能测试**
- 构造多个 NPU dense tensor;
- 使用 `torch._utils._flatten_dense_tensors` 进行 flatten;
- 调用 `torch._C._nn.unflatten_dense_tensors` 恢复 tensor;
- 检查输出数量、shape、dtype、device 和数值;
- 检查恢复 tensor 的 storage 共享关系及 storage offset。
2. **Empty Tensor 测试**
- 验证输入中包含空 tensor 时能够正常恢复;
- 检查恢复后的 shape、dtype 和 device。
3. **Autograd 测试**
- 验证恢复后的 tensor 可以正常参与后续计算;
- 验证反向传播路径可用。
4. **dtype 保持测试**
- 覆盖 `float16` 和 `float32`;
- 验证恢复后的 tensor dtype 与原始 tensor 保持一致。
本 PR:
```text
未新增 API 实现代码
未引入 transfer_to_npu 自动迁移
未修改 JIT / Dynamo 核心方法
未引入无关文件修改
```
# 〖资料变更〗
不涉及。
`torch._C._nn.unflatten_dense_tensors` 属于 PyTorch 内部 C++ 绑定接口。
本 PR 未新增公开 API,也未修改用户可见接口,因此不涉及公共 API 资料变更。
# 〖接口变更〗
不涉及。
本 PR 仅新增 NPU 测试文件,不涉及 API 实现及公开接口变更。
# 〖功能验证〗
验证环境:
```text
操作系统:Linux(Ubuntu)
昇腾硬件:910B4
CANN:8.5.0
目标分支:v2.7.1
PyTorch:2.7.1+cpu
torch_npu:2.7.1.post2
current accelerator:npu
npu available:True
npu device count:1
```
最终验证 Commit:
```text
d0397e37f40679f391d9d70bde916b4507c04950
```
已完成以下检查:
1. API 存在性验证;
2. NPU 设备可用性验证;
3. 基本 flatten / unflatten 功能验证;
4. 输出 tensor 数量、shape、dtype、device 验证;
5. 输出 tensor 数值一致性验证;
6. storage 共享关系及 storage offset 验证;
7. empty tensor 场景验证;
8. autograd 路径验证;
9. `float16` / `float32` dtype 保持验证;
10. 测试文件 Python 语法检查;
11. 测试文件修改范围检查;
12. `transfer_to_npu` 未引入检查;
13. JIT / Dynamo 核心方法未修改检查;
14. commit 单文件范围检查;
15. 最终 PR Commit 对应的真实 NPU Runtime 验证。
Runtime 验证结果:
```text
TORCH=2.7.1+cpu
TORCH_NPU=2.7.1.post2
DEVICE=npu
NPU_RUNTIME=PASS
....
----------------------------------------------------------------------
Ran 4 tests
OK
API43_RESULT=PASS
```
最终结果:
```text
API_EXISTENCE_RESULT=PASS
NPU_RUNTIME=PASS
API43_TEST_COUNT=4
API43_RESULT=PASS
FINAL_RESULT=PASS
```
远程分支与提交信息:
```text
源仓库:weixin_65332823/pytorch
源分支:test-unflatten-dense-tensors-npu-v271-issue2862
目标仓库:Ascend/pytorch
目标分支:v2.7.1
最终 Commit:d0397e37f40679f391d9d70bde916b4507c04950
```
# 〖CheckList〗
- [x] PR 标题已使用 `test` 类型标签
- [x] 已关联 Issue #2862
- [x] 已确认 `v2.7.1` 分支中 API 存在
- [x] 已使用统一 `device_type` 获取方式
- [x] 已完成基本功能验证
- [x] 已完成 storage 相关验证
- [x] 已完成 empty tensor 场景验证
- [x] 已完成 autograd 路径验证
- [x] 已完成 `float16` / `float32` dtype 验证
- [x] 已完成真实 NPU Runtime 验证
- [x] 4 个测试用例全部通过
- [x] 已检查新增测试文件内容
- [x] 已确认 commit 仅包含目标测试文件
- [x] 已确认未引入 `transfer_to_npu`
- [x] 已确认未修改 JIT / Dynamo 核心方法
- [x] 已确认不涉及 API 实现代码
- [x] 已确认不涉及资料变更
- [x] 已确认不涉及公开接口变更
# 验证截图




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
4 天前 评论:
4 天前 评论:
/lgtm


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


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

