Open
【API一致性任务】torch.ShortStorage 社区已有完整用例,现有 NPU 适配已覆盖 #3861
zhangjiahao-2026Add on Aug 9
Aug 9 associated Kanban: FrameworkPTAdapter 版本issue看板
Aug 9 Modify description of issue
Aug 10 Added label: event: api-consistencybot-triaged
TorchNPU-Bot
Aug 10 Comments:
Aug 10 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency


Aug 13 Change title to "【API一致性任务】torch.ShortStorage 社区已有用例且无需额外 NPU 适配", originally titled "torch.ShortStorage 社区已有用例且无需额外 NPU 适配".
Aug 13 Change title to "【API一致性任务】torch.ShortStorage 社区已有用例且无需额外 NPU 适配", originally titled "torch.ShortStorage 社区已有用例且无需额外 NPU 适配".
Aug 14 Modify description of issue
Aug 14 Change title to "【API一致性任务】torch.ShortStorage 社区已有完整用例,现有 NPU 适配已覆盖", originally titled "【API一致性任务】torch.ShortStorage 社区已有用例且无需额外 NPU 适配".
Aug 14 Change title to "【API一致性任务】torch.ShortStorage 社区已有完整用例,现有 NPU 适配已覆盖", originally titled "【API一致性任务】torch.ShortStorage 社区已有用例且无需额外 NPU 适配".
Aug 16 Modify description of issue
群青世界
Sep 7 Comments:
Sep 7 Comments:
缺少A5验证情况


Sep 7 Modify description of issue
Sep 7 Added label: help-wanted
TorchNPU-Bot
Sep 7 Comments:
Sep 7 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted


Sep 7 Modify description of issue
zhangjiahao-2026
Sep 7 Comments:
Sep 7 Comments:


群青世界
Sep 8 Comments:
Sep 8 Comments:
Ascend 950DT 标记暂不支持。
本次已补充 A5(Ascend950PR)实机验证并通过。资料仍维持现有支持范围,不新增或修改资料。
是否需要dosPR修改A5支持情况


Sep 8 Associated pull request
Sep 8 Modify description of issue
zhangjiahao-2026
Sep 9 Comments:
Sep 9 Comments:
Ascend 950DT 标记暂不支持。 本次已补充 A5(Ascend950PR)实机验证并通过。资料仍维持现有支持范围,不新增或修改资料。是否需要dosPR修改A5支持情况
需要修改资料。已根据 A5(Ascend950PR)实机验证结果提交 docs PR:#46018。
该 PR 在 master 分支下将 PyTorch 2.7.1、2.11.0、2.12.0 和 2.13.0 资料中 torch.ShortStorage 的 Ascend 950DT 支持状态由“不支持”更新为“支持”。Issue 中的资料检查和验证结论也已同步修改,请查阅。


Sep 30 Modify description of issue
API 一致性说明:
torch.ShortStorage社区已有完整用例,现有 NPU 适配已覆盖关联任务
torch.ShortStorage一、API 功能
torch.ShortStorage是 PyTorch Legacy TypedStorage 类型之一,用于保存16 位有符号整数数据,其元素类型对应
torch.int16。主要功能包括:
torch.ShortStorage之间进行类型转换;wrap_storage抛出异常。Legacy TypedStorage 已被 PyTorch 标记为 deprecated,但当前目标版本仍保留兼容支持。访问或创建
torch.ShortStorage时可能出现 TypedStorage deprecation warning,该告警属于 PyTorch 上游预期行为,不代表 API 执行失败。二、测试用例完备性
2.1 PyTorch 官方社区用例
PyTorch 官方仓库以下版本的
test/test_torch.py均包含torch.ShortStorage相关用例:torch.ShortStorage直接及异常验证位置v2.7.1test_storage_setitem:L217-L249;test_tensor_storage_type:L251-L260test_element_size:L7303-L7306;test_from_buffer:L7732-L7737;test_storage_error:L7766-L7864;test_storage_casts:L7881-L7921v2.11.0test_storage_setitem:L224-L257;test_tensor_storage_type:L270-L280test_element_size:L7095-L7098;test_from_buffer:L7527-L7532;test_storage_error:L7561-L7659;test_storage_casts:L7676-L7716v2.12.0test_storage_setitem:L177-L210;test_tensor_storage_type:L223-L233test_element_size:L7169-L7172;test_from_buffer:L7601-L7606;test_storage_error:L7635-L7733;test_storage_casts:L7750-L7790v2.13.0test_storage_setitem:L179-L212;test_tensor_storage_type:L225-L235test_element_size:L7189-L7192;test_from_buffer:L7621-L7626;test_storage_error:L7655-L7753;test_storage_casts:L7770-L7810maintest_storage_setitem:L179-L212;test_tensor_storage_type:L258-L268test_element_size:L7362-L7365;test_from_buffer:L7805-L7810;test_storage_error:L7839-L7937;test_storage_casts:L7928-L7968社区用例检索记录
在 PyTorch 官方仓库执行以下检索:
git grep -n "ShortStorage" v2.7.1 -- test/test_torch.py git grep -n "ShortStorage" v2.11.0 -- test/test_torch.py git grep -n "ShortStorage" v2.12.0 -- test/test_torch.py git grep -n "ShortStorage" v2.13.0 -- test/test_torch.py git grep -n "ShortStorage" main -- test/test_torch.py git grep -n -E "def test_(storage_setitem|tensor_storage_type|element_size|from_buffer|storage_error|storage_casts)" v2.7.1 -- test/test_torch.py git grep -n -E "def test_(storage_setitem|tensor_storage_type|element_size|from_buffer|storage_error|storage_casts)" v2.11.0 -- test/test_torch.py git grep -n -E "def test_(storage_setitem|tensor_storage_type|element_size|from_buffer|storage_error|storage_casts)" v2.12.0 -- test/test_torch.py git grep -n -E "def test_(storage_setitem|tensor_storage_type|element_size|from_buffer|storage_error|storage_casts)" v2.13.0 -- test/test_torch.py git grep -n -E "def test_(storage_setitem|tensor_storage_type|element_size|from_buffer|storage_error|storage_casts)" main -- test/test_torch.py检索结果表明,上述目标版本均存在 Storage 通用测试,并通过 Storage 类型映射或 Storage 类型集合覆盖
torch.ShortStorage;各用例的具体起止行见上表。行号采用带行号查看方式复核,不能仅以ShortStorage字面匹配位置代替用例范围。主要覆盖关系如下:
test_element_sizetest_storage_setitemtest_tensor_storage_typetest_from_buffertest_storage_castsdevice、dtype、未知关键字、多余位置参数、错误参数类型及错误wrap_storagetest_storage_error因此,PyTorch 官方用例已经覆盖
torch.ShortStorage的主要正常功能及异常入参场景,无需新增独立测试文件。2.2 Ascend for PyTorch 现有 NPU 用例
Ascend for PyTorch 的
test/test_torch.py已包含对应的 NPU 适配逻辑:storage_type_name = torch.storage._dtype_to_storage_type_map()[dtype] if torch.device(device).type == "npu": storage_type = eval("torch_npu.npu." + storage_type_name) else: storage_type = eval("torch." + storage_type_name)当
dtype=torch.int16、device=npu时,实际创建的 Storage 类型为:现有文件还通过以下代码将
TestTorchDeviceType实例化到privateuse1(NPU)设备:instantiate_device_type_tests( TestTorchDeviceType, globals(), only_for="privateuse1", )此外,
test_tensor_storage_type会验证 NPU int16 Tensor 对应的 Storage类型:
a = make_tensor((10,), dtype=dtype, device=device, low=-9, high=9) module = torch_npu.npu if torch.device(device).type == "npu" else torch expected_storage_type = getattr( module, torch.storage._dtype_to_storage_type_map()[dtype], ) self.assertEqual(a.storage_type(), expected_storage_type)因此,现有测试已经能够直接验证 NPU ShortStorage,无需新增测试方法。
三、API及测试用例适配
3.1 API适配
torch_npu.npu.ShortStorage已在 Ascend for PyTorch 中注册,并与torch.int16NPU Tensor 的 Storage 类型正确对应。本任务无需新增或修改 API 实现。3.2 测试用例适配
PyTorch 上游
test_storage_setitem原始代码只区分 CUDA 与 CPU:if torch.device(device).type == "cuda": storage_type = eval("torch.cuda." + storage_type_name) else: storage_type = eval("torch." + storage_type_name)当用例在
privateuse1(NPU)设备执行时,上述代码无法直接选择torch_npu.npu.ShortStorage,因此社区用例需要进行 NPU Storage 类型映射适配。Ascend for PyTorch 现有
test/test_torch.py已经完成该适配。本文中的“无需额外 NPU 适配”是指本任务无需继续修改代码,并非指 PyTorch 上游原始用例不需要适配。按照最新版验收规范,本任务不新增
test_upstreampatch,也无需提交测试代码 PR。3.3 现有
test_upstreampatch 检查已检查以下目标版本的
test_upstream/test/test_torch.py.patch:v2.7.1v2.11.0v2.12.0现有 patch 用于
test/test_torch.py中 CUDA、NPU/privateuse1 等通用路径的适配。其中包含 Storage 相关的 NPU 测试适配,但没有发现针对torch.ShortStorage的单独跳过、预期失败或掩盖 API 行为的修改。torch.ShortStorage对应的 NPU Storage 类型选择已经由 Ascend for PyTorch 的test/test_torch.py完成:storage_type_name = torch.storage._dtype_to_storage_type_map()[dtype] if torch.device(device).type == "npu": storage_type = eval("torch_npu.npu." + storage_type_name) else: storage_type = eval("torch." + storage_type_name)当
dtype=torch.int16时,该逻辑实际选择torch_npu.npu.ShortStorage。现有 patch 与当前测试实现不存在冲突,也没有需要修改的不合理内容。当前未检索到
v2.13.0对应的test_upstream/test/test_torch.py.patch。根据最新要求,不再接收新增的 NPU 适配 patch,因此本任务不新增或修改 patch。四、验证结果
4.1 测试命令
python3 -m pytest /data/pytorch/test/test_torch.py -v \ -k "test_storage_setitem_npu_int16 or test_tensor_storage_type_npu_int16 or test_element_size or test_from_buffer or test_storage_casts"4.2 测试日志
4.3 测试环境
环境信息获取命令:
python3 - <<'PY' import torch import torch_npu print("torch:", torch.__version__) print("torch_npu:", torch_npu.__version__) print("device:", torch.npu.get_device_name(0)) PY4.4
wrap_storage跨设备异常验证运行命令:
python3 - <<'PY' import pytest import torch import torch_npu cpu_storage = torch.empty( 1, dtype=torch.int16, ).untyped_storage() npu_storage = torch.empty( 1, dtype=torch.int16, device="npu", ).untyped_storage() with pytest.raises( RuntimeError, match="Device of 'wrap_storage' must be cpu", ): torch.ShortStorage(wrap_storage=npu_storage) with pytest.raises( RuntimeError, match="Device of 'wrap_storage' must be npu", ): torch_npu.npu.ShortStorage(wrap_storage=cpu_storage) print("2 passed") PYtorch.ShortStorage包装 NPU UntypedStorage 时,按预期抛出设备不匹配的RuntimeError;torch_npu.npu.ShortStorage包装 CPU UntypedStorage 时,按预期抛出设备不匹配的RuntimeError。运行过程中出现的 TypedStorage deprecation warning 属于 PyTorch 上游预期告警,不影响验证结论。
4.5 A5 硬件验证
验证环境:
验证场景:
element_sizefrom_buffertorch_npu.npu.ShortStoragetorch.ShortStorage(wrap_storage=npu_storage)抛出must be cputorch_npu.npu.ShortStorage(wrap_storage=cpu_storage)抛出must be npu运行结果:
验证结论:
torch.ShortStorage在 A5(Ascend950PR)上上述场景均通过。TypedStorage deprecation warning 属于 PyTorch 上游预期告警,不影响结论。五、资料检查与补齐
按照最新资料规范,已检查并更新 master 分支下以下版本的资料:
docs/zh/api/native_api/pytorch_2-7-1/torch-Storage.mddocs/zh/api/native_api/pytorch_2-9-0/torch-Storage.mddocs/zh/api/native_api/pytorch_2-10-0/torch-Storage.mddocs/zh/api/native_api/pytorch_2-11-0/torch-Storage.mddocs/zh/api/native_api/pytorch_2-12-0/torch-Storage.mddocs/zh/api/native_api/pytorch_2-13-0/torch-Storage.mddocs/zh/api/native_api/pytorch_2-14-0/torch-Storage.md上述资料均已包含
torch.ShortStorage条目,但 Ascend 950DT 系列产品的支持状态原标记为“不支持”。经 A5(Ascend950PR)实机验证,
torch.ShortStorage的正常构造、Storage 操作、类型转换、NPU int16 Tensor Storage 类型映射及wrap_storage跨设备异常场景均验证通过。Ascend950PR 与 Ascend 950DT 均属于 A5 产品系列,因此将上述七个版本中torch.ShortStorage的 Ascend 950DT 系列产品支持状态由“不支持”修改为“支持”。torch.ShortStorage属于非计算类 API,资料中的数据类型支持情况保持填写-,不填写“支持 fp32”等数据类型信息。资料补齐 PR:
六、验证结论
torch.ShortStorage相关社区用例;wrap_storage跨设备异常场景已在 A2 及 A5(Ascend950PR)实机验证通过;因此,本任务:
test_upstreampatch;