已开启
【API一致性任务】torch.UntypedStorage.to NPU 测试适配方案 #4041
Wo150创建于  8月15日
Wo150
8月15日 创建

torch.UntypedStorage.to NPU 测试适配方案

一、API 功能说明

适用版本:v2.7.1、v2.11.0、v2.12.0、master

当前状态: 有效。

可见性: 公开。

功能: torch.UntypedStorage.to 将无类型存储转移到指定设备;当目标设备与当前设备一致时可直接返回当前存储,否则创建目标设备上的存储并复制原始字节内容。cudahpu 等存储设备转换入口会复用该方法。

参数: device 指定目标设备;non_blocking 控制在底层条件允许时是否采用非阻塞复制,默认值为 False

返回: 返回位于目标设备、保存相同存储内容的 torch.UntypedStorage

设备约束: 未发现 Python 层显式设备限制;设备能力跟随输入张量并由底层实现决定。

二、PyTorch 社区测试用例情况

适用版本:v2.7.1、v2.11.0、v2.12.0

PyTorch 社区测试中检索到以下与 torch.UntypedStorage.to 相关的间接用例:

测试文件 测试用例 覆盖情况
test/test_torch.py test_storage_all_devices 通过存储设备转换包装入口覆盖底层 to 调用,验证存储在不同可用设备间转换时的设备属性和内容一致性;相关包装入口包括 cudahpu

现有社区测试覆盖了通用设备转换路径,但未形成面向 NPU 的非阻塞选项、同设备返回、空存储、跨 NPU 设备和非法参数专项验证。

适用版本:master

PyTorch 社区测试中检索到以下与 torch.UntypedStorage.to 相关的直接或间接用例:

测试文件 测试用例 覆盖情况
test/test_torch.py test_storage_all_devices 通过存储设备转换包装入口覆盖底层 to 调用,验证存储在不同可用设备间转换时的设备属性和内容一致性;相关包装入口包括 cudahpu
test/test_xpu.py test_storage_pin_memory 覆盖 XPU 存储固定内存及相关设备转移路径,验证存储在固定内存场景下的设备转换行为。

现有社区测试覆盖了通用设备转换和 XPU 固定内存相关路径,但未形成面向 NPU 的非阻塞选项、同设备返回、空存储、跨 NPU 设备和非法参数专项验证。
结论:PyTorch社区已有测试用例,但是测试用例不全

三、API 测试用例补齐及适配方案

适配方案所属版本:v2.7.1、v2.11.0、v2.12.0、master

新增测试文件 test/test_untyped_storage_to.py,补充以下测试用例:

测试用例 验证场景
test_to_npu_device_types 使用设备类型字符串和 torch.device 等目标设备表示方式,将存储转移到当前 NPU,验证返回类型、目标设备和存储内容。
test_to_npu_non_blocking_options 从固定内存存储分别执行阻塞与非阻塞 NPU 转移,并在同步后验证两种结果的设备类型和内容一致性。
test_to_cpu_non_blocking_options 将 NPU 存储转移到 CPU,覆盖 non_blockingNoneFalseTrue 的情况,验证目标设备、非固定内存状态及内容。
test_to_same_npu_returns_self 将存储转移到其当前 NPU 设备,验证返回对象与原存储为同一对象,且内容和设备保持不变。
test_to_empty_storage 对空的无类型存储执行 NPU 与 CPU 间转移,验证结果字节数为零且目标设备正确。
test_to_another_npu 在至少两个加速器可用时执行跨 NPU 设备复制,验证目标设备和存储内容;设备不足时跳过。
test_to_invalid_arguments 覆盖空设备、列表设备、无效设备字符串以及错误 non_blocking 类型,验证抛出 TypeErrorRuntimeError

测试内容: 测试在 NPU、CPU、固定内存和多设备场景下调用 torch.UntypedStorage.to,覆盖设备参数表示、阻塞与非阻塞复制、同设备快速路径、空存储、跨设备复制和非法参数,并通过对象身份、返回类型、目标设备、固定内存状态、字节内容和异常类型进行断言。辅助方法通过在存储上构造 uint8 张量统一校验原始字节内容。

新增原因及必要性: PyTorch 社区现有相关用例主要覆盖通用设备转换及部分固定内存路径,未直接覆盖上述 NPU 专项行为。新增测试用于确认存储在 NPU 与 CPU 之间、同一 NPU 以及不同 NPU 之间转换时的内容和对象语义,并保护非阻塞复制、空存储及参数校验行为免受后续实现回归影响。

四、资料变更

适用版本:v2.7.1、v2.11.0、v2.12.0、v2.13.0

结论:涉及。

当前API在master分支下的docs / zh / api / native_api 下的各版本文档资料中均未记录,需要补齐。

五、接口变更

适用版本:v2.7.1、v2.11.0、v2.12.0、master

结论:不涉及。

本次提交仅新增 test/test_untyped_storage_to.py 测试文件,未修改 API 实现、声明、注册或跨仓依赖,也未改变客户可见的参数、返回值、设备转换语义和运行行为,因此不涉及接口变更。

六、功能验证

v2.7.1

执行命令:

python test_untyped_storage_to.py -v

结果:

test_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... [W815 07:58:29.078067848 NPUCachingAllocator.cpp:198] Warning: The current CANN and Soc Version require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.695s

OK

v2.11.0

执行命令:

python test_untyped_storage_to.py -v

结果:

test_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... [W815 07:58:41.834264255 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.701s

OK

v2.12.0

执行命令:

python test_untyped_storage_to.py -v

结果:

W0815 07:58:53.380000 360160 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_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... [W815 07:58:54.518838793 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.694s

OK

master

执行命令:

python test_untyped_storage_to.py -v

结果:

W0815 07:59:05.841000 360255 opt/conda/envs/master/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_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... [W815 07:59:06.992339453 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.721s

OK

以下是A5服务器验证结果
获取芯片型号命令:

python3 -c "import acl;print(acl.get_soc_name())"

执行结果:

Ascend950PR_9579

参考截图:
image.png

v2.7.1

执行命令:

python test_untyped_storage_to.py -v

结果:

test_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... skipped 'Two accelerators are required for cross-device copy'
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... [W901 09:05:58.061925281 NPUCachingAllocator.cpp:198] Warning: The current CANN and Soc Version require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.609s

OK (skipped=1)

v2.11.0

执行命令:

python test_untyped_storage_to.py -v

结果:

test_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... skipped 'Two accelerators are required for cross-device copy'
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... [W901 09:06:27.112441288 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.620s

OK (skipped=1)

v2.12.0

执行命令:

python test_untyped_storage_to.py -v

结果:

test_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... skipped 'Two accelerators are required for cross-device copy'
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... [W901 09:07:03.074544793 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.627s

OK (skipped=1)

master

执行命令:

python test_untyped_storage_to.py -v

结果:

test_to_another_npu (__main__.TestUntypedStorageTo.test_to_another_npu) ... skipped 'Two accelerators are required for cross-device copy'
test_to_cpu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_cpu_non_blocking_options) ... [W901 09:08:06.592721122 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator())
ok
test_to_empty_storage (__main__.TestUntypedStorageTo.test_to_empty_storage) ... ok
test_to_invalid_arguments (__main__.TestUntypedStorageTo.test_to_invalid_arguments) ... ok
test_to_npu_device_types (__main__.TestUntypedStorageTo.test_to_npu_device_types) ... ok
test_to_npu_non_blocking_options (__main__.TestUntypedStorageTo.test_to_npu_non_blocking_options) ... ok
test_to_same_npu_returns_self (__main__.TestUntypedStorageTo.test_to_same_npu_returns_self) ... ok

----------------------------------------------------------------------
Ran 7 tests in 1.593s

OK (skipped=1)
likedislike
WWo150
8月15日 关联了看板:FrameworkPTAdapter 版本issue看板
WWo150
8月15日 修改了issue 的描述
WWo150
8月15日 关联了pull request:test:The test case coverage for the torch.UntypedStorage.to API is incomplete; additional test cases need to be added.
WWo150
8月15日 关联了pull request:test:The test case coverage for the torch.UntypedStorage.to API is incomplete; additional test cases need to be added.
WWo150
8月15日 关联了pull request:test:The test case coverage for the torch.UntypedStorage.to API is incomplete; additional test cases need to be added.
WWo150
8月15日 关联了pull request:test:The test case coverage for the torch.UntypedStorage.to API is incomplete; additional test cases need to be added.
WWo150
8月15日 关联了pull request:docs:The test case coverage for the torch.UntypedStorage.to API is incomplete; additional test cases and docs need to be added.
WWo150
8月15日 修改了issue 的描述
WWo150
8月16日 修改了issue 的描述
WWo150
8月16日 修改了issue 的描述
TorchNPU-BotTorchNPU-Bot成员
8月17日 添加了label:bot-triaged
TorchNPU-Bot
TorchNPU-Bot成员
8月17日 评论:

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

likedislike
WWo150
8月17日 修改了issue 的描述
WWo150
8月18日 修改标题为 “【API一致性任务】torch.UntypedStorage.to NPU 测试适配方案”,原标题为“torch.UntypedStorage.to NPU 测试适配方案”
TorchNPU-BotTorchNPU-Bot成员
8月18日 添加了label:help-wantedevent: api-consistency
TorchNPU-Bot
TorchNPU-Bot成员
8月18日 评论:

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

likedislike
WWo150
16 天前 修改了issue 的描述
WWo150
16 天前 修改了issue 的描述
WWo150
11 天前 关联了pull request:docs:Complete the documentation for the three APIs: torch.UntypedStorage.to, torch.UntypedStorage.untyped, torch.distributed.tensor.DTensor.placements