diff --git a/dpnp/tests/test_indexing.py b/dpnp/tests/test_indexing.py index 1efef172cf6c..5439b4dc484e 100644 --- a/dpnp/tests/test_indexing.py +++ b/dpnp/tests/test_indexing.py @@ -1200,15 +1200,17 @@ def test_empty_indices_error(self): ) def test_empty_indices(self): - assert_equal( - dpnp.ravel_multi_index( - (dpnp.array([], dtype=int), dpnp.array([], dtype=int)), (5, 3) - ), - [], - ) - assert_equal( - dpnp.ravel_multi_index(dpnp.array([[], []], dtype=int), (5, 3)), [] - ) + a = numpy.array([], dtype=int) + ia = dpnp.array(a) + result = dpnp.ravel_multi_index((ia, ia), (5, 3)) + expected = numpy.ravel_multi_index((a, a), (5, 3)) + assert_equal(result, expected) + + a = numpy.array([[], []], dtype=int) + ia = dpnp.array(a) + result = dpnp.ravel_multi_index(ia, (5, 3)) + expected = numpy.ravel_multi_index(a, (5, 3)) + assert_equal(result, expected) class TestUnravelIndex: diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index 21826c5da759..46fbad6dcb9b 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -293,7 +293,9 @@ def test_empty(self, shape, p): expected = numpy.linalg.cond(a, p=p) assert_dtype_allclose(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, 4), (2, 4, 3, 3)], ids=["(4, 4)", "(2, 4, 3, 3)"] ) @@ -2408,10 +2410,8 @@ def test_qr(self, dtype, shape, mode): if mode in ("complete", "reduced"): result = dpnp.linalg.qr(ia, mode) dpnp_q, dpnp_r = result.Q, result.R - assert_almost_equal( - dpnp.matmul(dpnp_q, dpnp_r), - a, - decimal=5, + assert dpnp.allclose( + dpnp.matmul(dpnp_q, dpnp_r), ia, atol=1e-05 ) else: # mode=="raw" dpnp_q, dpnp_r = dpnp.linalg.qr(ia, mode) @@ -2424,15 +2424,13 @@ def test_qr(self, dtype, shape, mode): @pytest.mark.parametrize( "shape", [(32, 32), (8, 16, 16)], - ids=[ - "(32, 32)", - "(8, 16, 16)", - ], + ids=["(32, 32)", "(8, 16, 16)"], ) @pytest.mark.parametrize("mode", ["r", "raw", "complete", "reduced"]) def test_qr_large(self, dtype, shape, mode): a = generate_random_numpy_array(shape, dtype, seed_value=81) ia = dpnp.array(a) + if mode == "r": np_r = numpy.linalg.qr(a, mode) dpnp_r = dpnp.linalg.qr(ia, mode) @@ -2443,11 +2441,7 @@ def test_qr_large(self, dtype, shape, mode): if mode in ("complete", "reduced"): result = dpnp.linalg.qr(ia, mode) dpnp_q, dpnp_r = result.Q, result.R - assert_almost_equal( - dpnp.matmul(dpnp_q, dpnp_r), - a, - decimal=5, - ) + assert dpnp.allclose(dpnp.matmul(dpnp_q, dpnp_r), ia, atol=1e-5) else: # mode=="raw" dpnp_q, dpnp_r = dpnp.linalg.qr(ia, mode) assert_allclose(dpnp_q, np_q, atol=1e-4) @@ -3169,9 +3163,7 @@ def test_test_tensorinv_errors(self): class TestTensorsolve: @pytest.mark.parametrize("dtype", get_all_dtypes()) @pytest.mark.parametrize( - "axes", - [None, (1,), (2,)], - ids=["None", "(1,)", "(2,)"], + "axes", [None, (1,), (2,)], ids=["None", "(1,)", "(2,)"] ) def test_tensorsolve_axes(self, dtype, axes): a = numpy.eye(12).reshape(12, 3, 4).astype(dtype) diff --git a/dpnp/tests/test_nanfunctions.py b/dpnp/tests/test_nanfunctions.py index c8289b352e73..ed0d95d2c7af 100644 --- a/dpnp/tests/test_nanfunctions.py +++ b/dpnp/tests/test_nanfunctions.py @@ -32,15 +32,15 @@ class TestNanArgmaxNanArgmin: @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2]) @pytest.mark.parametrize("keepdims", [False, True]) - @pytest.mark.parametrize("dtype", get_float_dtypes()) - def test_func(self, func, axis, keepdims, dtype): + @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) + def test_basic(self, func, axis, keepdims, dtype): a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8)) a[2:3, 2, 3:4, 4] = numpy.nan 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, func)(a, axis=axis, keepdims=keepdims) + result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) + assert_dtype_allclose(result, expected) def test_out(self, func): a = numpy.arange(12, dtype=numpy.float32).reshape((2, 2, 3)) @@ -48,43 +48,44 @@ def test_out(self, func): 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, func)(a, axis=0) + iout = dpnp.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, func)(ia, axis=0, out=iout) + assert iout is result + assert_allclose(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, func)(ia, axis=0, out=dpt_out) + assert dpt_out is result.get_array() + assert_allclose(result, expected) # out is a numpy array -> TypeError - dpnp_res = numpy.empty_like(np_res) + iout = numpy.empty_like(expected) with pytest.raises(TypeError): - getattr(dpnp, func)(ia, axis=0, out=dpnp_res) + getattr(dpnp, func)(ia, axis=0, out=iout) # out shape is incorrect -> ValueError - dpnp_res = dpnp.array(numpy.zeros((2, 2)), dtype=dpnp.intp) + iout = dpnp.array(numpy.zeros((2, 2)), dtype=dpnp.intp) with pytest.raises(ValueError): - getattr(dpnp, func)(ia, axis=0, out=dpnp_res) + getattr(dpnp, func)(ia, axis=0, out=iout) - @pytest.mark.parametrize("in_dt", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("in_dt", get_float_complex_dtypes()) @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) def test_out_dtype(self, func, in_dt, out_dt): - a = generate_random_numpy_array((2, 2, 3), dtype=in_dt) + a = generate_random_numpy_array((2, 3, 4), dtype=in_dt) + a[:, :, 2] = numpy.nan out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt) 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) + result = getattr(dpnp, func)(ia, out=iout, axis=2) + expected = getattr(numpy, func)(a, out=out, axis=2) 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, func), a, out=out, axis=2) + assert_raises(TypeError, getattr(dpnp, func), ia, out=iout, axis=2) def test_error(self, func): ia = dpnp.arange(12, dtype=dpnp.float32).reshape((2, 2, 3)) @@ -196,29 +197,29 @@ def test_out(self, func, axis): class TestNanMaxNanMin: @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_float_dtypes()) - def test_max_min(self, func, axis, keepdims, dtype): - a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8)) + @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) + def test_basic(self, func, axis, keepdims, dtype): + a = generate_random_numpy_array((4, 4, 6, 8), dtype=dtype) a[2:3, 2, 3:4, 4] = numpy.nan 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, func)(a, axis=axis, keepdims=keepdims) + result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) + assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_float_dtypes()) - def test_max_min_strided(self, func, dtype): + @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) + def test_strided(self, func, dtype): a = numpy.arange(20, dtype=dtype) a[::3] = numpy.nan ia = dpnp.array(a) - np_res = getattr(numpy, func)(a[::-1]) - dpnp_res = getattr(dpnp, func)(ia[::-1]) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(numpy, func)(a[::-1]) + result = getattr(dpnp, func)(ia[::-1]) + assert_dtype_allclose(result, expected) - np_res = getattr(numpy, func)(a[::2]) - dpnp_res = getattr(dpnp, func)(ia[::2]) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(numpy, func)(a[::2]) + result = getattr(dpnp, func)(ia[::2]) + assert_dtype_allclose(result, expected) def test_out(self, func): a = numpy.arange(12, dtype=numpy.float32).reshape((2, 2, 3)) @@ -226,43 +227,42 @@ def test_out(self, func): 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, func)(a, axis=0) + iout = dpnp.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, func)(ia, axis=0, out=iout) + assert iout is result + assert_allclose(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, func)(ia, axis=0, out=dpt_out) + assert dpt_out is result.get_array() + assert_allclose(result, expected) # output is numpy array -> Error - dpnp_res = numpy.empty_like(np_res) + iout = numpy.empty_like(expected) with pytest.raises(TypeError): - getattr(dpnp, func)(ia, axis=0, out=dpnp_res) + getattr(dpnp, func)(ia, axis=0, out=iout) # output has incorrect shape -> Error - dpnp_res = dpnp.array(numpy.zeros((4, 2))) + iout = dpnp.array(numpy.zeros((4, 2))) with pytest.raises(ValueError): - getattr(dpnp, func)(ia, axis=0, out=dpnp_res) + getattr(dpnp, func)(ia, axis=0, out=iout) @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("in_dt", get_all_dtypes(no_none=True)) + @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") + @pytest.mark.parametrize("in_dt", get_float_complex_dtypes()) @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) def test_out_dtype(self, func, in_dt, out_dt): - # TODO: update to use generate_random_numpy_array - a = ( - numpy.arange(12, dtype=numpy.float32) - .reshape((2, 2, 3)) - .astype(dtype=in_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, 3, 4), dtype=in_dt, low=low) + a[:, :, 2] = numpy.nan out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt) ia, iout = dpnp.array(a), dpnp.array(out) - result = getattr(dpnp, func)(ia, out=iout, axis=1) - expected = getattr(numpy, func)(a, out=out, axis=1) + result = getattr(dpnp, func)(ia, out=iout, axis=2) + expected = getattr(numpy, func)(a, out=out, axis=2) assert_array_equal(result, expected) assert result is iout @@ -276,23 +276,23 @@ def test_error(self, func): with pytest.raises(NotImplementedError): getattr(dpnp, func)(ia, initial=6) - @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) - def test_nanmax_nanmin_no_NaN(self, func, dtype): - a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) + def test_no_NaN(self, func, dtype): + a = generate_random_numpy_array((4, 4, 6, 8), dtype=dtype) ia = dpnp.array(a) - np_res = getattr(numpy, func)(a, axis=0) - dpnp_res = getattr(dpnp, func)(ia, axis=0) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(numpy, func)(a, axis=0) + result = getattr(dpnp, func)(ia, axis=0) + assert_dtype_allclose(result, expected) - def test_nanmax_nanmin_all_NaN(self, recwarn, func): + def test_all_NaN(self, recwarn, func): a = numpy.arange(12, dtype=numpy.float32).reshape((2, 2, 3)) a[:, :, 2] = numpy.nan ia = dpnp.array(a) - np_res = getattr(numpy, func)(a, axis=0) - dpnp_res = getattr(dpnp, func)(ia, axis=0) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(numpy, func)(a, axis=0) + result = getattr(dpnp, func)(ia, axis=0) + assert_dtype_allclose(result, expected) assert len(recwarn) == 2 assert all( @@ -305,31 +305,29 @@ class TestNanMean: @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) @pytest.mark.parametrize("axis", [None, 0, 1, (0, 1)]) @pytest.mark.parametrize("keepdims", [True, False]) - def test_nanmean(self, dtype, axis, keepdims): - dp_array = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) - np_array = dpnp.asnumpy(dp_array) + def test_basic(self, dtype, axis, keepdims): + ia = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) + a = dpnp.asnumpy(ia) - result = dpnp.nanmean(dp_array, axis=axis, keepdims=keepdims) - expected = numpy.nanmean(np_array, axis=axis, keepdims=keepdims) + result = dpnp.nanmean(ia, axis=axis, keepdims=keepdims) + expected = numpy.nanmean(a, axis=axis, keepdims=keepdims) assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) @pytest.mark.parametrize("axis", [0, 1]) - def test_nanmean_out(self, dtype, axis): - dp_array = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]], dtype=dtype) - np_array = dpnp.asnumpy(dp_array) + def test_out(self, dtype, axis): + ia = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]], dtype=dtype) + a = dpnp.asnumpy(ia) - expected = numpy.nanmean(np_array, axis=axis) + expected = numpy.nanmean(a, axis=axis) out = dpnp.empty_like(dpnp.asarray(expected)) - result = dpnp.nanmean(dp_array, axis=axis, out=out) + result = dpnp.nanmean(ia, axis=axis, out=out) assert out is result assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_nanmean_complex(self, dtype): - x1 = numpy.random.rand(10) - x2 = numpy.random.rand(10) - a = numpy.array(x1 + 1j * x2, dtype=dtype) + def test_complex(self, dtype): + a = generate_random_numpy_array(10, dtype=dtype) a[::3] = numpy.nan ia = dpnp.array(a) @@ -338,38 +336,38 @@ def test_nanmean_complex(self, dtype): assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - def test_nanmean_dtype(self, dtype): - dp_array = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]]) - np_array = dpnp.asnumpy(dp_array) + def test_dtype(self, dtype): + ia = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]]) + a = dpnp.asnumpy(ia) - expected = numpy.nanmean(np_array, dtype=dtype) - result = dpnp.nanmean(dp_array, dtype=dtype) + expected = numpy.nanmean(a, dtype=dtype) + result = dpnp.nanmean(ia, dtype=dtype) assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - def test_nanmean_strided(self, dtype): - dp_array = dpnp.arange(20, dtype=dtype) - dp_array[::3] = dpnp.nan - np_array = dpnp.asnumpy(dp_array) + def test_strided(self, dtype): + ia = dpnp.arange(20, dtype=dtype) + ia[::3] = dpnp.nan + a = dpnp.asnumpy(ia) - result = dpnp.nanmean(dp_array[::-1]) - expected = numpy.nanmean(np_array[::-1]) + result = dpnp.nanmean(ia[::-1]) + expected = numpy.nanmean(a[::-1]) assert_dtype_allclose(result, expected) - result = dpnp.nanmean(dp_array[::2]) - expected = numpy.nanmean(np_array[::2]) + result = dpnp.nanmean(ia[::2]) + expected = numpy.nanmean(a[::2]) assert_dtype_allclose(result, expected) @pytest.mark.usefixtures("suppress_mean_empty_slice_numpy_warnings") - def test_nanmean_scalar(self): - dp_array = dpnp.array(dpnp.nan) - np_array = dpnp.asnumpy(dp_array) + def test_scalar(self): + ia = dpnp.array(dpnp.nan) + a = dpnp.asnumpy(ia) - result = dpnp.nanmean(dp_array) - expected = numpy.nanmean(np_array) + result = dpnp.nanmean(ia) + expected = numpy.nanmean(a) assert_allclose(result, expected) - def test_nanmean_error(self): + def test_error(self): ia = dpnp.arange(5, dtype=dpnp.float32) ia[0] = dpnp.nan # where keyword is not implemented @@ -391,8 +389,7 @@ class TestNanMedian: @pytest.mark.parametrize("axis", [None, 0, (-1,), [0, 1], (0, -2, -1)]) @pytest.mark.parametrize("keepdims", [True, False]) def test_basic(self, dtype, axis, keepdims): - x = numpy.random.uniform(-5, 5, 24) - a = numpy.array(x, dtype=dtype).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4), dtype=dtype) a[0, 0, 0] = a[-2, -2, -2] = numpy.nan ia = dpnp.array(a) @@ -414,11 +411,10 @@ def test_empty(self, axis, shape): expected = numpy.nanmedian(a, axis=axis) assert_dtype_allclose(result, expected) - @pytest.mark.parametrize("dtype", get_all_dtypes()) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize("axis", [None, 0, (-1,), [0, 1], (0, -2, -1)]) def test_no_nan(self, dtype, axis): - x = numpy.random.uniform(-5, 5, 24) - a = numpy.array(x, dtype=dtype).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4), dtype=dtype) ia = dpnp.array(a) expected = numpy.nanmedian(a, axis=axis) @@ -435,7 +431,7 @@ def test_all_nan(self): expected = numpy.nanmedian(a) assert_dtype_allclose(result, expected) - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) a[:, :, 2] = numpy.nan ia = dpnp.array(a) @@ -445,7 +441,7 @@ def test_all_nan(self): @pytest.mark.parametrize("axis", [None, 0, -1, (0, -2, -1)]) def test_overwrite_input(self, axis): - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) a[0, 0, 0] = a[-2, -2, -2] = numpy.nan ia = dpnp.array(a) @@ -461,7 +457,7 @@ def test_overwrite_input(self, axis): @pytest.mark.parametrize("axis", [None, 0, (-1,), [0, 1]]) @pytest.mark.parametrize("overwrite_input", [True, False]) def test_usm_ndarray(self, axis, overwrite_input): - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) a[0, 0, 0] = a[-2, -2, -2] = numpy.nan ia = dpt.asarray(a) @@ -469,6 +465,7 @@ def test_usm_ndarray(self, axis, overwrite_input): a, axis=axis, overwrite_input=overwrite_input ) result = dpnp.nanmedian(ia, axis=axis, overwrite_input=overwrite_input) + assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) @pytest.mark.parametrize( @@ -501,89 +498,92 @@ def test_error(self): dpnp.nanmedian(a, axis=1, out=res) -class TestNanProd: +@pytest.mark.parametrize("func", ["nanprod", "nansum"]) +class TestNanProdSum: @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_float_complex_dtypes()) - def test_nanprod(self, axis, keepdims, dtype): - a = numpy.arange(1, 13, dtype=dtype).reshape((2, 2, 3)) + def test_basic(self, func, axis, keepdims, dtype): + a = generate_random_numpy_array((2, 3, 4), dtype=dtype) a[:, :, 2] = numpy.nan ia = dpnp.array(a) - np_res = numpy.nanprod(a, axis=axis, keepdims=keepdims) - dpnp_res = dpnp.nanprod(ia, axis=axis, keepdims=keepdims) + expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) + result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims) - assert dpnp_res.shape == np_res.shape - assert_allclose(dpnp_res, np_res) + assert result.shape == expected.shape + assert_allclose(result, expected, rtol=1e-5) @pytest.mark.usefixtures("suppress_complex_warning") @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") - @pytest.mark.parametrize("in_dtype", get_float_complex_dtypes()) - @pytest.mark.parametrize( - "out_dtype", get_all_dtypes(no_bool=True, no_none=True) - ) - def test_nanprod_dtype(self, in_dtype, out_dtype): - a = numpy.arange(1, 13, dtype=in_dtype).reshape((2, 2, 3)) + @pytest.mark.parametrize("in_dt", get_float_complex_dtypes()) + @pytest.mark.parametrize("dt", get_all_dtypes()) + def test_dtype(self, func, in_dt, dt): + a = generate_random_numpy_array((2, 3, 4), dtype=in_dt) a[:, :, 2] = numpy.nan ia = dpnp.array(a) - np_res = numpy.nanprod(a, dtype=out_dtype) - dpnp_res = dpnp.nanprod(ia, dtype=out_dtype) - assert_dtype_allclose(dpnp_res, np_res) + expected = getattr(numpy, func)(a, dtype=dt) + result = getattr(dpnp, func)(ia, dtype=dt) + assert_dtype_allclose(result, expected) @pytest.mark.usefixtures( "suppress_overflow_encountered_in_cast_numpy_warnings" ) - def test_nanprod_out(self): + def test_out(self, func): ia = dpnp.arange(1, 7).reshape((2, 3)) ia = ia.astype(dpnp.default_float_type(ia.device)) ia[:, 1] = dpnp.nan a = dpnp.asnumpy(ia) - # output is dpnp_array - np_res = numpy.nanprod(a, axis=0) - dpnp_out = dpnp.empty(np_res.shape, dtype=np_res.dtype) - dpnp_res = dpnp.nanprod(ia, axis=0, out=dpnp_out) - assert dpnp_out is dpnp_res - assert_allclose(dpnp_res, np_res) + # out is dpnp_array + expected = getattr(numpy, func)(a, axis=0) + iout = dpnp.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, func)(ia, axis=0, out=iout) + assert iout is result + assert_allclose(result, expected) - # output is usm_ndarray - dpt_out = dpt.empty(np_res.shape, dtype=np_res.dtype) - dpnp_res = dpnp.nanprod(ia, axis=0, out=dpt_out) - assert dpt_out is dpnp_res.get_array() - assert_allclose(dpnp_res, np_res) + # out is usm_ndarray + dpt_out = dpt.empty(expected.shape, dtype=expected.dtype) + result = getattr(dpnp, func)(ia, axis=0, out=dpt_out) + assert dpt_out is result.get_array() + assert_allclose(result, expected) # out is a numpy array -> TypeError - dpnp_res = numpy.empty_like(np_res) + iout = numpy.empty_like(expected) with pytest.raises(TypeError): - dpnp.nanprod(ia, axis=0, out=dpnp_res) + getattr(dpnp, func)(ia, axis=0, out=iout) # incorrect shape for out - dpnp_res = dpnp.array(numpy.empty((2, 3))) + iout = dpnp.array(numpy.empty((2, 3))) with pytest.raises(ValueError): - dpnp.nanprod(ia, axis=0, out=dpnp_res) + getattr(dpnp, func)(ia, axis=0, out=iout) @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("in_dt", get_float_complex_dtypes()) @pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True)) - @pytest.mark.parametrize("dtype", get_all_dtypes()) - def test_nanprod_out_dtype(self, arr_dt, out_dt, dtype): - a = numpy.arange(1, 7).reshape((2, 3)).astype(dtype=arr_dt) - out = numpy.zeros_like(a, shape=(2,), dtype=out_dt) - - ia = dpnp.array(a) - iout = dpnp.array(out) + def test_out_dtype(self, func, in_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 -5 + a = generate_random_numpy_array((2, 3, 4), dtype=in_dt, low=low, high=5) + a[:, :, 2] = numpy.nan + out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt) + ia, iout = dpnp.array(a), dpnp.array(out) - result = dpnp.nanprod(ia, out=iout, dtype=dtype, axis=1) - expected = numpy.nanprod(a, out=out, dtype=dtype, axis=1) - assert_array_equal(result, expected) + result = getattr(dpnp, func)(ia, out=iout, axis=2) + expected = getattr(numpy, func)(a, out=out, axis=2) + assert_allclose(result, expected, rtol=1e-06) assert result is iout - def test_nanprod_Error(self): - ia = dpnp.arange(5) + @pytest.mark.parametrize("stride", [-1, 2]) + def test_strided(self, func, stride): + a = numpy.arange(20.0) + a[::3] = numpy.nan + ia = dpnp.array(a) - with pytest.raises(TypeError): - dpnp.nanprod(dpnp.asnumpy(ia)) + result = getattr(dpnp, func)(ia[::stride]) + expected = getattr(numpy, func)(a[::stride]) + assert_allclose(result, expected) @pytest.mark.parametrize("func", ["nanstd", "nanvar"]) @@ -677,7 +677,7 @@ def test_out(self, func, dtype, axis, keepdims, ddof): assert_dtype_allclose(result, expected) @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - def test_strided_array(self, func, dtype): + def test_strided(self, func, dtype): a = numpy.arange(20, dtype=dtype) a[::3] = numpy.nan ia = dpnp.array(a) @@ -766,63 +766,3 @@ def test_error(self, func): # ddof should be an integer or float with pytest.raises(TypeError): getattr(dpnp, func)(ia, ddof="1") - - -class TestNanSum: - @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - @pytest.mark.parametrize("axis", [None, 0, 1, (0, 1)]) - @pytest.mark.parametrize("keepdims", [True, False]) - def test_nansum(self, dtype, axis, keepdims): - dp_array = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]], dtype=dtype) - np_array = dpnp.asnumpy(dp_array) - - expected = numpy.nansum(np_array, axis=axis, keepdims=keepdims) - result = dpnp.nansum(dp_array, axis=axis, keepdims=keepdims) - assert_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_complex_dtypes()) - def test_nansum_complex(self, dtype): - x1 = numpy.random.rand(10) - x2 = numpy.random.rand(10) - a = numpy.array(x1 + 1j * x2, dtype=dtype) - a[::3] = numpy.nan - ia = dpnp.array(a) - - expected = numpy.nansum(a) - result = dpnp.nansum(ia) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - @pytest.mark.parametrize("axis", [0, 1]) - def test_nansum_out(self, dtype, axis): - dp_array = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]], dtype=dtype) - np_array = dpnp.asnumpy(dp_array) - - expected = numpy.nansum(np_array, axis=axis) - out = dpnp.empty_like(dpnp.asarray(expected)) - result = dpnp.nansum(dp_array, axis=axis, out=out) - assert out is result - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - def test_nansum_dtype(self, dtype): - dp_array = dpnp.array([[dpnp.nan, 1, 2], [3, dpnp.nan, 0]]) - np_array = dpnp.asnumpy(dp_array) - - expected = numpy.nansum(np_array, dtype=dtype) - result = dpnp.nansum(dp_array, dtype=dtype) - assert_dtype_allclose(result, expected) - - @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) - def test_nansum_strided(self, dtype): - dp_array = dpnp.arange(20, dtype=dtype) - dp_array[::3] = dpnp.nan - np_array = dpnp.asnumpy(dp_array) - - result = dpnp.nansum(dp_array[::-1]) - expected = numpy.nansum(np_array[::-1]) - assert_allclose(result, expected) - - result = dpnp.nansum(dp_array[::2]) - expected = numpy.nansum(np_array[::2]) - assert_allclose(result, expected) diff --git a/dpnp/tests/test_product.py b/dpnp/tests/test_product.py index b7cbfcf61e6d..974c7070f3ff 100644 --- a/dpnp/tests/test_product.py +++ b/dpnp/tests/test_product.py @@ -902,10 +902,13 @@ def test_strided1(self, dtype, stride): expected = numpy.matmul(a, a) assert_dtype_allclose(result, expected, factor=16) - iOUT = dpnp.empty(shape, dtype=result.dtype) + OUT = numpy.empty(shape, dtype=result.dtype) + out = OUT[slices] + iOUT = dpnp.array(OUT) iout = iOUT[slices] result = dpnp.matmul(ia, ia, out=iout) assert result is iout + expected = numpy.matmul(a, a, out=out) assert_dtype_allclose(result, expected, factor=16) @pytest.mark.parametrize("dtype", _selected_dtypes) diff --git a/dpnp/tests/test_search.py b/dpnp/tests/test_search.py index c0b4e05fead9..64c4eb75f906 100644 --- a/dpnp/tests/test_search.py +++ b/dpnp/tests/test_search.py @@ -16,7 +16,7 @@ class TestArgmaxArgmin: @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2]) @pytest.mark.parametrize("keepdims", [False, True]) @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) - def test_func(self, func, axis, keepdims, dtype): + def test_basic(self, func, axis, keepdims, dtype): a = generate_random_numpy_array((4, 4, 6, 8), dtype=dtype) ia = dpnp.array(a) diff --git a/dpnp/tests/test_statistics.py b/dpnp/tests/test_statistics.py index 0ee5c02f4caf..e9d303ce6b90 100644 --- a/dpnp/tests/test_statistics.py +++ b/dpnp/tests/test_statistics.py @@ -602,20 +602,9 @@ def test_true_rowvar(self): class TestMaxMin: @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)) + @pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) def test_func(self, func, 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) - assert_dtype_allclose(result, expected) - - @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)) + a = generate_random_numpy_array((4, 4, 6, 8), dtype=dtype) ia = dpnp.array(a) expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims) @@ -792,7 +781,7 @@ def test_0d_array(self): @pytest.mark.parametrize("axis", [None, 0, (0, 1), (0, -2, -1)]) @pytest.mark.parametrize("keepdims", [True, False]) def test_nan(self, axis, keepdims): - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) a[0, 0, 0] = a[-1, -1, -1] = numpy.nan ia = dpnp.array(a) @@ -804,7 +793,7 @@ def test_nan(self, axis, keepdims): @pytest.mark.parametrize("axis", [None, 0, -1, (0, -2, -1)]) @pytest.mark.parametrize("keepdims", [True, False]) def test_overwrite_input(self, axis, keepdims): - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) ia = dpnp.array(a) b = a.copy() @@ -823,7 +812,7 @@ def test_overwrite_input(self, axis, keepdims): @pytest.mark.parametrize("axis", [None, 0, (-1,), [0, 1]]) @pytest.mark.parametrize("overwrite_input", [True, False]) def test_usm_ndarray(self, axis, overwrite_input): - a = numpy.random.uniform(-5, 5, 24).reshape(2, 3, 4) + a = generate_random_numpy_array((2, 3, 4)) ia = dpt.asarray(a) expected = numpy.median(a, axis=axis, overwrite_input=overwrite_input) diff --git a/dpnp/tests/test_sum.py b/dpnp/tests/test_sum.py index 8712e524b539..7197c5456f71 100644 --- a/dpnp/tests/test_sum.py +++ b/dpnp/tests/test_sum.py @@ -128,7 +128,7 @@ def test_sum_out(dtype, axis): expected = numpy.sum(a_np, axis=axis) res = dpnp.empty(expected.shape, dtype=dtype) a.sum(axis=axis, out=res) - assert_array_equal(expected, res.asnumpy()) + assert_array_equal(expected, res) @pytest.mark.usefixtures("suppress_complex_warning")