-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_dpnp_full.py
More file actions
183 lines (150 loc) · 5.1 KB
/
test_dpnp_full.py
File metadata and controls
183 lines (150 loc) · 5.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
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
"""Tests for dpnp ndarray constructors."""
import math
import sys
import dpctl
import dpnp
import numpy
import pytest
from numba import errors
from numba_dpex import dpjit
from numba_dpex.tests._helper import get_all_dtypes
shapes = [11, (3, 7)]
usm_types = ["device", "shared", "host"]
fill_values = [
7,
-7,
7.1,
-7.1,
math.pi,
math.e,
4294967295,
4294967295.0,
3.4028237e38,
]
@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("fill_value", fill_values)
def test_dpnp_full_default(shape, fill_value):
"""Test dpnp.full() with default parameters inside dpjit."""
if sys.platform == "win32" and fill_value == 4294967295:
pytest.skip("dpnp.full() doesn't work with large integers on windows.")
@dpjit
def func(shape, fill_value):
c = dpnp.full(shape, fill_value, dtype=numpy.float32)
return c
try:
c = func(shape, fill_value)
except Exception:
pytest.fail("Calling dpnp.full() inside dpjit failed.")
if len(c.shape) == 1:
assert c.shape[0] == shape
else:
assert c.shape == shape
dummy = dpnp.full(shape, fill_value, dtype=dpnp.float32)
if c.dtype != dummy.dtype:
if sys.platform != "linux":
pytest.xfail(
"Default bit length is not as same as that of linux for {0:s}".format(
str(dummy.dtype)
)
)
else:
pytest.fail("The dtype of the returned array doesn't conform.")
assert c.usm_type == dummy.usm_type
assert c.sycl_device == dummy.sycl_device
if c.sycl_queue != dummy.sycl_queue:
pytest.xfail(
"Returned queue does not have the same queue as in the dummy array."
)
assert c.sycl_queue == dpctl._sycl_queue_manager.get_device_cached_queue(
dummy.sycl_device
)
assert numpy.array_equal(c.asnumpy(), dummy.asnumpy())
@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("fill_value", fill_values)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
def test_dpnp_full_from_device(shape, fill_value, dtype, usm_type):
""" "Use device only in dpnp.full() inside dpjit."""
device = dpctl.SyclDevice().filter_string
@dpjit
def func(shape, fill_value):
c = dpnp.full(
shape, fill_value, dtype=dtype, usm_type=usm_type, device=device
)
return c
try:
c = func(shape, fill_value)
except Exception:
pytest.fail("Calling dpnp.full() inside dpjit failed.")
if len(c.shape) == 1:
assert c.shape[0] == shape
else:
assert c.shape == shape
assert c.dtype == dtype
assert c.usm_type == usm_type
assert c.sycl_device.filter_string == device
if c.sycl_queue != dpctl._sycl_queue_manager.get_device_cached_queue(
device
):
pytest.xfail(
"Returned queue does not have the same queue as cached against the device."
)
# dpnp can't cast 4294967295 into int32 and so on,
# but we can, also numpy can, so we are using numpy here
dummy = numpy.full(shape, fill_value, dtype=dtype)
assert numpy.array_equal(c.asnumpy(), dummy)
@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("fill_value", fill_values)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
def test_dpnp_full_from_queue(shape, fill_value, dtype, usm_type):
""" "Use queue only in dpnp.full() inside dpjit."""
@dpjit
def func(shape, fill_value, queue):
c = dpnp.full(
shape, fill_value, dtype=dtype, usm_type=usm_type, sycl_queue=queue
)
return c
try:
queue = dpctl.SyclQueue()
c = func(shape, fill_value, queue)
except Exception:
pytest.fail("Calling dpnp.full() inside dpjit failed.")
if len(c.shape) == 1:
assert c.shape[0] == shape
else:
assert c.shape == shape
assert c.dtype == dtype
assert c.usm_type == usm_type
assert c.sycl_device == queue.sycl_device
if c.sycl_queue != queue:
pytest.xfail(
"Returned queue does not have the same queue as the one passed to the dpnp function."
)
# dpnp can't cast 4294967295 into int32 and so on,
# but we can, also numpy can, so we are using numpy here
dummy = numpy.full(shape, fill_value, dtype=dtype)
assert numpy.array_equal(c.asnumpy(), dummy)
def test_dpnp_full_exceptions():
"""Test if exception is raised when both queue and device are specified."""
device = dpctl.SyclDevice().filter_string
@dpjit
def func(shape, fill_value, queue):
c = dpnp.ones(shape, fill_value, sycl_queue=queue, device=device)
return c
with pytest.raises((errors.TypingError, TypeError)):
queue = dpctl.SyclQueue()
func(10, 7, queue)