From 9534ccc0f691cbba74251076212803bcf76a94ac Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Fri, 11 Apr 2025 19:08:55 -0700 Subject: [PATCH 1/3] update test_ndarray.py --- dpnp/tests/test_ndarray.py | 73 ++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 88c996243821..56598c037122 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -22,8 +22,8 @@ class TestAsType: @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("res_dtype", get_all_dtypes()) - @pytest.mark.parametrize("arr_dtype", get_all_dtypes()) + @pytest.mark.parametrize("res_dtype", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("arr_dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "arr", [[-2, -1, 0, 1, 2], [[-2, -1], [1, 2]], []], @@ -35,7 +35,7 @@ def test_basic(self, arr, arr_dtype, res_dtype): expected = a.astype(res_dtype) result = ia.astype(res_dtype) - assert_allclose(expected, result) + assert_allclose(result, expected) def test_subok_error(self): x = dpnp.ones(4) @@ -88,18 +88,18 @@ def test_create_from_usm_ndarray_error(arr): dpnp.ndarray._create_from_usm_ndarray(arr) -@pytest.mark.parametrize("arr_dtype", get_all_dtypes()) +@pytest.mark.parametrize("arr_dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "arr", [[-2, -1, 0, 1, 2], [[-2, -1], [1, 2]], []], ids=["[-2, -1, 0, 1, 2]", "[[-2, -1], [1, 2]]", "[]"], ) def test_flatten(arr, arr_dtype): - numpy_array = get_abs_array(arr, arr_dtype) - dpnp_array = dpnp.array(numpy_array) - expected = numpy_array.flatten() - result = dpnp_array.flatten() - assert_array_equal(expected, result) + a = get_abs_array(arr, arr_dtype) + ia = dpnp.array(a) + expected = a.flatten() + result = ia.flatten() + assert_array_equal(result, expected) @pytest.mark.parametrize( @@ -110,17 +110,16 @@ def test_flatten(arr, arr_dtype): @pytest.mark.parametrize("order", ["C", "F"]) def test_flags(shape, order): usm_array = dpt.usm_ndarray(shape, order=order) - numpy_array = numpy.ndarray(shape, order=order) - dpnp_array = dpnp.ndarray(shape, order=order) - assert usm_array.flags == dpnp_array.flags - assert numpy_array.flags.c_contiguous == dpnp_array.flags.c_contiguous - assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous + a = numpy.ndarray(shape, order=order) + ia = dpnp.ndarray(shape, order=order) + assert usm_array.flags == ia.flags + assert a.flags.c_contiguous == ia.flags.c_contiguous + assert a.flags.f_contiguous == ia.flags.f_contiguous @pytest.mark.parametrize( "dtype", [numpy.complex64, numpy.float32, numpy.int64, numpy.int32, numpy.bool_], - ids=["complex64", "float32", "int64", "int32", "bool"], ) @pytest.mark.parametrize("strides", [(1, 4), (4, 1)], ids=["(1, 4)", "(4, 1)"]) @pytest.mark.parametrize("order", ["C", "F"]) @@ -130,13 +129,11 @@ def test_flags_strides(dtype, order, strides): usm_array = dpt.usm_ndarray( (4, 4), dtype=dtype, order=order, strides=strides ) - numpy_array = numpy.ndarray( - (4, 4), dtype=dtype, order=order, strides=numpy_strides - ) - dpnp_array = dpnp.ndarray((4, 4), dtype=dtype, order=order, strides=strides) - assert usm_array.flags == dpnp_array.flags - assert numpy_array.flags.c_contiguous == dpnp_array.flags.c_contiguous - assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous + a = numpy.ndarray((4, 4), dtype=dtype, order=order, strides=numpy_strides) + ia = dpnp.ndarray((4, 4), dtype=dtype, order=order, strides=strides) + assert usm_array.flags == ia.flags + assert a.flags.c_contiguous == ia.flags.c_contiguous + assert a.flags.f_contiguous == ia.flags.f_contiguous def test_flags_writable(): @@ -380,12 +377,12 @@ def test_print_dpnp_zero_shape(): @pytest.mark.parametrize("func", [bool, float, int, complex]) @pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)]) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_float16=False, no_complex=True) + "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) ) def test_scalar_type_casting(func, shape, dtype): - numpy_array = numpy.full(shape, 5, dtype=dtype) - dpnp_array = dpnp.full(shape, 5, dtype=dtype) - assert func(numpy_array) == func(dpnp_array) + a = numpy.full(shape, 5, dtype=dtype) + ia = dpnp.full(shape, 5, dtype=dtype) + assert func(a) == func(ia) # Numpy will raise an error when converting a.ndim > 0 to a scalar @@ -396,12 +393,12 @@ def test_scalar_type_casting(func, shape, dtype): ) @pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)]) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_float16=False, no_complex=True, no_none=True) + "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) ) def test_scalar_type_casting_by_method(method, shape, dtype): - numpy_array = numpy.full(shape, 4.7, dtype=dtype) - dpnp_array = dpnp.full(shape, 4.7, dtype=dtype) - assert getattr(numpy_array, method)() == getattr(dpnp_array, method)() + a = numpy.full(shape, 4.7, dtype=dtype) + ia = dpnp.full(shape, 4.7, dtype=dtype) + assert getattr(a, method)() == getattr(ia, method)() @pytest.mark.parametrize("shape", [(1,), (1, 1), (1, 1, 1)]) @@ -452,18 +449,18 @@ def test_ravel(): def test_repeat(): - numpy_array = numpy.arange(4).repeat(3) - dpnp_array = dpnp.arange(4).repeat(3) - assert_array_equal(numpy_array, dpnp_array) + a = numpy.arange(4).repeat(3) + ia = dpnp.arange(4).repeat(3) + assert_array_equal(a, ia) def test_clip(): - numpy_array = numpy.arange(10) - dpnp_array = dpnp.arange(10) - result = dpnp.clip(dpnp_array, 3, 7) - expected = numpy.clip(numpy_array, 3, 7) + a = numpy.arange(10) + ia = dpnp.arange(10) + result = dpnp.clip(ia, 3, 7) + expected = numpy.clip(a, 3, 7) - assert_array_equal(expected, result) + assert_array_equal(result, expected) def test_rmatmul_dpnp_array(): From 59627853329a3d1810dba23d1ad2f1d98363ab12 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad <120411540+vtavana@users.noreply.github.com> Date: Mon, 14 Apr 2025 11:10:29 -0500 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com> --- dpnp/tests/test_ndarray.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 56598c037122..179d203fb2b2 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -22,7 +22,7 @@ class TestAsType: @pytest.mark.usefixtures("suppress_complex_warning") - @pytest.mark.parametrize("res_dtype", get_all_dtypes(no_none=True)) + @pytest.mark.parametrize("res_dtype", get_all_dtypes()) @pytest.mark.parametrize("arr_dtype", get_all_dtypes(no_none=True)) @pytest.mark.parametrize( "arr", @@ -377,7 +377,7 @@ def test_print_dpnp_zero_shape(): @pytest.mark.parametrize("func", [bool, float, int, complex]) @pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)]) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) + "dtype", get_all_dtypes(no_float16=False, no_complex=True) ) def test_scalar_type_casting(func, shape, dtype): a = numpy.full(shape, 5, dtype=dtype) @@ -393,7 +393,7 @@ def test_scalar_type_casting(func, shape, dtype): ) @pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)]) @pytest.mark.parametrize( - "dtype", get_all_dtypes(no_none=True, no_float16=False, no_complex=True) + "dtype", get_all_dtypes(no_float16=False, no_complex=True) ) def test_scalar_type_casting_by_method(method, shape, dtype): a = numpy.full(shape, 4.7, dtype=dtype) From 18e048d9f2e2803b904507e16b74c945ebf72682 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 14 Apr 2025 14:04:43 -0500 Subject: [PATCH 3/3] use assert_allclose instead of assert --- dpnp/tests/test_ndarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 179d203fb2b2..b1840652d40b 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -398,7 +398,7 @@ def test_scalar_type_casting(func, shape, dtype): def test_scalar_type_casting_by_method(method, shape, dtype): a = numpy.full(shape, 4.7, dtype=dtype) ia = dpnp.full(shape, 4.7, dtype=dtype) - assert getattr(a, method)() == getattr(ia, method)() + assert_allclose(getattr(a, method)(), getattr(ia, method)(), rtol=1e-06) @pytest.mark.parametrize("shape", [(1,), (1, 1), (1, 1, 1)])