已合并
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
已合并
从已删除 :test_unflatten_dense_tensors_v2.7.1合入到Ascend/pytorchv2.7.1
Pull Request已成功合入, 合并人@ascend-robot
(感谢 的贡献)ascend-robot
7月18日 评论:
7月18日 评论:
atomgit-bot
7月18日 评论:
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_basic、test_single_tensor、test_multiple_tensors_different_sizes):验证_flatten_dense_tensors展平后经_unflatten_dense_tensors还原,结果张量的形状和数值与原输入完全一致,覆盖单张量、多个不同形状张量的场景。 - 新增空张量边界测试用例(
test_empty_tensor_in_list、test_all_empty_tensors):覆盖列表中混合空张量以及全部为空张量的场景,验证_unflatten_dense_tensors对零元素张量的numel()和形状还原正确性。 - 新增多 dtype 兼容性测试(
test_different_dtypes):使用subTest对float32、float16、int32三种 dtype 分别验证还原后的张量 dtype 与数值正确性。 - 新增压力与输入类型测试用例(
test_large_num_tensors、test_tuple_input):验证 50 个大数量张量的还原正确性,以及 tuple 类型(非 list)输入同样可被正常处理。


atomgit-bot
7月18日 评论:
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 被漏报。建议补全后可合入。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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


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



【合入来源】
issue任务地址:https://gitcode.com/Ascend/pytorch/issues/2932
【修改方案】
为
torch._utils._unflatten_dense_tensors新增 NPU 设备回归测试用例,放置在test/test_unflatten_dense_tensors.py。test/cpp/api/tensor_flatten.cpp),Python 层面无任何测试用例,且缺少 NPU 设备上的运行验证。TestCase框架,通过torch.accelerator.current_accelerator()自动获取 NPU 设备并完成张量迁移,覆盖以下维度:torch→torch.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
执行命令
结果
2.9.0
执行命令
结果
2.11.0
执行命令
结果
2.12.0
执行命令
结果
master
执行命令
结果
【CheckList】