Debian Codegraph @

tensilelite/Tensile/Tests/unit/Common/test_Architectures.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 import pytest
26 from unittest.mock import mock_open, patch
27 from pathlib import Path
28 from Tensile.Common.Utilities import isRhel8
29 from Tensile.Common.Architectures import (
30 SUPPORTED_ARCH_DEVICE_IDS,
31 SUPPORTED_ARCH_CU_COUNTS,
32 ARCH_DEVICE_ID_FALLBACKS,
33 ArchInfo,
34 splitArchsFromPredicates,
35 LogicFileError,
36 filterLogicFilesByPredicates,
37 _extractArchInfo,
38 _verifyPredicate,
39 )
40
41 # Test data
42 VALID_LOGIC_FILE_CONTENT = """- {MinimumRequiredVersion: 4.33.0}
43 - gfx950
44 - gfx950
45 - [Device 75a2]
46 """
47
48 VALID_LOGIC_FILE_WITH_CU = """- {MinimumRequiredVersion: 4.33.0}
49 - aquavanjaram
50 - {Architecture: gfx942, CUCount: 228}
51 - [Device 74a0]
52 """
53
54 INVALID_VERSION_FILE = """- Invalid Version Line
55 - aquavanjaram
56 - gfx950
57 - [Device 75a0]
58 """
59
60 INVALID_ARCH_FILE = """- {MinimumRequiredVersion: 4.33.0}
61 - aquavanjaram
62 - invalid_arch_line
63 - [Device 75a0]
64 """
65
66 INVALID_DEVICE_FILE = """- {MinimumRequiredVersion: 4.33.0}
67 - aquavanjaram
68 - gfx950
69 - invalid_device_line
70 """
71
72 @pytest.fixture
73 def mock_logic_file():
74 with patch("builtins.open", mock_open(read_data=VALID_LOGIC_FILE_CONTENT)) as mock_file:
75 yield mock_file
76
77 @pytest.fixture
78 def mock_logic_file_with_cu():
79 with patch("builtins.open", mock_open(read_data=VALID_LOGIC_FILE_WITH_CU)) as mock_file:
80 yield mock_file
81
82 @pytest.fixture
83 def mock_logic_file_invalid_version():
84 with patch("builtins.open", mock_open(read_data=INVALID_VERSION_FILE)) as mock_file:
85 yield mock_file
86
87 @pytest.fixture
88 def mock_logic_file_invalid_arch():
89 with patch("builtins.open", mock_open(read_data=INVALID_ARCH_FILE)) as mock_file:
90 yield mock_file
91
92 @pytest.fixture
93 def mock_logic_file_invalid_device():
94 with patch("builtins.open", mock_open(read_data=INVALID_DEVICE_FILE)) as mock_file:
95 yield mock_file
96
97 def test_extractArchInfo_success(mock_logic_file):
98 result = _extractArchInfo("dummy.yaml")
99 assert isinstance(result, ArchInfo)
100 assert result.Name == "gfx950"
101 assert result.Gfx == "gfx950"
102 assert result.DeviceIds == {"id=75a2"}
103 assert result.CUCount is None
104
105 def test_extractArchInfo_with_cu_count(mock_logic_file_with_cu):
106 result = _extractArchInfo("dummy.yaml")
107 assert isinstance(result, ArchInfo)
108 assert result.Name == "aquavanjaram"
109 assert result.Gfx == "gfx942"
110 assert result.DeviceIds == {"id=74a0"}
111 assert result.CUCount == "cu=228"
112
113 def test_extractArchInfo_with_invalid_version(mock_logic_file_invalid_version):
114 with pytest.raises(LogicFileError):
115 _extractArchInfo("dummy.yaml")
116
117 def test_extractArchInfo_with_invalid_arch(mock_logic_file_invalid_arch):
118 with pytest.raises(LogicFileError):
119 _extractArchInfo("dummy.yaml")
120
121 def test_extractArchInfo_with_invalid_device(mock_logic_file_invalid_device):
122 with pytest.raises(LogicFileError):
123 _extractArchInfo("dummy.yaml")
124
125 def test_filterLogicFilesByPredicates_fallback_id_match(mock_logic_file):
126 logicFiles = ["file1.yaml", "file2.yaml"]
127 predicateMap = {
128 "gfx950": {
129 "id=75a2": set(), # Should fallback to 75a0
130 "fallback": set()
131 }
132 }
133
134 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract:
135 mock_extract.return_value = ArchInfo("test", "gfx950", {"id=75a0"}, None)
136 result = filterLogicFilesByPredicates(logicFiles, predicateMap)
137 assert len(result) == 2
138 assert "file1.yaml" in result
139 assert "file2.yaml" in result
140 assert all(isinstance(r, str) for r in result)
141
142 def test_filterLogicFilesByPredicates_cu_match(mock_logic_file):
143 logicFiles = ["file1.yaml"]
144 predicateMap = {
145 "gfx942": {
146 "cu=80": set(),
147 "fallback": set()
148 }
149 }
150
151 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract:
152 mock_extract.return_value = ArchInfo("test", "gfx942", {"id=74a0"}, "cu=80")
153 result = filterLogicFilesByPredicates(logicFiles, predicateMap)
154 assert len(result) == 1
155 assert "file1.yaml" in result
156
157 def test_filterLogicFilesByPredicates_cu_fallback(mock_logic_file):
158 logicFiles = ["file1.yaml"]
159 predicateMap = {
160 "gfx942": {
161 "cu=96": set(), # Should fallback to None
162 "fallback": set()
163 }
164 }
165
166 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract:
167 mock_extract.return_value = ArchInfo("test", "gfx942", {"id=74a0"}, None)
168 result = filterLogicFilesByPredicates(logicFiles, predicateMap)
169 assert len(result) == 1
170 assert "file1.yaml" in result
171
172 def test_filterLogicFilesByPredicates_mixed_match_fallback(mock_logic_file):
173 logicFiles = ["file1.yaml", "file2.yaml"]
174 predicateMap = {
175 "gfx942": {
176 "cu=96": set(), # Should fallback to None
177 "fallback": set()
178 }
179 }
180
181 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract1:
182 mock_extract1.return_value = ArchInfo("test", "gfx942", {"id=74a0"}, None)
183 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract2:
184 mock_extract2.return_value = ArchInfo("test", "gfx942", {"id=74a0"}, "cu=96")
185 result = filterLogicFilesByPredicates(logicFiles, predicateMap)
186 assert len(result) == 2
187 assert "file1.yaml" in result
188 assert "file2.yaml" in result
189
190 def test_filterLogicFilesByPredicates_no_match(mock_logic_file):
191 logicFiles = ["file1.yaml"]
192 predicateMap = {
193 "gfx950": {
194 "id=75a0": set(),
195 "fallback": set()
196 }
197 }
198
199 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract:
200 mock_extract.return_value = ArchInfo("test", "gfx950", {"id=75a3"}, None)
201 result = filterLogicFilesByPredicates(logicFiles, predicateMap)
202 assert len(result) == 0
203
204 def test_filterLogicFilesByPredicates_match_emulation_ids(mock_logic_file):
205 logicFiles = ["file1.yaml"]
206 predicateMap = {
207 "gfx950": {
208 "id=75a0": set(),
209 "fallback": set()
210 }
211 }
212
213 with patch("Tensile.Common.Architectures._extractArchInfo") as mock_extract:
214 mock_extract.return_value = ArchInfo("test", "gfx950", {"id=0049"}, None)
215 result = filterLogicFilesByPredicates(logicFiles, predicateMap)
216 assert len(result) == 1
217 assert "file1.yaml" in result
218
219 def test_splitArchsFromPredicates_with_variants():
220 archSpecs = ["gfx950[id=75a0]", "gfx906"]
221 archs, variants = splitArchsFromPredicates(archSpecs)
222 assert "gfx906" in archs and "gfx950" in archs
223 assert variants["gfx950"] == ["id=75a0"]
224
225 def test_splitArchsFromPredicates_with_multiple_variants():
226 archSpecs = ["gfx950[id=75a0,id=75a2]", "gfx942"]
227 archs, variants = splitArchsFromPredicates(archSpecs)
228 assert "gfx942" in archs and "gfx950" in archs
229 assert set(variants["gfx950"]) == {"id=75a0", "id=75a2"}
230
231 def test_splitArchsFromPredicates_with_mixed_predicates():
232 archSpecs = ["gfx942[id=74a0,cu=80]", "gfx950[id=75a0]"]
233 archs, variants = splitArchsFromPredicates(archSpecs)
234 assert "gfx942" in archs and "gfx950" in archs
235 assert set(variants["gfx942"]) == {"id=74a0", "cu=80"}
236 assert variants["gfx950"] == ["id=75a0"]
237
238 def test_splitArchsFromPredicates_no_variants():
239 archSpecs = ["gfx950", "gfx906"]
240 archs, variants = splitArchsFromPredicates(archSpecs)
241 assert "gfx906" in archs and "gfx950" in archs
242 assert variants is None
243
244 def test_splitArchsFromPredicates_empty():
245 archSpecs = []
246 archs, variants = splitArchsFromPredicates(archSpecs)
247 assert archs == []
248 assert variants is None
249
250 def test_verifyPredicate_valid_device_id():
251 for device_id in SUPPORTED_ARCH_DEVICE_IDS:
252 arch = SUPPORTED_ARCH_DEVICE_IDS[device_id]
253 assert _verifyPredicate(device_id, arch) == device_id
254
255 def test_verifyPredicate_valid_cu_count():
256 for cu_count in SUPPORTED_ARCH_CU_COUNTS:
257 arch = SUPPORTED_ARCH_CU_COUNTS[cu_count]
258 assert _verifyPredicate(cu_count, arch) == cu_count
259
260 def test_verifyPredicate_invalid_device_id():
261 with pytest.raises(ValueError) as exc_info:
262 _verifyPredicate("id=invalid", "gfx950")
263 assert "device ID not supported" in str(exc_info.value)
264
265 def test_verifyPredicate_mismatched_device_id():
266 with pytest.raises(ValueError) as exc_info:
267 _verifyPredicate("id=75a0", "gfx942") # 75a0 is for gfx950
268 assert "not associated with" in str(exc_info.value)
269
270 def test_verifyPredicate_invalid_cu_count():
271 with pytest.raises(ValueError) as exc_info:
272 _verifyPredicate("cu=999", "gfx942")
273 assert "CU count not supported" in str(exc_info.value)
274
275 def test_verifyPredicate_mismatched_cu_count():
276 with pytest.raises(ValueError) as exc_info:
277 _verifyPredicate("cu=80", "gfx950") # cu=80 is for gfx942
278 assert "not associated with" in str(exc_info.value)
279
280 def test_verifyPredicate_invalid_predicate():
281 with pytest.raises(ValueError) as exc_info:
282 _verifyPredicate("invalid=value", "gfx950")
283 assert "only device ID and CU count-based predicates" in str(exc_info.value)
284