已关闭
【API一致性任务】 API一致性:torch.testing._comparison.TensorLikePair NPU 适配 #3602
jerry lee创建于  7月31日关闭于  8月15日
jerry lee
jerry lee
7月31日 创建

API 一致性:torch.testing._comparison.TensorLikePair

API 功能与上游覆盖

torch.testing._comparison.TensorLikePair.compare() 用于校验张量值、shape、layout、stride、device 和 dtype,失败时抛出 ErrorMeta。社区 test/test_testing.py 的相关测试仅 mock 构造或比较阶段的异常传播,未直接执行 NPU 张量的真实比较。

维护分支的调用路径为 TestAssertClose.test_tensor_like_pair_npu -> TensorLikePair(actual, expected) -> TensorLikePair.compare();master 为 TestTesting.test_tensor_like_pair -> TensorLikePair(actual, expected) -> TensorLikePair.compare()。

适配方案与必要性

测试现在覆盖默认 NPU 比较、check_device=False、check_dtype=False、check_layout=False、check_stride=True、非默认 rtol/atol 和 equal_nan=True,并验证 dtype、layout、stride、shape、数值失配均抛出 ErrorMeta。若缺少这些断言,比较选项与元数据检查的 NPU 行为没有直接回归保护。

v2.7.1、v2.11.0、v2.12.0 均先将已有 test_upstream/test/test_testing.py.patch 应用于对应官方 PyTorch tag,在结果上扩展目标用例后使用 git diff --unified=1 生成完整 patch,保留历史有效 hunks。三份 patch 分别在 v2.7.1 e2d141dbde55c2a4370fac5165b0561b6af4798b、v2.11.0 70d99e998b4955e0049d13a98d77ae1b14db1f45、v2.12.0 0d62256a2b23365f8e1604297eb23a6545102aa8 通过 git apply --check。master 已同步至当前源分支,只修改 torch-npu 自有测试。

资料检查

torch.testing._comparison.TensorLikePair 属于 PyTorch 内部/私有接口,不属于面向用户的 native API 支持清单范围,因此无需更新支持文档。

各版本 PR 概括

版本 处理方式
v2.7.1 修改既有 test_upstream/test/test_testing.py.patch 并保留历史有效 hunks。
v2.11.0 修改既有 test_upstream/test/test_testing.py.patch 并保留历史有效 hunks。
v2.12.0 修改既有 test_upstream/test/test_testing.py.patch 并保留历史有效 hunks。
master 在 test/npu/test_testing.py 扩展 torch-npu 自有直接用例。
v2.9.0 / v2.10.0 维护版本,不创建适配。

功能验证

v2.7.1

命令:

source /usr/local/Ascend/cann-9.0.0/set_env.sh
export PATH=/usr/local/python3.12.13/bin:$PATH
python3.12 ./tensorlike_v271_standalone.py

环境:torch 2.7.1,torch_npu 2.7.1.post8,npu_available=True,设备 Ascend910B4。

输出:

[W805 12:26:14.422739690 NPUCachingAllocator.cpp:198] Warning: The current CANN and Soc Version require processing for 32 padding size, with memory allocation. (function operator())
.
----------------------------------------------------------------------
Ran 1 test in 0.950s

OK

结论:官方 test/test_testing.py 在目标方法执行前因与本 API 无关的空 @ops 列表注册失败;以与修复后目标方法逐项一致的独立执行完成 1 项断言验证。CANN 对齐告警不影响断言。

v2.11.0

命令:

source /usr/local/Ascend/cann-9.0.0/set_env.sh
export PATH=/usr/local/python3.12.13/bin:$PATH
python3.12 ./pytorch-official-2.11.0/test/test_testing.py TestAssertClose.test_tensor_like_pair_npu

环境:torch 2.11.0,torch_npu 2.11.0,npu_available=True,设备 Ascend910B4。

输出:

[W805 12:22:39.565845920 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
.
----------------------------------------------------------------------
Ran 1 test in 0.468s

OK

结论:目标用例通过,CANN 对齐告警不影响 TensorLikePair 断言。

v2.12.0

命令:

source /usr/local/Ascend/cann-9.0.0/set_env.sh
export PATH=/usr/local/python3.12.13/bin:$PATH
python3.12 ./pytorch-official-2.12.0/test/test_testing.py TestAssertClose.test_tensor_like_pair_npu

环境:torch 2.12.0+cu130,torch_npu 2.12.0,npu_available=True,设备 Ascend910B4。

输出:

W0805 11:57:27.011000 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`
[W805 11:57:30.415371710 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
.
----------------------------------------------------------------------
Ran 1 test in 0.466s

OK

结论:目标用例通过;CuTeDSL 可选依赖和 CANN 对齐告警不影响 TensorLikePair 断言。

master

命令:

source /usr/local/Ascend/cann-9.0.0/set_env.sh
export PATH=/usr/local/python3.12.13/bin:$PATH
python3.12 ./test_testing_master_fixed.py TestTesting.test_tensor_like_pair

环境:torch 2.12.0+cu130,torch_npu 2.12.0,npu_available=True,设备 Ascend910B4。

输出:

W0805 12:32:58.690000 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`
[W805 12:33:01.304917380 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
.
----------------------------------------------------------------------
Ran 1 test in 0.947s

OK

结论:torch-npu 自有目标用例通过;CuTeDSL 可选依赖和 CANN 对齐告警不影响断言。

likedislike
jerry leejerry lee
7月31日 关联了pull request:test(npu): add TensorLikePair NPU validation
TorchNPU-BotTorchNPU-Bot成员
7月31日 添加了label:bot-triaged
TorchNPU-Bot
TorchNPU-Bot成员
7月31日 评论:

检测到当前 issue 已关联 PR,自动添加标签:bot-triaged

likedislike
jerry leejerry lee
7月31日 关联了pull request:test(npu): add TensorLikePair NPU validation
此处折叠了12条事件消息 查看更多
TorchNPU-BotTorchNPU-Bot成员
8月15日 添加了label:event: api-consistency
TorchNPU-Bot
TorchNPU-Bot成员
8月15日 评论:

检测到社区任务相关 issue,自动添加标签:event: api-consistency

likedislike
ascend-robotascend-robot成员
8月15日 关闭了 issue
ascend-robotascend-robot成员
8月15日 添加了label:resolved