已合并
【API一致性任务】test: Add consistency validation cases for torch.QUInt4x2Storage.dtype on NPU (#3536) #44413
luoxiaoyan2024创建于 8月12日
【API一致性任务】test: Add consistency validation cases for torch.QUInt4x2Storage.dtype on NPU (#3536) #44413
已合并
共 1 个文件变更+96-0
| @@ -0,0 +1,96 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# All rights reserved. | ||
| 3 | +# | ||
| 4 | +# Licensed under the BSD 3-Clause License (the "License"); | ||
| 5 | +# you may not use this file except in compliance with the License. | ||
| 6 | +# You may obtain a copy of the License at | ||
| 7 | +# | ||
| 8 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 9 | +# | ||
| 10 | +# Unless required by applicable law or agreed to in writing, software | ||
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | +# See the License for the specific language governing permissions and | ||
| 14 | +# limitations under the License. | ||
| 15 | + | ||
| 16 | +""" | ||
| 17 | +Add consistency validation cases for torch.QUInt4x2Storage.dtype on NPU (#3536). | ||
| 18 | + | ||
| 19 | +``torch.QUInt4x2Storage`` is the storage class backing the ``torch.quint4x2`` | ||
| 20 | +quantized dtype. Its ``dtype`` attribute must report ``torch.quint4x2`` | ||
| 21 | +consistently. This file validates the dtype value both on CPU and in an | ||
| 22 | +NPU-enabled environment, and asserts that the reported dtype is the expected | ||
| 23 | +quantized dtype (not just a truthy value). | ||
| 24 | + | ||
| 25 | +Note: NPU does not currently register a dedicated ``torch.npu.QUInt4x2Storage`` | ||
| 26 | +storage class (the curated NPU storage list only covers the numeric/bool/ | ||
| 27 | +bfloat16 storages), so the device-independent ``torch.QUInt4x2Storage.dtype`` | ||
| 28 | +class attribute is the canonical value validated here; should NPU later expose | ||
| 29 | +a ``torch.npu.QUInt4x2Storage`` class, the NPU test additionally checks it. | ||
| 30 | + | ||
| 31 | +This file can be extended with more storage-dtype consistency cases for other | ||
| 32 | +quantized storage classes (e.g. QUInt8Storage, QInt8Storage) following the same | ||
| 33 | +pattern. | ||
| 34 | +""" | ||
| 35 | + | ||
| 36 | +import torch | ||
| 37 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 38 | +import torch_npu # noqa: F401 # imported for side effects (loads the NPU backend) | ||
| 39 | + | ||
| 40 | + | ||
| 41 | +class TestQUInt4x2StorageDtype(TestCase): | ||
| 42 | + | ||
| 43 | + # ---------------- CPU: torch.QUInt4x2Storage.dtype ---------------- | ||
| 44 | + def test_cpu_quint4x2_storage_dtype(self): | ||
| 45 | + # The QUInt4x2Storage class must be provided by this PyTorch build. | ||
| 46 | + if not hasattr(torch, "QUInt4x2Storage"): | ||
| 47 | + self.skipTest( | ||
| 48 | + "torch.QUInt4x2Storage is not available in this PyTorch build" | ||
| 49 | + ) | ||
| 50 | + # The QUInt4x2Storage class must report the correct quantized dtype. | ||
| 51 | + self.assertIs(torch.QUInt4x2Storage.dtype, torch.quint4x2) | ||
| 52 | + # quint4x2 is a quantized dtype. NOTE: torch.dtype objects do not expose | ||
| 53 | + # an ``is_quantized`` attribute (that lives on torch.Tensor), so we | ||
| 54 | + # verify membership in the known quantized-dtype set instead. | ||
| 55 | + quantized_dtypes = ( | ||
| 56 | + torch.quint8, torch.qint8, torch.qint32, torch.quint4x2, | ||
| 57 | + ) | ||
| 58 | + self.assertIn(torch.QUInt4x2Storage.dtype, quantized_dtypes) | ||
| 59 | + # The reported dtype must be specifically quint4x2, not another | ||
| 60 | + # quantized dtype (boundary: distinguish from quint8 / qint8). | ||
| 61 | + self.assertIsNot(torch.QUInt4x2Storage.dtype, torch.quint8) | ||
| 62 | + self.assertIsNot(torch.QUInt4x2Storage.dtype, torch.qint8) | ||
| 63 | + # The instance dtype must be consistent with the class attribute dtype | ||
| 64 | + # (the canonical value validated above). | ||
| 65 | + inst = torch.QUInt4x2Storage() | ||
| 66 | + self.assertIs(inst.dtype, torch.QUInt4x2Storage.dtype) | ||
| 67 | + self.assertIs(inst.dtype, torch.quint4x2) | ||
| 68 | + | ||
| 69 | + # ---------------- NPU: consistency in an NPU-enabled environment ---------------- | ||
| 70 | + def test_npu_quint4x2_storage_dtype(self): | ||
| 71 | + if not hasattr(torch, "npu") or not torch.npu.is_available(): | ||
| 72 | + self.skipTest("NPU not available") | ||
| 73 | + # QUInt4x2Storage must be provided by this PyTorch build even on NPU; | ||
| 74 | + # guard it so a build without quantized storages skips gracefully | ||
| 75 | + # instead of raising AttributeError (consistent with the CPU test). | ||
| 76 | + if not hasattr(torch, "QUInt4x2Storage"): | ||
| 77 | + self.skipTest( | ||
| 78 | + "torch.QUInt4x2Storage is not available in this PyTorch build" | ||
| 79 | + ) | ||
| 80 | + | ||
| 81 | + # The dtype attribute is device-independent: it must remain | ||
| 82 | + # torch.quint4x2 even in an NPU-enabled environment. | ||
| 83 | + self.assertIs(torch.QUInt4x2Storage.dtype, torch.quint4x2) | ||
| 84 | + | ||
| 85 | + # If NPU later registers a dedicated quint4x2 storage class, verify it | ||
| 86 | + # reports the same dtype. Guarded so the test stays valid whether or not | ||
| 87 | + # the NPU storage class is currently exposed. | ||
| 88 | + if hasattr(torch.npu, "QUInt4x2Storage"): | ||
| 89 | + self.assertIs(torch.npu.QUInt4x2Storage.dtype, torch.quint4x2) | ||
| 90 | + ns = torch.npu.QUInt4x2Storage() | ||
| 91 | + self.assertIs(ns.dtype, torch.quint4x2) | ||
| 92 | + self.assertEqual(ns.size(), 0) | ||
| 93 | + | ||
| 94 | + | ||
| 95 | +if __name__ == "__main__": | ||
| 96 | + run_tests() | ||