From 92cfeb0f13fa698ee6f3341550f9ab86c3cf8c9e Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 12 Apr 2023 13:02:16 +0200 Subject: [PATCH 1/7] Extend out support for 1in_1out functions --- dpnp/dpnp_algo/dpnp_algo.pyx | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/dpnp/dpnp_algo/dpnp_algo.pyx b/dpnp/dpnp_algo/dpnp_algo.pyx index 24abd1b4b9e4..0e749a4911da 100644 --- a/dpnp/dpnp_algo/dpnp_algo.pyx +++ b/dpnp/dpnp_algo/dpnp_algo.pyx @@ -347,7 +347,7 @@ cdef utils.dpnp_descriptor call_fptr_1in_1out_strides(DPNPFuncName fptr_name, """ get the FPTR data structure """ cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(fptr_name, param1_type, param1_type) - result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type) + return_type = kernel_data.return_type cdef shape_type_c x1_shape = x1.shape cdef shape_type_c x1_strides = utils.strides_to_vector(x1.strides, x1_shape) @@ -355,25 +355,34 @@ cdef utils.dpnp_descriptor call_fptr_1in_1out_strides(DPNPFuncName fptr_name, cdef shape_type_c result_shape = x1_shape cdef utils.dpnp_descriptor result - if out is None: - """ Create result array with type given by FPTR data """ + """" Check `out` parameter data """ + if out is not None: + if out.shape != result_shape: + utils.checker_throw_value_error(func_name, 'out.shape', out.shape, result_shape) + + utils.get_common_usm_allocation(x1, out) #check USM allocation is common + + if out is None or out.is_array_overlapped(x1) or not out.match_ctype(return_type): + """" + Create result array with type given by FPTR data. + If 'out' array has another dtype than expected or overlaps a memory from any input array, + we have to create a temporary array and to copy data from the temporary into 'out' array, + once the computation is completed. + Otherwise simultaneously access to the same memory may cause a race condition issue + which will result into undefined behaviour. + """ + is_result_memory_allocated = True x1_obj = x1.get_array() result = utils.create_output_descriptor(result_shape, - kernel_data.return_type, + return_type, None, device=x1_obj.sycl_device, usm_type=x1_obj.usm_type, sycl_queue=x1_obj.sycl_queue) else: - if out.dtype != result_type: - utils.checker_throw_value_error(func_name, 'out.dtype', out.dtype, result_type) - if out.shape != result_shape: - utils.checker_throw_value_error(func_name, 'out.shape', out.shape, result_shape) - + is_result_memory_allocated = False result = out - utils.get_common_usm_allocation(x1, result) # check USM allocation is common - result_sycl_queue = result.get_array().sycl_queue cdef c_dpctl.SyclQueue q = result_sycl_queue @@ -400,7 +409,10 @@ cdef utils.dpnp_descriptor call_fptr_1in_1out_strides(DPNPFuncName fptr_name, with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref) c_dpctl.DPCTLEvent_Delete(event_ref) - return result + if out is not None and is_result_memory_allocated: + return out.get_result_desc(result) + + return result.get_result_desc() cdef utils.dpnp_descriptor call_fptr_2in_1out(DPNPFuncName fptr_name, From 5d6b2911159e1305985812bedc22352834df4c5d Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 12 Apr 2023 13:04:09 +0200 Subject: [PATCH 2/7] Add _check_nd_call func for trigonometric functions --- dpnp/dpnp_iface_trigonometric.py | 52 +++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/dpnp/dpnp_iface_trigonometric.py b/dpnp/dpnp_iface_trigonometric.py index 473401071643..7fd626e07a04 100644 --- a/dpnp/dpnp_iface_trigonometric.py +++ b/dpnp/dpnp_iface_trigonometric.py @@ -82,6 +82,36 @@ ] +def _check_nd_call(origin_func, dpnp_func, x1, out=None, where=True, dtype=None, subok=True, **kwargs): + """Choose function to call based on input and call chosen fucntion.""" + + if kwargs: + pass + elif where is not True: + pass + elif dtype is not None: + pass + elif subok is not True: + pass + elif dpnp.isscalar(x1): + pass + else: + x1_desc = dpnp.get_dpnp_descriptor( + x1, copy_when_strides=False, copy_when_nondefault_queue=False + ) + + if x1_desc: + if out is not None: + if not isinstance(out, (dpnp.ndarray, dpt.usm_ndarray)): + raise TypeError("return array must be of supported array type") + out_desc = dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False) or None + else: + out_desc = None + print("DPNP") + return dpnp_func(x1_desc, out=out_desc).get_pyobj() + print("NUMPY") + return call_origin(origin_func, x1, dtype=dtype, out=out, where=where, subok=subok, **kwargs) + def arccos(x1): """ Trigonometric inverse cosine, element-wise. @@ -907,7 +937,7 @@ def sinh(x1): return call_origin(numpy.sinh, x1, **kwargs) -def sqrt(x1, /, out = None, **kwargs): +def sqrt(x1, /, out = None, where=True, dtype=None, subok=True, **kwargs): """ Return the positive square-root of an array, element-wise. @@ -918,6 +948,8 @@ def sqrt(x1, /, out = None, **kwargs): Input array is supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`. Parameter `out` is supported as class:`dpnp.ndarray`, class:`dpctl.tensor.usm_ndarray` or with default value ``None``. + 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. Keyword arguments ``kwargs`` are currently unsupported. Input array data types are limited by supported DPNP :ref:`Data types`. @@ -932,23 +964,7 @@ def sqrt(x1, /, out = None, **kwargs): """ - x1_desc = ( - dpnp.get_dpnp_descriptor( - x1, copy_when_strides=False, copy_when_nondefault_queue=False - ) - if not kwargs - else None - ) - if x1_desc: - if out is not None: - if not isinstance(out, (dpnp.ndarray, dpt.usm_ndarray)): - raise TypeError("return array must be of supported array type") - out_desc = dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False) or None - else: - out_desc = None - return dpnp_sqrt(x1_desc, out=out_desc).get_pyobj() - - return call_origin(numpy.sqrt, x1, out=out, **kwargs) + return _check_nd_call(numpy.sqrt, dpnp_sqrt, x1, out=out, where=where, dtype=dtype, subok=subok, **kwargs) def square(x1): From a09713478f51530ee6dda2ce69a4ed86490ba63c Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 12 Apr 2023 13:04:44 +0200 Subject: [PATCH 3/7] Update tests for dpnp.sqrt --- tests/test_strides.py | 55 ++++++++++++++++++++++++++++++++++++++++++- tests/test_umath.py | 29 ++++++++++++++++------- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/tests/test_strides.py b/tests/test_strides.py index 7a859a72285a..117f3ea3d900 100644 --- a/tests/test_strides.py +++ b/tests/test_strides.py @@ -1,6 +1,6 @@ import math import pytest -from .helper import get_all_dtypes, is_cpu_device +from .helper import get_all_dtypes, get_float_dtypes import dpnp @@ -215,6 +215,59 @@ def test_strides_true_devide(dtype, shape): assert_allclose(result, expected) +@pytest.mark.parametrize("func_name", + ["sqrt",]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True)) +def test_strided_out_1args(func_name, dtype): + np_out = numpy.ones((5, 3, 2))[::3] + np_a = numpy.arange(numpy.prod(np_out.shape), dtype=dtype).reshape(np_out.shape) + + dp_out = dpnp.ones((5, 3, 2))[::3] + dp_a = dpnp.array(np_a) + + np_res = _getattr(numpy, func_name)(np_a, out=np_out) + dp_res = _getattr(dpnp, func_name)(dp_a, out=dp_out) + + assert_allclose(dp_res.asnumpy(), np_res) + assert_allclose(dp_out.asnumpy(), np_out) + +@pytest.mark.parametrize("func_name", + ["sqrt",]) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True)) +def test_strided_in_out_1args(func_name, dtype): + sh = (3, 4, 2) + prod = numpy.prod(sh) + + np_out = numpy.ones(sh, dtype=numpy.float64)[::2] + np_a = numpy.arange(prod, dtype=dtype).reshape(sh)[::2].T + + dp_out = dpnp.ones(sh, dtype=dpnp.float64)[::2] + dp_a = dpnp.arange(prod, dtype=dtype).reshape(sh)[::2].T + + np_res = _getattr(numpy, func_name)(np_a, out=np_out) + dp_res = _getattr(dpnp, func_name)(dp_a, out=dp_out) + + assert_allclose(dp_res.asnumpy(), np_res, rtol=1e-06) + assert_allclose(dp_out.asnumpy(), np_out, rtol=1e-06) + + +@pytest.mark.parametrize("func_name", + ["sqrt",]) +@pytest.mark.parametrize("dtype", get_float_dtypes()) +def test_strided_in_out_1args_overlap(func_name, dtype): + sh = (4, 3, 2) + prod = numpy.prod(sh) + + np_a = numpy.arange(prod, dtype=dtype).reshape(sh) + + dp_a = dpnp.arange(prod, dtype=dtype).reshape(sh) + + np_res = _getattr(numpy, func_name)(np_a[:3:], out=np_a[1::]) + dp_res = _getattr(dpnp, func_name)(dp_a[:3:], out=dp_a[1::]) + + assert_allclose(dp_res.asnumpy(), np_res, rtol=1e-06) + assert_allclose(dp_a.asnumpy(), np_a, rtol=1e-06) + @pytest.mark.parametrize("func_name", ["add", "multiply", "power"]) diff --git a/tests/test_umath.py b/tests/test_umath.py index 7b5c4b762d88..554782134b08 100644 --- a/tests/test_umath.py +++ b/tests/test_umath.py @@ -423,16 +423,29 @@ def test_sqrt_ordinary(self, dtype): numpy.testing.assert_allclose(expected, result) numpy.testing.assert_allclose(out, dp_out) - @pytest.mark.parametrize("dtype", - [numpy.int64, numpy.int32], - ids=['numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + def test_out_dtype(self, dtype): - dp_array = dpnp.arange(10, dtype=dpnp.float32) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=numpy.float32) + expected = numpy.sqrt(np_array, np_out) - with pytest.raises(ValueError): - dpnp.sqrt(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dpnp.float32) + result = dpnp.sqrt(dp_array, dp_out) + + assert_allclose(expected, result) + + @pytest.mark.parametrize("dtype", get_float_dtypes()) + def test_out_equal_array(self, dtype): + + np_array = numpy.arange(10, dtype=dtype) + expected = numpy.sqrt(np_array, np_array) + + dp_array = dpnp.arange(10, dtype=dtype) + result = dpnp.sqrt(dp_array, dp_array) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], From 1d89ab0eb2f8deddaca37059dd9bc55b15bd257a Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 12 Apr 2023 17:11:56 +0200 Subject: [PATCH 4/7] Remove prints --- dpnp/dpnp_iface_trigonometric.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/dpnp/dpnp_iface_trigonometric.py b/dpnp/dpnp_iface_trigonometric.py index 7fd626e07a04..eeeb5edbbc07 100644 --- a/dpnp/dpnp_iface_trigonometric.py +++ b/dpnp/dpnp_iface_trigonometric.py @@ -107,9 +107,7 @@ def _check_nd_call(origin_func, dpnp_func, x1, out=None, where=True, dtype=None, out_desc = dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False) or None else: out_desc = None - print("DPNP") return dpnp_func(x1_desc, out=out_desc).get_pyobj() - print("NUMPY") return call_origin(origin_func, x1, dtype=dtype, out=out, where=where, subok=subok, **kwargs) def arccos(x1): From 603d9fb8e28e8435a31b75d84cca363c42b9eeb7 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 12 Apr 2023 17:37:26 +0200 Subject: [PATCH 5/7] Replace test_invalid_dtype with test_out_dtype --- tests/test_mathematical.py | 60 ++++++++++------- tests/test_umath.py | 135 +++++++++++++++++++++---------------- 2 files changed, 113 insertions(+), 82 deletions(-) diff --git a/tests/test_mathematical.py b/tests/test_mathematical.py index 5f0d73b23b7b..94bb2df3d679 100644 --- a/tests/test_mathematical.py +++ b/tests/test_mathematical.py @@ -2,6 +2,7 @@ from .helper import ( get_all_dtypes, get_float_complex_dtypes, + get_float_dtypes, is_cpu_device, is_win_platform ) @@ -387,7 +388,7 @@ def test_ediff1d_int(self, array, data_type): expected = numpy.ediff1d(np_a) assert_array_equal(expected, result) - + @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_ediff1d_args(self): np_a = numpy.array([1, 2, 4, 7, 0]) @@ -532,16 +533,19 @@ def test_ceil(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.ceil(np_array, np_out) - with pytest.raises(ValueError): - dpnp.ceil(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.ceil(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -572,16 +576,19 @@ def test_floor(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.floor(np_array, np_out) - with pytest.raises(ValueError): - dpnp.floor(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.floor(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -612,16 +619,19 @@ def test_trunc(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.trunc(np_array, np_out) - with pytest.raises(ValueError): - dpnp.trunc(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.trunc(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], diff --git a/tests/test_umath.py b/tests/test_umath.py index 554782134b08..aa95d0270c73 100644 --- a/tests/test_umath.py +++ b/tests/test_umath.py @@ -100,16 +100,19 @@ def test_sin_ordinary(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.sin(np_array, np_out) - with pytest.raises(ValueError): - dpnp.sin(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.sin(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -140,16 +143,19 @@ def test_cos(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.cos(np_array, np_out) - with pytest.raises(ValueError): - dpnp.cos(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.cos(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -163,7 +169,7 @@ def test_invalid_shape(self, shape): dpnp.cos(dp_array, out=dp_out) -class TestsLog: +class TestLog: def test_log(self): array_data = numpy.arange(10) @@ -180,16 +186,19 @@ def test_log(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.log(np_array, np_out) - with pytest.raises(ValueError): - dpnp.log(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.log(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -220,16 +229,19 @@ def test_exp(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.exp(np_array, np_out) - with pytest.raises(ValueError): - dpnp.exp(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.exp(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -260,16 +272,19 @@ def test_arcsin(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.arcsin(np_array, np_out) - with pytest.raises(ValueError): - dpnp.arcsin(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.arcsin(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -300,16 +315,19 @@ def test_arctan(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.arctan(np_array, np_out) - with pytest.raises(ValueError): - dpnp.arctan(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.arctan(dp_array, dp_out) + + assert_allclose(expected, result) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -340,16 +358,19 @@ def test_tan(self): assert_array_equal(expected, result) - @pytest.mark.parametrize("dtype", - [numpy.float32, numpy.int64, numpy.int32], - ids=['numpy.float32', 'numpy.int64', 'numpy.int32']) - def test_invalid_dtype(self, dtype): + @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): - dp_array = dpnp.arange(10, dtype=dpnp.float64) - dp_out = dpnp.empty(10, dtype=dtype) + np_array = numpy.arange(10, dtype=dtype) + np_out = numpy.empty(10, dtype=dtype_out) + expected = numpy.tan(np_array, np_out) - with pytest.raises(ValueError): - dpnp.tan(dp_array, out=dp_out) + dp_array = dpnp.arange(10, dtype=dtype) + dp_out = dpnp.empty(10, dtype=dtype_out) + result = dpnp.tan(dp_array, dp_out) + + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], From ec15f9a0b6353f9f02c8ce3311d23b43a740bc75 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Fri, 14 Apr 2023 12:34:34 +0200 Subject: [PATCH 6/7] Add dtype_out parametrize for TestSqrt --- tests/test_umath.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_umath.py b/tests/test_umath.py index aa95d0270c73..d6e0ebe2f3ca 100644 --- a/tests/test_umath.py +++ b/tests/test_umath.py @@ -445,14 +445,15 @@ def test_sqrt_ordinary(self, dtype): numpy.testing.assert_allclose(out, dp_out) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)) - def test_out_dtype(self, dtype): + @pytest.mark.parametrize("dtype_out", get_float_dtypes()) + def test_out_dtype(self, dtype, dtype_out): np_array = numpy.arange(10, dtype=dtype) - np_out = numpy.empty(10, dtype=numpy.float32) + np_out = numpy.empty(10, dtype=dtype_out) expected = numpy.sqrt(np_array, np_out) dp_array = dpnp.arange(10, dtype=dtype) - dp_out = dpnp.empty(10, dtype=dpnp.float32) + dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.sqrt(dp_array, dp_out) assert_allclose(expected, result) From 8222f945a66ac65e4c3ece29fabc2579fd01aa1f Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Fri, 14 Apr 2023 15:24:46 +0200 Subject: [PATCH 7/7] Add rtol for assert_allclose for test_out_dtype --- tests/test_mathematical.py | 6 +++--- tests/test_umath.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_mathematical.py b/tests/test_mathematical.py index 94bb2df3d679..5f5fc2b94a7f 100644 --- a/tests/test_mathematical.py +++ b/tests/test_mathematical.py @@ -545,7 +545,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.ceil(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -588,7 +588,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.floor(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -631,7 +631,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.trunc(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], diff --git a/tests/test_umath.py b/tests/test_umath.py index d6e0ebe2f3ca..e3ede826f268 100644 --- a/tests/test_umath.py +++ b/tests/test_umath.py @@ -112,7 +112,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.sin(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -155,7 +155,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.cos(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -198,7 +198,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.log(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -241,7 +241,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.exp(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -284,7 +284,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.arcsin(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -327,7 +327,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.arctan(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -412,7 +412,7 @@ def test_out_dtypes(self, dtype): dp_out = dpnp.empty(size, dtype=dpnp.complex64) result = dpnp.arctan2(dp_array, dp_array, out=dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("shape", [(0,), (15, ), (2, 2)], @@ -456,7 +456,7 @@ def test_out_dtype(self, dtype, dtype_out): dp_out = dpnp.empty(10, dtype=dtype_out) result = dpnp.sqrt(dp_array, dp_out) - assert_allclose(expected, result) + assert_allclose(expected, result, rtol=1e-06) @pytest.mark.parametrize("dtype", get_float_dtypes()) def test_out_equal_array(self, dtype):