-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_sycl_device.py
More file actions
409 lines (349 loc) · 12.1 KB
/
test_sycl_device.py
File metadata and controls
409 lines (349 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Data Parallel Control (dpctl)
#
# Copyright 2020-2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
""" Defines unit test cases for the SyclDevice class.
"""
import pytest
import dpctl
from dpctl import SyclDeviceCreationError
from .helper import get_queue_or_skip
def test_standard_selectors(device_selector, check):
"""Tests if the standard SYCL device_selectors are able to select a
device.
"""
try:
device = device_selector()
except dpctl.SyclDeviceCreationError:
pytest.skip("Could not create default-selected device")
check(device)
def test_current_device(check):
"""Test is the device for the current queue is valid."""
q = get_queue_or_skip()
try:
device = q.get_sycl_device()
except dpctl.SyclDeviceCreationError:
pytest.skip("Could not create default-selected device")
check(device)
def test_valid_filter_selectors(valid_filter, check):
"""
Tests if we can create a SyclDevice using a supported filter selector
string.
"""
device = None
try:
device = dpctl.SyclDevice(valid_filter)
except SyclDeviceCreationError:
pytest.skip("Failed to create device with supported filter")
check(device)
def test_invalid_filter_selectors(invalid_filter):
"""
An invalid filter string should always be caught and a TypeError raised.
"""
exc = (
SyclDeviceCreationError
if isinstance(invalid_filter, str)
else TypeError
)
with pytest.raises(exc):
dpctl.SyclDevice(invalid_filter)
def test_filter_string(valid_filter):
"""
Test that filter_string reconstructs the same device.
"""
device = None
try:
device = dpctl.SyclDevice(valid_filter)
except SyclDeviceCreationError:
pytest.skip("Failed to create device with supported filter")
dev_id = device.filter_string
assert (
dpctl.SyclDevice(dev_id) == device
), "Reconstructed device is different, ({}, {})".format(
valid_filter, dev_id
)
def test_filter_string_property():
"""
Test that filter_string reconstructs the same device.
"""
devices = dpctl.get_devices()
for d in devices:
if d.default_selector_score >= 0:
dev_id = d.filter_string
d_r = dpctl.SyclDevice(dev_id)
assert d == d_r
assert hash(d) == hash(d_r)
def test_filter_string_method():
"""
Test that filter_string reconstructs the same device.
"""
devices = dpctl.get_devices()
for d in devices:
for be in [True, False]:
for dt in [True, False]:
if d.default_selector_score >= 0:
dev_id = d.get_filter_string(
include_backend=be, include_device_type=dt
)
d_r = dpctl.SyclDevice(dev_id)
assert d == d_r, "Failed "
assert hash(d) == hash(
d_r
), "Hash equality is inconsistent with __eq__"
def test_hashing_of_device():
"""
Test that a :class:`dpctl.SyclDevice` object can be used as
a dictionary key.
"""
try:
device = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("Could not create default-constructed device")
device_dict = {device: "default_device"}
assert device_dict
def test_equal():
try:
d1 = dpctl.SyclDevice()
d2 = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("Could not create default-selected device")
assert d1 != Ellipsis
assert d1 == d2
list_of_supported_aspects = [
"cpu",
"gpu",
"accelerator",
"custom",
"fp16",
"fp64",
"image",
"online_compiler",
"online_linker",
"queue_profiling",
"usm_device_allocations",
"usm_host_allocations",
"usm_shared_allocations",
"usm_system_allocations",
"host_debuggable",
"atomic64",
"usm_atomic_host_allocations",
"usm_atomic_shared_allocations",
"emulated",
"is_component",
"is_composite",
]
# SYCL 2020 spec aspects not presently
# supported in DPC++, and dpctl
list_of_unsupported_aspects = []
@pytest.fixture(params=list_of_supported_aspects)
def supported_aspect(request):
return request.param
@pytest.fixture(params=list_of_unsupported_aspects)
def unsupported_aspect(request):
return request.param
def test_supported_aspect(supported_aspect):
try:
d = dpctl.SyclDevice()
has_it = getattr(d, "has_aspect_" + supported_aspect)
except dpctl.SyclDeviceCreationError:
has_it = False
try:
d_wa = dpctl.select_device_with_aspects(supported_aspect)
assert getattr(d_wa, "has_aspect_" + supported_aspect)
except dpctl.SyclDeviceCreationError:
# ValueError may be raised if no device with
# requested aspect charateristics is available
assert not has_it
def test_unsupported_aspect(unsupported_aspect):
try:
d = dpctl.SyclDevice()
has_it = hasattr(d, "has_aspect_" + unsupported_aspect)
except dpctl.SyclDeviceCreationError:
has_it = False
if has_it:
raise AttributeError(
f"The {unsupported_aspect} aspect is now supported in dpctl"
)
def test_handle_no_device():
with pytest.raises(dpctl.SyclDeviceCreationError):
dpctl.select_device_with_aspects(["gpu", "cpu"])
with pytest.raises(dpctl.SyclDeviceCreationError):
dpctl.select_device_with_aspects("cpu", excluded_aspects="cpu")
def test_cpython_api_SyclDevice_GetDeviceRef():
import ctypes
import sys
try:
d = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("Could not create default-constructed device")
mod = sys.modules[d.__class__.__module__]
# get capsule storing SyclDevice_GetDeviceRef function ptr
d_ref_fn_cap = mod.__pyx_capi__["SyclDevice_GetDeviceRef"]
# construct Python callable to invoke "SyclDevice_GetDeviceRef"
cap_ptr_fn = ctypes.pythonapi.PyCapsule_GetPointer
cap_ptr_fn.restype = ctypes.c_void_p
cap_ptr_fn.argtypes = [ctypes.py_object, ctypes.c_char_p]
d_ref_fn_ptr = cap_ptr_fn(
d_ref_fn_cap, b"DPCTLSyclDeviceRef (struct PySyclDeviceObject *)"
)
callable_maker = ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)
get_device_ref_fn = callable_maker(d_ref_fn_ptr)
r2 = d.addressof_ref()
r1 = get_device_ref_fn(d)
assert r1 == r2
def test_cpython_api_SyclDevice_Make():
import ctypes
import sys
try:
d = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("Could not create default-constructed device")
mod = sys.modules[d.__class__.__module__]
# get capsule storign SyclContext_Make function ptr
make_d_fn_cap = mod.__pyx_capi__["SyclDevice_Make"]
# construct Python callable to invoke "SyclDevice_Make"
cap_ptr_fn = ctypes.pythonapi.PyCapsule_GetPointer
cap_ptr_fn.restype = ctypes.c_void_p
cap_ptr_fn.argtypes = [ctypes.py_object, ctypes.c_char_p]
make_d_fn_ptr = cap_ptr_fn(
make_d_fn_cap, b"struct PySyclDeviceObject *(DPCTLSyclDeviceRef)"
)
callable_maker = ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)
make_d_fn = callable_maker(make_d_fn_ptr)
d2 = make_d_fn(d.addressof_ref())
assert d == d2
def test_get_device_id_method():
"""
Test that the get_device_id method reconstructs the same device.
"""
devices = dpctl.get_devices()
for d in devices:
dev_id = d.get_device_id()
d_r = dpctl.SyclDevice(str(dev_id))
assert dev_id == devices.index(d)
assert d == d_r
assert hash(d) == hash(d_r)
def test_get_unpartitioned_parent_device_method():
"""
Test that the get_unpartitioned_parent method returns self for root
devices.
"""
devices = dpctl.get_devices()
for d in devices:
assert d == d.get_unpartitioned_parent_device()
def test_get_unpartitioned_parent_device_from_sub_device():
"""
Test that the get_unpartitioned_parent method returns the parent device
from the sub-device.
"""
try:
dev = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("No default device available")
try:
sdevs = dev.create_sub_devices(partition="next_partitionable")
except dpctl.SyclSubDeviceCreationError:
sdevs = None
try:
if sdevs is None:
sdevs = dev.create_sub_devices(partition=[1, 1])
except dpctl.SyclSubDeviceCreationError:
pytest.skip("Default device can not be partitioned")
assert isinstance(sdevs, list) and len(sdevs) > 0
assert dev == sdevs[0].get_unpartitioned_parent_device()
def test_composite_device_method():
"""
Test that the composite_device method returns a composite
device found in ``dpctl.get_composite_devices()``
"""
devices = dpctl.get_devices()
composite_devices = dpctl.get_composite_devices()
for d in devices:
if d.has_aspect_is_component:
Cd = d.composite_device
assert Cd in composite_devices
def test_get_component_devices_from_composite():
"""
Test that the component_devices method returns component
root devices.
"""
devices = dpctl.get_devices()
composite_devices = dpctl.get_composite_devices()
for Cd in composite_devices:
assert Cd.has_aspect_is_composite
component_devices = Cd.component_devices()
for d in component_devices:
assert d.has_aspect_is_component
# component devices are root devices
assert d in devices
@pytest.mark.parametrize("platform_name", ["level_zero", "cuda", "hip"])
def test_can_access_peer(platform_name):
"""
Test checks for peer access.
"""
try:
platform = dpctl.SyclPlatform(platform_name)
except ValueError as e:
pytest.skip(f"{str(e)} {platform_name}")
devices = platform.get_devices()
if len(devices) < 2:
pytest.skip(
f"Platform {platform_name} does not have enough devices to "
"test peer access"
)
dev0 = devices[0]
dev1 = devices[1]
assert isinstance(dev0.can_access_peer_access_supported(dev1), bool)
assert isinstance(dev0.can_access_peer_atomics_supported(dev1), bool)
@pytest.mark.parametrize("platform_name", ["level_zero", "cuda", "hip"])
def test_enable_disable_peer(platform_name):
"""
Test that peer access can be enabled and disabled.
"""
try:
platform = dpctl.SyclPlatform(platform_name)
except ValueError as e:
pytest.skip(f"{str(e)} {platform_name}")
devices = platform.get_devices()
if len(devices) < 2:
pytest.skip(
f"Platform {platform_name} does not have enough devices to "
"test peer access"
)
dev0 = devices[0]
dev1 = devices[1]
if dev0.can_access_peer_access_supported(dev1):
dev0.enable_peer_access(dev1)
dev0.disable_peer_access(dev1)
else:
pytest.skip(
f"Provided {platform_name} devices do not support peer access"
)
def test_peer_device_arg_validation():
"""
Test for validation of arguments to peer access related methods.
"""
try:
dev = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("No default device available")
bad_dev = dict()
with pytest.raises(TypeError):
dev.can_access_peer_access_supported(bad_dev)
with pytest.raises(TypeError):
dev.can_access_peer_atomics_supported(bad_dev)
with pytest.raises(TypeError):
dev.enable_peer_access(bad_dev)
with pytest.raises(TypeError):
dev.disable_peer_access(bad_dev)