From 1f6df8e788ef437888162b043e69fd84a362295c Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 31 Mar 2025 16:39:36 +0200 Subject: [PATCH 1/2] Add third party tests for dpnp.apply_over_axes --- .../cupy/lib_tests/test_shape_base.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py b/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py index 76da1cdb1de0..92726629d81a 100644 --- a/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py +++ b/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py @@ -109,6 +109,43 @@ def test_apply_along_axis_invalid_axis(): xp.apply_along_axis(xp.sum, axis, a) +class TestApplyOverAxes(unittest.TestCase): + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + def test_simple(self, xp): + a = xp.arange(24).reshape(2, 3, 4) + aoa_a = xp.apply_over_axes(xp.sum, a, [0, 2]) + return aoa_a + + def test_apply_over_axis_invalid_0darr(self): + # cupy will not accept 0darr, but numpy does + with pytest.raises(AxisError): + a = cupy.array(42) + cupy.apply_over_axes(cupy.sum, a, 0) + # test for numpy, it can run without error + a = numpy.array(42) + numpy.apply_over_axes(numpy.sum, a, 0) + + @testing.numpy_cupy_allclose(type_check=has_support_aspect64()) + def test_apply_over_axis_shape_preserve_func(self, xp): + a = xp.arange(10).reshape(2, 5, 1) + + def normalize(arr, axis): + """shape-preserve operation, return {x_i/sum(x)}""" + row_sums = arr.sum(axis=axis) + return a / row_sums[:, xp.newaxis] + + aoa_a = xp.apply_over_axes(normalize, a, 1) + assert a.shape == aoa_a.shape + return aoa_a + + def test_apply_over_axis_invalid_axis(self): + for xp in [numpy, cupy]: + a = xp.ones((8, 4)) + axis = 3 + with pytest.raises(AxisError): + xp.apply_over_axes(xp.sum, a, axis) + + class TestPutAlongAxis(unittest.TestCase): @testing.for_all_dtypes() From 9c60b11bff4155db4d7ed4f4a401b1055fe2b4ab Mon Sep 17 00:00:00 2001 From: Anton <100830759+antonwolfy@users.noreply.github.com> Date: Mon, 31 Mar 2025 17:53:47 +0200 Subject: [PATCH 2/2] Update dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py Co-authored-by: Vahid Tavanashad <120411540+vtavana@users.noreply.github.com> --- dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py b/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py index 92726629d81a..c241824fa81d 100644 --- a/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py +++ b/dpnp/tests/third_party/cupy/lib_tests/test_shape_base.py @@ -110,6 +110,7 @@ def test_apply_along_axis_invalid_axis(): class TestApplyOverAxes(unittest.TestCase): + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_simple(self, xp): a = xp.arange(24).reshape(2, 3, 4)