diff --git a/dpnp/tests/helper.py b/dpnp/tests/helper.py index de0c949f0508..5b43ebbeb6da 100644 --- a/dpnp/tests/helper.py +++ b/dpnp/tests/helper.py @@ -147,7 +147,9 @@ def get_float_complex_dtypes(no_float16=True, device=None): def get_abs_array(data, dtype=None): if numpy.issubdtype(dtype, numpy.unsignedinteger): data = numpy.abs(data) - return numpy.array(data, dtype=dtype) + # it is better to use astype with the default casting=unsafe + # otherwise, we need to skip test for cases where overflow occurs + return numpy.array(data).astype(dtype) def get_all_dtypes( diff --git a/dpnp/tests/test_amin_amax.py b/dpnp/tests/test_amin_amax.py index 35f45cd40822..dc97192a694e 100644 --- a/dpnp/tests/test_amin_amax.py +++ b/dpnp/tests/test_amin_amax.py @@ -9,7 +9,7 @@ @pytest.mark.parametrize("func", ["amax", "amin"]) @pytest.mark.parametrize("keepdims", [True, False]) -@pytest.mark.parametrize("dtype", get_all_dtypes()) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_amax_amin(func, keepdims, dtype): a = [ [[-2.0, 3.0], [9.1, 0.2]], @@ -22,14 +22,14 @@ def test_amax_amin(func, keepdims, dtype): for axis in range(len(a)): result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) - assert_allclose(expected, result) + assert_allclose(result, expected) -def _get_min_max_input(type, shape): +def _get_min_max_input(dtype, shape): size = numpy.prod(shape) - a = numpy.arange(size, dtype=type) + a = numpy.arange(size, dtype=dtype) a[int(size / 2)] = size + 5 - if numpy.issubdtype(type, numpy.unsignedinteger): + if numpy.issubdtype(dtype, numpy.unsignedinteger): a[int(size / 3)] = size else: a[int(size / 3)] = -(size + 5) @@ -37,37 +37,35 @@ def _get_min_max_input(type, shape): return a.reshape(shape) -@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True)) @pytest.mark.parametrize( - "shape", [(4,), (2, 3), (4, 5, 6)], ids=["(4,)", "(2, 3)", "(4, 5, 6)"] + "shape", [(4,), (2, 3), (4, 5, 6)], ids=["1D", "2D", "3D"] ) def test_amax_diff_shape(dtype, shape): a = _get_min_max_input(dtype, shape) - ia = dpnp.array(a) - np_res = numpy.amax(a) - dpnp_res = dpnp.amax(ia) - assert_array_equal(dpnp_res, np_res) + expected = numpy.amax(a) + result = dpnp.amax(ia) + assert_array_equal(result, expected) - np_res = a.max() - dpnp_res = ia.max() - numpy.testing.assert_array_equal(dpnp_res, np_res) + expected = a.max() + result = ia.max() + assert_array_equal(result, expected) -@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True)) @pytest.mark.parametrize( - "shape", [(4,), (2, 3), (4, 5, 6)], ids=["(4,)", "(2, 3)", "(4, 5, 6)"] + "shape", [(4,), (2, 3), (4, 5, 6)], ids=["1D", "2D", "3D"] ) def test_amin_diff_shape(dtype, shape): a = _get_min_max_input(dtype, shape) - ia = dpnp.array(a) - np_res = numpy.amin(a) - dpnp_res = dpnp.amin(ia) - assert_array_equal(dpnp_res, np_res) + expected = numpy.amin(a) + result = dpnp.amin(ia) + assert_array_equal(result, expected) - np_res = a.min() - dpnp_res = ia.min() - assert_array_equal(dpnp_res, np_res) + expected = a.min() + result = ia.min() + assert_array_equal(result, expected) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index f82bd606adb0..7d527a88d133 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -216,7 +216,7 @@ def test_arange(start, stop, step, dtype): func = lambda xp: xp.arange(start, stop=stop, step=step, dtype=dtype) exp_array = func(numpy) - res_array = func(dpnp).asnumpy() + res_array = func(dpnp) if dtype is None: _device = dpctl.SyclQueue().sycl_device @@ -234,7 +234,7 @@ def test_arange(start, stop, step, dtype): _dtype, dpnp.complexfloating ): assert_allclose( - exp_array, res_array, rtol=rtol_mult * numpy.finfo(_dtype).eps + res_array, exp_array, rtol=rtol_mult * numpy.finfo(_dtype).eps ) else: assert_array_equal(exp_array, res_array) @@ -540,7 +540,7 @@ def test_vander(array, dtype, n, increase): a_np = numpy.array(array, dtype=dtype) a_dpnp = dpnp.array(array, dtype=dtype) - assert_allclose(vander_func(numpy, a_np), vander_func(dpnp, a_dpnp)) + assert_allclose(vander_func(dpnp, a_dpnp), vander_func(numpy, a_np)) def test_vander_raise_error(): @@ -560,7 +560,7 @@ def test_vander_raise_error(): ) def test_vander_seq(sequence): vander_func = lambda xp, x: xp.vander(x) - assert_allclose(vander_func(numpy, sequence), vander_func(dpnp, sequence)) + assert_allclose(vander_func(dpnp, sequence), vander_func(numpy, sequence)) @pytest.mark.usefixtures("suppress_complex_warning") @@ -607,19 +607,19 @@ def test_full_order(order1, order2): assert ia.flags.c_contiguous == a.flags.c_contiguous assert ia.flags.f_contiguous == a.flags.f_contiguous - assert numpy.array_equal(dpnp.asnumpy(ia), a) + assert_equal(ia, a) def test_full_strides(): a = numpy.full((3, 3), numpy.arange(3, dtype="i4")) ia = dpnp.full((3, 3), dpnp.arange(3, dtype="i4")) assert ia.strides == tuple(el // a.itemsize for el in a.strides) - assert_array_equal(dpnp.asnumpy(ia), a) + assert_array_equal(ia, a) a = numpy.full((3, 3), numpy.arange(6, dtype="i4")[::2]) ia = dpnp.full((3, 3), dpnp.arange(6, dtype="i4")[::2]) assert ia.strides == tuple(el // a.itemsize for el in a.strides) - assert_array_equal(dpnp.asnumpy(ia), a) + assert_array_equal(ia, a) @pytest.mark.parametrize( @@ -762,20 +762,12 @@ def test_linspace(start, stop, num, dtype, retstep): assert_dtype_allclose(res_dp, res_np) +@pytest.mark.parametrize("func", ["geomspace", "linspace", "logspace"]) @pytest.mark.parametrize( - "func", - ["geomspace", "linspace", "logspace"], - ids=["geomspace", "linspace", "logspace"], + "start_dtype", [numpy.float64, numpy.float32, numpy.int64, numpy.int32] ) @pytest.mark.parametrize( - "start_dtype", - [numpy.float64, numpy.float32, numpy.int64, numpy.int32], - ids=["float64", "float32", "int64", "int32"], -) -@pytest.mark.parametrize( - "stop_dtype", - [numpy.float64, numpy.float32, numpy.int64, numpy.int32], - ids=["float64", "float32", "int64", "int32"], + "stop_dtype", [numpy.float64, numpy.float32, numpy.int64, numpy.int32] ) def test_space_numpy_dtype(func, start_dtype, stop_dtype): start = numpy.array([1, 2, 3], dtype=start_dtype) @@ -890,10 +882,7 @@ def test_geomspace(sign, dtype, num, endpoint): np_res = func(numpy) dpnp_res = func(dpnp) - if dtype in [numpy.int64, numpy.int32]: - assert_allclose(dpnp_res, np_res, rtol=1) - else: - assert_allclose(dpnp_res, np_res, rtol=1e-04) + assert_allclose(dpnp_res, np_res, rtol=1e-06) @pytest.mark.parametrize("start", [1j, 1 + 1j]) @@ -902,7 +891,7 @@ def test_geomspace_complex(start, stop): func = lambda xp: xp.geomspace(start, stop, num=10) np_res = func(numpy) dpnp_res = func(dpnp) - assert_allclose(dpnp_res, np_res, rtol=1e-04) + assert_allclose(dpnp_res, np_res, rtol=1e-06) @pytest.mark.parametrize("axis", [0, 1]) @@ -910,14 +899,14 @@ def test_geomspace_axis(axis): func = lambda xp: xp.geomspace([2, 3], [20, 15], num=10, axis=axis) np_res = func(numpy) dpnp_res = func(dpnp) - assert_allclose(dpnp_res, np_res, rtol=1e-04) + assert_allclose(dpnp_res, np_res, rtol=1e-06) def test_geomspace_num0(): func = lambda xp: xp.geomspace(1, 10, num=0, endpoint=False) np_res = func(numpy) dpnp_res = func(dpnp) - assert_allclose(dpnp_res, np_res, rtol=1e-04) + assert_allclose(dpnp_res, np_res) @pytest.mark.parametrize("dtype", get_all_dtypes()) @@ -935,10 +924,7 @@ def test_logspace(dtype, num, endpoint): np_res = func(numpy) dpnp_res = func(dpnp) - if dtype in [numpy.int64, numpy.int32]: - assert_allclose(dpnp_res, np_res, rtol=1) - else: - assert_allclose(dpnp_res, np_res, rtol=1e-04) + assert_allclose(dpnp_res, np_res, rtol=1e-06) @pytest.mark.parametrize("axis", [0, 1]) diff --git a/dpnp/tests/test_arraymanipulation.py b/dpnp/tests/test_arraymanipulation.py index 0f8b8f96d5ca..eafa0b853a89 100644 --- a/dpnp/tests/test_arraymanipulation.py +++ b/dpnp/tests/test_arraymanipulation.py @@ -2,12 +2,7 @@ import numpy import pytest from dpctl.tensor._numpy_helper import AxisError -from numpy.testing import ( - assert_allclose, - assert_array_equal, - assert_equal, - assert_raises, -) +from numpy.testing import assert_array_equal, assert_equal, assert_raises import dpnp @@ -171,9 +166,7 @@ def assert_broadcast_correct(self, input_shapes): dpnp_arrays = [dpnp.asarray(Xnp) for Xnp in np_arrays] out_dpnp_arrays = dpnp.broadcast_arrays(*dpnp_arrays) for Xnp, X in zip(out_np_arrays, out_dpnp_arrays): - assert_array_equal( - Xnp, dpnp.asnumpy(X), err_msg=f"Failed for {input_shapes})" - ) + assert_array_equal(Xnp, X, err_msg=f"Failed for {input_shapes})") def assert_broadcast_arrays_raise(self, input_shapes): dpnp_arrays = [dpnp.asarray(numpy.zeros(s)) for s in input_shapes] @@ -186,8 +179,8 @@ def test_broadcast_arrays_same(self): X = dpnp.asarray(Xnp) Y = dpnp.asarray(Ynp) res_X, res_Y = dpnp.broadcast_arrays(X, Y) - assert_array_equal(res_Xnp, dpnp.asnumpy(res_X)) - assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y)) + assert_array_equal(res_Xnp, res_X) + assert_array_equal(res_Ynp, res_Y) def test_broadcast_arrays_one_off(self): Xnp = numpy.array([[1, 2, 3]]) @@ -196,8 +189,8 @@ def test_broadcast_arrays_one_off(self): X = dpnp.asarray(Xnp) Y = dpnp.asarray(Ynp) res_X, res_Y = dpnp.broadcast_arrays(X, Y) - assert_array_equal(res_Xnp, dpnp.asnumpy(res_X)) - assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y)) + assert_array_equal(res_Xnp, res_X) + assert_array_equal(res_Ynp, res_Y) @pytest.mark.parametrize( "shapes", @@ -327,7 +320,7 @@ def test_1d_2d_arrays(self, data1, data2, dtype): np_res = numpy.column_stack((np_a, np_b)) dp_res = dpnp.column_stack((dp_a, dp_b)) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) def test_generator(self): with pytest.raises(TypeError, match="arrays to stack must be"): @@ -350,7 +343,7 @@ def test_axis_exceptions(self, ndim): dp_res = dpnp.concatenate((dp_a, dp_a), axis=0) np_res = numpy.concatenate((np_a, np_a), axis=0) - assert_equal(dp_res.asnumpy(), np_res) + assert_equal(dp_res, np_res) for axis in [ndim, -(ndim + 1)]: assert_raises(AxisError, dpnp.concatenate, (dp_a, dp_a), axis=axis) @@ -381,7 +374,7 @@ def test_shapes_match_exception(self): # shapes must match except for concatenation axis np_res = numpy.concatenate((np_a, np_b), axis=axis[0]) dp_res = dpnp.concatenate((dp_a, dp_b), axis=axis[0]) - assert_equal(dp_res.asnumpy(), np_res) + assert_equal(dp_res, np_res) for i in range(1, 3): assert_raises( @@ -410,7 +403,7 @@ def test_concatenate_axis_None(self, dtype): np_res = numpy.concatenate((np_a, np_a), axis=None) dp_res = dpnp.concatenate((dp_a, dp_a), axis=None) - assert_equal(dp_res.asnumpy(), np_res) + assert_equal(dp_res, np_res) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_bool=True, no_none=True) @@ -422,7 +415,7 @@ def test_large_concatenate_axis_None(self, dtype): np_res = numpy.concatenate(np_a, axis=None) dp_res = dpnp.concatenate(dp_a, axis=None) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) # numpy doesn't raise an exception here but probably should with pytest.raises(AxisError): @@ -438,7 +431,7 @@ def test_concatenate(self, dtype): np_res = numpy.concatenate((np_r4,)) dp_res = dpnp.concatenate((dp_r4,)) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) # 1D default concatenation r3 = list(range(3)) @@ -447,17 +440,17 @@ def test_concatenate(self, dtype): np_res = numpy.concatenate((np_r4, np_r3)) dp_res = dpnp.concatenate((dp_r4, dp_r3)) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) # Explicit axis specification np_res = numpy.concatenate((np_r4, np_r3), axis=0) dp_res = dpnp.concatenate((dp_r4, dp_r3), axis=0) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) # Including negative np_res = numpy.concatenate((np_r4, np_r3), axis=-1) dp_res = dpnp.concatenate((dp_r4, dp_r3), axis=-1) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_concatenate_2d(self, dtype): @@ -469,16 +462,16 @@ def test_concatenate_2d(self, dtype): np_res = numpy.concatenate((np_a23, np_a13)) dp_res = dpnp.concatenate((dp_a23, dp_a13)) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) np_res = numpy.concatenate((np_a23, np_a13), axis=0) dp_res = dpnp.concatenate((dp_a23, dp_a13), axis=0) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) for axis in [1, -1]: np_res = numpy.concatenate((np_a23.T, np_a13.T), axis=axis) dp_res = dpnp.concatenate((dp_a23.T, dp_a13.T), axis=axis) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) # Arrays much match shape assert_raises( @@ -505,11 +498,11 @@ def test_concatenate_3d(self, dtype): for axis in [2, -1]: np_res = numpy.concatenate((np_a0, np_a1, np_a2), axis=axis) dp_res = dpnp.concatenate((dp_a0, dp_a1, dp_a2), axis=axis) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) np_res = numpy.concatenate((np_a0.T, np_a1.T, np_a2.T), axis=0) dp_res = dpnp.concatenate((dp_a0.T, dp_a1.T, dp_a2.T), axis=0) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_bool=True, no_none=True) @@ -531,8 +524,7 @@ def test_concatenate_out(self, dtype): dp_res = dpnp.concatenate((dp_a0, dp_a1, dp_a2), axis=2, out=dp_out) assert dp_out is dp_res - assert_array_equal(dp_out.asnumpy(), np_out) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize( "dtype", get_all_dtypes(no_bool=True, no_none=True) @@ -548,7 +540,7 @@ def test_concatenate_casting(self, dtype, casting): np_res = numpy.concatenate((np_a, np_a), axis=2, casting=casting) dp_res = dpnp.concatenate((dp_a, dp_a), axis=2, casting=casting) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) def test_concatenate_out_dtype(self): x = dpnp.ones((5, 5)) @@ -576,7 +568,7 @@ def test_broadcast_array(self, sh, dt): dp_a = dpnp.array(0, dtype=dt) func = lambda xp, a: xp.broadcast_to(a, sh) - assert_allclose(func(numpy, np_a), func(dpnp, dp_a)) + assert_array_equal(func(numpy, np_a), func(dpnp, dp_a)) @pytest.mark.parametrize("dt", get_all_dtypes()) @pytest.mark.parametrize( @@ -587,7 +579,7 @@ def test_broadcast_ones(self, sh, dt): dp_a = dpnp.ones(1, dtype=dt) func = lambda xp, a: xp.broadcast_to(a, sh) - assert_allclose(func(numpy, np_a), func(dpnp, dp_a)) + assert_array_equal(func(numpy, np_a), func(dpnp, dp_a)) @pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( @@ -598,7 +590,7 @@ def test_broadcast_arange(self, sh, dt): dp_a = dpnp.arange(3, dtype=dt) func = lambda xp, a: xp.broadcast_to(a, sh) - assert_allclose(func(numpy, np_a), func(dpnp, dp_a)) + assert_array_equal(func(numpy, np_a), func(dpnp, dp_a)) @pytest.mark.parametrize("dt", get_all_dtypes()) @pytest.mark.parametrize( @@ -614,7 +606,7 @@ def test_broadcast_not_tuple(self, sh1, sh2, dt): dp_a = dpnp.ones(sh1, dtype=dt) func = lambda xp, a: xp.broadcast_to(a, sh2) - assert_allclose(func(numpy, np_a), func(dpnp, dp_a)) + assert_array_equal(func(numpy, np_a), func(dpnp, dp_a)) @pytest.mark.parametrize("dt", get_all_dtypes()) @pytest.mark.parametrize( @@ -630,7 +622,7 @@ def test_broadcast_zero_shape(self, sh1, sh2, dt): dp_a = dpnp.ones(sh1, dtype=dt) func = lambda xp, a: xp.broadcast_to(a, sh2) - assert_allclose(func(numpy, np_a), func(dpnp, dp_a)) + assert_array_equal(func(numpy, np_a), func(dpnp, dp_a)) @pytest.mark.parametrize( "sh1, sh2", @@ -681,7 +673,7 @@ def test_arrays(self, data1, data2, dtype): np_res = numpy.dstack([np_a, np_b]) dp_res = dpnp.dstack([dp_a, dp_b]) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) def test_generator(self): with pytest.raises(TypeError, match="arrays to stack must be"): @@ -737,7 +729,7 @@ class TestMatrixtranspose: @pytest.mark.parametrize( "shape", [(3, 5), (4, 2), (2, 5, 2), (2, 3, 3, 6)], - ids=["(3,5)", "(4,2)", "(2,5,2)", "(2,3,3,6)"], + ids=["(3, 5)", "(4, 2)", "(2, 5, 2)", "(2, 3, 3, 6)"], ) def test_matrix_transpose(self, dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) @@ -746,7 +738,7 @@ def test_matrix_transpose(self, dtype, shape): expected = numpy.matrix_transpose(a) result = dpnp.matrix_transpose(dp_a) - assert_allclose(result, expected) + assert_array_equal(result, expected) @pytest.mark.parametrize( "shape", @@ -760,7 +752,7 @@ def test_matrix_transpose_empty(self, shape): expected = numpy.matrix_transpose(a) result = dpnp.matrix_transpose(dp_a) - assert_allclose(result, expected) + assert_array_equal(result, expected) def test_matrix_transpose_errors(self): a_dp = dpnp.array([[1, 2], [3, 4]], dtype="float32") @@ -862,7 +854,7 @@ def test_0d_array_input(self, dtype): np_res = numpy.stack(np_arrays) dp_res = dpnp.stack(dp_arrays) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_1d_array_input(self, dtype): @@ -873,19 +865,19 @@ def test_1d_array_input(self, dtype): np_res = numpy.stack((np_a, np_b)) dp_res = dpnp.stack((dp_a, dp_b)) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) np_res = numpy.stack((np_a, np_b), axis=1) dp_res = dpnp.stack((dp_a, dp_b), axis=1) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) np_res = numpy.stack(list([np_a, np_b])) dp_res = dpnp.stack(list([dp_a, dp_b])) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) np_res = numpy.stack(numpy.array([np_a, np_b])) dp_res = dpnp.stack(dpnp.array([dp_a, dp_b])) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("axis", [0, 1, -1, -2]) @pytest.mark.parametrize( @@ -898,7 +890,7 @@ def test_1d_array_axis(self, axis, dtype): np_res = numpy.stack(np_arrays, axis=axis) dp_res = dpnp.stack(dp_arrays, axis=axis) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("axis", [2, -3]) @pytest.mark.parametrize( @@ -923,7 +915,7 @@ def test_2d_array_axis(self, axis, dtype): np_res = numpy.stack(np_arrays, axis=axis) dp_res = dpnp.stack(dp_arrays, axis=axis) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_empty_arrays_input(self, dtype): @@ -933,11 +925,11 @@ def test_empty_arrays_input(self, dtype): np_res = numpy.stack(np_arrays) dp_res = dpnp.stack(dp_arrays) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) np_res = numpy.stack(np_arrays, axis=1) dp_res = dpnp.stack(dp_arrays, axis=1) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_out(self, dtype): @@ -953,8 +945,7 @@ def test_out(self, dtype): dp_res = dpnp.stack((dp_a, dp_b), out=dp_out) assert dp_out is dp_res - assert_array_equal(dp_out.asnumpy(), np_out) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) def test_empty_list_input(self): with pytest.raises(TypeError): @@ -1000,7 +991,7 @@ def test_casting_dtype(self, arr_dtype, dtype): (np_a, np_b), axis=1, casting="unsafe", dtype=dtype ) dp_res = dpnp.stack((dp_a, dp_b), axis=1, casting="unsafe", dtype=dtype) - assert_array_equal(dp_res.asnumpy(), np_res) + assert_array_equal(dp_res, np_res) @pytest.mark.parametrize("arr_dtype", get_float_complex_dtypes()) @pytest.mark.parametrize("dtype", [dpnp.bool, dpnp.int32, dpnp.int64]) @@ -1073,7 +1064,7 @@ def test_1d_array(self, dtype): dp_res = dpnp.unstack(dp_a) assert len(dp_res) == len(np_res) for dp_arr, np_arr in zip(dp_res, np_res): - assert_array_equal(dp_arr.asnumpy(), np_arr) + assert_array_equal(dp_arr, np_arr) @pytest.mark.parametrize("dtype", get_all_dtypes()) def test_2d_array(self, dtype): @@ -1084,7 +1075,7 @@ def test_2d_array(self, dtype): dp_res = dpnp.unstack(dp_a, axis=0) assert len(dp_res) == len(np_res) for dp_arr, np_arr in zip(dp_res, np_res): - assert_array_equal(dp_arr.asnumpy(), np_arr) + assert_array_equal(dp_arr, np_arr) @pytest.mark.parametrize("axis", [0, 1, -1]) @pytest.mark.parametrize("dtype", get_all_dtypes()) @@ -1096,7 +1087,7 @@ def test_2d_array_axis(self, axis, dtype): dp_res = dpnp.unstack(dp_a, axis=axis) assert len(dp_res) == len(np_res) for dp_arr, np_arr in zip(dp_res, np_res): - assert_array_equal(dp_arr.asnumpy(), np_arr) + assert_array_equal(dp_arr, np_arr) @pytest.mark.parametrize("axis", [2, -3]) @pytest.mark.parametrize("dtype", get_all_dtypes()) diff --git a/dpnp/tests/test_binary_ufuncs.py b/dpnp/tests/test_binary_ufuncs.py index 5bf64684e6fe..4823b5a1cd9a 100644 --- a/dpnp/tests/test_binary_ufuncs.py +++ b/dpnp/tests/test_binary_ufuncs.py @@ -128,12 +128,15 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.add(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.add, a, 2, out) - assert_raises(TypeError, numpy.add, a.asnumpy(), 2, out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.add, a, 2, out) class TestBoundFuncs: @@ -194,13 +197,16 @@ def test_invalid_shape(self, func_params, shape): with pytest.raises(ValueError): getattr(dpnp, func_name)(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, func_params, out): + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, func_params, xp, out): func_name = func_params["func_name"] - a = dpnp.arange(10) - - assert_raises(TypeError, getattr(dpnp, func_name), a, 2, out) - assert_raises(TypeError, getattr(numpy, func_name), a.asnumpy(), 2, out) + a = xp.arange(10) + assert_raises(TypeError, getattr(xp, func_name), a, 2, out) class TestDivide: @@ -288,12 +294,15 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.divide(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.divide, a, 2, out) - assert_raises(TypeError, numpy.divide, a.asnumpy(), 2, out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.divide, a, 2, out) @pytest.mark.parametrize("func", ["floor_divide", "remainder"]) @@ -378,12 +387,15 @@ def test_invalid_shape(self, func, shape): with pytest.raises(ValueError): getattr(dpnp, func)(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, func, out): - a = dpnp.arange(10) - - assert_raises(TypeError, getattr(dpnp, func), a, 2, out) - assert_raises(TypeError, getattr(numpy, func), a.asnumpy(), 2, out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, func, xp, out): + a = xp.arange(10) + assert_raises(TypeError, getattr(xp, func), a, 2, out) class TestFmaxFmin: @@ -659,12 +671,15 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.multiply(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.multiply, a, 2, out) - assert_raises(TypeError, numpy.multiply, a.asnumpy(), 2, out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.multiply, a, 2, out) class TestNextafter: @@ -806,7 +821,7 @@ class TestPower: @pytest.mark.parametrize("val_type", ALL_DTYPES) @pytest.mark.parametrize("data_type", ALL_DTYPES) - @pytest.mark.parametrize("val", [1.5, 1, 3], ids=["1.5", "1", "3"]) + @pytest.mark.parametrize("val", [1.5, 1, 3]) @pytest.mark.parametrize( "array", [ @@ -911,12 +926,15 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.power(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.power, a, 2, out) - assert_raises(TypeError, numpy.power, a.asnumpy(), 2, out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.power, a, 2, out) @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") def test_complex_values(self): @@ -1124,9 +1142,12 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.subtract(a, b, out=out) - @pytest.mark.parametrize("out", [4, (), [], (3, 7), [2, 4]]) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.subtract, a, 2, out) - assert_raises(TypeError, numpy.subtract, a.asnumpy(), 2, out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) + @pytest.mark.parametrize( + "out", + [4, (), [], (3, 7), [2, 4]], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], + ) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.subtract, a, 2, out) diff --git a/dpnp/tests/test_copy.py b/dpnp/tests/test_copy.py index e2b161432abd..272755799820 100644 --- a/dpnp/tests/test_copy.py +++ b/dpnp/tests/test_copy.py @@ -2,10 +2,7 @@ import numpy import pytest -from numpy.testing import ( - assert_allclose, - assert_equal, -) +from numpy.testing import assert_allclose, assert_equal import dpnp diff --git a/dpnp/tests/test_fill.py b/dpnp/tests/test_fill.py index cc984f752428..b5b82e4cf052 100644 --- a/dpnp/tests/test_fill.py +++ b/dpnp/tests/test_fill.py @@ -1,22 +1,21 @@ import dpctl -import numpy as np import pytest from dpctl.utils import ExecutionPlacementError from numpy.testing import assert_array_equal -import dpnp as dnp +import dpnp @pytest.mark.parametrize( "val, error", [ - pytest.param(dnp.ones(2, dtype="i4"), ValueError, id="array"), + pytest.param(dpnp.ones(2, dtype="i4"), ValueError, id="array"), pytest.param(dict(), TypeError, id="dictionary"), pytest.param("0", TypeError, id="string"), ], ) def test_fill_non_scalar(val, error): - a = dnp.ones(5, dtype="i4") + a = dpnp.ones(5, dtype="i4") with pytest.raises(error): a.fill(val) @@ -25,18 +24,18 @@ def test_fill_compute_follows_data(): q1 = dpctl.SyclQueue() q2 = dpctl.SyclQueue() - a = dnp.ones(5, dtype="i4", sycl_queue=q1) - val = dnp.ones((), dtype=a.dtype, sycl_queue=q2) + a = dpnp.ones(5, dtype="i4", sycl_queue=q1) + val = dpnp.ones((), dtype=a.dtype, sycl_queue=q2) with pytest.raises(ExecutionPlacementError): a.fill(val) def test_fill_strided_array(): - a = dnp.zeros(100, dtype="i4") + a = dpnp.zeros(100, dtype="i4") b = a[::-2] - expected = dnp.tile(dnp.asarray([0, 1], dtype=a.dtype), 50) + expected = dpnp.tile(dpnp.asarray([0, 1], dtype=a.dtype), 50) b.fill(1) assert_array_equal(b, 1) @@ -45,10 +44,10 @@ def test_fill_strided_array(): @pytest.mark.parametrize("order", ["C", "F"]) def test_fill_strided_2d_array(order): - a = dnp.zeros((10, 10), dtype="i4", order=order) + a = dpnp.zeros((10, 10), dtype="i4", order=order) b = a[::-2, ::2] - expected = dnp.copy(a) + expected = dpnp.copy(a) expected[::-2, ::2] = 1 b.fill(1) @@ -58,14 +57,14 @@ def test_fill_strided_2d_array(order): @pytest.mark.parametrize("order", ["C", "F"]) def test_fill_memset(order): - a = dnp.ones((10, 10), dtype="i4", order=order) + a = dpnp.ones((10, 10), dtype="i4", order=order) a.fill(0) assert_array_equal(a, 0) def test_fill_float_complex_to_int(): - a = dnp.ones((10, 10), dtype="i4") + a = dpnp.ones((10, 10), dtype="i4") a.fill(complex(2, 0)) assert_array_equal(a, 2) @@ -75,13 +74,13 @@ def test_fill_float_complex_to_int(): def test_fill_complex_to_float(): - a = dnp.ones((10, 10), dtype="f4") + a = dpnp.ones((10, 10), dtype="f4") a.fill(complex(2, 0)) assert_array_equal(a, 2) def test_fill_bool(): - a = dnp.full(5, fill_value=7, dtype="i4") + a = dpnp.full(5, fill_value=7, dtype="i4") a.fill(True) assert_array_equal(a, 1) diff --git a/dpnp/tests/test_histogram.py b/dpnp/tests/test_histogram.py index 726de6a0fb01..4b7155073f02 100644 --- a/dpnp/tests/test_histogram.py +++ b/dpnp/tests/test_histogram.py @@ -2,7 +2,6 @@ import numpy import pytest from numpy.testing import ( - assert_, assert_allclose, assert_array_equal, assert_raises, @@ -407,8 +406,8 @@ def test_bin_edge_cases(self): # floating-point computations correctly place edge cases for x, left, right in zip(v, left_edges, right_edges): - assert_(x >= left) - assert_(x < right) + assert x >= left + assert x < right @pytest.mark.skipif(not has_support_aspect64(), reason="fp64 required") def test_last_bin_inclusive_range(self): @@ -612,7 +611,7 @@ def test_minlength(self, array, minlength): expected = numpy.bincount(np_a, minlength=minlength) result = dpnp.bincount(dpnp_a, minlength=minlength) - assert_allclose(expected, result) + assert_allclose(result, expected) @pytest.mark.filterwarnings("ignore::DeprecationWarning") @pytest.mark.parametrize( @@ -655,7 +654,7 @@ def test_weights(self, array, weights): expected = numpy.bincount(np_a, weights=np_weights) result = dpnp.bincount(dpnp_a, weights=dpnp_weights) - assert_allclose(expected, result) + assert_allclose(result, expected) @pytest.mark.parametrize( "data", diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index 891bc8e1887b..21826c5da759 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -151,7 +151,7 @@ class TestCholesky: [[[7, 2], [2, 7]], [[8, 3], [3, 8]]], ], ], - ids=["2D_array", "3D_array", "4D_array"], + ids=["2D", "3D", "4D"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_cholesky(self, array, dtype): @@ -171,7 +171,7 @@ def test_cholesky(self, array, dtype): [[[7, 2], [2, 7]], [[8, 3], [3, 8]]], ], ], - ids=["2D_array", "3D_array", "4D_array"], + ids=["2D", "3D", "4D"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_cholesky_upper(self, array, dtype): @@ -191,16 +191,14 @@ def test_cholesky_upper(self, array, dtype): ) else: reconstructed = res_reshaped[idx].T @ res_reshaped[idx] - assert_dtype_allclose( - reconstructed, ia_reshaped[idx], check_type=False - ) + assert dpnp.allclose(reconstructed, ia_reshaped[idx]) else: # Reconstruct the matrix using the Cholesky decomposition result if dpnp.issubdtype(dtype, dpnp.complexfloating): reconstructed = result.T.conj() @ result else: reconstructed = result.T @ result - assert_dtype_allclose(reconstructed, ia, check_type=False) + assert dpnp.allclose(reconstructed, ia) # upper parameter support will be added in numpy 2.0 version @testing.with_requires("numpy>=2.0") @@ -214,7 +212,7 @@ def test_cholesky_upper(self, array, dtype): [[[7, 2], [2, 7]], [[8, 3], [3, 8]]], ], ], - ids=["2D_array", "3D_array", "4D_array"], + ids=["2D", "3D", "4D"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_cholesky_upper_numpy(self, array, dtype): @@ -240,12 +238,12 @@ def test_cholesky_strides(self): # positive strides expected = numpy.linalg.cholesky(a_np[::2, ::2]) result = dpnp.linalg.cholesky(a_dp[::2, ::2]) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected) # negative strides expected = numpy.linalg.cholesky(a_np[::-2, ::-2]) result = dpnp.linalg.cholesky(a_dp[::-2, ::-2]) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected) @pytest.mark.parametrize( "shape", @@ -257,7 +255,7 @@ def test_cholesky_empty(self, shape): ia = dpnp.array(a) result = dpnp.linalg.cholesky(ia) expected = numpy.linalg.cholesky(a) - assert_array_equal(expected, result) + assert_array_equal(result, expected) def test_cholesky_errors(self): a_dp = dpnp.array([[1, 2], [2, 5]], dtype="float32") @@ -382,7 +380,7 @@ class TestDet: [[[1, 3], [3, 1]], [[0, 1], [1, 3]]], ], ], - ids=["2D_array", "3D_array", "4D_array"], + ids=["2D", "3D", "4D"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_det(self, array, dtype): @@ -390,7 +388,7 @@ def test_det(self, array, dtype): ia = dpnp.array(a) result = dpnp.linalg.det(ia) expected = numpy.linalg.det(a) - assert_allclose(expected, result) + assert_allclose(result, expected) def test_det_strides(self): a_np = numpy.array( @@ -408,12 +406,12 @@ def test_det_strides(self): # positive strides expected = numpy.linalg.det(a_np[::2, ::2]) result = dpnp.linalg.det(a_dp[::2, ::2]) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected, rtol=1e-5) # negative strides expected = numpy.linalg.det(a_np[::-2, ::-2]) result = dpnp.linalg.det(a_dp[::-2, ::-2]) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected) def test_det_empty(self): a = numpy.empty((0, 0, 2, 2), dtype=numpy.float32) @@ -425,7 +423,7 @@ def test_det_empty(self): assert dpnp_det.dtype == np_det.dtype assert dpnp_det.shape == np_det.shape - assert_allclose(np_det, dpnp_det) + assert_allclose(dpnp_det, np_det) @pytest.mark.parametrize( "matrix", @@ -453,7 +451,7 @@ def test_det_singular_matrix(self, matrix): expected = numpy.linalg.det(a_np) result = dpnp.linalg.det(a_dp) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected) # TODO: remove skipif when MKLD-13852 is resolved # _getrf_batch does not raise an error with singular matrices. @@ -468,7 +466,7 @@ def test_det_singular_matrix_3D(self): expected = numpy.linalg.det(a_np) result = dpnp.linalg.det(a_dp) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected) def test_det_errors(self): a_dp = dpnp.array([[1, 2], [3, 5]], dtype="float32") @@ -1456,12 +1454,12 @@ def test_different_paths(self, dtype): # Use einsum to compare to not have difference due to sum round-offs: result1 = dpnp.einsum(",i->", s_dp, a_dp) result2 = dpnp.einsum("i->", s_dp * a_dp) - assert_array_equal(result1.asnumpy(), result2.asnumpy()) + assert dpnp.allclose(result1, result2) # contig + scalar -> scalar # Use einsum to compare to not have difference due to sum round-offs: result3 = dpnp.einsum("i,->", a_dp, s_dp) - assert_array_equal(result2.asnumpy(), result3.asnumpy()) + assert dpnp.allclose(result2, result3) # contig + contig + contig -> scalar a = numpy.array([0.5, 0.5, 0.25, 4.5, 3.0], dtype=dtype) @@ -1683,7 +1681,7 @@ class TestInv: [[[1, 3], [3, 1]], [[0, 1], [1, 3]]], ], ], - ids=["2D_array", "3D_array", "4D_array"], + ids=["2D", "3D", "4D"], ) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_inv(self, array, dtype): @@ -1709,12 +1707,12 @@ def test_inv_strides(self): # positive strides expected = numpy.linalg.inv(a_np[::2, ::2]) result = dpnp.linalg.inv(a_dp[::2, ::2]) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected, rtol=1e-6) # negative strides expected = numpy.linalg.inv(a_np[::-2, ::-2]) result = dpnp.linalg.inv(a_dp[::-2, ::-2]) - assert_allclose(expected, result, rtol=1e-3, atol=1e-4) + assert_allclose(result, expected, rtol=1e-6) @pytest.mark.parametrize( "shape", @@ -2065,7 +2063,7 @@ def test_matrix_transpose(): expected = numpy.linalg.matrix_transpose(a) result = dpnp.linalg.matrix_transpose(a_dp) - assert_allclose(expected, result) + assert_allclose(result, expected) with assert_raises_regex( ValueError, "array must be at least 2-dimensional" @@ -2452,9 +2450,9 @@ def test_qr_large(self, dtype, shape, mode): ) else: # mode=="raw" dpnp_q, dpnp_r = dpnp.linalg.qr(ia, mode) - assert_allclose(np_q, dpnp_q, atol=1e-4) + assert_allclose(dpnp_q, np_q, atol=1e-4) if mode in ("raw", "r"): - assert_allclose(np_r, dpnp_r, atol=1e-4) + assert_allclose(dpnp_r, np_r, atol=1e-4) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) @pytest.mark.parametrize( @@ -2553,7 +2551,7 @@ def test_solve(self, dtype): expected = numpy.linalg.solve(a_np, a_np) result = dpnp.linalg.solve(a_dp, a_dp) - assert_allclose(expected, result, rtol=1e-06) + assert_allclose(result, expected) @testing.with_requires("numpy>=2.0") @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) @@ -2626,12 +2624,12 @@ def test_solve_strides(self): # positive strides expected = numpy.linalg.solve(a_np[::2, ::2], b_np[::2]) result = dpnp.linalg.solve(a_dp[::2, ::2], b_dp[::2]) - assert_allclose(expected, result, rtol=1e-05) + assert_allclose(result, expected, rtol=1e-6) # negative strides expected = numpy.linalg.solve(a_np[::-2, ::-2], b_np[::-2]) result = dpnp.linalg.solve(a_dp[::-2, ::-2], b_dp[::-2]) - assert_allclose(expected, result, rtol=1e-05) + assert_allclose(result, expected) @pytest.mark.parametrize( "matrix, vector", @@ -2700,8 +2698,8 @@ def test_slogdet_2d(self, dtype): result = dpnp.linalg.slogdet(a_dp) sign_result, logdet_result = result.sign, result.logabsdet - assert_allclose(sign_expected, sign_result) - assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4) + assert_allclose(sign_result, sign_expected) + assert_allclose(logdet_result, logdet_expected) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_slogdet_3d(self, dtype): @@ -2719,8 +2717,8 @@ def test_slogdet_3d(self, dtype): result = dpnp.linalg.slogdet(a_dp) sign_result, logdet_result = result.sign, result.logabsdet - assert_allclose(sign_expected, sign_result) - assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4) + assert_allclose(sign_result, sign_expected) + assert_allclose(logdet_result, logdet_expected) def test_slogdet_strides(self): a_np = numpy.array( @@ -2739,15 +2737,15 @@ def test_slogdet_strides(self): sign_expected, logdet_expected = numpy.linalg.slogdet(a_np[::2, ::2]) result = dpnp.linalg.slogdet(a_dp[::2, ::2]) sign_result, logdet_result = result.sign, result.logabsdet - assert_allclose(sign_expected, sign_result) - assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4) + assert_allclose(sign_result, sign_expected) + assert_allclose(logdet_result, logdet_expected) # negative strides sign_expected, logdet_expected = numpy.linalg.slogdet(a_np[::-2, ::-2]) result = dpnp.linalg.slogdet(a_dp[::-2, ::-2]) sign_result, logdet_result = result.sign, result.logabsdet - assert_allclose(sign_expected, sign_result) - assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4) + assert_allclose(sign_result, sign_expected) + assert_allclose(logdet_result, logdet_expected) @pytest.mark.parametrize( "matrix", @@ -2776,8 +2774,8 @@ def test_slogdet_singular_matrix(self, matrix): result = dpnp.linalg.slogdet(a_dp) sign_result, logdet_result = result.sign, result.logabsdet - assert_allclose(sign_expected, sign_result) - assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4) + assert_allclose(sign_result, sign_expected) + assert_allclose(logdet_result, logdet_expected) # TODO: remove skipif when MKLD-13852 is resolved # _getrf_batch does not raise an error with singular matrices. @@ -2793,8 +2791,8 @@ def test_slogdet_singular_matrix_3D(self): result = dpnp.linalg.slogdet(a_dp) sign_result, logdet_result = result.sign, result.logabsdet - assert_allclose(sign_expected, sign_result) - assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4) + assert_allclose(sign_result, sign_expected) + assert_allclose(logdet_result, logdet_expected) def test_slogdet_errors(self): a_dp = dpnp.array([[1, 2], [3, 5]], dtype="float32") @@ -2862,13 +2860,13 @@ def check_decomposition( np_vt[..., i, :] = -np_vt[..., i, :] for i in range(numpy.count_nonzero(np_s > tol)): assert_allclose( - dpnp.asnumpy(dp_u[..., :, i]), + dp_u[..., :, i], np_u[..., :, i], rtol=tol, atol=tol, ) assert_allclose( - dpnp.asnumpy(dp_vt[..., i, :]), + dp_vt[..., i, :], np_vt[..., i, :], rtol=tol, atol=tol, @@ -3034,7 +3032,7 @@ def test_pinv(self, dtype, shape): else: # a.ndim > 2 reconstructed = dpnp.matmul(a_dp, dpnp.matmul(B_dp, a_dp)) - assert_allclose(reconstructed, a_dp, rtol=tol, atol=tol) + assert dpnp.allclose(reconstructed, a_dp, rtol=tol, atol=tol) @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) @pytest.mark.parametrize( @@ -3054,7 +3052,7 @@ def test_pinv_hermitian(self, dtype, shape): tol = self._tol reconstructed = dpnp.dot(dpnp.dot(a_dp, B_dp), a_dp) - assert_allclose(reconstructed, a_dp, rtol=tol, atol=tol) + assert dpnp.allclose(reconstructed, a_dp, rtol=tol, atol=tol) # rtol kwarg was added in numpy 2.0 @testing.with_requires("numpy>=2.0") @@ -3092,18 +3090,15 @@ def test_pinv_strides(self): a = generate_random_numpy_array((5, 5)) a_dp = dpnp.array(a) - self.get_tol(a_dp.dtype) - tol = self._tol - # positive strides B = numpy.linalg.pinv(a[::2, ::2]) B_dp = dpnp.linalg.pinv(a_dp[::2, ::2]) - assert_allclose(B_dp, B, rtol=tol, atol=tol) + assert_allclose(B_dp, B, rtol=1e-6, atol=1e-6) # negative strides B = numpy.linalg.pinv(a[::-2, ::-2]) B_dp = dpnp.linalg.pinv(a_dp[::-2, ::-2]) - assert_allclose(B_dp, B, rtol=tol, atol=tol) + assert_allclose(B_dp, B, rtol=1e-6, atol=1e-6) def test_pinv_errors(self): a_dp = dpnp.array([[1, 2], [3, 4]], dtype="float32") diff --git a/dpnp/tests/test_logic.py b/dpnp/tests/test_logic.py index 658ef5b966b3..b820528f9470 100644 --- a/dpnp/tests/test_logic.py +++ b/dpnp/tests/test_logic.py @@ -25,8 +25,8 @@ def test_all_any(self, func, dtype, axis, keepdims): assert_allclose(result, expected) @pytest.mark.parametrize("func", ["all", "any"]) - @pytest.mark.parametrize("a_dtype", get_all_dtypes()) - @pytest.mark.parametrize("out_dtype", get_all_dtypes()) + @pytest.mark.parametrize("a_dtype", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("out_dtype", get_all_dtypes(no_none=True)) def test_all_any_out(self, func, a_dtype, out_dtype): dp_array = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=a_dtype) np_array = dpnp.asnumpy(dp_array) @@ -546,7 +546,7 @@ def test_array_equiv(a, b): result = dpnp.array_equiv(dpnp.array(a), dpnp.array(b)) expected = numpy.array_equiv(a, b) - assert_equal(expected, result) + assert_equal(result, expected) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True)) @@ -558,12 +558,12 @@ def test_array_equiv_dtype(dtype): result = dpnp.array_equiv(dpnp.array(a), dpnp.array(b)) expected = numpy.array_equiv(a, b) - assert_equal(expected, result) + assert_equal(result, expected) result = dpnp.array_equiv(dpnp.array(a), dpnp.array(c)) expected = numpy.array_equiv(a, c) - assert_equal(expected, result) + assert_equal(result, expected) @pytest.mark.parametrize("a", [numpy.array([1, 2]), numpy.array([1, 1])]) @@ -572,7 +572,7 @@ def test_array_equiv_scalar(a): result = dpnp.array_equiv(dpnp.array(a), b) expected = numpy.array_equiv(a, b) - assert_equal(expected, result) + assert_equal(result, expected) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_complex=True)) @@ -585,12 +585,12 @@ def test_array_equal_dtype(dtype, equal_nan): result = dpnp.array_equal(dpnp.array(a), dpnp.array(b), equal_nan=equal_nan) expected = numpy.array_equal(a, b, equal_nan=equal_nan) - assert_equal(expected, result) + assert_equal(result, expected) result = dpnp.array_equal(dpnp.array(a), dpnp.array(c), equal_nan=equal_nan) expected = numpy.array_equal(a, c, equal_nan=equal_nan) - assert_equal(expected, result) + assert_equal(result, expected) @pytest.mark.parametrize( @@ -605,11 +605,11 @@ def test_array_equal_same_arr(a): expected = numpy.array_equal(a, a) b = dpnp.array(a) result = dpnp.array_equal(b, b) - assert_equal(expected, result) + assert_equal(result, expected) expected = numpy.array_equal(a, a, equal_nan=True) result = dpnp.array_equal(b, b, equal_nan=True) - assert_equal(expected, result) + assert_equal(result, expected) @pytest.mark.parametrize( @@ -625,4 +625,4 @@ def test_array_equal_nan(a): b = numpy.array([1.0, 2.0]) result = dpnp.array_equal(dpnp.array(a), dpnp.array(b), equal_nan=True) expected = numpy.array_equal(a, b, equal_nan=True) - assert_equal(expected, result) + assert_equal(result, expected) diff --git a/dpnp/tests/test_mathematical.py b/dpnp/tests/test_mathematical.py index 6fe2e70bb59d..d53be2fb4665 100644 --- a/dpnp/tests/test_mathematical.py +++ b/dpnp/tests/test_mathematical.py @@ -2518,16 +2518,15 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.hypot(dp_array1, dp_array2, out=dp_out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) @pytest.mark.parametrize( "out", [4, (), [], (3, 7), [2, 4]], - ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], ) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.hypot, a, 2, out) - assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.hypot, a, 2, out) class TestLogSumExp: diff --git a/dpnp/tests/test_nanfunctions.py b/dpnp/tests/test_nanfunctions.py index a0b5d808697a..ebc512fcfa83 100644 --- a/dpnp/tests/test_nanfunctions.py +++ b/dpnp/tests/test_nanfunctions.py @@ -89,7 +89,7 @@ def test_out_dtype(self, func, arr_dt, out_dt): if numpy.can_cast(out.dtype, numpy.intp, casting="safe"): result = getattr(dpnp, func)(ia, out=iout, axis=1) expected = getattr(numpy, func)(a, out=out, axis=1) - assert_array_equal(expected, result) + assert_array_equal(result, expected) assert result is iout else: assert_raises(TypeError, getattr(numpy, func), a, out=out, axis=1) @@ -283,7 +283,7 @@ def test_out_dtype(self, func, arr_dt, out_dt): result = getattr(dpnp, func)(ia, out=iout, axis=1) expected = getattr(numpy, func)(a, out=out, axis=1) - assert_array_equal(expected, result) + assert_array_equal(result, expected) assert result is iout @pytest.mark.parametrize("func", ["nanmax", "nanmin"]) @@ -390,7 +390,7 @@ def test_nanmean_scalar(self): result = dpnp.nanmean(dp_array) expected = numpy.nanmean(np_array) - assert_allclose(expected, result) + assert_allclose(result, expected) def test_nanmean_error(self): ia = dpnp.arange(5, dtype=dpnp.float32) @@ -599,7 +599,7 @@ def test_nanprod_out_dtype(self, arr_dt, out_dt, dtype): result = dpnp.nanprod(ia, out=iout, dtype=dtype, axis=1) expected = numpy.nanprod(a, out=out, dtype=dtype, axis=1) - assert_array_equal(expected, result) + assert_array_equal(result, expected) assert result is iout def test_nanprod_Error(self): diff --git a/dpnp/tests/test_search.py b/dpnp/tests/test_search.py index 693a3b86b9bd..b7103cf427b3 100644 --- a/dpnp/tests/test_search.py +++ b/dpnp/tests/test_search.py @@ -1,107 +1,92 @@ import dpctl.tensor as dpt import numpy import pytest -from numpy.testing import ( - assert_allclose, - assert_array_equal, - assert_equal, - assert_raises, -) +from numpy.testing import assert_array_equal, assert_equal, assert_raises import dpnp -from .helper import assert_dtype_allclose, get_all_dtypes +from .helper import ( + generate_random_numpy_array, + get_all_dtypes, +) +from .third_party.cupy import testing +@testing.parameterize( + *testing.product( + { + "func": ("argmax", "argmin"), + } + ) +) class TestArgmaxArgmin: - @pytest.mark.parametrize("func", ["argmax", "argmin"]) @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2]) @pytest.mark.parametrize("keepdims", [False, True]) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) - def test_func(self, func, axis, keepdims, dtype): - a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8)) - ia = dpnp.array(a) - - np_res = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) - dpnp_res = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) - assert_dtype_allclose(dpnp_res, np_res) - - @pytest.mark.parametrize("func", ["argmax", "argmin"]) - @pytest.mark.parametrize("axis", [None, 0, 1, -1]) - @pytest.mark.parametrize("keepdims", [False, True]) - def test_bool(self, func, axis, keepdims): - a = numpy.arange(2, dtype=numpy.bool_) - a = numpy.tile(a, (2, 2)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) + def test_func(self, axis, keepdims, dtype): + a = generate_random_numpy_array((4, 4, 6, 8), dtype=dtype) ia = dpnp.array(a) - np_res = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) - dpnp_res = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(numpy, self.func)(a, axis=axis, keepdims=keepdims) + result = getattr(dpnp, self.func)(ia, axis=axis, keepdims=keepdims) + assert_array_equal(result, expected) - @pytest.mark.parametrize("func", ["argmax", "argmin"]) - def test_out(self, func): - a = numpy.arange(12, dtype=numpy.float32).reshape((2, 2, 3)) + def test_out(self): + a = generate_random_numpy_array((2, 2, 3), dtype=numpy.float32) ia = dpnp.array(a) # out is dpnp_array - np_res = getattr(numpy, func)(a, axis=0) - dpnp_out = dpnp.empty(np_res.shape, dtype=np_res.dtype) - dpnp_res = getattr(dpnp, func)(ia, axis=0, out=dpnp_out) - assert dpnp_out is dpnp_res - assert_allclose(dpnp_res, np_res) + expected = getattr(numpy, self.func)(a, axis=0) + dpnp_out = dpnp.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, self.func)(ia, axis=0, out=dpnp_out) + assert dpnp_out is result + assert_array_equal(result, expected) # out is usm_ndarray - dpt_out = dpt.empty(np_res.shape, dtype=np_res.dtype) - dpnp_res = getattr(dpnp, func)(ia, axis=0, out=dpt_out) - assert dpt_out is dpnp_res.get_array() - assert_allclose(dpnp_res, np_res) + dpt_out = dpt.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, self.func)(ia, axis=0, out=dpt_out) + assert dpt_out is result.get_array() + assert_array_equal(result, expected) # out is a numpy array -> TypeError - dpnp_res = numpy.empty_like(np_res) + result = numpy.empty_like(expected) with pytest.raises(TypeError): - getattr(dpnp, func)(ia, axis=0, out=dpnp_res) + getattr(dpnp, self.func)(ia, axis=0, out=result) # out shape is incorrect -> ValueError - dpnp_res = dpnp.array(numpy.zeros((2, 2)), dtype=dpnp.intp) + result = dpnp.array(numpy.zeros((2, 2)), dtype=dpnp.intp) with pytest.raises(ValueError): - getattr(dpnp, func)(ia, axis=0, out=dpnp_res) + getattr(dpnp, self.func)(ia, axis=0, out=result) - @pytest.mark.parametrize("func", ["argmax", "argmin"]) @pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) - def test_out_dtype(self, func, arr_dt, out_dt): - a = ( - numpy.arange(12, dtype=numpy.float32) - .reshape((2, 2, 3)) - .astype(dtype=arr_dt) - ) + def test_out_dtype(self, arr_dt, out_dt): + a = generate_random_numpy_array((2, 2, 3), dtype=arr_dt) out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt) - - ia = dpnp.array(a) - iout = dpnp.array(out) + ia, iout = dpnp.array(a), dpnp.array(out) if numpy.can_cast(out.dtype, numpy.intp, casting="safe"): - result = getattr(dpnp, func)(ia, out=iout, axis=1) - expected = getattr(numpy, func)(a, out=out, axis=1) - assert_array_equal(expected, result) + result = getattr(dpnp, self.func)(ia, out=iout, axis=1) + expected = getattr(numpy, self.func)(a, out=out, axis=1) + assert_array_equal(result, expected) assert result is iout else: - assert_raises(TypeError, getattr(numpy, func), a, out=out, axis=1) - assert_raises(TypeError, getattr(dpnp, func), ia, out=iout, axis=1) + assert_raises( + TypeError, getattr(numpy, self.func), a, out=out, axis=1 + ) + assert_raises( + TypeError, getattr(dpnp, self.func), ia, out=iout, axis=1 + ) @pytest.mark.parametrize("axis", [None, 0, 1, -1]) @pytest.mark.parametrize("keepdims", [False, True]) def test_ndarray(self, axis, keepdims): - a = numpy.arange(192, dtype=numpy.float32).reshape((4, 6, 8)) + a = generate_random_numpy_array((4, 6, 8), dtype=numpy.float32) ia = dpnp.array(a) - np_res = a.argmax(axis=axis, keepdims=keepdims) - dpnp_res = ia.argmax(axis=axis, keepdims=keepdims) - assert_dtype_allclose(dpnp_res, np_res) - - np_res = a.argmin(axis=axis, keepdims=keepdims) - dpnp_res = ia.argmin(axis=axis, keepdims=keepdims) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(a, self.func)(axis=axis, keepdims=keepdims) + result = getattr(ia, self.func)(axis=axis, keepdims=keepdims) + assert_array_equal(result, expected) class TestArgwhere: @@ -168,13 +153,13 @@ def test_basic(self, dtype): a = numpy.ones(53, dtype=bool) ia = dpnp.array(a) - np_res = numpy.where(a, dtype(0), dtype(1)) - dpnp_res = dpnp.where(ia, dtype(0), dtype(1)) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(a, dtype(0), dtype(1)) + result = dpnp.where(ia, dtype(0), dtype(1)) + assert_array_equal(result, expected) - np_res = numpy.where(~a, dtype(0), dtype(1)) - dpnp_res = dpnp.where(~ia, dtype(0), dtype(1)) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(~a, dtype(0), dtype(1)) + result = dpnp.where(~ia, dtype(0), dtype(1)) + assert_array_equal(result, expected) d = numpy.ones_like(a).astype(dtype) e = numpy.zeros_like(d) @@ -184,13 +169,13 @@ def test_basic(self, dtype): id = dpnp.array(d) ie = dpnp.array(e) - np_res = numpy.where(a, e, e) - dpnp_res = dpnp.where(ia, ie, ie) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(a, e, e) + result = dpnp.where(ia, ie, ie) + assert_array_equal(result, expected) - np_res = numpy.where(a, d, e) - dpnp_res = dpnp.where(ia, id, ie) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(a, d, e) + result = dpnp.where(ia, id, ie) + assert_array_equal(result, expected) @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( @@ -243,9 +228,9 @@ def test_strided(self, dtype, slice_a, slice_d, slice_e): id = dpnp.array(d) ie = dpnp.array(e) - np_res = numpy.where(a[slice_a], d[slice_d], e[slice_e]) - dpnp_res = dpnp.where(ia[slice_a], id[slice_d], ie[slice_e]) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(a[slice_a], d[slice_d], e[slice_e]) + result = dpnp.where(ia[slice_a], id[slice_d], ie[slice_e]) + assert_array_equal(result, expected) def test_zero_sized(self): a = numpy.array([], dtype=bool).reshape(0, 3) @@ -254,9 +239,9 @@ def test_zero_sized(self): ia = dpnp.array(a) ib = dpnp.array(b) - np_res = numpy.where(a, 0, b) - dpnp_res = dpnp.where(ia, 0, ib) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(a, 0, b) + result = dpnp.where(ia, 0, ib) + assert_array_equal(result, expected) def test_ndim(self): a = numpy.zeros((2, 25)) @@ -267,13 +252,13 @@ def test_ndim(self): ib = dpnp.array(b) ic = dpnp.array(c) - np_res = numpy.where(c[:, numpy.newaxis], a, b) - dpnp_res = dpnp.where(ic[:, dpnp.newaxis], ia, ib) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(c[:, numpy.newaxis], a, b) + result = dpnp.where(ic[:, dpnp.newaxis], ia, ib) + assert_array_equal(result, expected) - np_res = numpy.where(c, a.T, b.T) - dpnp_res = dpnp.where(ic, ia.T, ib.T) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(c, a.T, b.T) + result = dpnp.where(ic, ia.T, ib.T) + assert_array_equal(result, expected) def test_dtype_mix(self): a = numpy.uint32(1) @@ -302,25 +287,25 @@ def test_dtype_mix(self): ib = dpnp.array(b) ic = dpnp.array(c) - np_res = numpy.where(c, a, b) - dpnp_res = dpnp.where(ic, ia, ib) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(c, a, b) + result = dpnp.where(ic, ia, ib) + assert_array_equal(result, expected) b = b.astype(numpy.int64) ib = dpnp.array(b) - np_res = numpy.where(c, a, b) - dpnp_res = dpnp.where(ic, ia, ib) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(c, a, b) + result = dpnp.where(ic, ia, ib) + assert_array_equal(result, expected) # non bool mask c = c.astype(int) c[c != 0] = 34242324 ic = dpnp.array(c) - np_res = numpy.where(c, a, b) - dpnp_res = dpnp.where(ic, ia, ib) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(c, a, b) + result = dpnp.where(ic, ia, ib) + assert_array_equal(result, expected) # invert tmpmask = c != 0 @@ -328,9 +313,9 @@ def test_dtype_mix(self): c[tmpmask] = 0 ic = dpnp.array(c) - np_res = numpy.where(c, a, b) - dpnp_res = dpnp.where(ic, ia, ib) - assert_array_equal(np_res, dpnp_res) + expected = numpy.where(c, a, b) + result = dpnp.where(ic, ia, ib) + assert_array_equal(result, expected) def test_error(self): c = dpnp.array([True, True]) @@ -343,9 +328,9 @@ def test_empty_result(self): a = numpy.zeros((1, 1)) ia = dpnp.array(a) - np_res = numpy.vstack(numpy.where(a == 99.0)) - dpnp_res = dpnp.vstack(dpnp.where(ia == 99.0)) - assert_array_equal(np_res, dpnp_res) + expected = numpy.vstack(numpy.where(a == 99.0)) + result = dpnp.vstack(dpnp.where(ia == 99.0)) + assert_array_equal(result, expected) @pytest.mark.parametrize("x_dt", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("y_dt", get_all_dtypes(no_none=True)) @@ -362,7 +347,7 @@ def test_out(self, x_dt, y_dt): result = dpnp.where(icond, ix, iy, out=iout) expected = numpy.where(cond, x, y) - assert_array_equal(expected, result) + assert_array_equal(result, expected) @pytest.mark.parametrize("order", ["C", "F", "A", "K", None]) def test_order(self, order): @@ -376,7 +361,7 @@ def test_order(self, order): result = dpnp.where(icond, ix, iy, order=order) expected = numpy.where(cond, x, y) - assert_array_equal(expected, result) + assert_array_equal(result, expected) if order == "F": assert result.flags.f_contiguous diff --git a/dpnp/tests/test_special.py b/dpnp/tests/test_special.py index 7d2e1b7cb7b0..3c567dcd3ca6 100644 --- a/dpnp/tests/test_special.py +++ b/dpnp/tests/test_special.py @@ -8,9 +8,7 @@ def test_erf(): a = numpy.linspace(2.0, 3.0, num=10) - ia = dpnp.linspace(2.0, 3.0, num=10) - - assert_allclose(a, ia) + ia = dpnp.array(a) expected = numpy.empty_like(a) for idx, val in enumerate(a): diff --git a/dpnp/tests/test_statistics.py b/dpnp/tests/test_statistics.py index bbf5cbf3ee4f..b2e40a595bad 100644 --- a/dpnp/tests/test_statistics.py +++ b/dpnp/tests/test_statistics.py @@ -586,7 +586,7 @@ def test_false_rowvar_1x3(self): expected = numpy.cov(a, rowvar=False) result = dpnp.cov(ia, rowvar=False) - assert_allclose(expected, result) + assert_allclose(result, expected) # numpy 2.2 properly transposes 2d array when rowvar=False @with_requires("numpy>=2.2") @@ -597,88 +597,90 @@ def test_true_rowvar(self): expected = numpy.cov(a, ddof=0, rowvar=True) result = dpnp.cov(ia, ddof=0, rowvar=True) - assert_allclose(expected, result) + assert_allclose(result, expected) +@testing.parameterize( + *testing.product( + { + "func": ("max", "min"), + } + ) +) class TestMaxMin: - @pytest.mark.parametrize("func", ["max", "min"]) @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2, (1, 2), (0, -2)]) @pytest.mark.parametrize("keepdims", [False, True]) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) - def test_func(self, func, axis, keepdims, dtype): + def test_func(self, axis, keepdims, dtype): a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8)) ia = dpnp.array(a) - expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) - result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) + expected = getattr(numpy, self.func)(a, axis=axis, keepdims=keepdims) + result = getattr(dpnp, self.func)(ia, axis=axis, keepdims=keepdims) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("func", ["max", "min"]) @pytest.mark.parametrize("axis", [None, 0, 1, -1]) @pytest.mark.parametrize("keepdims", [False, True]) - def test_bool(self, func, axis, keepdims): + def test_bool(self, axis, keepdims): a = numpy.arange(2, dtype=numpy.bool_) a = numpy.tile(a, (2, 2)) ia = dpnp.array(a) - expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) - result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) + expected = getattr(numpy, self.func)(a, axis=axis, keepdims=keepdims) + result = getattr(dpnp, self.func)(ia, axis=axis, keepdims=keepdims) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("func", ["max", "min"]) - def test_out(self, func): + def test_out(self): a = numpy.arange(12, dtype=numpy.float32).reshape((2, 2, 3)) ia = dpnp.array(a) # out is dpnp_array - expected = getattr(numpy, func)(a, axis=0) + expected = getattr(numpy, self.func)(a, axis=0) dpnp_out = dpnp.empty(expected.shape, dtype=expected.dtype) - result = getattr(dpnp, func)(ia, axis=0, out=dpnp_out) + result = getattr(dpnp, self.func)(ia, axis=0, out=dpnp_out) assert dpnp_out is result assert_allclose(result, expected) # out is usm_ndarray dpt_out = dpt.empty(expected.shape, dtype=expected.dtype) - result = getattr(dpnp, func)(ia, axis=0, out=dpt_out) + result = getattr(dpnp, self.func)(ia, axis=0, out=dpt_out) assert dpt_out is result.get_array() assert_allclose(result, expected) # output is numpy array -> Error result = numpy.empty_like(expected) with pytest.raises(TypeError): - getattr(dpnp, func)(ia, axis=0, out=result) + getattr(dpnp, self.func)(ia, axis=0, out=result) # output has incorrect shape -> Error result = dpnp.array(numpy.zeros((4, 2))) with pytest.raises(ValueError): - getattr(dpnp, func)(ia, axis=0, out=result) + getattr(dpnp, self.func)(ia, axis=0, out=result) @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("func", ["max", "min"]) @pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) - def test_out_dtype(self, func, arr_dt, out_dt): - a = numpy.arange(12).reshape(2, 2, 3).astype(arr_dt) + def test_out_dtype(self, arr_dt, out_dt): + # if out_dt is unsigned, input cannot be signed otherwise overflow occurs + low = 0 if dpnp.issubdtype(out_dt, dpnp.unsignedinteger) else -10 + a = generate_random_numpy_array((2, 2, 3), dtype=arr_dt, low=low) out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - ia = dpnp.array(a) - iout = dpnp.array(out) - - result = getattr(dpnp, func)(ia, out=iout, axis=1) - expected = getattr(numpy, func)(a, out=out, axis=1) - assert_array_equal(result, expected) + result = getattr(dpnp, self.func)(ia, out=iout, axis=1) + expected = getattr(numpy, self.func)(a, out=out, axis=1) + assert_dtype_allclose(result, expected) assert result is iout - @pytest.mark.parametrize("func", ["max", "min"]) - def test_error(self, func): + def test_error(self): ia = dpnp.arange(5) # where is not supported with pytest.raises(NotImplementedError): - getattr(dpnp, func)(ia, where=False) + getattr(dpnp, self.func)(ia, where=False) # initial is not supported with pytest.raises(NotImplementedError): - getattr(dpnp, func)(ia, initial=6) + getattr(dpnp, self.func)(ia, initial=6) class TestMean: diff --git a/dpnp/tests/test_sum.py b/dpnp/tests/test_sum.py index 54a998bfa3a6..8712e524b539 100644 --- a/dpnp/tests/test_sum.py +++ b/dpnp/tests/test_sum.py @@ -51,36 +51,29 @@ def test_sum(shape, dtype_in, dtype_out, transpose, keepdims, order): axes.append(tuple(axes_range)) for axis in axes: - if ( - numpy.issubdtype(dtype_out, numpy.bool_) - and numpy.issubdtype(dtype_in, numpy.signedinteger) - and not a_np.sum(axis=axis).all() - ): - # TODO: remove workaround when dpctl-issue#1944 is resolved - a = a.astype(dpnp.bool) - dpnp_res = a.sum(axis=axis, dtype=dtype_out, keepdims=keepdims) - else: - dpnp_res = a.sum(axis=axis, dtype=dtype_out, keepdims=keepdims) + dpnp_res = a.sum(axis=axis, dtype=dtype_out, keepdims=keepdims) numpy_res = a_np.sum(axis=axis, dtype=dtype_out, keepdims=keepdims) assert_dtype_allclose(dpnp_res, numpy_res, factor=16) -@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) +@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True)) def test_sum_empty_out(dtype): a = dpnp.empty((1, 2, 0, 4), dtype=dtype) out = dpnp.ones((), dtype=dtype) res = a.sum(out=out) - assert_array_equal(out.asnumpy(), res.asnumpy()) - assert_array_equal(out.asnumpy(), numpy.array(0, dtype=dtype)) + assert out is res + assert_array_equal(out, numpy.array(0, dtype=dtype)) -@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True, no_bool=True)) +@pytest.mark.parametrize( + "dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True) +) @pytest.mark.parametrize("axis", [None, 0, 1, 2, 3]) def test_sum_empty(dtype, axis): a = numpy.empty((1, 2, 0, 4), dtype=dtype) numpy_res = a.sum(axis=axis) dpnp_res = dpnp.array(a).sum(axis=axis) - assert_array_equal(numpy_res, dpnp_res.asnumpy()) + assert_array_equal(numpy_res, dpnp_res) @pytest.mark.parametrize("dtype", get_float_dtypes()) diff --git a/dpnp/tests/test_umath.py b/dpnp/tests/test_umath.py index 199dbe3850aa..8152af1f3ab8 100644 --- a/dpnp/tests/test_umath.py +++ b/dpnp/tests/test_umath.py @@ -110,10 +110,7 @@ def test_umaths(test_cases): ): pytest.skip("numpy.ldexp doesn't have a loop for the input types") - # original expected = getattr(numpy, umath)(*args) - - # DPNP result = getattr(dpnp, umath)(*iargs) assert_allclose(result, expected, rtol=1e-6) @@ -129,7 +126,7 @@ def _get_numpy_arrays_1in_1out(func_name, dtype, range): low = range[0] high = range[1] size = range[2] - if dtype == numpy.bool_: + if dtype == dpnp.bool: np_array = numpy.arange(2, dtype=dtype) result = getattr(numpy, func_name)(np_array) elif dpnp.issubdtype(dtype, dpnp.complexfloating): @@ -155,7 +152,7 @@ def _get_numpy_arrays_2in_1out(func_name, dtype, range): low = range[0] high = range[1] size = range[2] - if dtype == numpy.bool_: + if dtype == dpnp.bool: np_array1 = numpy.arange(2, dtype=dtype) np_array2 = numpy.arange(2, dtype=dtype) result = getattr(numpy, func_name)(np_array1, np_array2) @@ -471,12 +468,11 @@ def test_values(self, dt): assert_dtype_allclose(result, expected) @pytest.mark.parametrize( - "dt", - [numpy.bool_, numpy.int32, numpy.int64, numpy.float32, numpy.float64], + "dt", get_all_dtypes(no_none=True, no_complex=True) ) def test_range(self, dt): - a = numpy.array([1000000, -1000000, 1000200, -1000200], dtype=dt) - b = numpy.array([1000200, -1000200, 1000000, -1000000], dtype=dt) + a = get_abs_array([1000000, -1000000, 1000200, -1000200], dtype=dt) + b = get_abs_array([1000200, -1000200, 1000000, -1000000], dtype=dt) ia, ib = dpnp.array(a), dpnp.array(b) result = dpnp.logaddexp2(ia, ib) @@ -637,7 +633,7 @@ def test_square(self, dtype): ) dp_array = dpnp.array(np_array) - out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype + out_dtype = numpy.int8 if dtype == dpnp.bool else dtype dp_out = dpnp.empty(expected.shape, dtype=out_dtype) result = dpnp.square(dp_array, out=dp_out) @@ -662,16 +658,15 @@ def test_invalid_shape(self, shape): with pytest.raises(ValueError): dpnp.square(dp_array, out=dp_out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) @pytest.mark.parametrize( "out", [4, (), [], (3, 7), [2, 4]], - ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], ) - def test_invalid_out(self, out): - a = dpnp.arange(10) - - assert_raises(TypeError, dpnp.square, a, out) - assert_raises(TypeError, numpy.square, a.asnumpy(), out) + def test_invalid_out(self, xp, out): + a = xp.arange(10) + assert_raises(TypeError, xp.square, a, out) class TestUmath: @@ -762,16 +757,16 @@ def test_invalid_shape(self, func_params, shape): with pytest.raises(ValueError): getattr(dpnp, func_name)(dp_array, out=dp_out) + @pytest.mark.parametrize("xp", [dpnp, numpy]) @pytest.mark.parametrize( "out", [4, (), [], (3, 7), [2, 4]], - ids=["4", "()", "[]", "(3, 7)", "[2, 4]"], + ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"], ) - def test_invalid_out(self, func_params, out): + def test_invalid_out(self, func_params, xp, out): func_name = func_params["func_name"] - a = dpnp.arange(10) - assert_raises(TypeError, getattr(dpnp, func_name), a, out) - assert_raises(TypeError, getattr(numpy, func_name), a.asnumpy(), out) + a = xp.arange(10) + assert_raises(TypeError, getattr(xp, func_name), a, out) def test_trigonometric_hyperbolic_aliases():