-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_manipulation.py
More file actions
451 lines (357 loc) · 13.4 KB
/
test_manipulation.py
File metadata and controls
451 lines (357 loc) · 13.4 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
import dpctl.tensor as dpt
import numpy
import pytest
from numpy.testing import assert_array_equal, assert_raises
import dpnp
from .helper import (
get_all_dtypes,
get_complex_dtypes,
get_float_dtypes,
has_support_aspect64,
)
testdata = []
testdata += [
([True, False, True], dtype)
for dtype in get_all_dtypes(no_none=True, no_complex=True)
]
testdata += [
([1, -1, 0], dtype)
for dtype in get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
]
testdata += [([0.1, 0.0, -0.1], dtype) for dtype in get_float_dtypes()]
testdata += [([1j, -1j, 1 - 2j], dtype) for dtype in get_complex_dtypes()]
@pytest.mark.parametrize("in_obj, out_dtype", testdata)
def test_copyto_dtype(in_obj, out_dtype):
ndarr = numpy.array(in_obj)
expected = numpy.empty(ndarr.size, dtype=out_dtype)
numpy.copyto(expected, ndarr)
dparr = dpnp.array(in_obj)
result = dpnp.empty(dparr.size, dtype=out_dtype)
dpnp.copyto(result, dparr)
assert_array_equal(result, expected)
@pytest.mark.parametrize("dst", [7, numpy.ones(10), (2, 7), [5], range(3)])
def test_copyto_dst_raises(dst):
a = dpnp.array(4)
with pytest.raises(
TypeError,
match="Destination array must be any of supported type, but got",
):
dpnp.copyto(dst, a)
@pytest.mark.parametrize("where", [numpy.ones(10), (2, 7), [5], range(3)])
def test_copyto_where_raises(where):
a = dpnp.empty((2, 3))
b = dpnp.arange(6).reshape((2, 3))
with pytest.raises(
TypeError, match="`where` array must be any of supported type, but got"
):
dpnp.copyto(a, b, where=where)
def test_result_type():
X = [dpnp.ones((2), dtype=dpnp.int64), dpnp.int32, "float32"]
X_np = [numpy.ones((2), dtype=numpy.int64), numpy.int32, "float32"]
if has_support_aspect64():
assert dpnp.result_type(*X) == numpy.result_type(*X_np)
else:
assert dpnp.result_type(*X) == dpnp.default_float_type(X[0].device)
def test_result_type_only_dtypes():
X = [dpnp.int64, dpnp.int32, dpnp.bool, dpnp.float32]
X_np = [numpy.int64, numpy.int32, numpy.bool_, numpy.float32]
assert dpnp.result_type(*X) == numpy.result_type(*X_np)
def test_result_type_only_arrays():
X = [dpnp.ones((2), dtype=dpnp.int64), dpnp.ones((7, 4), dtype=dpnp.int32)]
X_np = [
numpy.ones((2), dtype=numpy.int64),
numpy.ones((7, 4), dtype=numpy.int32),
]
assert dpnp.result_type(*X) == numpy.result_type(*X_np)
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize(
"array",
[[1, 2, 3], [1, 2, 2, 1, 2, 4], [2, 2, 2, 2], []],
ids=["[1, 2, 3]", "[1, 2, 2, 1, 2, 4]", "[2, 2, 2, 2]", "[]"],
)
def test_unique(array):
np_a = numpy.array(array)
dpnp_a = dpnp.array(array)
expected = numpy.unique(np_a)
result = dpnp.unique(dpnp_a)
assert_array_equal(expected, result)
class TestRepeat:
@pytest.mark.parametrize(
"data",
[[], [1, 2, 3, 4], [[1, 2], [3, 4]], [[[1], [2]], [[3], [4]]]],
ids=[
"[]",
"[1, 2, 3, 4]",
"[[1, 2], [3, 4]]",
"[[[1], [2]], [[3], [4]]]",
],
)
@pytest.mark.parametrize("dtype", get_all_dtypes())
def test_data(self, data, dtype):
a = numpy.array(data, dtype=dtype)
ia = dpnp.array(a)
expected = numpy.repeat(a, 2)
result = dpnp.repeat(ia, 2)
assert_array_equal(expected, result)
@pytest.mark.parametrize(
"repeats", [2, (2, 2, 2, 2, 2)], ids=["scalar", "tuple"]
)
def test_scalar_sequence_agreement(self, repeats):
a = numpy.arange(5, dtype="i4")
ia = dpnp.array(a)
expected = numpy.repeat(a, repeats)
result = dpnp.repeat(ia, repeats)
assert_array_equal(expected, result)
@pytest.mark.parametrize("axis", [0, 1])
def test_broadcasting(self, axis):
reps = 5
a = numpy.arange(reps, dtype="i4")
if axis == 0:
sh = (reps, 1)
else:
sh = (1, reps)
a = a.reshape(sh)
ia = dpnp.array(a)
expected = numpy.repeat(a, reps)
result = dpnp.repeat(ia, reps)
assert_array_equal(expected, result)
@pytest.mark.parametrize("axis", [0, 1])
def test_axes(self, axis):
reps = 2
a = numpy.arange(5 * 10, dtype="i4").reshape((5, 10))
ia = dpnp.array(a)
expected = numpy.repeat(a, reps, axis=axis)
result = dpnp.repeat(ia, reps, axis=axis)
assert_array_equal(expected, result)
def test_size_0_outputs(self):
reps = 10
a = dpnp.ones((3, 0, 5), dtype="i4")
ia = dpnp.array(a)
expected = numpy.repeat(a, reps, axis=0)
result = dpnp.repeat(ia, reps, axis=0)
assert_array_equal(expected, result)
expected = numpy.repeat(a, reps, axis=1)
result = dpnp.repeat(ia, reps, axis=1)
assert_array_equal(expected, result)
reps = (2, 2, 2)
expected = numpy.repeat(a, reps, axis=0)
result = dpnp.repeat(ia, reps, axis=0)
assert_array_equal(expected, result)
a = numpy.ones((3, 2, 5))
ia = dpnp.array(a)
reps = 0
expected = numpy.repeat(a, reps, axis=1)
result = dpnp.repeat(ia, reps, axis=1)
assert_array_equal(expected, result)
reps = (0, 0)
expected = numpy.repeat(a, reps, axis=1)
result = dpnp.repeat(ia, reps, axis=1)
assert_array_equal(expected, result)
def test_strides_0(self):
reps = 2
a = numpy.arange(10 * 10, dtype="i4").reshape((10, 10))
ia = dpnp.array(a)
a = a[::-2, :]
ia = ia[::-2, :]
expected = numpy.repeat(a, reps, axis=0)
result = dpnp.repeat(ia, reps, axis=0)
assert_array_equal(expected, result)
expected = numpy.repeat(a, (reps,) * a.shape[0], axis=0)
result = dpnp.repeat(ia, (reps,) * ia.shape[0], axis=0)
assert_array_equal(expected, result)
def test_strides_1(self):
reps = 2
a = numpy.arange(10 * 10, dtype="i4").reshape((10, 10))
ia = dpnp.array(a)
a = a[:, ::-2]
ia = ia[:, ::-2]
expected = numpy.repeat(a, reps, axis=1)
result = dpnp.repeat(ia, reps, axis=1)
assert_array_equal(expected, result)
expected = numpy.repeat(a, (reps,) * a.shape[1], axis=1)
result = dpnp.repeat(ia, (reps,) * ia.shape[1], axis=1)
assert_array_equal(expected, result)
def test_casting(self):
a = numpy.arange(5, dtype="i4")
ia = dpnp.array(a)
# i4 is cast to i8
reps = numpy.ones(5, dtype="i4")
ireps = dpnp.array(reps)
expected = numpy.repeat(a, reps)
result = dpnp.repeat(ia, ireps)
assert_array_equal(expected, result)
def test_strided_repeats(self):
a = numpy.arange(5, dtype="i4")
ia = dpnp.array(a)
reps = numpy.ones(10, dtype="i8")
reps[::2] = 0
ireps = dpnp.array(reps)
reps = reps[::-2]
ireps = ireps[::-2]
expected = numpy.repeat(a, reps)
result = dpnp.repeat(ia, ireps)
assert_array_equal(expected, result)
def test_usm_ndarray_as_input_array(self):
reps = [1, 3, 2, 1, 1, 2]
a = numpy.array([[1, 2, 3, 4, 5, 6]])
ia = dpt.asarray(a)
expected = numpy.repeat(a, reps)
result = dpnp.repeat(ia, reps)
assert_array_equal(expected, result)
assert isinstance(result, dpnp.ndarray)
def test_scalar_as_input_array(self):
assert_raises(TypeError, dpnp.repeat, 3, 2)
def test_usm_ndarray_as_repeats(self):
a = numpy.array([1, 2, 3, 4, 5, 6]).reshape((2, 3))
ia = dpnp.asarray(a)
reps = numpy.array([1, 3, 2])
ireps = dpt.asarray(reps)
expected = a.repeat(reps, axis=1)
result = ia.repeat(ireps, axis=1)
assert_array_equal(expected, result)
assert isinstance(result, dpnp.ndarray)
def test_unsupported_array_as_repeats(self):
assert_raises(TypeError, dpnp.arange(5, dtype="i4"), numpy.array(3))
@pytest.mark.parametrize(
"data, dtype",
[
pytest.param([1, 2**7 - 1, -(2**7)], numpy.int8, id="int8"),
pytest.param([1, 2**15 - 1, -(2**15)], numpy.int16, id="int16"),
pytest.param([1, 2**31 - 1, -(2**31)], numpy.int32, id="int32"),
pytest.param([1, 2**63 - 1, -(2**63)], numpy.int64, id="int64"),
],
)
def test_maximum_signed_integers(self, data, dtype):
reps = 129
a = numpy.array(data, dtype=dtype)
ia = dpnp.asarray(a)
expected = a.repeat(reps)
result = ia.repeat(reps)
assert_array_equal(expected, result)
@pytest.mark.parametrize(
"data, dtype",
[
pytest.param(
[1, -(2**7), -(2**7) + 1, 2**7 - 1], numpy.int8, id="int8"
),
pytest.param(
[1, -(2**15), -(2**15) + 1, 2**15 - 1], numpy.int16, id="int16"
),
pytest.param(
[1, -(2**31), -(2**31) + 1, 2**31 - 1], numpy.int32, id="int32"
),
pytest.param(
[1, -(2**63), -(2**63) + 1, 2**63 - 1], numpy.int64, id="int64"
),
],
)
def test_minimum_signed_integers(self, data, dtype):
reps = 129
a = numpy.array(data, dtype=dtype)
ia = dpnp.asarray(a)
expected = a.repeat(reps)
result = ia.repeat(reps)
assert_array_equal(expected, result)
class TestTranspose:
@pytest.mark.parametrize("axes", [(0, 1), (1, 0), [0, 1]])
def test_2d_with_axes(self, axes):
na = numpy.array([[1, 2], [3, 4]])
da = dpnp.array(na)
expected = numpy.transpose(na, axes)
result = dpnp.transpose(da, axes)
assert_array_equal(expected, result)
# ndarray
expected = na.transpose(axes)
result = da.transpose(axes)
assert_array_equal(expected, result)
@pytest.mark.parametrize(
"axes",
[
(1, 0, 2),
[1, 0, 2],
((1, 0, 2),),
([1, 0, 2],),
[(1, 0, 2)],
[[1, 0, 2]],
],
)
def test_3d_with_packed_axes(self, axes):
na = numpy.ones((1, 2, 3))
da = dpnp.array(na)
expected = na.transpose(*axes)
result = da.transpose(*axes)
assert_array_equal(expected, result)
# ndarray
expected = na.transpose(*axes)
result = da.transpose(*axes)
assert_array_equal(expected, result)
@pytest.mark.parametrize("shape", [(10,), (2, 4), (5, 3, 7), (3, 8, 4, 1)])
def test_none_axes(self, shape):
na = numpy.ones(shape)
da = dpnp.ones(shape)
assert_array_equal(numpy.transpose(na), dpnp.transpose(da))
assert_array_equal(numpy.transpose(na, None), dpnp.transpose(da, None))
# ndarray
assert_array_equal(na.transpose(), da.transpose())
assert_array_equal(na.transpose(None), da.transpose(None))
def test_ndarray_axes_n_int(self):
na = numpy.ones((1, 2, 3))
da = dpnp.array(na)
expected = na.transpose(1, 0, 2)
result = da.transpose(1, 0, 2)
assert_array_equal(expected, result)
class TestTrimZeros:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
def test_basic(self, dtype):
a = numpy.array([0, 0, 1, 0, 2, 3, 4, 0], dtype=dtype)
ia = dpnp.array(a)
result = dpnp.trim_zeros(ia)
expected = numpy.trim_zeros(a)
assert_array_equal(expected, result)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("trim", ["F", "B"])
def test_trim(self, dtype, trim):
a = numpy.array([0, 0, 1, 0, 2, 3, 4, 0], dtype=dtype)
ia = dpnp.array(a)
result = dpnp.trim_zeros(ia, trim)
expected = numpy.trim_zeros(a, trim)
assert_array_equal(expected, result)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("trim", ["F", "B"])
def test_all_zero(self, dtype, trim):
a = numpy.zeros((8,), dtype=dtype)
ia = dpnp.array(a)
result = dpnp.trim_zeros(ia, trim)
expected = numpy.trim_zeros(a, trim)
assert_array_equal(expected, result)
def test_size_zero(self):
a = numpy.zeros(0)
ia = dpnp.array(a)
result = dpnp.trim_zeros(ia)
expected = numpy.trim_zeros(a)
assert_array_equal(expected, result)
@pytest.mark.parametrize(
"a", [numpy.array([0, 2**62, 0]), numpy.array([0, 2**63, 0])]
)
def test_overflow(self, a):
ia = dpnp.array(a)
result = dpnp.trim_zeros(ia)
expected = numpy.trim_zeros(a)
assert_array_equal(expected, result)
def test_trim_no_rule(self):
a = numpy.array([0, 0, 1, 0, 2, 3, 4, 0])
ia = dpnp.array(a)
trim = "ADE" # no "F" or "B" in trim string
result = dpnp.trim_zeros(ia, trim)
expected = numpy.trim_zeros(a, trim)
assert_array_equal(expected, result)
def test_list_array(self):
assert_raises(TypeError, dpnp.trim_zeros, [0, 0, 1, 0, 2, 3, 4, 0])
@pytest.mark.parametrize(
"trim", [1, ["F"], numpy.array("B")], ids=["int", "list", "array"]
)
def test_unsupported_trim(self, trim):
a = numpy.array([0, 0, 1, 0, 2, 3, 4, 0])
ia = dpnp.array(a)
assert_raises(TypeError, dpnp.trim_zeros, ia, trim)
assert_raises(AttributeError, numpy.trim_zeros, a, trim)