-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_tensor_asarray.py
More file actions
649 lines (532 loc) · 18.1 KB
/
test_tensor_asarray.py
File metadata and controls
649 lines (532 loc) · 18.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# 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.
import numpy as np
import pytest
import dpctl
import dpctl.tensor as dpt
from .helper import get_queue_or_skip, skip_if_dtype_not_supported
@pytest.mark.parametrize(
"src_usm_type, dst_usm_type",
[
("device", "shared"),
("device", "host"),
("shared", "device"),
("shared", "host"),
("host", "device"),
("host", "shared"),
],
)
def test_asarray_change_usm_type(src_usm_type, dst_usm_type):
try:
d = dpctl.SyclDevice()
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
X = dpt.empty(10, dtype="u1", usm_type=src_usm_type)
Y = dpt.asarray(X, usm_type=dst_usm_type)
assert X.shape == Y.shape
assert X.usm_type == src_usm_type
assert Y.usm_type == dst_usm_type
with pytest.raises(ValueError):
# zero copy is not possible
dpt.asarray(X, usm_type=dst_usm_type, copy=False)
Y = dpt.asarray(X, usm_type=dst_usm_type, sycl_queue=X.sycl_queue)
assert X.shape == Y.shape
assert Y.usm_type == dst_usm_type
Y = dpt.asarray(
X,
usm_type=dst_usm_type,
sycl_queue=X.sycl_queue,
device=d.get_filter_string(),
)
assert X.shape == Y.shape
assert Y.usm_type == dst_usm_type
def test_asarray_from_numpy():
Xnp = np.arange(10)
try:
Y = dpt.asarray(Xnp, usm_type="device")
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == Xnp.shape
assert Y.dtype == Xnp.dtype
# Fortran contiguous case
Xnp = np.array([[1, 2, 3], [4, 5, 6]], dtype="f4", order="F")
Y = dpt.asarray(Xnp, usm_type="shared")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == Xnp.shape
assert Y.dtype == Xnp.dtype
# general strided case
Xnp = np.array([[1, 2, 3], [4, 5, 6]], dtype="i8")
Y = dpt.asarray(Xnp[::-1, ::-1], usm_type="host")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == Xnp.shape
assert Y.dtype == Xnp.dtype
def test_asarray_from_sequence():
X = [1, 2, 3]
try:
Y = dpt.asarray(X, usm_type="device")
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
assert type(Y) is dpt.usm_ndarray
X = [(1, 1), (2.0, 2.0 + 1.0j), range(4, 6), np.array([3, 4], dtype="c16")]
Y = dpt.asarray(X, usm_type="device")
assert type(Y) is dpt.usm_ndarray
assert Y.ndim == 2
assert Y.shape == (len(X), 2)
X = []
Y = dpt.asarray(X, usm_type="device")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == (0,)
X = [[], []]
Y = dpt.asarray(X, usm_type="device")
assert type(Y) is dpt.usm_ndarray
assert Y.shape == (2, 0)
X = [True, False]
Y = dpt.asarray(X, usm_type="device")
assert type(Y) is dpt.usm_ndarray
assert Y.dtype.kind == "b"
def test_asarray_from_object_with_suai():
"""Test that asarray can deal with opaque objects implementing SUAI"""
class Dummy:
def __init__(self, obj, iface):
self.obj = obj
self.__sycl_usm_array_interface__ = iface
try:
X = dpt.empty((2, 3, 4), dtype="f4")
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
Y = dpt.asarray(Dummy(X, X.__sycl_usm_array_interface__))
assert Y.shape == X.shape
assert X.usm_type == Y.usm_type
assert X.dtype == Y.dtype
assert X.sycl_device == Y.sycl_device
def test_asarray_input_validation():
with pytest.raises(TypeError):
# copy keyword is not of right type
dpt.asarray([1], copy="invalid")
with pytest.raises(TypeError):
# order keyword is not valid
dpt.asarray([1], order=1)
with pytest.raises(TypeError):
# dtype is not valid
dpt.asarray([1], dtype="invalid")
with pytest.raises(ValueError):
# unexpected value of order
dpt.asarray([1], order="Z")
with pytest.raises(TypeError):
# usm_type is of wrong type
dpt.asarray([1], usm_type=dict())
with pytest.raises(ValueError):
# usm_type has wrong value
dpt.asarray([1], usm_type="mistake")
try:
wrong_queue_type = dpctl.SyclContext()
except dpctl.SyclContextCreationError:
# use any other type
wrong_queue_type = Ellipsis
with pytest.raises(TypeError):
# sycl_queue type is not right
dpt.asarray([1], sycl_queue=wrong_queue_type)
with pytest.raises(ValueError):
# sequence is not rectangular
dpt.asarray([[1], 2])
with pytest.raises(OverflowError):
# Python int too large for type
dpt.asarray(-9223372036854775809, dtype="i4")
with pytest.raises(ValueError):
# buffer to usm_ndarray requires a copy
dpt.asarray(memoryview(np.arange(5)), copy=False)
with pytest.raises(ValueError):
# Numpy array to usm_ndarray requires a copy
dpt.asarray(np.arange(5), copy=False)
with pytest.raises(ValueError):
# Python sequence to usm_ndarray requires a copy
dpt.asarray([1, 2, 3], copy=False)
with pytest.raises(ValueError):
# Python scalar to usm_ndarray requires a copy
dpt.asarray(5, copy=False)
def test_asarray_input_validation2():
d = dpctl.get_devices()
if len(d) < 2:
pytest.skip("Not enough SYCL devices available")
d0, d1 = d[:2]
try:
q0 = dpctl.SyclQueue(d0)
except dpctl.SyclQueueCreationError:
pytest.skip(f"SyclQueue could not be created for {d0}")
try:
q1 = dpctl.SyclQueue(d1)
except dpctl.SyclQueueCreationError:
pytest.skip(f"SyclQueue could not be created for {d1}")
with pytest.raises(TypeError):
dpt.asarray([1, 2], sycl_queue=q0, device=q1)
def test_asarray_scalars():
import ctypes
try:
Y = dpt.asarray(5)
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
assert Y.dtype == dpt.dtype(int)
Y = dpt.asarray(5.2)
if Y.sycl_device.has_aspect_fp64:
assert Y.dtype == dpt.dtype(float)
else:
assert Y.dtype == dpt.dtype(dpt.float32)
Y = dpt.asarray(np.float32(2.3))
assert Y.dtype == dpt.dtype(dpt.float32)
Y = dpt.asarray(1.0j)
if Y.sycl_device.has_aspect_fp64:
assert Y.dtype == dpt.dtype(complex)
else:
assert Y.dtype == dpt.dtype(dpt.complex64)
Y = dpt.asarray(ctypes.c_int(8))
assert Y.dtype == dpt.dtype(ctypes.c_int)
def test_asarray_copy_false():
q = get_queue_or_skip()
rng = np.random.default_rng()
Xnp = rng.integers(low=-255, high=255, size=(10, 4), dtype=np.int64)
X = dpt.from_numpy(Xnp, usm_type="device", sycl_queue=q)
Y1 = dpt.asarray(X, copy=False, order="K")
assert Y1 is X
Y1c = dpt.asarray(X, copy=True, order="K")
assert not (Y1c is X)
Y2 = dpt.asarray(X, copy=False, order="C")
assert Y2 is X
Y3 = dpt.asarray(X, copy=False, order="A")
assert Y3 is X
with pytest.raises(ValueError):
Y1 = dpt.asarray(X, copy=False, order="F")
Xf = dpt.empty(
X.shape,
dtype=X.dtype,
usm_type="device",
sycl_queue=X.sycl_queue,
order="F",
)
Xf[:] = X
Y4 = dpt.asarray(Xf, copy=False, order="K")
assert Y4 is Xf
Y5 = dpt.asarray(Xf, copy=False, order="F")
assert Y5 is Xf
Y6 = dpt.asarray(Xf, copy=False, order="A")
assert Y6 is Xf
with pytest.raises(ValueError):
dpt.asarray(Xf, copy=False, order="C")
def test_asarray_invalid_dtype():
q = get_queue_or_skip()
Xnp = np.array([1, 2, 3], dtype=object)
with pytest.raises(TypeError):
dpt.asarray(Xnp, sycl_queue=q)
def test_asarray_cross_device():
q = get_queue_or_skip()
qprof = dpctl.SyclQueue(property="enable_profiling")
x = dpt.empty(10, dtype="i8", sycl_queue=q)
y = dpt.asarray(x, sycl_queue=qprof)
assert y.sycl_queue == qprof
def test_asarray_seq_of_arrays_simple():
get_queue_or_skip()
r = dpt.arange(10)
m = dpt.asarray(
[
r,
]
* 4
)
assert m.shape == (4,) + r.shape
assert m.dtype == r.dtype
assert m.device == r.device
def test_asarray_seq_of_arrays():
get_queue_or_skip()
m = dpt.ones((2, 4), dtype="i4")
w = dpt.zeros(4)
v = dpt.full(4, -1)
ar = dpt.asarray([m, [w, v]])
assert ar.shape == (2, 2, 4)
assert ar.device == m.device
assert ar.device == w.device
assert ar.device == v.device
def test_asarray_seq_of_array_different_queue():
get_queue_or_skip()
m = dpt.ones((2, 4), dtype="i4")
w = dpt.zeros(4)
v = dpt.full(4, -1)
qprof = dpctl.SyclQueue(property="enable_profiling")
ar = dpt.asarray([m, [w, v]], sycl_queue=qprof)
assert ar.shape == (2, 2, 4)
assert ar.sycl_queue == qprof
def test_asarray_seq_of_suai():
get_queue_or_skip()
class Dummy:
def __init__(self, obj, iface):
self.obj = obj
self.__sycl_usm_array_interface__ = iface
o = dpt.empty(0, usm_type="shared")
d = Dummy(o, o.__sycl_usm_array_interface__)
x = dpt.asarray(d)
assert x.shape == (0,)
assert x.usm_type == o.usm_type
assert x._pointer == o._pointer
assert x.sycl_queue == o.sycl_queue
x = dpt.asarray([d, d])
assert x.shape == (2, 0)
assert x.usm_type == o.usm_type
assert x.sycl_queue == o.sycl_queue
def test_asarray_seq_of_suai_different_queue():
q = get_queue_or_skip()
class Dummy:
def __init__(self, obj, iface):
self.obj = obj
self.__sycl_usm_array_interface__ = iface
@property
def shape(self):
return self.__sycl_usm_array_interface__["shape"]
q2 = dpctl.SyclQueue()
assert q != q2
o = dpt.empty((2, 2), usm_type="shared", sycl_queue=q2)
d = Dummy(o, o.__sycl_usm_array_interface__)
x = dpt.asarray(d, sycl_queue=q)
assert x.sycl_queue == q
assert x.shape == d.shape
x = dpt.asarray([d], sycl_queue=q)
assert x.sycl_queue == q
assert x.shape == (1,) + d.shape
x = dpt.asarray([d, d], sycl_queue=q)
assert x.sycl_queue == q
assert x.shape == (2,) + d.shape
def test_asarray_seq_of_arrays_on_different_queues():
q = get_queue_or_skip()
m = dpt.empty((2, 4), dtype="i2", sycl_queue=q)
q2 = dpctl.SyclQueue()
w = dpt.empty(4, dtype="i1", sycl_queue=q2)
q3 = dpctl.SyclQueue()
py_seq = [
0,
] * w.shape[0]
res = dpt.asarray([m, [w, py_seq]], sycl_queue=q3)
assert res.sycl_queue == q3
assert dpt.isdtype(res.dtype, "integral")
res = dpt.asarray([m, [w, range(w.shape[0])]], sycl_queue=q3)
assert res.sycl_queue == q3
assert dpt.isdtype(res.dtype, "integral")
res = dpt.asarray([m, [w, w]], sycl_queue=q)
assert res.sycl_queue == q
assert dpt.isdtype(res.dtype, "integral")
res = dpt.asarray([m, [w, dpt.asnumpy(w)]], sycl_queue=q2)
assert res.sycl_queue == q2
assert dpt.isdtype(res.dtype, "integral")
res = dpt.asarray([w, dpt.asnumpy(w)])
assert res.sycl_queue == w.sycl_queue
assert dpt.isdtype(res.dtype, "integral")
with pytest.raises(dpctl.utils.ExecutionPlacementError):
dpt.asarray([m, [w, py_seq]])
def test_ulonglong_gh_1167():
get_queue_or_skip()
x = dpt.asarray(9223372036854775807, dtype="u8")
assert x.dtype == dpt.uint64
x = dpt.asarray(9223372036854775808, dtype="u8")
assert x.dtype == dpt.uint64
def test_orderK_gh_1350():
get_queue_or_skip()
a = dpt.empty((2, 3, 4), dtype="u1")
b = dpt.permute_dims(a, (2, 0, 1))
c = dpt.asarray(b, copy=True, order="K")
assert c.shape == b.shape
assert c.strides == b.strides
assert c._element_offset == 0
assert not c._pointer == b._pointer
def _typesafe_arange(n: int, dtype_: dpt.dtype, device: object):
n_half = n // 2
if dtype_.kind in "ui":
ii = dpt.iinfo(dtype_)
m0 = max(ii.min, -n_half)
m1 = min(m0 + n, ii.max)
n_tiles = (n + m1 - m0 - 1) // (m1 - m0)
res = dpt.arange(m0, m1, dtype=dtype_, device=device)
elif dtype_.kind == "b":
n_tiles = (n + 1) // 2
res = dpt.asarray([False, True], dtype=dtype_, device=device)
else:
m0 = -n_half
m1 = m0 + n
n_tiles = 1
res = dpt.linspace(m0, m1, num=n, dtype=dtype_, device=device)
if n_tiles > 1:
res = dpt.tile(res, n_tiles)[:n]
return res
_all_dtypes = [
"b1",
"i1",
"u1",
"i2",
"u2",
"i4",
"u4",
"i8",
"u8",
"f2",
"f4",
"f8",
"c8",
"c16",
]
@pytest.mark.parametrize("dt", _all_dtypes)
def test_as_c_contig_rect(dt):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dt, q)
dtype_ = dpt.dtype(dt)
n0, n1, n2 = 6, 35, 37
arr_flat = _typesafe_arange(n0 * n1 * n2, dtype_, q)
x = dpt.reshape(arr_flat, (n0, n1, n2)).mT
y = dpt.asarray(x, order="C")
assert dpt.all(x == y)
x2 = x[0]
y2 = dpt.asarray(x2, order="C")
assert dpt.all(x2 == y2)
x3 = dpt.flip(x, axis=1)
y3 = dpt.asarray(x3, order="C")
assert dpt.all(x3 == y3)
x4 = dpt.reshape(arr_flat, (2, 3, n1, n2)).mT
x5 = x4[:, :2]
y5 = dpt.asarray(x5, order="C")
assert dpt.all(x5 == y5)
x6 = dpt.reshape(arr_flat, (n0, n1, n2), order="F")
y6 = dpt.asarray(x6, order="C")
assert dpt.all(x6 == y6)
@pytest.mark.parametrize("dt", _all_dtypes)
def test_as_f_contig_rect(dt):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dt, q)
dtype_ = dpt.dtype(dt)
n0, n1, n2 = 6, 35, 37
arr_flat = _typesafe_arange(n0 * n1 * n2, dtype_, q)
x = dpt.reshape(arr_flat, (n0, n1, n2))
y = dpt.asarray(x, order="F")
assert dpt.all(x == y)
x2 = x[0]
y2 = dpt.asarray(x2, order="F")
assert dpt.all(x2 == y2)
x3 = dpt.flip(x, axis=1)
y3 = dpt.asarray(x3, order="F")
assert dpt.all(x3 == y3)
x4 = dpt.reshape(arr_flat, (2, 3, n1, n2))
x5 = dpt.moveaxis(x4[:, :2], (2, 3), (0, 1))
y5 = dpt.asarray(x5, order="F")
assert dpt.all(x5 == y5)
@pytest.mark.parametrize("dt", _all_dtypes)
def test_as_c_contig_square(dt):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dt, q)
dtype_ = dpt.dtype(dt)
n0, n1 = 4, 53
arr_flat = _typesafe_arange(n0 * n1 * n1, dtype_, q)
x = dpt.reshape(arr_flat, (n0, n1, n1)).mT
y = dpt.asarray(x, order="C")
assert dpt.all(x == y)
x2 = x[0]
y2 = dpt.asarray(x2, order="C")
assert dpt.all(x2 == y2)
x3 = dpt.flip(x, axis=1)
y3 = dpt.asarray(x3, order="C")
assert dpt.all(x3 == y3)
@pytest.mark.parametrize("dt", _all_dtypes)
def test_as_f_contig_square(dt):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dt, q)
dtype_ = dpt.dtype(dt)
n0, n1 = 6, 53
arr_flat = _typesafe_arange(n0 * n1 * n1, dtype_, q)
x = dpt.moveaxis(dpt.reshape(arr_flat, (n0, n1, n1)), (1, 2), (0, 1))
y = dpt.asarray(x, order="F")
assert dpt.all(x == y)
x2 = x[..., 0]
y2 = dpt.asarray(x2, order="F")
assert dpt.all(x2 == y2)
x3 = dpt.flip(x, axis=1)
y3 = dpt.asarray(x3, order="F")
assert dpt.all(x3 == y3)
class MockArrayWithBothProtocols:
"""
Object that implements both __sycl_usm_array_interface__
and __usm_ndarray__ properties.
"""
def __init__(self, usm_ar):
if not isinstance(usm_ar, dpt.usm_ndarray):
raise TypeError
self._arr = usm_ar
@property
def __usm_ndarray__(self):
return self._arr
@property
def __sycl_usm_array_interface__(self):
return self._arr.__sycl_usm_array_interface__
class MockArrayWithSUAIOnly:
"""
Object that implements only the
__sycl_usm_array_interface__ property.
"""
def __init__(self, usm_ar):
if not isinstance(usm_ar, dpt.usm_ndarray):
raise TypeError
self._arr = usm_ar
@property
def __sycl_usm_array_interface__(self):
return self._arr.__sycl_usm_array_interface__
@pytest.mark.parametrize("usm_type", ["shared", "device", "host"])
def test_asarray_support_for_usm_ndarray_protocol(usm_type):
get_queue_or_skip()
x = dpt.arange(256, dtype="i4", usm_type=usm_type)
o1 = MockArrayWithBothProtocols(x)
o2 = MockArrayWithSUAIOnly(x)
y1 = dpt.asarray(o1)
assert x.sycl_queue == y1.sycl_queue
assert x.usm_type == y1.usm_type
assert x.dtype == y1.dtype
assert y1.usm_data.reference_obj is None
assert dpt.all(x == y1)
y2 = dpt.asarray(o2)
assert x.sycl_queue == y2.sycl_queue
assert x.usm_type == y2.usm_type
assert x.dtype == y2.dtype
assert not (y2.usm_data.reference_obj is None)
assert dpt.all(x == y2)
y3 = dpt.asarray([o1, o2])
assert x.sycl_queue == y3.sycl_queue
assert x.usm_type == y3.usm_type
assert x.dtype == y3.dtype
assert y3.usm_data.reference_obj is None
assert dpt.all(x[dpt.newaxis, :] == y3)
@pytest.mark.parametrize("dt", [dpt.float16, dpt.float64, dpt.complex128])
def test_asarray_to_device_with_unsupported_dtype(dt):
aspect = "fp16" if dt == dpt.float16 else "fp64"
try:
d0 = dpctl.select_device_with_aspects(aspect)
except dpctl.SyclDeviceCreationError:
pytest.skip("No device with aspect for test")
d1 = None
for d in dpctl.get_devices():
if d.default_selector_score < 0:
pass
try:
d1 = dpctl.select_device_with_aspects(
d.device_type.name, excluded_aspects=[aspect]
)
except dpctl.SyclDeviceCreationError:
pass
if d1 is None:
pytest.skip("No device with missing aspect for test")
x = dpt.ones(10, dtype=dt, device=d0)
y = dpt.asarray(x, device=d1)
assert y.sycl_device == d1