From 08fe8bac5fdb329d685b95a9d7b7947b01b13af6 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Fri, 12 Jan 2024 10:32:40 -0600 Subject: [PATCH 1/3] update out keyword elementwise update out keyword elementwise --- doc/reference/math.rst | 1 + doc/reference/ufunc.rst | 2 + dpnp/dpnp_algo/dpnp_elementwise_common.py | 340 +++++++-- dpnp/dpnp_iface_mathematical.py | 2 +- dpnp/dpnp_iface_trigonometric.py | 2 +- tests/test_umath.py | 801 ++++++---------------- 6 files changed, 469 insertions(+), 679 deletions(-) diff --git a/doc/reference/math.rst b/doc/reference/math.rst index 430ce5b6de67..2b8b2befd799 100644 --- a/doc/reference/math.rst +++ b/doc/reference/math.rst @@ -205,6 +205,7 @@ Miscellaneous dpnp.sqrt dpnp.cbrt dpnp.square + dpnp.rsqrt dpnp.abs dpnp.absolute dpnp.fabs diff --git a/doc/reference/ufunc.rst b/doc/reference/ufunc.rst index 4c1e9f4d5d98..025692a0adf8 100644 --- a/doc/reference/ufunc.rst +++ b/doc/reference/ufunc.rst @@ -44,8 +44,10 @@ Math operations dpnp.log1p dpnp.proj dpnp.sqrt + dpnp.cbrt dpnp.square dpnp.reciprocal + dpnp.rsqrt dpnp.gcd dpnp.lcm diff --git a/dpnp/dpnp_algo/dpnp_elementwise_common.py b/dpnp/dpnp_algo/dpnp_elementwise_common.py index b6bf9faf8ace..5f1441f423a0 100644 --- a/dpnp/dpnp_algo/dpnp_elementwise_common.py +++ b/dpnp/dpnp_algo/dpnp_elementwise_common.py @@ -273,7 +273,10 @@ def dpnp_abs(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = abs_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _acos_docstring = """ @@ -314,7 +317,10 @@ def dpnp_acos(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = acos_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _acosh_docstring = """ @@ -355,7 +361,10 @@ def dpnp_acosh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = acosh_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _add_docstring = """ @@ -401,7 +410,10 @@ def dpnp_add(x1, x2, out=None, order="K"): res_usm = add_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _asin_docstring = """ @@ -442,7 +454,10 @@ def dpnp_asin(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = asin_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _asinh_docstring = """ @@ -483,7 +498,10 @@ def dpnp_asinh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = asinh_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _atan_docstring = """ @@ -524,7 +542,10 @@ def dpnp_atan(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = atan_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _atan2_docstring = """ @@ -574,7 +595,10 @@ def dpnp_atan2(x1, x2, out=None, order="K"): res_usm = atan2_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _atanh_docstring = """ @@ -615,7 +639,10 @@ def dpnp_atanh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = atanh_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _bitwise_and_docstring = """ @@ -659,7 +686,10 @@ def dpnp_bitwise_and(x1, x2, out=None, order="K"): res_usm = bitwise_and_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _bitwise_or_docstring = """ @@ -703,7 +733,10 @@ def dpnp_bitwise_or(x1, x2, out=None, order="K"): res_usm = bitwise_or_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _bitwise_xor_docstring = """ @@ -747,7 +780,10 @@ def dpnp_bitwise_xor(x1, x2, out=None, order="K"): res_usm = bitwise_xor_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _cbrt_docstring = """ @@ -787,7 +823,10 @@ def dpnp_cbrt(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = cbrt_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _ceil_docstring = """ @@ -827,7 +866,10 @@ def dpnp_ceil(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = ceil_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _conj_docstring = """ @@ -866,7 +908,10 @@ def dpnp_conj(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = conj_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _copysign_docstring = """ @@ -907,7 +952,10 @@ def dpnp_copysign(x1, x2, out=None, order="K"): res_usm = copysign_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _cos_docstring = """ @@ -947,7 +995,10 @@ def dpnp_cos(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = cos_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _cosh_docstring = """ @@ -987,7 +1038,10 @@ def dpnp_cosh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = cosh_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _divide_docstring = """ @@ -1033,7 +1087,10 @@ def dpnp_divide(x1, x2, out=None, order="K"): res_usm = divide_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _equal_docstring = """ @@ -1073,7 +1130,10 @@ def dpnp_equal(x1, x2, out=None, order="K"): res_usm = equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _exp_docstring = """ @@ -1114,7 +1174,10 @@ def dpnp_exp(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = exp_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _exp2_docstring = """ @@ -1155,7 +1218,10 @@ def dpnp_exp2(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = exp2_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _expm1_docstring = """ @@ -1198,7 +1264,10 @@ def dpnp_expm1(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = expm1_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _floor_docstring = """ @@ -1238,7 +1307,10 @@ def dpnp_floor(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = floor_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _floor_divide_docstring = """ @@ -1282,7 +1354,10 @@ def dpnp_floor_divide(x1, x2, out=None, order="K"): res_usm = floor_divide_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _greater_docstring = """ @@ -1322,7 +1397,10 @@ def dpnp_greater(x1, x2, out=None, order="K"): res_usm = greater_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _greater_equal_docstring = """ @@ -1364,7 +1442,10 @@ def dpnp_greater_equal(x1, x2, out=None, order="K"): res_usm = greater_equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _hypot_docstring = """ @@ -1410,7 +1491,10 @@ def dpnp_hypot(x1, x2, out=None, order="K"): res_usm = hypot_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _imag_docstring = """ @@ -1447,7 +1531,10 @@ def dpnp_imag(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = imag_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _invert_docstring = """ @@ -1482,7 +1569,10 @@ def dpnp_invert(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = invert_func(x_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _isfinite_docstring = """ @@ -1517,7 +1607,10 @@ def dpnp_isfinite(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = isfinite_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _isinf_docstring = """ @@ -1551,7 +1644,10 @@ def dpnp_isinf(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = isinf_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _isnan_docstring = """ @@ -1585,7 +1681,10 @@ def dpnp_isnan(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = isnan_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _left_shift_docstring = """ @@ -1629,7 +1728,10 @@ def dpnp_left_shift(x1, x2, out=None, order="K"): res_usm = left_shift_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _less_docstring = """ @@ -1669,7 +1771,10 @@ def dpnp_less(x1, x2, out=None, order="K"): res_usm = less_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _less_equal_docstring = """ @@ -1711,7 +1816,10 @@ def dpnp_less_equal(x1, x2, out=None, order="K"): res_usm = less_equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _log_docstring = """ @@ -1752,7 +1860,10 @@ def dpnp_log(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _log10_docstring = """ @@ -1793,7 +1904,10 @@ def dpnp_log10(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log10_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _log1p_docstring = """ @@ -1833,7 +1947,10 @@ def dpnp_log1p(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log1p_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _log2_docstring = """ @@ -1874,7 +1991,10 @@ def dpnp_log2(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log2_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _logaddexp_docstring = """ @@ -1922,7 +2042,10 @@ def dpnp_logaddexp(x1, x2, out=None, order="K"): res_usm = logaddexp_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _logical_and_docstring = """ @@ -1963,7 +2086,10 @@ def dpnp_logical_and(x1, x2, out=None, order="K"): res_usm = logical_and_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _logical_not_docstring = """ @@ -1998,7 +2124,10 @@ def dpnp_logical_not(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = logical_not_func(x_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _logical_or_docstring = """ @@ -2039,7 +2168,10 @@ def dpnp_logical_or(x1, x2, out=None, order="K"): res_usm = logical_or_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _logical_xor_docstring = """ @@ -2080,7 +2212,10 @@ def dpnp_logical_xor(x1, x2, out=None, order="K"): res_usm = logical_xor_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _maximum_docstring = """ @@ -2120,7 +2255,10 @@ def dpnp_maximum(x1, x2, out=None, order="K"): res_usm = maximum_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _minimum_docstring = """ @@ -2160,7 +2298,10 @@ def dpnp_minimum(x1, x2, out=None, order="K"): res_usm = minimum_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _multiply_docstring = """ @@ -2210,7 +2351,10 @@ def dpnp_multiply(x1, x2, out=None, order="K"): res_usm = multiply_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _negative_docstring = """ @@ -2250,7 +2394,10 @@ def dpnp_negative(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = negative_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _not_equal_docstring = """ @@ -2292,7 +2439,10 @@ def dpnp_not_equal(x1, x2, out=None, order="K"): res_usm = not_equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _positive_docstring = """ @@ -2331,7 +2481,10 @@ def dpnp_positive(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = positive_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _power_docstring = """ @@ -2378,7 +2531,10 @@ def dpnp_power(x1, x2, out=None, order="K"): res_usm = power_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _proj_docstring = """ @@ -2412,7 +2568,10 @@ def dpnp_proj(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = proj_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _real_docstring = """ @@ -2449,7 +2608,10 @@ def dpnp_real(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = real_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _remainder_docstring = """ @@ -2490,7 +2652,10 @@ def dpnp_remainder(x1, x2, out=None, order="K"): res_usm = remainder_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _right_shift_docstring = """ @@ -2533,7 +2698,10 @@ def dpnp_right_shift(x1, x2, out=None, order="K"): res_usm = right_shift_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _round_docstring = """ @@ -2573,7 +2741,10 @@ def dpnp_round(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = round_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _rsqrt_docstring = """ @@ -2608,7 +2779,10 @@ def dpnp_rsqrt(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = rsqrt_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _sign_docstring = """ @@ -2650,7 +2824,10 @@ def dpnp_sign(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sign_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _signbit_docstring = """ @@ -2685,7 +2862,10 @@ def dpnp_signbit(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = signbit_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _sin_docstring = """ @@ -2725,7 +2905,10 @@ def dpnp_sin(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sin_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _sinh_docstring = """ @@ -2765,7 +2948,10 @@ def dpnp_sinh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sinh_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _sqrt_docstring = """ @@ -2804,7 +2990,10 @@ def dpnp_sqrt(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sqrt_func(x_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _square_docstring = """ @@ -2843,7 +3032,10 @@ def dpnp_square(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = square_func(x_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _subtract_docstring = """ @@ -2904,7 +3096,10 @@ def dpnp_subtract(x1, x2, out=None, order="K"): res_usm = subtract_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _tan_docstring = """ @@ -2944,7 +3139,10 @@ def dpnp_tan(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = tan_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _tanh_docstring = """ @@ -2984,7 +3182,10 @@ def dpnp_tanh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = tanh_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out _trunc_docstring = """ @@ -3026,4 +3227,7 @@ def dpnp_trunc(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = trunc_func(x1_usm, out=out_usm, order=order) - return dpnp_array._create_from_usm_ndarray(res_usm) + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out diff --git a/dpnp/dpnp_iface_mathematical.py b/dpnp/dpnp_iface_mathematical.py index a38fe9b8cfbe..add9c1304ce6 100644 --- a/dpnp/dpnp_iface_mathematical.py +++ b/dpnp/dpnp_iface_mathematical.py @@ -574,7 +574,7 @@ def copysign( Parameters `where`, `dtype` and `subok` are supported with their default values. Keyword argument `kwargs` is currently unsupported. Otherwise the function will be executed sequentially on CPU. - Input array data types are limited by supported real data types. + Input array data types are limited by supported real-valued data types. See Also -------- diff --git a/dpnp/dpnp_iface_trigonometric.py b/dpnp/dpnp_iface_trigonometric.py index 18c874215e43..f21f67f43689 100644 --- a/dpnp/dpnp_iface_trigonometric.py +++ b/dpnp/dpnp_iface_trigonometric.py @@ -437,7 +437,7 @@ def arctan2( Parameters `where`, `dtype` and `subok` are supported with their default values. Keyword arguments `kwargs` are currently unsupported. Otherwise the function will be executed sequentially on CPU. - Input array data types are limited by supported DPNP :ref:`Data types`. + Input array data types are limited by real-valued data types. See Also -------- diff --git a/tests/test_umath.py b/tests/test_umath.py index 35955c935bcb..05c75b51a6ae 100644 --- a/tests/test_umath.py +++ b/tests/test_umath.py @@ -2,15 +2,13 @@ import pytest from numpy.testing import ( assert_allclose, - assert_array_almost_equal, - assert_array_equal, ) import dpnp from .helper import ( + assert_dtype_allclose, get_all_dtypes, - get_complex_dtypes, get_float_dtypes, has_support_aspect16, has_support_aspect64, @@ -93,65 +91,60 @@ def test_umaths(test_cases): assert_allclose(result, expected, rtol=1e-6) -class TestSin: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) - def test_sin(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.sin(dp_array, out=dp_out) - - # original - expected = numpy.sin(np_array, out=np_out) +def _get_numpy_arrays(func_name, dtype, range): + """ + Return a sample array and an output array. + + Create an appropriate array specified by `dtype` and `range` which is used as + an input for a function specified by `func_name` to obtain the output. + """ + low = range[0] + high = range[1] + size = range[2] + if dtype == numpy.bool_: + np_array = numpy.arange(2, dtype=dtype) + result = getattr(numpy, func_name)(np_array) + elif dpnp.issubdtype(dtype, dpnp.complexfloating): + a = numpy.random.uniform(low=low, high=high, size=size) + b = numpy.random.uniform(low=low, high=high, size=size) + np_array = numpy.array(a + 1j * b, dtype=dtype) + result = getattr(numpy, func_name)(np_array) + else: + a = numpy.random.uniform(low=low, high=high, size=size) + np_array = numpy.array(a, dtype=dtype) + result = getattr(numpy, func_name)(np_array) - precision = numpy.finfo(dtype=result.dtype).precision - assert_array_almost_equal(expected, result.asnumpy(), decimal=precision) + return np_array, result - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_sin_complex(self, dtype): - np_array = numpy.arange(10, 20, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - # DPNP - dp_out_dtype = dpnp.complex64 +def _get_output_data_type(dtype): + """Return a data type specified by input `dtype` and device capabilities.""" + if dpnp.issubdtype(dtype, dpnp.bool): + out_dtype = dpnp.float16 if has_support_aspect16() else dpnp.float32 + elif dpnp.issubdtype(dtype, dpnp.complexfloating): + out_dtype = dpnp.complex64 if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.sin(dp_array, out=dp_out) + out_dtype = dpnp.complex128 + else: + out_dtype = dpnp.float32 + if has_support_aspect64() and dtype != dpnp.float32: + out_dtype = dpnp.float64 - # original - expected = numpy.sin(np_array, out=np_out) + return out_dtype - precision = numpy.finfo(dtype=result.dtype).precision - assert_array_almost_equal(expected, result.asnumpy(), decimal=precision) - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.skipif( - not has_support_aspect16(), reason="No fp16 support by device" - ) - def test_sin_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.float16) +class TestSin: + @pytest.mark.parametrize("dtype", get_all_dtypes()) + def test_sin(self, dtype): + np_array, expected = _get_numpy_arrays("sin", dtype, [-5, 5, 10]) - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.sin(dp_array, out=dp_out) - # original - expected = numpy.sin(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", @@ -177,64 +170,17 @@ def test_invalid_shape(self, shape): class TestSinh: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_sinh(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.sinh(dp_array, out=dp_out) - - # original - expected = numpy.sinh(np_array, out=np_out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_sinh_complex(self, dtype): - np_array = numpy.arange(10, 20, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 + np_array, expected = _get_numpy_arrays("sinh", dtype, [-5, 5, 10]) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.sinh(dp_array, out=dp_out) - # original - expected = numpy.sinh(np_array, out=np_out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) - - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.skipif( - not has_support_aspect16(), reason="No fp16 support by device" - ) - def test_sinh_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.float16) - - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) - result = dpnp.sinh(dp_array, out=dp_out) - - # original - expected = numpy.sinh(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", @@ -260,64 +206,17 @@ def test_invalid_shape(self, shape): class TestCos: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_cos(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.cos(dp_array, out=dp_out) - - # original - expected = numpy.cos(np_array, out=np_out) - - precision = numpy.finfo(dtype=result.dtype).precision - assert_array_almost_equal(expected, result.asnumpy(), decimal=precision) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_cos_complex(self, dtype): - np_array = numpy.arange(10, 20, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.cos(dp_array, out=dp_out) - - # original - expected = numpy.cos(np_array, out=np_out) - - precision = numpy.finfo(dtype=result.dtype).precision - assert_array_almost_equal(expected, result.asnumpy(), decimal=precision) - - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.skipif( - not has_support_aspect16(), reason="No fp16 support by device" - ) - def test_cos_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.float16) + np_array, expected = _get_numpy_arrays("cos", dtype, [-5, 5, 10]) - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.cos(dp_array, out=dp_out) - # original - expected = numpy.cos(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", @@ -343,64 +242,17 @@ def test_invalid_shape(self, shape): class TestCosh: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_cosh(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.cosh(dp_array, out=dp_out) - - # original - expected = numpy.cosh(np_array, out=np_out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_cosh_complex(self, dtype): - np_array = numpy.arange(10, 20, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.cosh(dp_array, out=dp_out) - - # original - expected = numpy.cosh(np_array, out=np_out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + np_array, expected = _get_numpy_arrays("cosh", dtype, [-5, 5, 10]) - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.skipif( - not has_support_aspect16(), reason="No fp16 support by device" - ) - def test_cosh_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.float16) - - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.cosh(dp_array, out=dp_out) - # original - expected = numpy.cosh(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", @@ -425,65 +277,19 @@ def test_invalid_shape(self, shape): dpnp.cosh(dp_array, out=dp_out) -class TestsLog: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) - def test_log(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.log(dp_array, out=dp_out) - - # original - expected = numpy.log(np_array, out=np_out) - - precision = numpy.finfo(dtype=result.dtype).precision - assert_array_almost_equal(expected, result.asnumpy(), decimal=precision) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_log_complex(self, dtype): - np_array = numpy.arange(10, 20, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.log(dp_array, out=dp_out) - - # original - expected = numpy.log(np_array, out=np_out) - - precision = numpy.finfo(dtype=result.dtype).precision - assert_array_almost_equal(expected, result.asnumpy(), decimal=precision) - +class TestLog: @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.skipif( - not has_support_aspect16(), reason="No fp16 support by device" - ) - def test_log_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.float16) + @pytest.mark.parametrize("dtype", get_all_dtypes()) + def test_log(self, dtype): + np_array, expected = _get_numpy_arrays("log", dtype, [0.1, 10, 10]) - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.log(dp_array, out=dp_out) - # original - expected = numpy.log(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", @@ -509,50 +315,17 @@ def test_invalid_shape(self, shape): class TestExp: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_exp(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_array = dpnp.array(np_array, dtype=dp_out_dtype) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - result = dpnp.exp(dp_array, out=dp_out) - - # original - expected = numpy.exp(np_array, out=np_out) + np_array, expected = _get_numpy_arrays("exp", dtype, [-3, 8, 10]) - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_exp_complex(self, dtype): - x1 = numpy.linspace(0, 8, num=10) - x2 = numpy.linspace(0, 6, num=10) - Xnp = x1 + 1j * x2 - np_array = numpy.asarray(Xnp, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_array = dpnp.array(np_array, dtype=dp_out_dtype) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.exp(dp_array, out=dp_out) - # original - expected = numpy.exp(np_array, out=np_out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -578,50 +351,17 @@ def test_invalid_shape(self, shape, dtype): class TestExp2: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_exp2(self, dtype): - np_array = numpy.arange(7, dtype=dtype) - np_out = numpy.empty(7, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 + np_array, expected = _get_numpy_arrays("exp2", dtype, [-3, 8, 10]) - dp_array = dpnp.array(np_array, dtype=dp_out_dtype) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.exp2(dp_array, out=dp_out) - # original - expected = numpy.exp2(np_array, out=np_out) - - tol = dpnp.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_exp2_complex(self, dtype): - x1 = numpy.linspace(0, 8, num=10) - x2 = numpy.linspace(0, 6, num=10) - Xnp = x1 + 1j * x2 - np_array = numpy.asarray(Xnp, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_array = dpnp.array(np_array, dtype=dp_out_dtype) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - result = dpnp.exp2(dp_array, out=dp_out) - - # original - expected = numpy.exp2(np_array, out=np_out) - - tol = dpnp.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -647,27 +387,17 @@ def test_invalid_shape(self, shape, dtype): class TestCbrt: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) def test_cbrt(self, dtype): - np_array = numpy.arange(7, dtype=dtype) - np_out = numpy.empty(7, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 + np_array, expected = _get_numpy_arrays("cbrt", dtype, [-5, 5, 10]) - dp_array = dpnp.array(np_array, dtype=dp_out_dtype) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.cbrt(dp_array, out=dp_out) - # original - expected = numpy.cbrt(np_array, out=np_out) - - tol = dpnp.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -693,27 +423,19 @@ def test_invalid_shape(self, shape, dtype): class TestRsqrt: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) + @pytest.mark.usefixtures("suppress_divide_numpy_warnings") + @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) def test_rsqrt(self, dtype): - np_array = numpy.arange(1, 10, dtype=dtype) - np_out = numpy.empty(9, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 + np_array, expected = _get_numpy_arrays("sqrt", dtype, [0, 10, 10]) + expected = numpy.reciprocal(expected) - dp_array = dpnp.array(np_array, dtype=dp_out_dtype) - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.rsqrt(dp_array, out=dp_out) - # original - expected = numpy.reciprocal(numpy.sqrt(np_array), out=np_out) - - tol = dpnp.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -739,23 +461,17 @@ def test_invalid_shape(self, shape, dtype): class TestArccos: - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_arccos(self, dtype): - array_data = numpy.arange(-9, 10, 2) / 10 - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("arccos", dtype, [-1, 1, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.arccos(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arccos(np_array, out=out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -781,23 +497,18 @@ def test_invalid_shape(self, shape, dtype): class TestArccosh: - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_arccosh(self, dtype): - array_data = numpy.arange(2, 12) - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("arccosh", dtype, [1, 10, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.arccosh(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arccosh(np_array, out=out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -823,23 +534,17 @@ def test_invalid_shape(self, shape, dtype): class TestArcsin: - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_arcsin(self, dtype): - array_data = numpy.arange(-9, 10, 2) / 10 - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("arcsin", dtype, [-1, 1, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.arcsin(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arcsin(np_array, out=out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -865,23 +570,17 @@ def test_invalid_shape(self, shape, dtype): class TestArcsinh: - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_arcsinh(self, dtype): - array_data = numpy.arange(10) - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("arcsinh", dtype, [-5, 5, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.arcsinh(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arcsinh(np_array, out=out) - - tol = numpy.finfo(dtype=result.dtype).resolution - assert_allclose(expected, result.asnumpy(), rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -907,22 +606,17 @@ def test_invalid_shape(self, shape, dtype): class TestArctan: - @pytest.mark.parametrize("dtype", get_float_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_arctan(self, dtype): - array_data = numpy.arange(10) - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("arctan", dtype, [-5, 5, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.arctan(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arctan(np_array, out=out) - - tol = numpy.finfo(dtype).resolution - assert_allclose(expected, result, tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -948,22 +642,18 @@ def test_invalid_shape(self, shape, dtype): class TestArctanh: - @pytest.mark.parametrize("dtype", get_float_dtypes()) + @pytest.mark.usefixtures("suppress_divide_numpy_warnings") + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_arctanh(self, dtype): - array_data = numpy.arange(-9, 10, 2) / 10 - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("arctanh", dtype, [-1, 1, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.arctanh(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arctanh(np_array, out=out) - - tol = numpy.finfo(dtype).resolution - assert_allclose(expected, result, tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -989,22 +679,17 @@ def test_invalid_shape(self, shape, dtype): class TestTan: - @pytest.mark.parametrize("dtype", get_float_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_tan(self, dtype): - array_data = numpy.arange(10) - out = numpy.empty(10, dtype=dtype) + np_array, expected = _get_numpy_arrays("tan", dtype, [-5, 5, 10]) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.tan(dp_array, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.tan(np_array, out=out) - - tol = numpy.finfo(dtype).resolution - assert_allclose(expected, result, rtol=tol) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] @@ -1030,41 +715,20 @@ def test_invalid_shape(self, shape, dtype): class TestArctan2: - @pytest.mark.parametrize("dtype", get_float_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) def test_arctan2(self, dtype): - array_data = numpy.arange(10) - out = numpy.empty(10, dtype=dtype) - - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) - result = dpnp.arctan2(dp_array, dp_array, out=dp_out) - - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.arctan2(np_array, np_array, out=out) + np_array1, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) + np_array2, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) + expected = numpy.arctan2(np_array1, np_array2) - assert_allclose(expected, result) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True) - ) - def test_out_dtypes(self, dtype): - if has_support_aspect64() and dtype != numpy.float32: - dtype_out = numpy.float64 - else: - dtype_out = numpy.float32 - size = 2 if dtype == dpnp.bool else 10 + dp_array1 = dpnp.array(np_array1) + dp_array2 = dpnp.array(np_array2) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) + result = dpnp.arctan2(dp_array1, dp_array2, out=dp_out) - np_array = numpy.arange(size, dtype=dtype) - np_out = numpy.empty(size, dtype=dtype_out) - expected = numpy.arctan2(np_array, np_array, out=np_out) - - dp_array = dpnp.arange(size, dtype=dtype) - dp_out = dpnp.empty(size, dtype=dtype_out) - result = dpnp.arctan2(dp_array, dp_array, out=dp_out) - - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( @@ -1079,41 +743,20 @@ def test_invalid_shape(self, shape, dtype): class TestCopySign: - @pytest.mark.parametrize("dtype", get_float_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) def test_copysign(self, dtype): - array_data = numpy.arange(10) - out = numpy.empty(10, dtype=dtype) + np_array1, _ = _get_numpy_arrays("array", dtype, [1, 10, 10]) + np_array2, _ = _get_numpy_arrays("array", dtype, [-10, -1, 10]) + expected = numpy.copysign(np_array1, np_array2) - # DPNP - dp_array = dpnp.array(array_data, dtype=dtype) - dp_out = dpnp.array(out, dtype=dtype) - result = dpnp.copysign(dp_array, -dp_array, out=dp_out) + dp_array1 = dpnp.array(np_array1) + dp_array2 = dpnp.array(np_array2) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) + result = dpnp.copysign(dp_array1, dp_array2, out=dp_out) - # original - np_array = numpy.array(array_data, dtype=dtype) - expected = numpy.copysign(np_array, -np_array, out=out) - - assert_allclose(expected, result) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True) - ) - def test_out_dtypes(self, dtype): - if has_support_aspect64() and dtype != numpy.float32: - dtype_out = numpy.float64 - else: - dtype_out = numpy.float32 - size = 2 if dtype == dpnp.bool else 10 - - np_array = numpy.arange(size, dtype=dtype) - np_out = numpy.empty(size, dtype=dtype_out) - expected = numpy.copysign(np_array, -np_array, out=np_out) - - dp_array = dpnp.arange(size, dtype=dtype) - dp_out = dpnp.empty(size, dtype=dtype_out) - result = dpnp.copysign(dp_array, -dp_array, out=dp_out) - - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( @@ -1128,60 +771,17 @@ def test_invalid_shape(self, shape, dtype): class TestSqrt: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_complex=True) - ) - def test_sqrt_int_float(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float64) - - # DPNP - dp_out_dtype = dpnp.float32 - if has_support_aspect64() and dtype != dpnp.float32: - dp_out_dtype = dpnp.float64 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.sqrt(dp_array, out=dp_out) - - # original - expected = numpy.sqrt(np_array, out=np_out) - assert_allclose(expected, result) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_sqrt_complex(self, dtype): - np_array = numpy.arange(10, 20, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.complex128) - - # DPNP - dp_out_dtype = dpnp.complex64 - if has_support_aspect64() and dtype != dpnp.complex64: - dp_out_dtype = dpnp.complex128 - - dp_out = dpnp.array(np_out, dtype=dp_out_dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.sqrt(dp_array, out=dp_out) + @pytest.mark.parametrize("dtype", get_all_dtypes()) + def test_sqrt(self, dtype): + np_array, expected = _get_numpy_arrays("sqrt", dtype, [0, 10, 10]) - # original - expected = numpy.sqrt(np_array, out=np_out) - assert_allclose(expected, result) - - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.skipif( - not has_support_aspect16(), reason="No fp16 support by device" - ) - def test_sqrt_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.float16) - - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) + dp_array = dpnp.array(np_array) + out_dtype = _get_output_data_type(dtype) + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.sqrt(dp_array, out=dp_out) - # original - expected = numpy.sqrt(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", [numpy.int64, numpy.int32], ids=["numpy.int64", "numpy.int32"] @@ -1216,34 +816,17 @@ def test_invalid_out(self, out): class TestSquare: - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_none=True) - ) + @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_square(self, dtype): - np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=dtype) - - # DPNP - dp_out = dpnp.array(np_out, dtype=dtype) - dp_array = dpnp.array(np_array, dtype=dtype) - result = dpnp.square(dp_array, out=dp_out) - - # original - expected = numpy.square(np_array, out=np_out) - assert_allclose(expected, result) - - def test_square_bool(self): - np_array = numpy.arange(2, dtype=numpy.bool_) - np_out = numpy.empty(2, dtype=numpy.int8) + np_array, expected = _get_numpy_arrays("square", dtype, [-5, 5, 10]) - # DPNP - dp_array = dpnp.array(np_array, dtype=np_array.dtype) - dp_out = dpnp.array(np_out, dtype=np_out.dtype) + dp_array = dpnp.array(np_array) + out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype + dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.square(dp_array, out=dp_out) - # original - expected = numpy.square(np_array, out=np_out) - assert_allclose(expected, result) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_bool=True, no_none=True) From b6f4b4b51ba03cbb50e6979e3e1e846847ddab25 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 15 Jan 2024 10:43:36 -0600 Subject: [PATCH 2/3] improve test coverage --- dpnp/dpnp_iface_trigonometric.py | 2 +- tests/test_absolute.py | 18 +- tests/test_bitwise.py | 14 + tests/test_logic.py | 21 +- tests/test_mathematical.py | 68 ++- tests/test_umath.py | 690 +++++++------------------------ 6 files changed, 255 insertions(+), 558 deletions(-) diff --git a/dpnp/dpnp_iface_trigonometric.py b/dpnp/dpnp_iface_trigonometric.py index f21f67f43689..c293707e2b9f 100644 --- a/dpnp/dpnp_iface_trigonometric.py +++ b/dpnp/dpnp_iface_trigonometric.py @@ -1305,7 +1305,7 @@ def logaddexp( Parameters `where`, `dtype` and `subok` are supported with their default values. Keyword arguments `kwargs` are currently unsupported. Otherwise the function will be executed sequentially on CPU. - Input array data types are limited by supported DPNP :ref:`Data types`. + Input array data types are limited by real-valued data types. See Also -------- diff --git a/tests/test_absolute.py b/tests/test_absolute.py index aac8a71ca585..1c6a42e98c64 100644 --- a/tests/test_absolute.py +++ b/tests/test_absolute.py @@ -1,10 +1,15 @@ import numpy import pytest -from numpy.testing import assert_array_equal, assert_equal +from numpy.testing import assert_equal import dpnp -from .helper import get_all_dtypes, get_complex_dtypes, get_float_complex_dtypes +from .helper import ( + assert_dtype_allclose, + get_all_dtypes, + get_complex_dtypes, + get_float_complex_dtypes, +) @pytest.mark.parametrize("func", ["abs", "absolute"]) @@ -15,8 +20,13 @@ def test_abs(func, dtype): result = getattr(dpnp, func)(ia) expected = getattr(numpy, func)(a) - assert_array_equal(expected, result) - assert_equal(result.dtype, expected.dtype) + assert_dtype_allclose(result, expected) + + # out keyword + dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, func)(ia, out=dp_out) + assert result is dp_out + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("stride", [-4, -2, -1, 1, 2, 4]) diff --git a/tests/test_bitwise.py b/tests/test_bitwise.py index f8484eaecb5b..e3393e43beeb 100644 --- a/tests/test_bitwise.py +++ b/tests/test_bitwise.py @@ -4,6 +4,8 @@ import dpnp as inp +from .helper import assert_dtype_allclose, get_integer_dtypes + @pytest.mark.parametrize( "lhs", @@ -134,3 +136,15 @@ def test_right_shift(self, lhs, rhs, dtype): dp_a >>= dp_b np_a >>= np_b assert_array_equal(dp_a, np_a) + + +@pytest.mark.parametrize("dtype", get_integer_dtypes()) +def test_invert_out(dtype): + np_a = numpy.arange(-5, 5, dtype=dtype) + dp_a = inp.array(np_a) + + expected = numpy.invert(np_a) + dp_out = inp.empty(expected.shape, dtype=expected.dtype) + result = inp.invert(dp_a, out=dp_out) + assert result is dp_out + assert_dtype_allclose(result, expected) diff --git a/tests/test_logic.py b/tests/test_logic.py index b9d2a9b4303a..1e110b4c717b 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -250,6 +250,11 @@ def test_logical_not(dtype): dpnp_res = dpnp.logical_not(a) assert_equal(dpnp_res, np_res) + dp_out = dpnp.empty(np_res.shape, dtype=dpnp.bool) + dpnp_res = dpnp.logical_not(a, out=dp_out) + assert dpnp_res is dp_out + assert_equal(dpnp_res, np_res) + @pytest.mark.parametrize( "op", @@ -316,6 +321,13 @@ def test_elemwise_comparison(op, x1, x2, dtype): dpnp_res = getattr(dpnp, op)(dp_x1[::-1], dp_x2) assert_equal(dpnp_res, np_res) + # out keyword + np_res = getattr(numpy, op)(np_x1, np_x2) + dp_out = dpnp.empty(np_res.shape, dtype=dpnp.bool) + dpnp_res = getattr(dpnp, op)(dp_x1, dp_x2, out=dp_out) + assert dp_out is dpnp_res + assert_equal(dpnp_res, np_res) + @pytest.mark.parametrize( "op", @@ -374,6 +386,11 @@ def test_comparison_no_broadcast_with_shapes(op, sh1, sh2): @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) def test_finite(op, data, dtype): x = dpnp.asarray(data, dtype=dtype) - np_res = getattr(dpnp, op)(x) - dpnp_res = getattr(numpy, op)(x.asnumpy()) + np_res = getattr(numpy, op)(x.asnumpy()) + dpnp_res = getattr(dpnp, op)(x) + assert_equal(dpnp_res, np_res) + + dp_out = dpnp.empty(np_res.shape, dtype=dpnp.bool) + dpnp_res = getattr(dpnp, op)(x, out=dp_out) + assert dp_out is dpnp_res assert_equal(dpnp_res, np_res) diff --git a/tests/test_mathematical.py b/tests/test_mathematical.py index 6c95093a01b9..86c31dc25ae3 100644 --- a/tests/test_mathematical.py +++ b/tests/test_mathematical.py @@ -660,6 +660,17 @@ def test_negative(data, dtype): expected = numpy.negative(np_a) assert_allclose(result, expected) + result = -dpnp_a + expected = -np_a + assert_allclose(result, expected) + + # out keyword + if dtype is not None: + dp_out = dpnp.empty(expected.shape, dtype=dtype) + result = dpnp.negative(dpnp_a, out=dp_out) + assert result is dp_out + assert_allclose(result, expected) + def test_negative_boolean(): dpnp_a = dpnp.array([True, False]) @@ -686,6 +697,13 @@ def test_positive(data, dtype): expected = +np_a assert_allclose(result, expected) + # out keyword + if dtype is not None: + dp_out = dpnp.empty(expected.shape, dtype=dtype) + result = dpnp.positive(dpnp_a, out=dp_out) + assert result is dp_out + assert_allclose(result, expected) + def test_positive_boolean(): dpnp_a = dpnp.array([True, False]) @@ -811,7 +829,14 @@ def test_sign(data, dtype): result = dpnp.sign(dpnp_a) expected = numpy.sign(np_a) - assert_allclose(result, expected) + assert_dtype_allclose(result, expected) + + # out keyword + if dtype is not None: + dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) + result = dpnp.sign(dpnp_a, out=dp_out) + assert dp_out is result + assert_dtype_allclose(result, expected) def test_sign_boolean(): @@ -833,9 +858,20 @@ def test_signbit(data, dtype): result = dpnp.signbit(dpnp_a) expected = numpy.signbit(np_a) - assert_allclose(result, expected) + assert_dtype_allclose(result, expected) + + # out keyword + dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) + result = dpnp.signbit(dpnp_a, out=dp_out) + assert dp_out is result + assert_dtype_allclose(result, expected) +@pytest.mark.parametrize( + "func", + ["real", "imag", "conj"], + ids=["real", "imag", "conj"], +) @pytest.mark.parametrize( "data", [complex(-1, -4), complex(-1, 2), complex(3, -7), complex(4, 12)], @@ -847,17 +883,20 @@ def test_signbit(data, dtype): ], ) @pytest.mark.parametrize("dtype", get_complex_dtypes()) -def test_real_imag(data, dtype): +def test_complex_funcs(func, data, dtype): np_a = numpy.array(data, dtype=dtype) dpnp_a = dpnp.array(data, dtype=dtype) - result = dpnp.real(dpnp_a) - expected = numpy.real(np_a) - assert_allclose(result, expected) + result = getattr(dpnp, func)(dpnp_a) + expected = getattr(numpy, func)(np_a) + assert_dtype_allclose(result, expected) - result = dpnp.imag(dpnp_a) - expected = numpy.imag(np_a) - assert_allclose(result, expected) + # out keyword + if func == "conj": + dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, func)(dpnp_a, out=dp_out) + assert dp_out is result + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_complex_dtypes()) @@ -875,9 +914,16 @@ def test_projection_infinity(dtype): complex(dpnp.inf, 0.0), ] - result = dpnp.proj(dpnp.array(X, dtype=dtype)) + a = dpnp.array(X, dtype=dtype) + result = dpnp.proj(a) expected = dpnp.array(Y, dtype=dtype) - assert_allclose(result, expected) + assert_dtype_allclose(result, expected) + + # out keyword + dp_out = dpnp.empty(expected.shape, dtype=expected.dtype) + result = dpnp.proj(a, out=dp_out) + assert dp_out is result + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_all_dtypes()) diff --git a/tests/test_umath.py b/tests/test_umath.py index 05c75b51a6ae..d9bbacdf4ef3 100644 --- a/tests/test_umath.py +++ b/tests/test_umath.py @@ -133,257 +133,100 @@ def _get_output_data_type(dtype): return out_dtype -class TestSin: +class TestUmath: + @pytest.fixture( + params=[ + {"func_name": "arccos", "input_values": [-1, 1, 10]}, + {"func_name": "arccosh", "input_values": [1, 10, 10]}, + {"func_name": "arcsin", "input_values": [-1, 1, 10]}, + {"func_name": "arcsinh", "input_values": [-5, 5, 10]}, + {"func_name": "arctan", "input_values": [-5, 5, 10]}, + {"func_name": "arctanh", "input_values": [-1, 1, 10]}, + {"func_name": "cos", "input_values": [-5, 5, 10]}, + {"func_name": "cosh", "input_values": [-5, 5, 10]}, + {"func_name": "exp", "input_values": [-3, 8, 10]}, + {"func_name": "exp2", "input_values": [-5, 5, 10]}, + {"func_name": "expm1", "input_values": [-5, 5, 10]}, + {"func_name": "log", "input_values": [0, 10, 10]}, + {"func_name": "log10", "input_values": [0, 10, 10]}, + {"func_name": "log2", "input_values": [0, 10, 10]}, + {"func_name": "log1p", "input_values": [0, 10, 10]}, + {"func_name": "sin", "input_values": [-5, 5, 10]}, + {"func_name": "sinh", "input_values": [-5, 5, 10]}, + {"func_name": "sqrt", "input_values": [0, 10, 10]}, + {"func_name": "tan", "input_values": [-1.5, 1.5, 10]}, + {"func_name": "tanh", "input_values": [-5, 5, 10]}, + ], + ids=[ + "arccos", + "arccosh", + "arcsin", + "arcsinh", + "arctan", + "arctanh", + "cos", + "cosh", + "exp", + "exp2", + "expm1", + "log", + "log10", + "log2", + "log1p", + "sin", + "sinh", + "sqrt", + "tan", + "tnah", + ], + ) + def func_params(self, request): + return request.param + + @pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings") @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_sin(self, dtype): - np_array, expected = _get_numpy_arrays("sin", dtype, [-5, 5, 10]) + def test_out(self, func_params, dtype): + func_name = func_params["func_name"] + input_values = func_params["input_values"] + np_array, expected = _get_numpy_arrays(func_name, dtype, input_values) dp_array = dpnp.array(np_array) out_dtype = _get_output_data_type(dtype) dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.sin(dp_array, out=dp_out) + result = getattr(dpnp, func_name)(dp_array, out=dp_out) assert result is dp_out assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( - "dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=["numpy.float32", "numpy.int64", "numpy.int32"], - ) - def test_invalid_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.complex64) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.sin(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10) - dp_out = dpnp.empty(shape, dtype=dp_array.dtype) - - with pytest.raises(ValueError): - dpnp.sin(dp_array, out=dp_out) - - -class TestSinh: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_sinh(self, dtype): - np_array, expected = _get_numpy_arrays("sinh", dtype, [-5, 5, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.sinh(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=["numpy.float32", "numpy.int64", "numpy.int32"], - ) - def test_invalid_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.complex64) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.sinh(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10) - dp_out = dpnp.empty(shape, dtype=dp_array.dtype) - - with pytest.raises(ValueError): - dpnp.sinh(dp_array, out=dp_out) - - -class TestCos: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_cos(self, dtype): - np_array, expected = _get_numpy_arrays("cos", dtype, [-5, 5, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.cos(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=["numpy.float32", "numpy.int64", "numpy.int32"], - ) - def test_invalid_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.complex64) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.cos(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10) - dp_out = dpnp.empty(shape, dtype=dp_array.dtype) - - with pytest.raises(ValueError): - dpnp.cos(dp_array, out=dp_out) - - -class TestCosh: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_cosh(self, dtype): - np_array, expected = _get_numpy_arrays("cosh", dtype, [-5, 5, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.cosh(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=["numpy.float32", "numpy.int64", "numpy.int32"], - ) - def test_invalid_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.complex64) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.cosh(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10) - dp_out = dpnp.empty(shape, dtype=dp_array.dtype) - - with pytest.raises(ValueError): - dpnp.cosh(dp_array, out=dp_out) - - -class TestLog: - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_log(self, dtype): - np_array, expected = _get_numpy_arrays("log", dtype, [0.1, 10, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.log(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=["numpy.float32", "numpy.int64", "numpy.int32"], - ) - def test_invalid_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.complex64) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.log(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10) - dp_out = dpnp.empty(shape, dtype=dp_array.dtype) - - with pytest.raises(ValueError): - dpnp.log(dp_array, out=dp_out) - - -class TestExp: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_exp(self, dtype): - np_array, expected = _get_numpy_arrays("exp", dtype, [-3, 8, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.exp(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)[:-1]) + def test_invalid_dtype(self, func_params, dtype): + func_name = func_params["func_name"] + dpnp_dtype = get_all_dtypes(no_none=True)[-1] dp_array = dpnp.arange(10, dtype=dpnp_dtype) dp_out = dpnp.empty(10, dtype=dtype) with pytest.raises(TypeError): - dpnp.exp(dp_array, out=dp_out) + getattr(dpnp, func_name)(dp_array, out=dp_out) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - + def test_invalid_shape(self, func_params, shape): + func_name = func_params["func_name"] + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): - dpnp.exp(dp_array, out=dp_out) - - -class TestExp2: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_exp2(self, dtype): - np_array, expected = _get_numpy_arrays("exp2", dtype, [-3, 8, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.exp2(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) + getattr(dpnp, func_name)(dp_array, out=dp_out) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.exp2(dp_array, out=dp_out) - - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.exp2(dp_array, out=dp_out) + def test_invalid_out(self, func_params, out): + func_name = func_params["func_name"] + a = dpnp.arange(10) + numpy.testing.assert_raises(TypeError, getattr(dpnp, func_name), a, out) class TestCbrt: @@ -410,17 +253,25 @@ def test_invalid_dtype(self, dtype): with pytest.raises(TypeError): dpnp.cbrt(dp_array, out=dp_out) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) + def test_invalid_shape(self, shape): + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): dpnp.cbrt(dp_array, out=dp_out) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], + ) + def test_invalid_out(self, out): + a = dpnp.arange(10) + numpy.testing.assert_raises(TypeError, dpnp.cbrt, a, out) + class TestRsqrt: @pytest.mark.usefixtures("suppress_divide_numpy_warnings") @@ -448,245 +299,80 @@ def test_invalid_dtype(self, dtype): with pytest.raises(TypeError): dpnp.rsqrt(dp_array, out=dp_out) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - + def test_invalid_shape(self, shape): + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): dpnp.rsqrt(dp_array, out=dp_out) - -class TestArccos: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_arccos(self, dtype): - np_array, expected = _get_numpy_arrays("arccos", dtype, [-1, 1, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arccos(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.arccos(dp_array, out=dp_out) - - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.arccos(dp_array, out=dp_out) - - -class TestArccosh: - @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_arccosh(self, dtype): - np_array, expected = _get_numpy_arrays("arccosh", dtype, [1, 10, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arccosh(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.arccosh(dp_array, out=dp_out) - - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.arccosh(dp_array, out=dp_out) - - -class TestArcsin: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_arcsin(self, dtype): - np_array, expected = _get_numpy_arrays("arcsin", dtype, [-1, 1, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arcsin(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.arcsin(dp_array, out=dp_out) - - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.arcsin(dp_array, out=dp_out) - - -class TestArcsinh: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_arcsinh(self, dtype): - np_array, expected = _get_numpy_arrays("arcsinh", dtype, [-5, 5, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arcsinh(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.arcsinh(dp_array, out=dp_out) - - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.arcsinh(dp_array, out=dp_out) + def test_invalid_out(self, out): + a = dpnp.arange(10) + numpy.testing.assert_raises(TypeError, dpnp.rsqrt, a, out) -class TestArctan: +class TestSquare: @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_arctan(self, dtype): - np_array, expected = _get_numpy_arrays("arctan", dtype, [-5, 5, 10]) + def test_square(self, dtype): + np_array, expected = _get_numpy_arrays("square", dtype, [-5, 5, 10]) dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) + out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arctan(dp_array, out=dp_out) + result = dpnp.square(dp_array, out=dp_out) assert result is dp_out assert_dtype_allclose(result, expected) - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)[:-1]) def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] + dpnp_dtype = get_all_dtypes(no_none=True)[-1] dp_array = dpnp.arange(10, dtype=dpnp_dtype) dp_out = dpnp.empty(10, dtype=dtype) with pytest.raises(TypeError): - dpnp.arctan(dp_array, out=dp_out) + dpnp.square(dp_array, out=dp_out) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - + def test_invalid_shape(self, shape): + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): - dpnp.arctan(dp_array, out=dp_out) - - -class TestArctanh: - @pytest.mark.usefixtures("suppress_divide_numpy_warnings") - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_arctanh(self, dtype): - np_array, expected = _get_numpy_arrays("arctanh", dtype, [-1, 1, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arctanh(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] - ) - def test_invalid_dtype(self, dtype): - dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] - dp_array = dpnp.arange(10, dtype=dpnp_dtype) - dp_out = dpnp.empty(10, dtype=dtype) - - with pytest.raises(TypeError): - dpnp.arctanh(dp_array, out=dp_out) + dpnp.square(dp_array, out=dp_out) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) + def test_invalid_out(self, out): + a = dpnp.arange(10) - with pytest.raises(ValueError): - dpnp.arctanh(dp_array, out=dp_out) + numpy.testing.assert_raises(TypeError, dpnp.square, a, out) + numpy.testing.assert_raises(TypeError, numpy.square, a.asnumpy(), out) -class TestTan: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_tan(self, dtype): - np_array, expected = _get_numpy_arrays("tan", dtype, [-5, 5, 10]) +class TestArctan2: + @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + def test_arctan2(self, dtype): + np_array1, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) + np_array2, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) + expected = numpy.arctan2(np_array1, np_array2) - dp_array = dpnp.array(np_array) + dp_array1 = dpnp.array(np_array1) + dp_array2 = dpnp.array(np_array2) out_dtype = _get_output_data_type(dtype) dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.tan(dp_array, out=dp_out) + result = dpnp.arctan2(dp_array1, dp_array2, out=dp_out) assert result is dp_out assert_dtype_allclose(result, expected) @@ -700,44 +386,14 @@ def test_invalid_dtype(self, dtype): dp_out = dpnp.empty(10, dtype=dtype) with pytest.raises(TypeError): - dpnp.tan(dp_array, out=dp_out) - - @pytest.mark.parametrize("dtype", get_float_dtypes()) - @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.tan(dp_array, out=dp_out) - - -class TestArctan2: - @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) - def test_arctan2(self, dtype): - np_array1, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) - np_array2, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) - expected = numpy.arctan2(np_array1, np_array2) - - dp_array1 = dpnp.array(np_array1) - dp_array2 = dpnp.array(np_array2) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.arctan2(dp_array1, dp_array2, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) + dpnp.arctan2(dp_array, dp_array, out=dp_out) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - + def test_invalid_shape(self, shape): + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): dpnp.arctan2(dp_array, dp_array, out=dp_out) @@ -758,103 +414,57 @@ def test_copysign(self, dtype): assert result is dp_out assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_float_dtypes()) @pytest.mark.parametrize( - "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] - ) - def test_invalid_shape(self, shape, dtype): - dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(shape, dtype=dtype) - - with pytest.raises(ValueError): - dpnp.copysign(dp_array, dp_array, out=dp_out) - - -class TestSqrt: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_sqrt(self, dtype): - np_array, expected = _get_numpy_arrays("sqrt", dtype, [0, 10, 10]) - - dp_array = dpnp.array(np_array) - out_dtype = _get_output_data_type(dtype) - dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.sqrt(dp_array, out=dp_out) - - assert result is dp_out - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize( - "dtype", [numpy.int64, numpy.int32], ids=["numpy.int64", "numpy.int32"] + "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] ) def test_invalid_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.float32) + dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] + dp_array = dpnp.arange(10, dtype=dpnp_dtype) dp_out = dpnp.empty(10, dtype=dtype) - with pytest.raises(TypeError): - dpnp.sqrt(dp_array, out=dp_out) + dpnp.copysign(dp_array, dp_array, out=dp_out) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10, dtype=dpnp.float32) - dp_out = dpnp.empty(shape, dtype=dpnp.float32) - + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): - dpnp.sqrt(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "out", - [4, (), [], (3, 7), [2, 4]], - ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], - ) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - numpy.testing.assert_raises(TypeError, dpnp.sqrt, a, out) - numpy.testing.assert_raises(TypeError, numpy.sqrt, a.asnumpy(), out) + dpnp.copysign(dp_array, dp_array, out=dp_out) -class TestSquare: - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_square(self, dtype): - np_array, expected = _get_numpy_arrays("square", dtype, [-5, 5, 10]) +class TestLogaddexp: + @pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True)) + def test_logaddexp(self, dtype): + np_array1, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) + np_array2, _ = _get_numpy_arrays("array", dtype, [-5, 5, 10]) + expected = numpy.logaddexp(np_array1, np_array2) - dp_array = dpnp.array(np_array) - out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype + dp_array1 = dpnp.array(np_array1) + dp_array2 = dpnp.array(np_array2) + out_dtype = _get_output_data_type(dtype) dp_out = dpnp.empty(expected.shape, dtype=out_dtype) - result = dpnp.square(dp_array, out=dp_out) + result = dpnp.logaddexp(dp_array1, dp_array2, out=dp_out) assert result is dp_out assert_dtype_allclose(result, expected) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_bool=True, no_none=True) + "dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1] ) def test_invalid_dtype(self, dtype): - dp_array = dpnp.ones(10, dtype=dpnp.bool) + dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1] + dp_array = dpnp.arange(10, dtype=dpnp_dtype) dp_out = dpnp.empty(10, dtype=dtype) - with pytest.raises(TypeError): - dpnp.square(dp_array, out=dp_out) + dpnp.logaddexp(dp_array, dp_array, out=dp_out) @pytest.mark.parametrize( "shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"] ) def test_invalid_shape(self, shape): - dp_array = dpnp.arange(10, dtype=dpnp.float32) - dp_out = dpnp.empty(shape, dtype=dpnp.float32) - + dp_array = dpnp.arange(10) + dp_out = dpnp.empty(shape) with pytest.raises(ValueError): - dpnp.square(dp_array, out=dp_out) - - @pytest.mark.parametrize( - "out", - [4, (), [], (3, 7), [2, 4]], - ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], - ) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - numpy.testing.assert_raises(TypeError, dpnp.square, a, out) - numpy.testing.assert_raises(TypeError, numpy.square, a.asnumpy(), out) + dpnp.logaddexp(dp_array, dp_array, out=dp_out) From 50374ba904bd8cf6559b3fc6977aa6038ead688e Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Wed, 17 Jan 2024 08:06:03 -0600 Subject: [PATCH 3/3] address comments --- dpnp/dpnp_algo/dpnp_elementwise_common.py | 347 +++++----------------- 1 file changed, 75 insertions(+), 272 deletions(-) diff --git a/dpnp/dpnp_algo/dpnp_elementwise_common.py b/dpnp/dpnp_algo/dpnp_elementwise_common.py index 5f1441f423a0..a3ac7a8f0a6c 100644 --- a/dpnp/dpnp_algo/dpnp_elementwise_common.py +++ b/dpnp/dpnp_algo/dpnp_elementwise_common.py @@ -177,6 +177,13 @@ def check_nd_call_func( ) +def _get_result(res_usm, out=None): + if out is None: + return dpnp_array._create_from_usm_ndarray(res_usm) + else: + return out + + def _make_unary_func( name, dpt_unary_fn, fn_docstring, mkl_fn_to_call=None, mkl_impl_fn=None ): @@ -273,10 +280,7 @@ def dpnp_abs(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = abs_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _acos_docstring = """ @@ -317,10 +321,7 @@ def dpnp_acos(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = acos_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _acosh_docstring = """ @@ -361,10 +362,7 @@ def dpnp_acosh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = acosh_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _add_docstring = """ @@ -410,10 +408,7 @@ def dpnp_add(x1, x2, out=None, order="K"): res_usm = add_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _asin_docstring = """ @@ -454,10 +449,7 @@ def dpnp_asin(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = asin_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _asinh_docstring = """ @@ -498,10 +490,7 @@ def dpnp_asinh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = asinh_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _atan_docstring = """ @@ -542,10 +531,7 @@ def dpnp_atan(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = atan_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _atan2_docstring = """ @@ -595,10 +581,7 @@ def dpnp_atan2(x1, x2, out=None, order="K"): res_usm = atan2_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _atanh_docstring = """ @@ -639,10 +622,7 @@ def dpnp_atanh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = atanh_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _bitwise_and_docstring = """ @@ -686,10 +666,7 @@ def dpnp_bitwise_and(x1, x2, out=None, order="K"): res_usm = bitwise_and_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _bitwise_or_docstring = """ @@ -733,10 +710,7 @@ def dpnp_bitwise_or(x1, x2, out=None, order="K"): res_usm = bitwise_or_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _bitwise_xor_docstring = """ @@ -780,10 +754,7 @@ def dpnp_bitwise_xor(x1, x2, out=None, order="K"): res_usm = bitwise_xor_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _cbrt_docstring = """ @@ -823,10 +794,7 @@ def dpnp_cbrt(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = cbrt_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _ceil_docstring = """ @@ -866,10 +834,7 @@ def dpnp_ceil(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = ceil_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _conj_docstring = """ @@ -908,10 +873,7 @@ def dpnp_conj(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = conj_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _copysign_docstring = """ @@ -952,10 +914,7 @@ def dpnp_copysign(x1, x2, out=None, order="K"): res_usm = copysign_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _cos_docstring = """ @@ -995,10 +954,7 @@ def dpnp_cos(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = cos_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _cosh_docstring = """ @@ -1038,10 +994,7 @@ def dpnp_cosh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = cosh_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _divide_docstring = """ @@ -1087,10 +1040,7 @@ def dpnp_divide(x1, x2, out=None, order="K"): res_usm = divide_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _equal_docstring = """ @@ -1130,10 +1080,7 @@ def dpnp_equal(x1, x2, out=None, order="K"): res_usm = equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _exp_docstring = """ @@ -1174,10 +1121,7 @@ def dpnp_exp(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = exp_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _exp2_docstring = """ @@ -1218,10 +1162,7 @@ def dpnp_exp2(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = exp2_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _expm1_docstring = """ @@ -1264,10 +1205,7 @@ def dpnp_expm1(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = expm1_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _floor_docstring = """ @@ -1307,10 +1245,7 @@ def dpnp_floor(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = floor_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _floor_divide_docstring = """ @@ -1354,10 +1289,7 @@ def dpnp_floor_divide(x1, x2, out=None, order="K"): res_usm = floor_divide_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _greater_docstring = """ @@ -1397,10 +1329,7 @@ def dpnp_greater(x1, x2, out=None, order="K"): res_usm = greater_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _greater_equal_docstring = """ @@ -1442,10 +1371,7 @@ def dpnp_greater_equal(x1, x2, out=None, order="K"): res_usm = greater_equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _hypot_docstring = """ @@ -1491,10 +1417,7 @@ def dpnp_hypot(x1, x2, out=None, order="K"): res_usm = hypot_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _imag_docstring = """ @@ -1531,10 +1454,7 @@ def dpnp_imag(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = imag_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _invert_docstring = """ @@ -1569,10 +1489,7 @@ def dpnp_invert(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = invert_func(x_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _isfinite_docstring = """ @@ -1607,10 +1524,7 @@ def dpnp_isfinite(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = isfinite_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _isinf_docstring = """ @@ -1644,10 +1558,7 @@ def dpnp_isinf(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = isinf_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _isnan_docstring = """ @@ -1681,10 +1592,7 @@ def dpnp_isnan(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = isnan_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _left_shift_docstring = """ @@ -1728,10 +1636,7 @@ def dpnp_left_shift(x1, x2, out=None, order="K"): res_usm = left_shift_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _less_docstring = """ @@ -1771,10 +1676,7 @@ def dpnp_less(x1, x2, out=None, order="K"): res_usm = less_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _less_equal_docstring = """ @@ -1816,10 +1718,7 @@ def dpnp_less_equal(x1, x2, out=None, order="K"): res_usm = less_equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _log_docstring = """ @@ -1860,10 +1759,7 @@ def dpnp_log(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _log10_docstring = """ @@ -1904,10 +1800,7 @@ def dpnp_log10(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log10_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _log1p_docstring = """ @@ -1947,10 +1840,7 @@ def dpnp_log1p(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log1p_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _log2_docstring = """ @@ -1991,10 +1881,7 @@ def dpnp_log2(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = log2_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _logaddexp_docstring = """ @@ -2042,10 +1929,7 @@ def dpnp_logaddexp(x1, x2, out=None, order="K"): res_usm = logaddexp_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _logical_and_docstring = """ @@ -2086,10 +1970,7 @@ def dpnp_logical_and(x1, x2, out=None, order="K"): res_usm = logical_and_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _logical_not_docstring = """ @@ -2124,10 +2005,7 @@ def dpnp_logical_not(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = logical_not_func(x_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _logical_or_docstring = """ @@ -2168,10 +2046,7 @@ def dpnp_logical_or(x1, x2, out=None, order="K"): res_usm = logical_or_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _logical_xor_docstring = """ @@ -2212,10 +2087,7 @@ def dpnp_logical_xor(x1, x2, out=None, order="K"): res_usm = logical_xor_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _maximum_docstring = """ @@ -2255,10 +2127,7 @@ def dpnp_maximum(x1, x2, out=None, order="K"): res_usm = maximum_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _minimum_docstring = """ @@ -2298,10 +2167,7 @@ def dpnp_minimum(x1, x2, out=None, order="K"): res_usm = minimum_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _multiply_docstring = """ @@ -2351,10 +2217,7 @@ def dpnp_multiply(x1, x2, out=None, order="K"): res_usm = multiply_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _negative_docstring = """ @@ -2394,10 +2257,7 @@ def dpnp_negative(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = negative_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _not_equal_docstring = """ @@ -2439,10 +2299,7 @@ def dpnp_not_equal(x1, x2, out=None, order="K"): res_usm = not_equal_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _positive_docstring = """ @@ -2481,10 +2338,7 @@ def dpnp_positive(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = positive_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _power_docstring = """ @@ -2531,10 +2385,7 @@ def dpnp_power(x1, x2, out=None, order="K"): res_usm = power_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _proj_docstring = """ @@ -2568,10 +2419,7 @@ def dpnp_proj(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = proj_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _real_docstring = """ @@ -2608,10 +2456,7 @@ def dpnp_real(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = real_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _remainder_docstring = """ @@ -2652,10 +2497,7 @@ def dpnp_remainder(x1, x2, out=None, order="K"): res_usm = remainder_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _right_shift_docstring = """ @@ -2698,10 +2540,7 @@ def dpnp_right_shift(x1, x2, out=None, order="K"): res_usm = right_shift_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _round_docstring = """ @@ -2741,10 +2580,7 @@ def dpnp_round(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = round_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _rsqrt_docstring = """ @@ -2779,10 +2615,7 @@ def dpnp_rsqrt(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = rsqrt_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _sign_docstring = """ @@ -2824,10 +2657,7 @@ def dpnp_sign(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sign_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _signbit_docstring = """ @@ -2862,10 +2692,7 @@ def dpnp_signbit(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = signbit_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _sin_docstring = """ @@ -2905,10 +2732,7 @@ def dpnp_sin(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sin_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _sinh_docstring = """ @@ -2948,10 +2772,7 @@ def dpnp_sinh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sinh_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _sqrt_docstring = """ @@ -2990,10 +2811,7 @@ def dpnp_sqrt(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = sqrt_func(x_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _square_docstring = """ @@ -3032,10 +2850,7 @@ def dpnp_square(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = square_func(x_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _subtract_docstring = """ @@ -3096,10 +2911,7 @@ def dpnp_subtract(x1, x2, out=None, order="K"): res_usm = subtract_func( x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order ) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _tan_docstring = """ @@ -3139,10 +2951,7 @@ def dpnp_tan(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = tan_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _tanh_docstring = """ @@ -3182,10 +2991,7 @@ def dpnp_tanh(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = tanh_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out) _trunc_docstring = """ @@ -3227,7 +3033,4 @@ def dpnp_trunc(x, out=None, order="K"): out_usm = None if out is None else dpnp.get_usm_ndarray(out) res_usm = trunc_func(x1_usm, out=out_usm, order=order) - if out is None: - return dpnp_array._create_from_usm_ndarray(res_usm) - else: - return out + return _get_result(res_usm, out=out)