Open
【API一致性任务】补齐 torch.IntStorage NPU 用例 #3594
沐染。Add on  Jul 31
沐染。
沐染。
Jul 31 Create

API 信息

  • API:torch.IntStorage
  • 分类:PyTorch 官方社区已有相关用例,但构造方式覆盖不完整
  • 验收路径:1.2②

API 功能

torch.IntStorage 用于表示元素数据类型为 torch.int32 的类型化 Storage。

本次重点验证 torch.IntStorage 构造行为在 CPU 和 NPU 上的一致性,包括:

  • 无参构造
  • size 构造
  • Sequence 构造
  • wrap_storage 构造
  • 边界场景
  • 非法参数
  • Storage 元素数量
  • Storage 数据内容
  • Storage 类型
  • Storage dtype
  • Storage 所属设备

PyTorch 社区用例情况

PyTorch 官方社区已在以下用例中直接验证 torch.IntStorage:

test/test_torch.py::TestTorch::test_storage_casts

各版本官方源码中的相关位置为:

  • v2.7.1:test/test_torch.py 第 7882~7886 行
  • v2.11.0:test/test_torch.py 第 7677~7681 行
  • v2.12.0:test/test_torch.py 第 7751~7755 行

社区用例主要验证:

storage = torch.IntStorage([-1, 0, 1, 2, 3, 4])
self.assertEqual(storage.size(), 6)
self.assertEqual(storage.tolist(), [-1, 0, 1, 2, 3, 4])
self.assertEqual(storage.type(), "torch.IntStorage")
self.assertIs(storage.dtype, torch.int32)

该用例直接覆盖了 Sequence 构造以及:

  • Storage 元素数量
  • Storage 数据内容
  • Storage 类型
  • Storage dtype

此外,PyTorch 官方测试中还存在:

torch.IntStorage().element_size()
torch.IntStorage(10).fill_("asdf")

但上述两处分别用于验证 element_size() 和 fill_(),并未完整验证 torch.IntStorage 构造接口本身。

PyTorch v2.7.1 社区用例截图

image.png

PyTorch v2.11.0 社区用例截图

image.png

PyTorch v2.12.0 社区用例截图

image.png

PyTorch 社区构造引用截图

image.png

社区覆盖完整性分析

PyTorch 官方社区虽然已经直接覆盖 torch.IntStorage,但构造 API 的整体覆盖仍不完整。

现有社区用例主要完整验证:

torch.IntStorage([-1, 0, 1, 2, 3, 4])

但缺少对以下构造形式的系统验证:

torch.IntStorage()
torch.IntStorage(size)
torch.IntStorage(wrap_storage=...)

同时缺少以下边界场景:

torch.IntStorage(0)
torch.IntStorage([])

以及以下非法参数组合:

torch.IntStorage(1, 2)
torch.IntStorage(object())
torch.IntStorage(dtype=torch.int32)
torch.IntStorage(device="cpu")
torch.IntStorage(wrap_storage=object())

torch.IntStorage 属于 Storage API,构造行为包含设备语义。

CPU 与 NPU 下需要分别验证:

  • Storage size
  • Storage values
  • Storage type
  • Storage dtype
  • Storage device

因此,本任务按照当前验收要求归类为:

1.2②
PyTorch 官方已有测试,但覆盖不完整
+
API 涉及设备语义
+
在 torch-npu test 目录补充测试

测试适配方案

本次不修改:

test_upstream/test/test_torch.py.patch

测试直接新增在:

test/npu/test_storage.py

新增测试:

def test_int_storage_constructor(self):

CPU 正常场景

覆盖:

torch.IntStorage()
torch.IntStorage(4)
torch.IntStorage(values)
torch.IntStorage(wrap_storage=...)

验证:

  • Storage size
  • Storage values
  • Storage dtype
  • Storage type
  • Storage device

CPU 边界场景

覆盖:

torch.IntStorage(0)
torch.IntStorage([])

预期:

size = 0
values = []
dtype = torch.int32
device = cpu
type = torch.IntStorage

NPU 正常场景

使用真实 NPU Tensor:

npu_tensor = torch.tensor(
    values,
    dtype=torch.int32,
    device="npu",
)

通过:

npu_untyped = npu_tensor.untyped_storage()

获取真实 NPU UntypedStorage。

覆盖:

torch_npu.npu.IntStorage(wrap_storage=npu_untyped)
torch_npu.npu.IntStorage(4)
torch_npu.npu.IntStorage(values)

验证:

Storage size
Storage values
Storage dtype
Storage type
Storage device

预期:

type = torch_npu.npu.IntStorage
dtype = torch.int32
device = npu

NPU 边界场景

覆盖:

torch_npu.npu.IntStorage([])

预期:

size = 0
values = []
dtype = torch.int32
type = torch_npu.npu.IntStorage
device = npu

非法参数场景

覆盖:

torch.IntStorage(1, 2)
torch.IntStorage(object())
torch.IntStorage(dtype=torch.int32)
torch.IntStorage(device="cpu")
torch.IntStorage(wrap_storage=object())

验证对应异常类型。

用例完整性分析

本次新增测试覆盖:

正常构造

无参构造
size 构造
Sequence 构造
wrap_storage 构造

边界场景

size = 0
空 Sequence
NPU 空 Sequence

非法参数

多个位置参数
非法对象类型
显式 dtype
显式 device
非法 wrap_storage

属性验证

Storage size
Storage values
Storage type
Storage dtype
Storage device

相比 PyTorch 社区现有测试,本次补充了构造接口完整性、边界输入、异常参数以及 NPU 设备语义验证。

边界行为分析

独立验证中:

torch.IntStorage(0)

返回:

type = torch.IntStorage
dtype = torch.int32
device = cpu
size = 0
values = []
torch.IntStorage([])

返回:

type = torch.IntStorage
dtype = torch.int32
device = cpu
size = 0
values = []

NPU 空 Sequence:

torch_npu.npu.IntStorage([])

返回:

type = torch_npu.npu.IntStorage
dtype = torch.int32
device = npu:0
size = 0
values = []

另外观察到:

torch_npu.npu.IntStorage(0)

当前实际返回:

type = torch.IntStorage
dtype = torch.int32
device = cpu
size = 0
values = []

而:

torch_npu.npu.IntStorage([])

返回 NPU Storage。

两种空 Storage 构造路径的设备行为存在差异。

因此,本次测试未将:

torch_npu.npu.IntStorage(0)

当前返回 CPU Storage 的行为固化为正确预期,仅在本 Issue 中记录该边界现象。

边界验证截图

image.png

NPU 支持性验证

已在 NPU 环境中执行与新增测试一致的独立验证。

验证范围包括:

CPU constructors
NPU constructors
boundary cases
invalid arguments

运行结果:

===== CPU IntStorage =====
CPU cases: PASS

===== NPU IntStorage =====
NPU cases: PASS

===== Invalid arguments =====
invalid cases: PASS

ALL INTSTORAGE CHECKS PASSED

运行过程中存在 PyTorch TypedStorage deprecation warning,该 warning 为 PyTorch TypedStorage 的统一弃用提示,不影响本次 API 功能验证结果。

NPU 独立功能验证截图

image.png

API 实现情况

本次验证范围内:

torch.IntStorage
torch_npu.npu.IntStorage

主要构造行为已经能够正常工作。

因此,本任务当前无需新增 API 实现代码。

对于:

torch_npu.npu.IntStorage(0)

返回 CPU Storage 的边界行为,本 Issue 中作为现象记录,不在本次测试中将其定义为正确预期。

测试修改情况

按照当前验收规则,本次测试直接新增至:

test/npu/test_storage.py

不修改:

test_upstream/test/test_torch.py.patch

新增:

TestStorage.test_int_storage_constructor

测试约新增 85 行,覆盖 CPU、NPU、边界和非法参数场景。

测试版本范围

本次测试分别处理:

  • v2.7.1
  • v2.11.0
  • v2.12.0
  • master

v2.9.0 和 v2.10.0 当前属于维护版本,不回填新增测试。

后续新增 v2.13.0 对应代码分支时,同步补充该测试。

Commit 信息

v2.7.1

5e421e483 test: add coverage for torch.IntStorage

v2.11.0

5d00763e3b test: add coverage for torch.IntStorage

v2.12.0

4d736b147 test: add coverage for torch.IntStorage

master

316c3ef64 test: add coverage for torch.IntStorage

各分支均保持单 commit。

提交身份:

Author: 沐染。 <1628038450@qq.com>
Signed-off-by: 沐染。 <1628038450@qq.com>

资料检查

资料统一在 master 分支以下目录检查:

docs/zh/api/native_api

检查范围:

  • PyTorch 2.7.1
  • PyTorch 2.11.0
  • PyTorch 2.12.0
  • PyTorch 2.13.0

当前资料中均已包含:

torch.IntStorage
torch.IntStorage.dtype

支持状态:

PyTorch版本 资料目录 torch.IntStorage torch.IntStorage.dtype Atlas A2 Atlas A3 Ascend 950DT
2.7.1 pytorch_2-7-1 已包含 已包含 ✔ ✔ ✘
2.11.0 pytorch_2-11-0 已包含 已包含 ✔ ✔ ✘
2.12.0 pytorch_2-12-0 已包含 已包含 ✔ ✔ ✘
2.13.0 pytorch_2-13-0 已包含 已包含 ✔ ✔ ✘

同时检查:

docs/zh/api/native_api/pytorch_2-9-0
docs/zh/api/native_api/pytorch_2-10-0

当前 master 中上述目录均已删除,因此不属于当前资料刷新范围。

torch.IntStorage 属于非计算类 API。

如果资料存在数据类型字段,应填写:

-

不应填写:

支持 fp32

经检查,当前资料已覆盖 torch.IntStorage 和 torch.IntStorage.dtype,且支持状态正确,因此本任务无需新增或修改资料。

资料检查

资料统一在 master 分支以下目录检查:

docs/zh/api/native_api

本次实际检查范围:

  • PyTorch 2.7.1
  • PyTorch 2.11.0
  • PyTorch 2.12.0
  • PyTorch 2.13.0

经实际检查,上述版本资料中均已包含:

torch.IntStorage
torch.IntStorage.dtype

同时检查:

docs/zh/api/native_api/pytorch_2-9-0
docs/zh/api/native_api/pytorch_2-10-0

当前 master 分支中上述两个资料目录均不存在,因此不属于当前资料刷新范围。

产品支持情况

image.png
image.png
image.png
image.png
截图中现有资料的产品支持情况为:

产品支持情况:

• Atlas A2 训练系列产品:支持
• Atlas A3 训练系列产品:支持
• Ascend 950PR:支持
• Ascend 950DT:不支持

该截图反映的是当前资料中已登记的产品支持状态。

PR 情况

本任务测试分别提交至:

PR 标题统一为:

test: add coverage for torch.IntStorage

测试文件:

test/npu/test_storage.py

不使用 /sync。

结论

PyTorch 官方社区已经直接覆盖:

torch.IntStorage([-1, 0, 1, 2, 3, 4])

并验证:

size
values
type
dtype

但 torch.IntStorage 构造 API 的整体覆盖仍不完整,缺少:

无参构造完整验证
size 构造完整验证
wrap_storage
边界场景
非法参数
NPU Storage 行为

由于 Storage 构造涉及设备语义,本任务按照当前验收要求在:

test/npu/test_storage.py

新增:

test_int_storage_constructor

补充 CPU/NPU 构造完整性测试。

独立验证结果:

CPU cases: PASS
NPU cases: PASS
invalid cases: PASS

ALL INTSTORAGE CHECKS PASSED

因此,本任务结论为:

PyTorch 社区已有相关测试,但构造覆盖不完整
+
torch.IntStorage 涉及设备语义
+
需要在 torch-npu test/npu 下补充测试
+
当前主要 API 行为无需新增实现代码

同时观察到:

torch_npu.npu.IntStorage(0)

当前返回 CPU torch.IntStorage 的边界行为,与 NPU 空 Sequence 构造行为存在差异,本 Issue 中予以记录,但本次测试不将该行为固化为正确预期。

likedislike
沐染。沐染。
Jul 31 Associated pull request
TorchNPU-BotTorchNPU-BotCollaborators
Jul 31 Added label: bot-triaged
TorchNPU-Bot
TorchNPU-BotCollaborators
Jul 31 Comments:

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

likedislike
沐染。沐染。
Jul 31 Associated pull request
沐染。沐染。
Jul 31 Modify description of issue
沐染。沐染。
Jul 31 Associated pull request
沐染。沐染。
Aug 16 Change title to "[API一致性任务] 补齐 torch.IntStorage NPU 用例", originally titled "[API Consistency] 补齐 torch.IntStorage NPU 用例".
沐染。沐染。
Aug 16 associated Kanban: FrameworkPTAdapter 版本issue看板
沐染。沐染。
Aug 17 Associated pull request
沐染。沐染。
Aug 17 Associated pull request
沐染。沐染。
Aug 17 Associated pull request
沐染。沐染。
Aug 17 Associated pull request
沐染。沐染。
Aug 17 Modify description of issue
TorchNPU-BotTorchNPU-BotCollaborators
Aug 17 Added label: help-wantedevent: api-consistency
TorchNPU-Bot
TorchNPU-BotCollaborators
Aug 17 Comments:

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

likedislike
群青世界群青世界Collaborators
Aug 18 Change title to "【API一致性任务】补齐 torch.IntStorage NPU 用例", originally titled "[API一致性任务] 补齐 torch.IntStorage NPU 用例".
沐染。沐染。
Sep 4 Modify description of issue
沐染。沐染。
Sep 4 Modify description of issue
沐染。沐染。
Sep 4 Modify description of issue