已合并
test:This PR adds missing test cases for torch._utils._unflatten_dense_tensors, as there are currently no community-provided tests for this function. #42048
创建于 7月18日
test:This PR adds missing test cases for torch._utils._unflatten_dense_tensors, as there are currently no community-provided tests for this function. #42048
已合并
创建于 7月18日
已删除 :test_unflatten_dense_tensors_v2.7.1合入到Ascend/pytorchv2.7.1
7月18日

【合入来源】

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

issue任务地址:https://gitcode.com/Ascend/pytorch/issues/2932

【修改方案】

torch._utils._unflatten_dense_tensors 新增 NPU 设备回归测试用例,放置在 test/test_unflatten_dense_tensors.py

  1. PyTorch 社区对该 API 仅有 C++ 层单元测试(test/cpp/api/tensor_flatten.cpp),Python 层面无任何测试用例,且缺少 NPU 设备上的运行验证。
  2. 新增 8 个测试用例,基于 PyTorch 标准 TestCase 框架,通过 torch.accelerator.current_accelerator() 自动获取 NPU 设备并完成张量迁移,覆盖以下维度:
    • 展平-还原往返操作正确性
    • 单张量与多张量(1D/2D/3D 不同形状)
    • 空张量混合与全空张量边界场景
    • float32 / float16 / int32 多 dtype 兼容性
    • 50 个大数量张量压力场景
    • tuple 类型输入兼容性
  3. 遵循 NPU 测试用例规范:导入顺序为 torchtorch.testing._internal.common_utils,不依赖 torch_npu,使用 self.assert* 断言,无 print/try-except,注释精简英文。

【资料变更】

不涉及。torch._utils._unflatten_dense_tensors 为 PyTorch 内部工具函数(_ 前缀),不属于公开文档覆盖范围。

【接口变更】

不涉及。仅新增测试文件,无对外接口变更。

【功能验证】

测试场景:在 NPU 设备上验证 torch._utils._unflatten_dense_tensors 将展平 1D 张量按目标形状还原为多个张量的功能正确性。

2.7.1
执行命令

python test_unflatten_dense_tensors.py -v

结果

test_all_empty_tensors (__main__.TestUnflattenDenseTensors.test_all_empty_tensors) ... ok
test_different_dtypes (__main__.TestUnflattenDenseTensors.test_different_dtypes) ... ok
test_empty_tensor_in_list (__main__.TestUnflattenDenseTensors.test_empty_tensor_in_list) ... ok
test_large_num_tensors (__main__.TestUnflattenDenseTensors.test_large_num_tensors) ... ok
test_multiple_tensors_different_sizes (__main__.TestUnflattenDenseTensors.test_multiple_tensors_different_sizes) ... ok
test_round_trip_basic (__main__.TestUnflattenDenseTensors.test_round_trip_basic) ... ok
test_single_tensor (__main__.TestUnflattenDenseTensors.test_single_tensor) ... ok
test_tuple_input (__main__.TestUnflattenDenseTensors.test_tuple_input) ... ok

----------------------------------------------------------------------
Ran 8 tests in 1.339s

OK

2.9.0
执行命令

python test_unflatten_dense_tensors.py -v

结果

test_all_empty_tensors (__main__.TestUnflattenDenseTensors.test_all_empty_tensors) ... ok
test_different_dtypes (__main__.TestUnflattenDenseTensors.test_different_dtypes) ... ok
test_empty_tensor_in_list (__main__.TestUnflattenDenseTensors.test_empty_tensor_in_list) ... ok
test_large_num_tensors (__main__.TestUnflattenDenseTensors.test_large_num_tensors) ... ok
test_multiple_tensors_different_sizes (__main__.TestUnflattenDenseTensors.test_multiple_tensors_different_sizes) ... ok
test_round_trip_basic (__main__.TestUnflattenDenseTensors.test_round_trip_basic) ... ok
test_single_tensor (__main__.TestUnflattenDenseTensors.test_single_tensor) ... ok
test_tuple_input (__main__.TestUnflattenDenseTensors.test_tuple_input) ... ok

----------------------------------------------------------------------
Ran 8 tests in 1.331s

OK

2.11.0
执行命令

python test_unflatten_dense_tensors.py -v

结果

test_all_empty_tensors (__main__.TestUnflattenDenseTensors.test_all_empty_tensors) ... ok
test_different_dtypes (__main__.TestUnflattenDenseTensors.test_different_dtypes) ... [W718 07:12:27.082786050 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_empty_tensor_in_list (__main__.TestUnflattenDenseTensors.test_empty_tensor_in_list) ... ok
test_large_num_tensors (__main__.TestUnflattenDenseTensors.test_large_num_tensors) ... ok
test_multiple_tensors_different_sizes (__main__.TestUnflattenDenseTensors.test_multiple_tensors_different_sizes) ... ok
test_round_trip_basic (__main__.TestUnflattenDenseTensors.test_round_trip_basic) ... ok
test_single_tensor (__main__.TestUnflattenDenseTensors.test_single_tensor) ... ok
test_tuple_input (__main__.TestUnflattenDenseTensors.test_tuple_input) ... ok

----------------------------------------------------------------------
Ran 8 tests in 1.357s

OK

2.12.0
执行命令

python test_unflatten_dense_tensors.py -v

结果

W0718 07:12:38.397000 45485 opt/conda/envs/v2.12.0/lib/python3.12/site-packages/torch/_native/cutedsl_utils.py:55] CuTeDSL operators require optional Python packages `nvidia-cutlass-dsl` and `apache-tvm-ffi`; missing optional dependency `nvidia_cutlass_dsl` (importlib.util.find_spec(nvidia_cutlass_dsl) failed)
test_all_empty_tensors (__main__.TestUnflattenDenseTensors.test_all_empty_tensors) ... ok
test_different_dtypes (__main__.TestUnflattenDenseTensors.test_different_dtypes) ... [W718 07:12:39.554782459 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_empty_tensor_in_list (__main__.TestUnflattenDenseTensors.test_empty_tensor_in_list) ... ok
test_large_num_tensors (__main__.TestUnflattenDenseTensors.test_large_num_tensors) ... ok
test_multiple_tensors_different_sizes (__main__.TestUnflattenDenseTensors.test_multiple_tensors_different_sizes) ... ok
test_round_trip_basic (__main__.TestUnflattenDenseTensors.test_round_trip_basic) ... ok
test_single_tensor (__main__.TestUnflattenDenseTensors.test_single_tensor) ... ok
test_tuple_input (__main__.TestUnflattenDenseTensors.test_tuple_input) ... ok

----------------------------------------------------------------------
Ran 8 tests in 1.374s

OK

master
执行命令

python test_unflatten_dense_tensors.py -v

结果

test_all_empty_tensors (__main__.TestUnflattenDenseTensors.test_all_empty_tensors) ... ok
test_different_dtypes (__main__.TestUnflattenDenseTensors.test_different_dtypes) ... [W718 07:12:49.941707130 NPUCachingAllocator.cpp:174] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_empty_tensor_in_list (__main__.TestUnflattenDenseTensors.test_empty_tensor_in_list) ... ok
test_large_num_tensors (__main__.TestUnflattenDenseTensors.test_large_num_tensors) ... ok
test_multiple_tensors_different_sizes (__main__.TestUnflattenDenseTensors.test_multiple_tensors_different_sizes) ... ok
test_round_trip_basic (__main__.TestUnflattenDenseTensors.test_round_trip_basic) ... ok
test_single_tensor (__main__.TestUnflattenDenseTensors.test_single_tensor) ... ok
test_tuple_input (__main__.TestUnflattenDenseTensors.test_tuple_input) ... ok

----------------------------------------------------------------------
Ran 8 tests in 1.372s

OK

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 的贡献)
-此账号已注销
7月18日 创建了 pull request,commit 0fa21a1b
-此账号已注销
7月18日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(19)
atomgit-bot
atomgit-bot
7月18日 评论:

变更摘要

此 PR 为 torch._utils._unflatten_dense_tensors 新增 Python 层 NPU 设备回归测试文件 test/test_unflatten_dense_tensors.py。由于 PyTorch 社区对该函数仅有 C++ 层测试而缺少 Python 单元测试,本次变更新增 8 个测试用例,涵盖展平-还原往返正确性、不同形状/数量/ dtype 的张量输入、空张量边界场景及 tuple 类型兼容性。测试通过 torch.accelerator.current_accelerator() 自动获取 NPU 设备并完成张量迁移,遵循 NPU 测试用例规范,无额外外部依赖。

主要改动

  • 新增 TestUnflattenDenseTensors 测试类:继承自 TestCase,在 setUp 中通过 torch.accelerator.current_accelerator() 自动探测当前加速器设备类型,并通过 _to_device 辅助方法将张量迁移至目标设备,确保测试可跨设备运行。
  • 新增往返正确性测试用例test_round_trip_basictest_single_tensortest_multiple_tensors_different_sizes):验证 _flatten_dense_tensors 展平后经 _unflatten_dense_tensors 还原,结果张量的形状和数值与原输入完全一致,覆盖单张量、多个不同形状张量的场景。
  • 新增空张量边界测试用例test_empty_tensor_in_listtest_all_empty_tensors):覆盖列表中混合空张量以及全部为空张量的场景,验证 _unflatten_dense_tensors 对零元素张量的 numel() 和形状还原正确性。
  • 新增多 dtype 兼容性测试test_different_dtypes):使用 subTestfloat32float16int32 三种 dtype 分别验证还原后的张量 dtype 与数值正确性。
  • 新增压力与输入类型测试用例test_large_num_tensorstest_tuple_input):验证 50 个大数量张量的还原正确性,以及 tuple 类型(非 list)输入同样可被正常处理。
likedislike
atomgit-bot
atomgit-bot
7月18日 评论:

代码审查

边界值、热点路径和同位置二次缺陷的最终扫描均未发现额外问题。以下是审查结论。


审查结论

审查文件:

  • test/test_unflatten_dense_tensors.py — 已审查,发现 1 个 P3 问题

各优先级问题数量:

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 1

整体风险评估:低风险。 该 PR 新增的是一个测试文件,代码结构清晰、遵循了项目现有的 NPU 测试规范。发现的唯一问题是一个测试用例中的断言覆盖缺口:test_empty_tensor_in_list 对列表中空张量的 shape 和值相等检查不完整,但这不会使现有测试失败,仅可能导致特定 bug 被漏报。建议补全后可合入。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
此处折叠了103条消息 查看更多
梁松伟
梁松伟成员
7月23日 评论:

/approve

likedislike
ascend-robotascend-robot成员
7月23日 添加了label:approvedlgtm
ascend-robotascend-robot成员
7月23日 合入了pull request
ascend-robot
ascend-robot成员
7月23日 评论:
流水线 pytorch_gitcode_PR_multiVersion#13029 [ commitID:e1d5bf95 ] 已完成
likedislike
xin0366
19 小时前 评论:

image.png
由于此前账号资料调整,该任务由本人承接与完成,特此说明。

likedislike