From 3f540bb8734fe5a9bcfd9499502512e9ac69d78a Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 19 Apr 2023 11:43:03 +0200 Subject: [PATCH 1/2] reuse dpctl.tensor.moveaxis for dpnp.moveaxis --- dpnp/dpnp_iface_manipulation.py | 41 +++++++------------ tests/skipped_tests.tbl | 2 - tests/skipped_tests_gpu.tbl | 2 - .../cupy/manipulation_tests/test_transpose.py | 7 ---- 4 files changed, 15 insertions(+), 37 deletions(-) diff --git a/dpnp/dpnp_iface_manipulation.py b/dpnp/dpnp_iface_manipulation.py index 567661bdb57..02de340e06d 100644 --- a/dpnp/dpnp_iface_manipulation.py +++ b/dpnp/dpnp_iface_manipulation.py @@ -388,17 +388,25 @@ def hstack(tup): return call_origin(numpy.hstack, tup_new) -def moveaxis(x1, source, destination): +def moveaxis(x, source, destination): """ Move axes of an array to new positions. Other axes remain in their original order. For full documentation refer to :obj:`numpy.moveaxis`. + Returns + ------- + out : dpnp.ndarray + Array with moved axes. + The returned array must has the same data type as `x`, + is created on the same device as `x` and has the same + USM allocation type as `x`. + Limitations ----------- - Input array ``x1`` is supported as :obj:`dpnp.ndarray`. + Parameters `x` is supported as either :class:`dpnp.ndarray` + or :class:`dpctl.tensor.usm_ndarray`. Otherwise the function will be executed sequentially on CPU. - Sizes of normalized input arrays are supported to be equal. Input array data types are limited by supported DPNP :ref:`Data types`. See Also @@ -417,30 +425,11 @@ def moveaxis(x1, source, destination): """ - x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False) - if x1_desc: - source_norm = normalize_axis(source, x1_desc.ndim) - destination_norm = normalize_axis(destination, x1_desc.ndim) - - if len(source_norm) != len(destination_norm): - pass - else: - # 'do nothing' pattern for transpose() with no elements in 'source' - input_permute = [] - for i in range(x1_desc.ndim): - if i not in source_norm: - input_permute.append(i) - - # insert moving axes into proper positions - for destination_id, source_id in sorted(zip(destination_norm, source_norm)): - # if destination_id in input_permute: - # pytest tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_3 - # checker_throw_value_error("swapaxes", "source_id exists", source_id, input_permute) - input_permute.insert(destination_id, source_id) - - return transpose(x1_desc.get_pyobj(), axes=input_permute) + if isinstance(x, dpnp_array) or isinstance(x, dpt.usm_ndarray): + dpt_array = x.get_array() if isinstance(x, dpnp_array) else x + return dpnp_array._create_from_usm_ndarray(dpt.moveaxis(dpt_array, source, destination)) - return call_origin(numpy.moveaxis, x1, source, destination) + return call_origin(numpy.moveaxis, x, source, destination) def ravel(x1, order='C'): diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index a41b881ae7b..aba1c85217c 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -747,8 +747,6 @@ tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_2_{reps tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_3_{reps=(0, 1)}::test_array_tile tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_4_{reps=(2, 3)}::test_array_tile tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_5_{reps=(2, 3, 4, 5)}::test_array_tile -tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_2 -tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_3 tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_455_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='floor_divide', use_dtype=False}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_457_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='fmod', use_dtype=False}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_459_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='remainder', use_dtype=False}::test_binary diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index fee79df860f..a7aec444f06 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -908,8 +908,6 @@ tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_2_{reps tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_3_{reps=(0, 1)}::test_array_tile tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_4_{reps=(2, 3)}::test_array_tile tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_5_{reps=(2, 3, 4, 5)}::test_array_tile -tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_2 -tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_3 tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_279_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0., 1., 2.], [3., 4., 5.]], dtype=float32), dtype=float64, name='floor_divide', use_dtype=False}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_287_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0., 1., 2.], [3., 4., 5.]]), dtype=float64, name='floor_divide', use_dtype=False}::test_binary diff --git a/tests/third_party/cupy/manipulation_tests/test_transpose.py b/tests/third_party/cupy/manipulation_tests/test_transpose.py index ed06f050f77..91e8fe9a2cd 100644 --- a/tests/third_party/cupy/manipulation_tests/test_transpose.py +++ b/tests/third_party/cupy/manipulation_tests/test_transpose.py @@ -41,14 +41,12 @@ def test_moveaxis6(self, xp): return xp.moveaxis(a, [0, 2, 1], [3, 4, 0]) # dim is too large - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid1_1(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) with pytest.raises(numpy.AxisError): xp.moveaxis(a, [0, 1], [1, 3]) - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid1_2(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) @@ -56,14 +54,12 @@ def test_moveaxis_invalid1_2(self): xp.moveaxis(a, [0, 1], [1, 3]) # dim is too small - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid2_1(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) with pytest.raises(numpy.AxisError): xp.moveaxis(a, [0, -4], [1, 2]) - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid2_2(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) @@ -71,7 +67,6 @@ def test_moveaxis_invalid2_2(self): xp.moveaxis(a, [0, -4], [1, 2]) # len(source) != len(destination) - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid3(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) @@ -79,7 +74,6 @@ def test_moveaxis_invalid3(self): xp.moveaxis(a, [0, 1, 2], [1, 2]) # len(source) != len(destination) - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid4(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) @@ -87,7 +81,6 @@ def test_moveaxis_invalid4(self): xp.moveaxis(a, [0, 1], [1, 2, 0]) # Use the same axis twice - @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_moveaxis_invalid5_1(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) From baf5101238bed71e17a33bf6c594445b582e6184 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Thu, 27 Apr 2023 13:49:51 +0200 Subject: [PATCH 2/2] Fix remark --- dpnp/dpnp_iface_manipulation.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dpnp/dpnp_iface_manipulation.py b/dpnp/dpnp_iface_manipulation.py index 02de340e06d..60c1c5e79ef 100644 --- a/dpnp/dpnp_iface_manipulation.py +++ b/dpnp/dpnp_iface_manipulation.py @@ -398,9 +398,8 @@ def moveaxis(x, source, destination): ------- out : dpnp.ndarray Array with moved axes. - The returned array must has the same data type as `x`, - is created on the same device as `x` and has the same - USM allocation type as `x`. + The returned array will have the same data and + the same USM allocation type as `x`. Limitations -----------