Co-authored-by: Atomgitcodee<986200182@qq.com> # message auto-generated for no-merge-commit merge: !43728 merge add-quint8storage-test-master into master test: add test cases for torch.QUInt8Storage on NPU Created-by: m0_50316716 Commit-by: Atomgitcodee Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 关联issue:https://gitcode.com/Ascend/pytorch/issues/3537 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 torch.QUInt8Storage 是 PyTorch 的遗留量化 Storage 类(存储 quint8 数据), 属于 _LegacyStorage 子类,由 PyTorch 原生提供。 PyTorch 官方社区在 test/test_torch.py 的 test_storage_error 中遍历全局注册表 torch._storage_classes 进行 storage 错误路径测试。该注册表在 NPU 环境下混入 torch.cuda.* 与 torch_npu.npu.* 等设备相关 storage 类,无法直接用于 QUInt8Storage 的针对性验证;且 test_torch.py 为大型社区文件,不宜在其上叠加改动。因此新增独立 测试文件 test/test_quantized_storage.py。 QUInt8Storage 为 Storage 类,构造、dtype、element_size 等均为 CPU 侧设备无关行为, 不涉及 NPU 适配,无需代码修改。新增测试覆盖: - 空构造与 dtype、element_size - 非空容量构造(size=0/1/16,等价类+边界值) - 数据序列构造及内容校验(tolist) - wrap_storage 合法构造(可选参数传入与不传入对照) - 构造错误参数(device/dtype/非法关键字/多余位置参数/非法类型) - wrap_storage 错误路径 # 【资料变更】 涉及。上游 PyTorch 在 test/test_torch.py 的 test_storage_error 中有相关测试,但作为 Storage 类通用测试的一部分,未针对 QUInt8Storage 编写独立测试。测试文件位于 test/test_quantized_storage.py。 文档方面,修正了 docs/zh/api/native_api/pytorch_2-7-1、pytorch_2-11-0、pytorch_2-12-0、pytorch_2-13-0 目录下 torch-Storage.md 中 torch.QUInt8Storage 的支持状态(否→是)。 # 【接口变更】 不涉及 # 【功能验证】 社区用例位置(原 torch 文件 test/test_torch.py 的 test_storage_error): - v2.7.1:L7766~L7865 - v2.11.0:L7561~L7660 - v2.12.0:L7635~L7734 - master:L7813~L7912 v2.7.1 既有 test_upstream/test/test_torch.py.patch 未包含 test_storage_error 段落 (该段的 NPU 适配未沉淀在 patch 中),本次不改动既有 patch。 测试命令: master:python -m pytest test/test_quantized_storage.py -v ``` ========================== test session starts =========================== platform linux -- Python 3.12.13, pytest-8.3.2, pluggy-1.6.0 -- /workspace/envs/torch-npu-test/bin/python cachedir: .pytest_cache rootdir: /workspace/user_data/pytorch-master configfile: pyproject.toml plugins: xdist-3.6.1 collected 6 items ../workspace/user_data/pytorch-master/test/test_quantized_storage.py::TestQUInt8Storage::test_construct_empty PASSED [ 16%] ../workspace/user_data/pytorch-master/test/test_quantized_storage.py::TestQUInt8Storage::test_construct_with_data PASSED [ 33%] ../workspace/user_data/pytorch-master/test/test_quantized_storage.py::TestQUInt8Storage::test_construct_with_size PASSED [ 50%] ../workspace/user_data/pytorch-master/test/test_quantized_storage.py::TestQUInt8Storage::test_construct_with_wrap_storage PASSED [ 66%] ../workspace/user_data/pytorch-master/test/test_quantized_storage.py::TestQUInt8Storage::test_constructor_errors PASSED [ 83%] ../workspace/user_data/pytorch-master/test/test_quantized_storage.py::TestQUInt8Storage::test_wrap_storage_errors PASSED [100%] ============================ warnings summary ============================ test/test_quantized_storage.py::TestQUInt8Storage::test_construct_empty /workspace/user_data/pytorch-master/test/test_quantized_storage.py:31: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly. To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage() storage = torch.QUInt8Storage() -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ====================== 6 passed, 1 warning in 5.73s ====================== ``` # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!43728
80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
# Copyright (c) 2026 Huawei Technologies Co., Ltd
|
|
# All rights reserved.
|
|
#
|
|
# Licensed under the BSD 3-Clause License (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""
|
|
Add validation cases for torch.QUInt8Storage on NPU:
|
|
1. PyTorch community test_storage_error iterates torch._storage_classes and mixes
|
|
CPU, CUDA and NPU storage classes, which makes it unsuitable for focused
|
|
QUInt8Storage validation on NPU.
|
|
2. This file validates torch.QUInt8Storage construction (empty, size, data,
|
|
wrap_storage), dtype, element_size, and error handling.
|
|
3. Storage construction is device-independent, so tensors are created on CPU
|
|
and only used as invalid constructor arguments.
|
|
"""
|
|
import torch
|
|
from torch.testing._internal.common_utils import TestCase, run_tests
|
|
|
|
|
|
class TestQUInt8Storage(TestCase):
|
|
def test_construct_empty(self):
|
|
storage = torch.QUInt8Storage()
|
|
self.assertEqual(storage.size(), 0)
|
|
self.assertEqual(storage.dtype, torch.QUInt8Storage.dtype)
|
|
self.assertEqual(storage.element_size(), 1)
|
|
|
|
def test_construct_with_size(self):
|
|
for size in (0, 1, 16):
|
|
storage = torch.QUInt8Storage(size)
|
|
self.assertEqual(storage.size(), size)
|
|
self.assertEqual(storage.dtype, torch.QUInt8Storage.dtype)
|
|
self.assertEqual(storage.nbytes(), size)
|
|
|
|
def test_construct_with_data(self):
|
|
data = [0, 1, 2, 255]
|
|
storage = torch.QUInt8Storage(data)
|
|
self.assertEqual(storage.size(), len(data))
|
|
self.assertEqual(storage.tolist(), data)
|
|
|
|
def test_construct_with_wrap_storage(self):
|
|
storage = torch.QUInt8Storage(4)
|
|
wrapped = torch.QUInt8Storage(wrap_storage=storage.untyped())
|
|
self.assertEqual(wrapped.size(), storage.size())
|
|
self.assertEqual(wrapped.dtype, storage.dtype)
|
|
self.assertEqual(wrapped.tolist(), storage.tolist())
|
|
|
|
def test_constructor_errors(self):
|
|
with self.assertRaises(RuntimeError):
|
|
torch.QUInt8Storage(device="cpu")
|
|
with self.assertRaises(RuntimeError):
|
|
torch.QUInt8Storage(dtype=torch.float)
|
|
with self.assertRaises(TypeError):
|
|
torch.QUInt8Storage(invalid_argument=torch.float)
|
|
with self.assertRaises(RuntimeError):
|
|
torch.QUInt8Storage(0, 0)
|
|
with self.assertRaises(TypeError):
|
|
torch.QUInt8Storage("string")
|
|
with self.assertRaises(TypeError):
|
|
torch.QUInt8Storage(torch.tensor([]))
|
|
|
|
def test_wrap_storage_errors(self):
|
|
storage = torch.QUInt8Storage()
|
|
with self.assertRaises(RuntimeError):
|
|
torch.QUInt8Storage(0, wrap_storage=storage.untyped())
|
|
with self.assertRaises(TypeError):
|
|
torch.QUInt8Storage(wrap_storage=storage)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|