Open
【API一致性任务】torch.ShortStorage 社区已有完整用例,现有 NPU 适配已覆盖 #3861
zhangjiahao-2026Add on  Aug 9
zhangjiahao-2026
zhangjiahao-2026
Aug 9 Create

API 一致性说明:torch.ShortStorage 社区已有完整用例,现有 NPU 适配已覆盖

关联任务

  • API:torch.ShortStorage
  • 关联任务:#3557
  • 资料 PR:#46018

一、API 功能

torch.ShortStorage 是 PyTorch Legacy TypedStorage 类型之一,用于保存
16 位有符号整数数据,其元素类型对应 torch.int16。

主要功能包括:

  1. 无参数创建空 Storage;
  2. 根据元素数量或数据序列创建 Storage;
  3. 对 Storage 元素及切片进行读写;
  4. 从 buffer 创建 Storage;
  5. 在其他 TypedStorage 与 torch.ShortStorage 之间进行类型转换;
  6. 获取 Storage 长度、内容及元素大小;
  7. 对非法构造参数和设备不匹配的 wrap_storage 抛出异常。

Legacy TypedStorage 已被 PyTorch 标记为 deprecated,但当前目标版本仍保留兼容支持。访问或创建 torch.ShortStorage 时可能出现 TypedStorage deprecation warning,该告警属于 PyTorch 上游预期行为,不代表 API 执行失败。

二、测试用例完备性

2.1 PyTorch 官方社区用例

PyTorch 官方仓库以下版本的 test/test_torch.py 均包含 torch.ShortStorage 相关用例:

PyTorch 版本 NPU 参数化用例位置 torch.ShortStorage 直接及异常验证位置
v2.7.1 test_storage_setitem:L217-L249;test_tensor_storage_type:L251-L260 test_element_size:L7303-L7306;test_from_buffer:L7732-L7737;test_storage_error:L7766-L7864;test_storage_casts:L7881-L7921
v2.11.0 test_storage_setitem:L224-L257;test_tensor_storage_type:L270-L280 test_element_size:L7095-L7098;test_from_buffer:L7527-L7532;test_storage_error:L7561-L7659;test_storage_casts:L7676-L7716
v2.12.0 test_storage_setitem:L177-L210;test_tensor_storage_type:L223-L233 test_element_size:L7169-L7172;test_from_buffer:L7601-L7606;test_storage_error:L7635-L7733;test_storage_casts:L7750-L7790
v2.13.0 test_storage_setitem:L179-L212;test_tensor_storage_type:L225-L235 test_element_size:L7189-L7192;test_from_buffer:L7621-L7626;test_storage_error:L7655-L7753;test_storage_casts:L7770-L7810
main test_storage_setitem:L179-L212;test_tensor_storage_type:L258-L268 test_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_size
元素数量、数据序列、元素及切片赋值 test_storage_setitem
NPU int16 Tensor 与 NPU Storage 类型映射 test_tensor_storage_type
从 buffer 创建 test_from_buffer
Storage 类型转换 test_storage_casts
非法 device、dtype、未知关键字、多余位置参数、错误参数类型及错误 wrap_storage test_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 类型为:

torch_npu.npu.ShortStorage

现有文件还通过以下代码将 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.int16 NPU 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_upstream patch,也无需提交测试代码 PR。

3.3 现有 test_upstream patch 检查

已检查以下目标版本的 test_upstream/test/test_torch.py.patch:

  • v2.7.1
  • v2.11.0
  • v2.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 测试日志

QQ20260814-200034.png

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))
PY

QQ20260816-104516.png

4.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")
PY

QQ20260816-104851.png

  1. torch.ShortStorage 包装 NPU UntypedStorage 时,按预期抛出设备不匹配的 RuntimeError;
  2. torch_npu.npu.ShortStorage 包装 CPU UntypedStorage 时,按预期抛出设备不匹配的 RuntimeError。

运行过程中出现的 TypedStorage deprecation warning 属于 PyTorch 上游预期告警,不影响验证结论。

4.5 A5 硬件验证

验证环境:

python: 3.12.13
torch: 2.12.0+cpu
torch_npu: 2.12.0
npu_available: True
device: Ascend950PR_9579

验证场景:

  1. 无参数创建及 element_size
  2. 按元素数量、数据序列创建
  3. CPU / NPU 元素及切片赋值
  4. from_buffer
  5. Storage 类型转换
  6. NPU int16 Tensor 映射为 torch_npu.npu.ShortStorage
  7. torch.ShortStorage(wrap_storage=npu_storage) 抛出 must be cpu
  8. torch_npu.npu.ShortStorage(wrap_storage=cpu_storage) 抛出 must be npu

运行结果:

QQ20260907-180358.png

验证结论:torch.ShortStorage 在 A5(Ascend950PR)上上述场景均通过。TypedStorage deprecation warning 属于 PyTorch 上游预期告警,不影响结论。

五、资料检查与补齐

按照最新资料规范,已检查并更新 master 分支下以下版本的资料:

  • docs/zh/api/native_api/pytorch_2-7-1/torch-Storage.md
  • docs/zh/api/native_api/pytorch_2-9-0/torch-Storage.md
  • docs/zh/api/native_api/pytorch_2-10-0/torch-Storage.md
  • docs/zh/api/native_api/pytorch_2-11-0/torch-Storage.md
  • docs/zh/api/native_api/pytorch_2-12-0/torch-Storage.md
  • docs/zh/api/native_api/pytorch_2-13-0/torch-Storage.md
  • docs/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:

六、验证结论

  1. PyTorch 官方各目标版本均存在 torch.ShortStorage 相关社区用例;
  2. 社区用例覆盖正常构造、Storage 操作、类型转换及异常入参场景;
  3. PyTorch 上游用例需要进行 NPU Storage 类型映射适配;
  4. Ascend for PyTorch 现有代码已经完成所需的 NPU 适配;
  5. 正常功能、NPU int16 路径及 wrap_storage 跨设备异常场景已在 A2 及 A5(Ascend950PR)实机验证通过;
  6. API 实现和测试适配无需修改;资料中的 Ascend 950DT 支持状态需要更新,已提交 PR #46018。

因此,本任务:

  • 无需新增测试用例;
  • 无需新增或修改 test_upstream patch;
  • 无需修改 API 实现;
  • 需要修改 API 资料,已提交 PR #46018;
likedislike
zhangjiahao-2026zhangjiahao-2026
Aug 9 associated Kanban: FrameworkPTAdapter 版本issue看板
zhangjiahao-2026zhangjiahao-2026
Aug 9 Modify description of issue
TorchNPU-BotTorchNPU-BotCollaborators
Aug 10 Added label: event: api-consistencybot-triaged
TorchNPU-Bot
TorchNPU-BotCollaborators
Aug 10 Comments:

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

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

缺少A5验证情况

likedislike
zhangjiahao-2026zhangjiahao-2026
Sep 7 Modify description of issue
TorchNPU-BotTorchNPU-BotCollaborators
Sep 7 Added label: help-wanted
TorchNPU-Bot
TorchNPU-BotCollaborators
Sep 7 Comments:

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

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

缺少A5验证情况

@wjq1027895128

已在 4.5 节补充 A5(Ascend950PR)环境的验证情况及运行结果,相关场景均验证通过,请查阅。

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

是否需要dosPR修改A5支持情况

likedislike
zhangjiahao-2026zhangjiahao-2026
Sep 8 Associated pull request
zhangjiahao-2026zhangjiahao-2026
Sep 8 Modify description of issue
zhangjiahao-2026
zhangjiahao-2026
Sep 9 Comments:
Ascend 950DT 标记暂不支持。
本次已补充 A5(Ascend950PR)实机验证并通过。资料仍维持现有支持范围,不新增或修改资料。

是否需要dosPR修改A5支持情况

@wjq1027895128

需要修改资料。已根据 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 中的资料检查和验证结论也已同步修改,请查阅。

likedislike
zhangjiahao-2026zhangjiahao-2026
Sep 30 Modify description of issue