|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 |
|
17 | | -import ctypes |
18 | | - |
19 | 17 | import numpy as np |
20 | 18 | import pytest |
21 | 19 |
|
22 | | -import dpctl |
23 | 20 | import dpctl.tensor as dpt |
24 | 21 | from dpctl.tensor._tensor_elementwise_impl import _divide_by_scalar |
25 | 22 | from dpctl.tensor._type_utils import _can_cast |
26 | 23 | from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported |
27 | 24 | from dpctl.utils import SequentialOrderManager |
28 | 25 |
|
29 | | -from .utils import ( |
30 | | - _all_dtypes, |
31 | | - _compare_dtypes, |
32 | | - _complex_fp_dtypes, |
33 | | - _real_fp_dtypes, |
34 | | - _usm_types, |
35 | | -) |
| 26 | +from .utils import _all_dtypes, _compare_dtypes |
36 | 27 |
|
37 | 28 |
|
38 | 29 | @pytest.mark.parametrize("op1_dtype", _all_dtypes) |
@@ -69,147 +60,6 @@ def test_divide_dtype_matrix(op1_dtype, op2_dtype): |
69 | 60 | assert (dpt.asnumpy(r) == expected.astype(r.dtype)).all() |
70 | 61 |
|
71 | 62 |
|
72 | | -@pytest.mark.parametrize("op1_usm_type", _usm_types) |
73 | | -@pytest.mark.parametrize("op2_usm_type", _usm_types) |
74 | | -def test_divide_usm_type_matrix(op1_usm_type, op2_usm_type): |
75 | | - get_queue_or_skip() |
76 | | - |
77 | | - sz = 128 |
78 | | - ar1 = dpt.ones(sz, dtype="i4", usm_type=op1_usm_type) |
79 | | - ar2 = dpt.ones_like(ar1, dtype="i4", usm_type=op2_usm_type) |
80 | | - |
81 | | - r = dpt.divide(ar1, ar2) |
82 | | - assert isinstance(r, dpt.usm_ndarray) |
83 | | - expected_usm_type = dpctl.utils.get_coerced_usm_type( |
84 | | - (op1_usm_type, op2_usm_type) |
85 | | - ) |
86 | | - assert r.usm_type == expected_usm_type |
87 | | - |
88 | | - |
89 | | -def test_divide_order(): |
90 | | - get_queue_or_skip() |
91 | | - |
92 | | - ar1 = dpt.ones((20, 20), dtype="i4", order="C") |
93 | | - ar2 = dpt.ones((20, 20), dtype="i4", order="C") |
94 | | - r1 = dpt.divide(ar1, ar2, order="C") |
95 | | - assert r1.flags.c_contiguous |
96 | | - r2 = dpt.divide(ar1, ar2, order="F") |
97 | | - assert r2.flags.f_contiguous |
98 | | - r3 = dpt.divide(ar1, ar2, order="A") |
99 | | - assert r3.flags.c_contiguous |
100 | | - r4 = dpt.divide(ar1, ar2, order="K") |
101 | | - assert r4.flags.c_contiguous |
102 | | - |
103 | | - ar1 = dpt.ones((20, 20), dtype="i4", order="F") |
104 | | - ar2 = dpt.ones((20, 20), dtype="i4", order="F") |
105 | | - r1 = dpt.divide(ar1, ar2, order="C") |
106 | | - assert r1.flags.c_contiguous |
107 | | - r2 = dpt.divide(ar1, ar2, order="F") |
108 | | - assert r2.flags.f_contiguous |
109 | | - r3 = dpt.divide(ar1, ar2, order="A") |
110 | | - assert r3.flags.f_contiguous |
111 | | - r4 = dpt.divide(ar1, ar2, order="K") |
112 | | - assert r4.flags.f_contiguous |
113 | | - |
114 | | - ar1 = dpt.ones((40, 40), dtype="i4", order="C")[:20, ::-2] |
115 | | - ar2 = dpt.ones((40, 40), dtype="i4", order="C")[:20, ::-2] |
116 | | - r4 = dpt.divide(ar1, ar2, order="K") |
117 | | - assert r4.strides == (20, -1) |
118 | | - |
119 | | - ar1 = dpt.ones((40, 40), dtype="i4", order="C")[:20, ::-2].mT |
120 | | - ar2 = dpt.ones((40, 40), dtype="i4", order="C")[:20, ::-2].mT |
121 | | - r4 = dpt.divide(ar1, ar2, order="K") |
122 | | - assert r4.strides == (-1, 20) |
123 | | - |
124 | | - |
125 | | -def test_divide_broadcasting(): |
126 | | - get_queue_or_skip() |
127 | | - |
128 | | - m = dpt.ones((100, 5), dtype="i4") |
129 | | - v = dpt.arange(1, 6, dtype="i4") |
130 | | - |
131 | | - r = dpt.divide(m, v) |
132 | | - |
133 | | - expected = np.divide( |
134 | | - np.ones((100, 5), dtype="i4"), np.arange(1, 6, dtype="i4") |
135 | | - ) |
136 | | - assert (dpt.asnumpy(r) == expected.astype(r.dtype)).all() |
137 | | - |
138 | | - r2 = dpt.divide(v, m) |
139 | | - expected2 = np.divide( |
140 | | - np.arange(1, 6, dtype="i4"), np.ones((100, 5), dtype="i4") |
141 | | - ) |
142 | | - assert (dpt.asnumpy(r2) == expected2.astype(r2.dtype)).all() |
143 | | - |
144 | | - |
145 | | -@pytest.mark.parametrize("arr_dt", _all_dtypes) |
146 | | -def test_divide_python_scalar(arr_dt): |
147 | | - q = get_queue_or_skip() |
148 | | - skip_if_dtype_not_supported(arr_dt, q) |
149 | | - |
150 | | - X = dpt.ones((10, 10), dtype=arr_dt, sycl_queue=q) |
151 | | - py_ones = ( |
152 | | - bool(1), |
153 | | - int(1), |
154 | | - float(1), |
155 | | - complex(1), |
156 | | - np.float32(1), |
157 | | - ctypes.c_int(1), |
158 | | - ) |
159 | | - for sc in py_ones: |
160 | | - R = dpt.divide(X, sc) |
161 | | - assert isinstance(R, dpt.usm_ndarray) |
162 | | - R = dpt.divide(sc, X) |
163 | | - assert isinstance(R, dpt.usm_ndarray) |
164 | | - |
165 | | - |
166 | | -class MockArray: |
167 | | - def __init__(self, arr): |
168 | | - self.data_ = arr |
169 | | - |
170 | | - @property |
171 | | - def __sycl_usm_array_interface__(self): |
172 | | - return self.data_.__sycl_usm_array_interface__ |
173 | | - |
174 | | - |
175 | | -def test_divide_mock_array(): |
176 | | - get_queue_or_skip() |
177 | | - a = dpt.arange(10) |
178 | | - b = dpt.ones(10) |
179 | | - c = MockArray(b) |
180 | | - r = dpt.divide(a, c) |
181 | | - assert isinstance(r, dpt.usm_ndarray) |
182 | | - |
183 | | - |
184 | | -def test_divide_canary_mock_array(): |
185 | | - get_queue_or_skip() |
186 | | - a = dpt.arange(10) |
187 | | - |
188 | | - class Canary: |
189 | | - def __init__(self): |
190 | | - pass |
191 | | - |
192 | | - @property |
193 | | - def __sycl_usm_array_interface__(self): |
194 | | - return None |
195 | | - |
196 | | - c = Canary() |
197 | | - with pytest.raises(ValueError): |
198 | | - dpt.divide(a, c) |
199 | | - |
200 | | - |
201 | | -@pytest.mark.parametrize("dtype", _real_fp_dtypes + _complex_fp_dtypes) |
202 | | -def test_divide_inplace_python_scalar(dtype): |
203 | | - q = get_queue_or_skip() |
204 | | - skip_if_dtype_not_supported(dtype, q) |
205 | | - X = dpt.zeros((10, 10), dtype=dtype, sycl_queue=q) |
206 | | - dt_kind = X.dtype.kind |
207 | | - if dt_kind == "f": |
208 | | - X /= float(1) |
209 | | - elif dt_kind == "c": |
210 | | - X /= complex(1) |
211 | | - |
212 | | - |
213 | 63 | @pytest.mark.parametrize("op1_dtype", _all_dtypes) |
214 | 64 | @pytest.mark.parametrize("op2_dtype", _all_dtypes) |
215 | 65 | def test_divide_inplace_dtype_matrix(op1_dtype, op2_dtype): |
|
0 commit comments