Skip to content

Commit 008dc96

Browse files
authored
update test_umath.py (#2367)
In this PR, `test_umath.py` is updated.
1 parent d5617de commit 008dc96

File tree

3 files changed

+250
-416
lines changed

3 files changed

+250
-416
lines changed

dpnp/tests/test_binary_ufuncs.py

+37-66
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .helper import (
1515
assert_dtype_allclose,
16+
generate_random_numpy_array,
1617
get_abs_array,
1718
get_all_dtypes,
1819
get_complex_dtypes,
@@ -22,10 +23,6 @@
2223
has_support_aspect16,
2324
numpy_version,
2425
)
25-
from .test_umath import (
26-
_get_numpy_arrays_2in_1out,
27-
_get_output_data_type,
28-
)
2926

3027
"""
3128
The scope includes tests with only functions which are instances of
@@ -39,7 +36,9 @@ class TestAdd:
3936

4037
@pytest.mark.parametrize("dtype", ALL_DTYPES)
4138
def test_add(self, dtype):
42-
a, b, expected = _get_numpy_arrays_2in_1out("add", dtype, [-5, 5, 10])
39+
a = generate_random_numpy_array(10, dtype)
40+
b = generate_random_numpy_array(10, dtype)
41+
expected = numpy.add(a, b)
4342

4443
ia, ib = dpnp.array(a), dpnp.array(b)
4544
iout = dpnp.empty(expected.shape, dtype=dtype)
@@ -139,91 +138,63 @@ def test_invalid_out(self, xp, out):
139138
assert_raises(TypeError, xp.add, a, 2, out)
140139

141140

141+
@pytest.mark.parametrize("func", ["fmax", "fmin", "maximum", "minimum"])
142142
class TestBoundFuncs:
143-
@pytest.fixture(
144-
params=[
145-
{"func_name": "fmax", "input_values": [-5, 5, 10]},
146-
{"func_name": "fmin", "input_values": [-5, 5, 10]},
147-
{"func_name": "maximum", "input_values": [-5, 5, 10]},
148-
{"func_name": "minimum", "input_values": [-5, 5, 10]},
149-
],
150-
ids=[
151-
"fmax",
152-
"fmin",
153-
"maximum",
154-
"minimum",
155-
],
156-
)
157-
def func_params(self, request):
158-
return request.param
159-
160-
@pytest.mark.parametrize(
161-
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
162-
)
163-
def test_out(self, func_params, dtype):
164-
func_name = func_params["func_name"]
165-
input_values = func_params["input_values"]
166-
a, b, expected = _get_numpy_arrays_2in_1out(
167-
func_name, dtype, input_values
168-
)
143+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
144+
def test_out(self, func, dtype):
145+
a = generate_random_numpy_array(10, dtype)
146+
b = generate_random_numpy_array(10, dtype)
147+
expected = getattr(numpy, func)(a, b)
169148

170149
ia, ib = dpnp.array(a), dpnp.array(b)
171150
iout = dpnp.empty(expected.shape, dtype=dtype)
172-
result = getattr(dpnp, func_name)(ia, ib, out=iout)
151+
result = getattr(dpnp, func)(ia, ib, out=iout)
173152

174153
assert result is iout
175154
assert_dtype_allclose(result, expected)
176155

177156
@pytest.mark.parametrize(
178-
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
157+
"dtype", get_all_dtypes(no_none=True, no_bool=True)
179158
)
180-
def test_out_overlap(self, func_params, dtype):
181-
func_name = func_params["func_name"]
159+
def test_out_overlap(self, func, dtype):
182160
size = 15
183161
a = numpy.arange(2 * size, dtype=dtype)
184162
ia = dpnp.array(a)
185163

186-
getattr(dpnp, func_name)(ia[size::], ia[::2], out=ia[:size:])
187-
getattr(numpy, func_name)(a[size::], a[::2], out=a[:size:])
164+
getattr(dpnp, func)(ia[size::], ia[::2], out=ia[:size:])
165+
getattr(numpy, func)(a[size::], a[::2], out=a[:size:])
188166

189167
assert_dtype_allclose(ia, a)
190168

191169
@pytest.mark.parametrize("shape", [(0,), (15,), (2, 2)])
192-
def test_invalid_shape(self, func_params, shape):
193-
func_name = func_params["func_name"]
170+
def test_invalid_shape(self, func, shape):
194171
a, b = dpnp.arange(10), dpnp.arange(10)
195172
out = dpnp.empty(shape)
196173

197174
with pytest.raises(ValueError):
198-
getattr(dpnp, func_name)(a, b, out=out)
175+
getattr(dpnp, func)(a, b, out=out)
199176

200177
@pytest.mark.parametrize("xp", [dpnp, numpy])
201178
@pytest.mark.parametrize(
202179
"out",
203180
[4, (), [], (3, 7), [2, 4]],
204181
ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"],
205182
)
206-
def test_invalid_out(self, func_params, xp, out):
207-
func_name = func_params["func_name"]
183+
def test_invalid_out(self, func, xp, out):
208184
a = xp.arange(10)
209-
assert_raises(TypeError, getattr(xp, func_name), a, 2, out)
185+
assert_raises(TypeError, getattr(xp, func), a, 2, out)
210186

211187

212188
class TestDivide:
213189
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
214-
@pytest.mark.parametrize(
215-
"dtype", get_all_dtypes(no_none=True, no_bool=True)
216-
)
190+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
217191
def test_divide(self, dtype):
218-
a, b, expected = _get_numpy_arrays_2in_1out(
219-
"divide", dtype, [-5, 5, 10]
220-
)
192+
a = generate_random_numpy_array(10, dtype)
193+
b = generate_random_numpy_array(10, dtype)
194+
expected = numpy.divide(a, b)
221195

222196
ia, ib = dpnp.array(a), dpnp.array(b)
223-
if numpy.issubdtype(dtype, numpy.integer):
224-
out_dtype = map_dtype_to_device(dpnp.float64, ia.sycl_device)
225-
else:
226-
out_dtype = _get_output_data_type(dtype)
197+
out_dtype = map_dtype_to_device(expected.dtype, ia.sycl_device)
227198
iout = dpnp.empty(expected.shape, dtype=out_dtype)
228199
result = dpnp.divide(ia, ib, out=iout)
229200

@@ -318,7 +289,9 @@ def do_inplace_op(self, base, other, func):
318289
@pytest.mark.usefixtures("suppress_divide_numpy_warnings")
319290
@pytest.mark.parametrize("dtype", ALL_DTYPES)
320291
def test_basic(self, func, dtype):
321-
a, b, expected = _get_numpy_arrays_2in_1out(func, dtype, [-5, 5, 10])
292+
a = generate_random_numpy_array(10, dtype)
293+
b = generate_random_numpy_array(10, dtype)
294+
expected = getattr(numpy, func)(a, b)
322295

323296
ia, ib = dpnp.array(a), dpnp.array(b)
324297
iout = dpnp.empty(expected.shape, dtype=dtype)
@@ -398,9 +371,9 @@ def test_invalid_out(self, func, xp, out):
398371
assert_raises(TypeError, getattr(xp, func), a, 2, out)
399372

400373

374+
@pytest.mark.parametrize("func", ["fmax", "fmin"])
401375
class TestFmaxFmin:
402376
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
403-
@pytest.mark.parametrize("func", ["fmax", "fmin"])
404377
def test_half(self, func):
405378
a = numpy.array([0, 1, 2, 4, 2], dtype=numpy.float16)
406379
b = numpy.array([-2, 5, 1, 4, 3], dtype=numpy.float16)
@@ -415,7 +388,6 @@ def test_half(self, func):
415388
expected = getattr(numpy, func)(b, c)
416389
assert_equal(result, expected)
417390

418-
@pytest.mark.parametrize("func", ["fmax", "fmin"])
419391
@pytest.mark.parametrize("dtype", get_float_dtypes())
420392
def test_float_nans(self, func, dtype):
421393
a = numpy.array([0, numpy.nan, numpy.nan], dtype=dtype)
@@ -426,7 +398,6 @@ def test_float_nans(self, func, dtype):
426398
expected = getattr(numpy, func)(a, b)
427399
assert_equal(result, expected)
428400

429-
@pytest.mark.parametrize("func", ["fmax", "fmin"])
430401
@pytest.mark.parametrize("dtype", get_complex_dtypes())
431402
@pytest.mark.parametrize(
432403
"nan_val",
@@ -446,7 +417,6 @@ def test_complex_nans(self, func, dtype, nan_val):
446417
expected = getattr(numpy, func)(a, b)
447418
assert_equal(result, expected)
448419

449-
@pytest.mark.parametrize("func", ["fmax", "fmin"])
450420
@pytest.mark.parametrize("dtype", get_float_dtypes(no_float16=False))
451421
def test_precision(self, func, dtype):
452422
dtmin = numpy.finfo(dtype).min
@@ -602,9 +572,9 @@ class TestMultiply:
602572

603573
@pytest.mark.parametrize("dtype", ALL_DTYPES)
604574
def test_multiply(self, dtype):
605-
a, b, expected = _get_numpy_arrays_2in_1out(
606-
"multiply", dtype, [0, 10, 10]
607-
)
575+
a = generate_random_numpy_array(10, dtype)
576+
b = generate_random_numpy_array(10, dtype)
577+
expected = numpy.multiply(a, b)
608578

609579
ia, ib = dpnp.array(a), dpnp.array(b)
610580
iout = dpnp.empty(expected.shape, dtype=dtype)
@@ -853,8 +823,9 @@ def test_basic(self, array, val, data_type, val_type):
853823

854824
@pytest.mark.parametrize("dtype", ALL_DTYPES)
855825
def test_power(self, dtype):
856-
numpy.random.seed(42)
857-
a, b, expected = _get_numpy_arrays_2in_1out("power", dtype, [0, 10, 10])
826+
a = generate_random_numpy_array(10, dtype, low=0)
827+
b = generate_random_numpy_array(10, dtype, low=0)
828+
expected = numpy.power(a, b)
858829

859830
ia, ib = dpnp.array(a), dpnp.array(b)
860831
out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype
@@ -1075,9 +1046,9 @@ class TestSubtract:
10751046

10761047
@pytest.mark.parametrize("dtype", ALL_DTYPES)
10771048
def test_add(self, dtype):
1078-
a, b, expected = _get_numpy_arrays_2in_1out(
1079-
"subtract", dtype, [-5, 5, 10]
1080-
)
1049+
a = generate_random_numpy_array(10, dtype)
1050+
b = generate_random_numpy_array(10, dtype)
1051+
expected = numpy.subtract(a, b)
10811052

10821053
ia, ib = dpnp.array(a), dpnp.array(b)
10831054
iout = dpnp.empty(expected.shape, dtype=dtype)

dpnp/tests/test_mathematical.py

+39-29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import dpnp
1919
from dpnp.dpnp_array import dpnp_array
20+
from dpnp.dpnp_utils import map_dtype_to_device
2021

2122
from .helper import (
2223
assert_dtype_allclose,
@@ -32,14 +33,33 @@
3233
has_support_aspect64,
3334
numpy_version,
3435
)
35-
from .test_umath import (
36-
_get_numpy_arrays_1in_1out,
37-
_get_numpy_arrays_2in_1out,
38-
_get_output_data_type,
39-
)
4036
from .third_party.cupy import testing
4137

4238

39+
def _get_output_data_type(dtype):
40+
"""Return a data type specified by input `dtype` and device capabilities."""
41+
dtype_float16 = any(
42+
dpnp.issubdtype(dtype, t) for t in (dpnp.bool, dpnp.int8, dpnp.uint8)
43+
)
44+
dtype_float32 = any(
45+
dpnp.issubdtype(dtype, t) for t in (dpnp.int16, dpnp.uint16)
46+
)
47+
if dtype_float16:
48+
out_dtype = dpnp.float16 if has_support_aspect16() else dpnp.float32
49+
elif dtype_float32:
50+
out_dtype = dpnp.float32
51+
elif dpnp.issubdtype(dtype, dpnp.complexfloating):
52+
out_dtype = dpnp.complex64
53+
if has_support_aspect64() and dtype != dpnp.complex64:
54+
out_dtype = dpnp.complex128
55+
else:
56+
out_dtype = dpnp.float32
57+
if has_support_aspect64() and dtype != dpnp.float32:
58+
out_dtype = dpnp.float64
59+
60+
return out_dtype
61+
62+
4363
@pytest.mark.parametrize("deg", [True, False])
4464
class TestAngle:
4565
def test_angle_bool(self, deg):
@@ -2271,30 +2291,19 @@ def test_projection(self, dtype):
22712291
assert dpnp.allclose(result, expected)
22722292

22732293

2294+
@pytest.mark.parametrize("func", ["ceil", "floor", "trunc"])
22742295
class TestRoundingFuncs:
2275-
@pytest.fixture(
2276-
params=[
2277-
{"func_name": "ceil", "input_values": [-5, 5, 10]},
2278-
{"func_name": "floor", "input_values": [-5, 5, 10]},
2279-
{"func_name": "trunc", "input_values": [-5, 5, 10]},
2280-
],
2281-
ids=["ceil", "floor", "trunc"],
2282-
)
2283-
def func_params(self, request):
2284-
return request.param
2285-
22862296
@pytest.mark.parametrize(
22872297
"dtype", get_all_dtypes(no_none=True, no_complex=True)
22882298
)
2289-
def test_out(self, func_params, dtype):
2290-
func_name = func_params["func_name"]
2291-
input_values = func_params["input_values"]
2292-
a, expected = _get_numpy_arrays_1in_1out(func_name, dtype, input_values)
2299+
def test_out(self, func, dtype):
2300+
a = generate_random_numpy_array(10, dtype)
2301+
expected = getattr(numpy, func)(a)
22932302

22942303
ia = dpnp.array(a)
22952304
out_dt = numpy.int8 if dtype == dpnp.bool else dtype
22962305
iout = dpnp.empty(expected.shape, dtype=out_dt)
2297-
result = getattr(dpnp, func_name)(ia, out=iout)
2306+
result = getattr(dpnp, func)(ia, out=iout)
22982307

22992308
assert result is iout
23002309
# numpy.ceil, numpy.floor, numpy.trunc always return float dtype for
@@ -2310,34 +2319,35 @@ def test_out(self, func_params, dtype):
23102319
@pytest.mark.parametrize(
23112320
"dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1]
23122321
)
2313-
def test_invalid_dtype(self, func_params, dtype):
2314-
func_name = func_params["func_name"]
2322+
def test_invalid_dtype(self, func, dtype):
23152323
dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1]
23162324
ia = dpnp.arange(10, dtype=dpnp_dtype)
23172325
iout = dpnp.empty(10, dtype=dtype)
23182326

23192327
with pytest.raises(ValueError):
2320-
getattr(dpnp, func_name)(ia, out=iout)
2328+
getattr(dpnp, func)(ia, out=iout)
23212329

23222330
@pytest.mark.parametrize(
23232331
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15,)", "(2, 2)"]
23242332
)
2325-
def test_invalid_shape(self, func_params, shape):
2326-
func_name = func_params["func_name"]
2333+
def test_invalid_shape(self, func, shape):
23272334
ia = dpnp.arange(10, dtype=dpnp.float32)
23282335
iout = dpnp.empty(shape, dtype=dpnp.float32)
2329-
assert_raises(ValueError, getattr(dpnp, func_name), ia, out=iout)
2336+
2337+
assert_raises(ValueError, getattr(dpnp, func), ia, out=iout)
23302338

23312339

23322340
class TestHypot:
23332341
@pytest.mark.parametrize(
23342342
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
23352343
)
23362344
def test_hypot(self, dtype):
2337-
a, b, expected = _get_numpy_arrays_2in_1out("hypot", dtype, [0, 10, 10])
2345+
a = generate_random_numpy_array(10, dtype, low=0)
2346+
b = generate_random_numpy_array(10, dtype, low=0)
2347+
expected = numpy.hypot(a, b)
23382348

23392349
ia, ib = dpnp.array(a), dpnp.array(b)
2340-
out_dt = _get_output_data_type(dtype)
2350+
out_dt = map_dtype_to_device(expected.dtype, ia.sycl_device)
23412351
iout = dpnp.empty(expected.shape, dtype=out_dt)
23422352
result = dpnp.hypot(ia, ib, out=iout)
23432353

0 commit comments

Comments
 (0)