已合并
【API一致性任务】test: add consistency validation cases for torch.BoolStorage / torch_npu.npu.BoolStorage (#2955) #42273
【API一致性任务】test: add consistency validation cases for torch.BoolStorage / torch_npu.npu.BoolStorage (#2955) #42273
已合并
luoxiaoyan2024创建于 7月21日
1 个文件变更+149-0
Atest/test_bool_storage.py+149-0
ascend-robotascend-robot7月22日

【openlibing.ci】检测到当前PR中存在代码检查告警抑制 2 处,详情见下表,请Committer检视合理性。 / Detected 2 code check alert suppression(s) in this PR, see table below. Committers please review.

文件路径/File 行号/Line 代码片段/Snippet 工具/Tool
test/test_bool_storage.py 18 import torch_npu # noqa: F401 flake8,ruff
test/test_bool_storage.py 19 import torch_npu.testing # noqa: F401 flake8,ruff
likedislike
@@ -0,0 +1,149 @@
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.BoolStorage / torch_npu.npu.BoolStorage
18+on NPU (#2955).
19+ 
20+The PyTorch community lacks a dedicated NPU consistency test for the BoolStorage
21+storage class. Upstream only checks ``torch.BoolStorage().element_size()`` in
22+``test_torch.py::test_element_size`` (a CPU-only property check), and the existing
23+``test/npu/test_storage.py`` exercises storage via tensor ``.storage()`` rather than
24+the BoolStorage class itself. This file validates the BoolStorage class behavior
25+(construction from size / sequence, empty object, element_size, indexing, out-of-
26+bounds access, fill_, tolist, roundtrip) on both CPU and NPU, and asserts an NPU
27+bool tensor's ``.storage()`` is a ``torch_npu.npu.BoolStorage`` instance with the
28+expected dtype and data consistency.
29+"""
30+ 
31+import torch
32+from torch.testing._internal.common_utils import TestCase, run_tests
33+import torch_npu.npu as npu
34+ 
35+ 
36+class TestBoolStorage(TestCase):
37+ 
38+ # ---------------- CPU: torch.BoolStorage ----------------
39+ def test_cpu_bool_storage_basic(self):
40+ # Core CPU BoolStorage behavior (construction by size, indexing, fill_).
41+ s = torch.BoolStorage(3)
42+ s[0] = True
43+ s[1] = False
44+ s[2] = True
45+ self.assertEqual(s.tolist(), [True, False, True])
46+ self.assertEqual(s.size(), 3)
47+ self.assertEqual(s.element_size(), 1)
48+ self.assertEqual(torch.BoolStorage().element_size(), 1)
49+ 
50+ # fill_ then tolist
51+ s.fill_(False)
52+ self.assertEqual(s.tolist(), [False, False, False])
53+ 
54+ # roundtrip through torch.BoolTensor (CPU storage -> CPU tensor)
55+ t = torch.BoolTensor(s)
56+ self.assertEqual(t.dtype, torch.bool)
57+ self.assertEqual(t.tolist(), [False, False, False])
58+ 
59+ def test_cpu_bool_storage_from_sequence(self):
60+ # Minimal sequence construction (single element).
61+ s1 = torch.BoolStorage([True])
62+ self.assertEqual(s1.tolist(), [True])
63+ self.assertEqual(s1.size(), 1)
64+ 
65+ # Sequence construction (multiple elements).
66+ s2 = torch.BoolStorage([True, False, True])
67+ self.assertEqual(s2.tolist(), [True, False, True])
68+ self.assertEqual(s2.size(), 3)
69+ 
70+ def test_cpu_bool_storage_empty(self):
71+ # Empty object construction.
72+ s = torch.BoolStorage()
73+ self.assertEqual(s.size(), 0)
74+ self.assertEqual(s.tolist(), [])
75+ 
76+ # Explicit zero-size construction.
77+ s0 = torch.BoolStorage(0)
78+ self.assertEqual(s0.size(), 0)
79+ self.assertEqual(s0.tolist(), [])
80+ 
81+ def test_cpu_bool_storage_out_of_bounds(self):
82+ s = torch.BoolStorage(3)
83+ # Read beyond the end raises IndexError.
84+ with self.assertRaises(IndexError):
85+ _ = s[3]
86+ # Write beyond the end raises IndexError.
87+ with self.assertRaises(IndexError):
88+ s[3] = True
89+ 
90+ # ---------------- NPU: torch_npu.npu.BoolStorage ----------------
91+ def test_npu_bool_storage_basic(self):
92+ if not torch.npu.is_available():
93+ self.skipTest("NPU not available")
94+ 
95+ self.assertTrue(hasattr(npu, "BoolStorage"))
96+ ns = npu.BoolStorage(4)
97+ ns[0] = True
98+ ns[1] = False
99+ ns[2] = True
100+ ns[3] = False
101+ self.assertEqual(ns.tolist(), [True, False, True, False])
102+ self.assertEqual(ns.dtype, torch.bool)
103+ self.assertEqual(ns.element_size(), 1)
104+ self.assertEqual(ns.size(), 4)
105+ 
106+ ns.fill_(True)
107+ self.assertEqual(ns.tolist(), [True, True, True, True])
108+ 
109+ def test_npu_bool_storage_from_sequence(self):
110+ if not torch.npu.is_available():
111+ self.skipTest("NPU not available")
112+ 
113+ # Minimal sequence construction on NPU.
114+ ns = npu.BoolStorage([True, False])
115+ self.assertEqual(ns.tolist(), [True, False])
116+ self.assertEqual(ns.size(), 2)
117+ 
118+ def test_npu_bool_storage_empty(self):
119+ if not torch.npu.is_available():
120+ self.skipTest("NPU not available")
121+ 
122+ ns = npu.BoolStorage()
123+ self.assertEqual(ns.size(), 0)
124+ self.assertEqual(ns.tolist(), [])
125+ 
126+ def test_npu_bool_storage_out_of_bounds(self):
127+ if not torch.npu.is_available():
128+ self.skipTest("NPU not available")
129+ 
130+ ns = npu.BoolStorage(2)
131+ with self.assertRaises(IndexError):
132+ _ = ns[2]
133+ with self.assertRaises(IndexError):
134+ ns[2] = True
135+ 
136+ def test_npu_tensor_storage_consistency(self):
137+ if not torch.npu.is_available():
138+ self.skipTest("NPU not available")
139+ 
140+ # An NPU bool tensor's storage is a torch_npu.npu.BoolStorage instance,
141+ # with matching dtype and data consistency.
142+ x = torch.tensor([True, False, True], device="npu")
143+ self.assertIsInstance(x.storage(), npu.BoolStorage)
144+ self.assertEqual(x.storage().dtype, torch.bool)
145+ self.assertEqual(x.storage().tolist(), [True, False, True])
146+ 
147+ 
148+if __name__ == "__main__":
149+ run_tests()