From 0932328e4a3dcf77d7549b21e5bba1b45bc0f718 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 12:23:30 +0800 Subject: [PATCH 01/20] update --- python/paddle/__init__.py | 2 + .../fluid/tests/unittests/test_rot90_op.py | 134 ++++++++++++++++++ python/paddle/tensor/__init__.py | 2 + python/paddle/tensor/manipulation.py | 78 ++++++++++ 4 files changed, 216 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_rot90_op.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 5823cf460ee9f..8f16b811b72c6 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -150,6 +150,7 @@ from .tensor.manipulation import unsqueeze_ # noqa: F401 from .tensor.manipulation import unstack # noqa: F401 from .tensor.manipulation import flip # noqa: F401 +from .tensor.manipulation import rot90 # noqa: F401 from .tensor.manipulation import unbind # noqa: F401 from .tensor.manipulation import roll # noqa: F401 from .tensor.manipulation import chunk # noqa: F401 @@ -404,6 +405,7 @@ 'bitwise_not', 'mm', 'flip', + "rot90", 'bincount', 'histogram', 'multiplex', diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py new file mode 100644 index 0000000000000..1dfb7d06a7186 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -0,0 +1,134 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +import unittest +import numpy as np +import paddle +import paddle.fluid as fluid +import paddle.fluid.core as core +from paddle.fluid import Program, program_guard +from op_test import OpTest + + +class TestRot90Op(unittest.TestCase): + """Test rot90 api.""" + + def test_static_graph(self): + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + axis = [0] + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.flip(input, axis) + output = paddle.flip(output, -1) + output = output.flip(0) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + out_np = np.array(res[0]) + out_ref = np.array([[3, 2, 1], [6, 5, 4]]).astype(np.float32) + self.assertTrue( + (out_np == out_ref).all(), + msg='flip output is wrong, out =' + str(out_np)) + + def test_dygraph(self): + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + with fluid.dygraph.guard(): + inputs = fluid.dygraph.to_variable(img) + ret = paddle.flip(inputs, [0]) + ret = ret.flip(0) + ret = paddle.flip(ret, 1) + out_ref = np.array([[3, 2, 1], [6, 5, 4]]).astype(np.float32) + + self.assertTrue( + (ret.numpy() == out_ref).all(), + msg='flip output is wrong, out =' + str(ret.numpy())) + + +class TestFlipOp(OpTest): + def setUp(self): + self.op_type = 'flip' + self.init_test_case() + self.inputs = {'X': np.random.random(self.in_shape).astype('float64')} + self.init_attrs() + self.outputs = {'Out': self.calc_ref_res()} + + def init_attrs(self): + self.attrs = {"axis": self.axis} + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + self.check_grad(["X"], "Out") + + def init_test_case(self): + self.in_shape = (6, 4, 2, 3) + self.axis = [0, 1] + + def calc_ref_res(self): + res = self.inputs['X'] + if isinstance(self.axis, int): + return np.flip(res, self.axis) + for axis in self.axis: + res = np.flip(res, axis) + return res + + +class TestFlipOpAxis1(TestFlipOp): + def init_test_case(self): + self.in_shape = (2, 4, 4) + self.axis = [0] + + +class TestFlipOpAxis2(TestFlipOp): + def init_test_case(self): + self.in_shape = (4, 4, 6, 3) + self.axis = [0, 2] + + +class TestFlipOpAxis3(TestFlipOp): + def init_test_case(self): + self.in_shape = (4, 3, 1) + self.axis = [0, 1, 2] + + +class TestFlipOpAxis4(TestFlipOp): + def init_test_case(self): + self.in_shape = (6, 4, 2, 2) + self.axis = [0, 1, 2, 3] + + +class TestFlipOpEmptyAxis(TestFlipOp): + def init_test_case(self): + self.in_shape = (6, 4, 2, 2) + self.axis = [] + + +class TestFlipOpNegAxis(TestFlipOp): + def init_test_case(self): + self.in_shape = (6, 4, 2, 2) + self.axis = [-1] + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 21d1dd1793b2c..4dbb3fde7d4cd 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -106,6 +106,7 @@ from .manipulation import unsqueeze_ # noqa: F401 from .manipulation import unstack # noqa: F401 from .manipulation import flip # noqa: F401 +from .manipulation import rot90 # noqa: F401 from .manipulation import unbind # noqa: F401 from .manipulation import roll # noqa: F401 from .manipulation import chunk # noqa: F401 @@ -366,6 +367,7 @@ 'unsqueeze_', 'unstack', 'flip', + "rot90", 'unbind', 'roll', 'tile', diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 9b9b2d9431eeb..b17c4909a96ea 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -495,6 +495,84 @@ def flip(x, axis, name=None): return out +def rot90(x, k, dims, name=None): + """ + Rotate a n-D tensor by 90 degrees in the plane specified by dims axis. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0. + + Args: + x (Tensor): The input Tensor(or LoDTensor). The data type of the input Tensor x + should be float32, float64, int32, int64, bool. + k (int): Number of times to rotate + dims (list|tuple): Axis to rotate + name (str, optional): The default value is None. Normally there is no need for user to set this property. + For more information, please refer to :ref:`api_guide_Name` . + + Returns: + Tensor: Tensor or LoDTensor calculated by rot90 layer. The data type is same with input x. + + Examples: + .. code-block:: python + + import paddle + import numpy as np + + data = paddle.arange(4) + data = paddle.reshape(data, (2, 2)) + print(data) ## [[0, 1],[2, 3]] + y = paddle.rot90(data, 1, [0, 1]) + print(y) #[[1, 3],[0, 2]] + y= paddle.rot90(data, -1, [0, 1]) + print(y) #[[2, 0],[3, 1]] + data2 = paddle.arange(8) + print(data2) ###[[[0, 1],[2, 3]],[[4, 5],[6, 7]]] + y = paddle.rot90(data2, 1, [1, 2]) + print(y) ### [[[1, 3],[0, 2]],[[5, 7],[4, 6]]] + """ + + helper = LayerHelper("rot90", **locals()) + check_type(x, 'X', (Variable), 'rot90') + dtype = helper.input_dtype('x') + check_dtype(dtype, 'X', + ['float16', 'float32', 'float64', 'int32', 'int64', 'bool'], + 'rot90') + check_type(dims, 'dims', (list, tuple), 'rot90') + + input_total_dims = len(x.shape) + total_rot_dims = len(dims) + if total_rot_dims != 2: + raise ValueError("expected total rotation dims == 2, but got dims = {}". + format(total_rot_dims)) + if input_total_dims < 2: + raise ValueError("expected total dims >= 2, but got total dims = {}". + format(input_total_dims)) + + if not (dims[0] != dims[1] and abs(dims[0] - dims[1]) != input_total_dims): + raise ValueError( + "expected rotation dims to be different, but got dim0 = {}, and dim1 = {}". + format(dims[0], dims[1])) + + if not (dims[0] < input_total_dims and dims[0] >= -input_total_dims): + raise ValueError("Rotation dim0 out of range, dim0 =".format(dims[0])) + if not (dims[1] < input_total_dims and dims[1] >= -input_total_dims): + raise ValueError("Rotation dim1 out of range, dim1 =".format(dims[1])) + + ## k % 4 + k = k % 4 if k >= 0 else 4 - (-k % 4) + if k == 0: + return x + if k == 2: + return flip(flip(x, dims[0]), dims[1]) + + axes_list = list(range(0, input_total_dims)) + (axes_list[dims[0]], axes_list[dims[1]]) = (axes_list[dims[1]], + axes_list[dims[0]]) + if k == 1: + return transpose(flip(x, dims[1]), axes_list) + else: + # k == 3 + return flip(transpose(x, axes_list), dims[1]) + + def flatten(x, start_axis=0, stop_axis=-1, name=None): r""" **Flatten op** From 7ade7ced5620d56b1e34804b4142be85464a6277 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 12:31:40 +0800 Subject: [PATCH 02/20] update. test=develop --- python/paddle/__init__.py | 2 +- python/paddle/tensor/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 8f16b811b72c6..7a06caeafe0ff 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -405,7 +405,7 @@ 'bitwise_not', 'mm', 'flip', - "rot90", + 'rot90', 'bincount', 'histogram', 'multiplex', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 4dbb3fde7d4cd..d38dbad101a2c 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -367,7 +367,7 @@ 'unsqueeze_', 'unstack', 'flip', - "rot90", + 'rot90', 'unbind', 'roll', 'tile', From 63474df9f58f659c406c6c857d8a295a77f4e307 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 14:24:19 +0800 Subject: [PATCH 03/20] fix. test=develop --- python/paddle/fluid/tests/unittests/test_rot90_op.py | 9 +++++++-- python/paddle/tensor/manipulation.py | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index 1dfb7d06a7186..5c6d710eda6c5 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -32,7 +32,7 @@ def test_static_graph(self): with fluid.program_guard(train_program, startup_program): axis = [0] input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.flip(input, axis) + output = paddle.flip(input, k=1, dims=[0, 1]) output = paddle.flip(output, -1) output = output.flip(0) place = fluid.CPUPlace() @@ -40,15 +40,20 @@ def test_static_graph(self): place = fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(startup_program) + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) res = exe.run(train_program, feed={'input': img}, fetch_list=[output]) + out_np = np.array(res[0]) out_ref = np.array([[3, 2, 1], [6, 5, 4]]).astype(np.float32) + self.assertTrue( (out_np == out_ref).all(), - msg='flip output is wrong, out =' + str(out_np)) + msg='rot90 output is wrong, out =' + str(out_np)) + + def test_dygraph(self): img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index b17c4909a96ea..27616d19243b8 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -552,9 +552,9 @@ def rot90(x, k, dims, name=None): format(dims[0], dims[1])) if not (dims[0] < input_total_dims and dims[0] >= -input_total_dims): - raise ValueError("Rotation dim0 out of range, dim0 =".format(dims[0])) + raise ValueError("Rotation dim0 out of range, dim0 = {}".format(dims[0])) if not (dims[1] < input_total_dims and dims[1] >= -input_total_dims): - raise ValueError("Rotation dim1 out of range, dim1 =".format(dims[1])) + raise ValueError("Rotation dim1 out of range, dim1 = {}".format(dims[1])) ## k % 4 k = k % 4 if k >= 0 else 4 - (-k % 4) From 60a0955d7a221113b53416aacae71cb7f2cae085 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 17:23:50 +0800 Subject: [PATCH 04/20] fix ut. test=develop --- .../fluid/tests/unittests/test_rot90_op.py | 78 +++++++++++-------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index 5c6d710eda6c5..93c1c34f32d14 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -23,7 +23,7 @@ from op_test import OpTest -class TestRot90Op(unittest.TestCase): +class TestRot90Op_API(unittest.TestCase): """Test rot90 api.""" def test_static_graph(self): @@ -32,9 +32,9 @@ def test_static_graph(self): with fluid.program_guard(train_program, startup_program): axis = [0] input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.flip(input, k=1, dims=[0, 1]) - output = paddle.flip(output, -1) - output = output.flip(0) + output = paddle.rot90(input, k=1, dims=[0, 1]) + output = paddle.rot90(output, k=1, dims=[0, 1]) + output = output.rot90(k=1, dims=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -47,38 +47,37 @@ def test_static_graph(self): fetch_list=[output]) out_np = np.array(res[0]) - out_ref = np.array([[3, 2, 1], [6, 5, 4]]).astype(np.float32) + out_ref = np.array([[4, 1], [5, 2], [6, 3]]).astype(np.float32) self.assertTrue( (out_np == out_ref).all(), msg='rot90 output is wrong, out =' + str(out_np)) - - def test_dygraph(self): img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) with fluid.dygraph.guard(): inputs = fluid.dygraph.to_variable(img) - ret = paddle.flip(inputs, [0]) - ret = ret.flip(0) - ret = paddle.flip(ret, 1) - out_ref = np.array([[3, 2, 1], [6, 5, 4]]).astype(np.float32) + + ret = paddle.rot90(inputs, k=1, dims=[0, 1]) + ret = ret.rot90(1, dims=[0, 1]) + ret = paddle.rot90(ret, k=1, dims=[0, 1]) + out_ref = np.array([[4, 1], [5, 2], [6, 3]]).astype(np.float32) self.assertTrue( (ret.numpy() == out_ref).all(), - msg='flip output is wrong, out =' + str(ret.numpy())) + msg='rot90 output is wrong, out =' + str(ret.numpy())) -class TestFlipOp(OpTest): +class TestRot90Op(OpTest): def setUp(self): - self.op_type = 'flip' + self.op_type = 'rot90' self.init_test_case() self.inputs = {'X': np.random.random(self.in_shape).astype('float64')} self.init_attrs() self.outputs = {'Out': self.calc_ref_res()} def init_attrs(self): - self.attrs = {"axis": self.axis} + self.attrs = {"k": self.k, "dims": self.dims} def test_check_output(self): self.check_output() @@ -88,51 +87,66 @@ def test_check_grad(self): def init_test_case(self): self.in_shape = (6, 4, 2, 3) - self.axis = [0, 1] + self.k = 1 + self.dims = [0, 1] def calc_ref_res(self): res = self.inputs['X'] - if isinstance(self.axis, int): - return np.flip(res, self.axis) - for axis in self.axis: - res = np.flip(res, axis) + res = np.rot90(res, self.k, self.dims) + #if isinstance(self.axis, int): + # return np.flip(res, self.axis) + #for axis in self.axis: + # res = np.flip(res, axis) return res -class TestFlipOpAxis1(TestFlipOp): +class TestRot90OpK1(TestRot90Op): def init_test_case(self): self.in_shape = (2, 4, 4) - self.axis = [0] + self.k = 1 + self.dims = [0, 1] -class TestFlipOpAxis2(TestFlipOp): +class TestRot90OpK2(TestRot90Op): def init_test_case(self): self.in_shape = (4, 4, 6, 3) - self.axis = [0, 2] + self.k = 2 + self.dims = [0, 1] -class TestFlipOpAxis3(TestFlipOp): +class TestRot90OpK3(TestRot90Op): def init_test_case(self): self.in_shape = (4, 3, 1) - self.axis = [0, 1, 2] + self.k = 3 + self.dims = [0, 1] + + +class TestRot90OpKNeg1(TestRot90Op): + def init_test_case(self): + self.in_shape = (6, 4, 2, 2) + self.k = -1 + self.dims = [0, 1] -class TestFlipOpAxis4(TestFlipOp): +class TestRot90OpKNeg2(TestRot90Op): def init_test_case(self): self.in_shape = (6, 4, 2, 2) - self.axis = [0, 1, 2, 3] + self.k = -2 + self.dims = [0, 1] -class TestFlipOpEmptyAxis(TestFlipOp): +class TestRot90OpKNeg3(TestRot90Op): def init_test_case(self): self.in_shape = (6, 4, 2, 2) - self.axis = [] + self.k = -3 + self.dims = [0, 1] -class TestFlipOpNegAxis(TestFlipOp): +class TestRot90OpK0(TestRot90Op): def init_test_case(self): self.in_shape = (6, 4, 2, 2) - self.axis = [-1] + self.k = 0 + self.dims = [0, 1] if __name__ == "__main__": From 7892e16f644f4d5f86a36b015eef8a505e3c964e Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 17:35:21 +0800 Subject: [PATCH 05/20] fix ut. test=develop --- paddle/fluid/framework/heter_pipeline_trainer_test.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/paddle/fluid/framework/heter_pipeline_trainer_test.cc b/paddle/fluid/framework/heter_pipeline_trainer_test.cc index af8eca32ee2f4..417c7685bcbeb 100644 --- a/paddle/fluid/framework/heter_pipeline_trainer_test.cc +++ b/paddle/fluid/framework/heter_pipeline_trainer_test.cc @@ -115,8 +115,6 @@ TEST(HeterPipelineTrainerTest, GPU) { t3.add_trainers(1); t3.add_trainers(1); t3.add_trainers(1); - t3.add_dump_fields("hello"); - t3.add_dump_param("fc_0"); auto* heter_section_param3 = t3.mutable_heter_section_param(); heter_section_param3->set_num_pipeline_stages(3); heter_section_param3->set_pipeline_stage(2); From 7cbc6b3ddfe43bf774d0a76f4e15fc889fe61587 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 18:04:44 +0800 Subject: [PATCH 06/20] fix ut. test=develop --- .../fluid/tests/unittests/test_rot90_op.py | 85 +------------------ 1 file changed, 1 insertion(+), 84 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index 93c1c34f32d14..a4a24ba554f43 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -20,10 +20,8 @@ import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid import Program, program_guard -from op_test import OpTest - -class TestRot90Op_API(unittest.TestCase): +class TestRot90_API(unittest.TestCase): """Test rot90 api.""" def test_static_graph(self): @@ -68,86 +66,5 @@ def test_dygraph(self): msg='rot90 output is wrong, out =' + str(ret.numpy())) -class TestRot90Op(OpTest): - def setUp(self): - self.op_type = 'rot90' - self.init_test_case() - self.inputs = {'X': np.random.random(self.in_shape).astype('float64')} - self.init_attrs() - self.outputs = {'Out': self.calc_ref_res()} - - def init_attrs(self): - self.attrs = {"k": self.k, "dims": self.dims} - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad(["X"], "Out") - - def init_test_case(self): - self.in_shape = (6, 4, 2, 3) - self.k = 1 - self.dims = [0, 1] - - def calc_ref_res(self): - res = self.inputs['X'] - res = np.rot90(res, self.k, self.dims) - #if isinstance(self.axis, int): - # return np.flip(res, self.axis) - #for axis in self.axis: - # res = np.flip(res, axis) - return res - - -class TestRot90OpK1(TestRot90Op): - def init_test_case(self): - self.in_shape = (2, 4, 4) - self.k = 1 - self.dims = [0, 1] - - -class TestRot90OpK2(TestRot90Op): - def init_test_case(self): - self.in_shape = (4, 4, 6, 3) - self.k = 2 - self.dims = [0, 1] - - -class TestRot90OpK3(TestRot90Op): - def init_test_case(self): - self.in_shape = (4, 3, 1) - self.k = 3 - self.dims = [0, 1] - - -class TestRot90OpKNeg1(TestRot90Op): - def init_test_case(self): - self.in_shape = (6, 4, 2, 2) - self.k = -1 - self.dims = [0, 1] - - -class TestRot90OpKNeg2(TestRot90Op): - def init_test_case(self): - self.in_shape = (6, 4, 2, 2) - self.k = -2 - self.dims = [0, 1] - - -class TestRot90OpKNeg3(TestRot90Op): - def init_test_case(self): - self.in_shape = (6, 4, 2, 2) - self.k = -3 - self.dims = [0, 1] - - -class TestRot90OpK0(TestRot90Op): - def init_test_case(self): - self.in_shape = (6, 4, 2, 2) - self.k = 0 - self.dims = [0, 1] - - if __name__ == "__main__": unittest.main() From 9efb107f9761a8de3d1d8d9fc3a361c6f3a3272d Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 18:33:14 +0800 Subject: [PATCH 07/20] update. test=develop --- python/paddle/tensor/manipulation.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 27616d19243b8..3ae451f71fdec 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -512,7 +512,6 @@ def rot90(x, k, dims, name=None): Examples: .. code-block:: python - import paddle import numpy as np @@ -552,9 +551,11 @@ def rot90(x, k, dims, name=None): format(dims[0], dims[1])) if not (dims[0] < input_total_dims and dims[0] >= -input_total_dims): - raise ValueError("Rotation dim0 out of range, dim0 = {}".format(dims[0])) + raise ValueError("Rotation dim0 out of range, dim0 = {}".format(dims[ + 0])) if not (dims[1] < input_total_dims and dims[1] >= -input_total_dims): - raise ValueError("Rotation dim1 out of range, dim1 = {}".format(dims[1])) + raise ValueError("Rotation dim1 out of range, dim1 = {}".format(dims[ + 1])) ## k % 4 k = k % 4 if k >= 0 else 4 - (-k % 4) From 4637f1e122561860dee78da529f2be4b85a8acf9 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 18:53:40 +0800 Subject: [PATCH 08/20] fix ut. test=develop --- python/paddle/fluid/tests/unittests/test_rot90_op.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index a4a24ba554f43..23795f7c9b96d 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -21,6 +21,7 @@ import paddle.fluid.core as core from paddle.fluid import Program, program_guard + class TestRot90_API(unittest.TestCase): """Test rot90 api.""" @@ -55,7 +56,7 @@ def test_dygraph(self): img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) with fluid.dygraph.guard(): inputs = fluid.dygraph.to_variable(img) - + ret = paddle.rot90(inputs, k=1, dims=[0, 1]) ret = ret.rot90(1, dims=[0, 1]) ret = paddle.rot90(ret, k=1, dims=[0, 1]) From 3a5564faa0a6d8ca64f356aea467a9cf7c0d1430 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 19:16:38 +0800 Subject: [PATCH 09/20] fix ut. test=develop --- python/paddle/fluid/tests/unittests/test_rot90_op.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index 23795f7c9b96d..d7c0bf2e5362c 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -26,6 +26,7 @@ class TestRot90_API(unittest.TestCase): """Test rot90 api.""" def test_static_graph(self): + paddle.enable_static() startup_program = fluid.Program() train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): From 1dd77f5ca7dbb87883dda5126986c5f249b9cdf9 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sat, 27 Nov 2021 19:42:35 +0800 Subject: [PATCH 10/20] fix sample code. test=develop --- python/paddle/tensor/manipulation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 3ae451f71fdec..e49c7abd5f343 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -523,6 +523,7 @@ def rot90(x, k, dims, name=None): y= paddle.rot90(data, -1, [0, 1]) print(y) #[[2, 0],[3, 1]] data2 = paddle.arange(8) + data2 = paddle.reshape(data2, (2,2,2)) print(data2) ###[[[0, 1],[2, 3]],[[4, 5],[6, 7]]] y = paddle.rot90(data2, 1, [1, 2]) print(y) ### [[[1, 3],[0, 2]],[[5, 7],[4, 6]]] From 613e4ceb50cec7b8c1d103d366308fd18d0567be Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sun, 28 Nov 2021 14:21:33 +0800 Subject: [PATCH 11/20] fix ut. test=develop --- .../fluid/tests/unittests/test_rot90_op.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index d7c0bf2e5362c..7a75ba5638103 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -53,6 +53,46 @@ def test_static_graph(self): (out_np == out_ref).all(), msg='rot90 output is wrong, out =' + str(out_np)) + def test_k(): + paddle.enable_static() + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=0, dims=[0, 1]) + output = paddle.rot90(input, k=2, dims=[0, 1]) + output = paddle.rot90(input, k=3, dims=[0, 1]) + + def test_error_api(): + ## dims error + def run1(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=1, dims=[0]) + + self.assertRaises(ValueError, run1) + + ## input dims error + def run2(): + input = fluid.data(name='input', dtype='float32', shape=[2]) + output = paddle.rot90(input, k=1, dims=[0, 1]) + + self.assertRaises(ValueError, run2) + + def run3(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=1, dims=[0, 0]) + + self.assertRaises(ValueError, run3) + + def run4(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=1, dims=[3, 1]) + + self.assertRaises(ValueError, run4) + + def run5(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=1, dims=[0, 3]) + + self.assertRaises(ValueError, run5) + def test_dygraph(self): img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) with fluid.dygraph.guard(): From 9db00b490657bc77d1d9c895890bf0524ba4cf88 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sun, 28 Nov 2021 14:50:02 +0800 Subject: [PATCH 12/20] fix ut. test=develop --- python/paddle/fluid/tests/unittests/test_rot90_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index 7a75ba5638103..c2eb58f70a436 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -53,14 +53,14 @@ def test_static_graph(self): (out_np == out_ref).all(), msg='rot90 output is wrong, out =' + str(out_np)) - def test_k(): + def test_k(self): paddle.enable_static() input = fluid.data(name='input', dtype='float32', shape=[2, 3]) output = paddle.rot90(input, k=0, dims=[0, 1]) output = paddle.rot90(input, k=2, dims=[0, 1]) output = paddle.rot90(input, k=3, dims=[0, 1]) - def test_error_api(): + def test_error_api(self): ## dims error def run1(): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) From b1764206b44b64a07e878cae8ed23c5e84b12fea Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sun, 28 Nov 2021 16:35:43 +0800 Subject: [PATCH 13/20] fix ut. test=develop --- python/paddle/fluid/tests/unittests/test_rot90_op.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index c2eb58f70a436..b2f9524e59c4f 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -61,6 +61,8 @@ def test_k(self): output = paddle.rot90(input, k=3, dims=[0, 1]) def test_error_api(self): + paddle.enable_static() + ## dims error def run1(): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) From f0005e1fe8872190f91b844439e82093fa387fb3 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Sun, 28 Nov 2021 21:55:24 +0800 Subject: [PATCH 14/20] fix ut. test=develop --- python/paddle/fluid/tests/unittests/test_rot90_op.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index b2f9524e59c4f..ae8fed3806a89 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -60,6 +60,13 @@ def test_k(self): output = paddle.rot90(input, k=2, dims=[0, 1]) output = paddle.rot90(input, k=3, dims=[0, 1]) + def test_neg_k(self): + paddle.enable_static() + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=-1, dims=[0, 1]) + output = paddle.rot90(input, k=-2, dims=[0, 1]) + output = paddle.rot90(input, k=-3, dims=[0, 1]) + def test_error_api(self): paddle.enable_static() From 12098edb63450e1618f1b83bfb59e1dde5d6de36 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Mon, 29 Nov 2021 01:56:55 +0800 Subject: [PATCH 15/20] fix paddle.rot90 doc. test=develop --- python/paddle/tensor/manipulation.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index e49c7abd5f343..b8a080535678a 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -510,6 +510,11 @@ def rot90(x, k, dims, name=None): Returns: Tensor: Tensor or LoDTensor calculated by rot90 layer. The data type is same with input x. + Raises: + TypeError: If the data type of ``x`` is not Variable + TypeError: If the dtype of ``x`` is not float16, float32, float64, int32, int64, bool + TypeError: If the data type of ``dims`` is not list, tuple + Examples: .. code-block:: python import paddle From 592d746ebffae44ba25083d9927c0779f47b2986 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Mon, 29 Nov 2021 15:16:56 +0800 Subject: [PATCH 16/20] update ut. test=develop --- .../fluid/tests/unittests/test_rot90_op.py | 159 +++++++++++++++++- 1 file changed, 150 insertions(+), 9 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index ae8fed3806a89..dc70d05d672a2 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -30,7 +30,6 @@ def test_static_graph(self): startup_program = fluid.Program() train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): - axis = [0] input = fluid.data(name='input', dtype='float32', shape=[2, 3]) output = paddle.rot90(input, k=1, dims=[0, 1]) output = paddle.rot90(output, k=1, dims=[0, 1]) @@ -53,19 +52,161 @@ def test_static_graph(self): (out_np == out_ref).all(), msg='rot90 output is wrong, out =' + str(out_np)) - def test_k(self): + def test_static_k_0(self): paddle.enable_static() input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=0, dims=[0, 1]) - output = paddle.rot90(input, k=2, dims=[0, 1]) - output = paddle.rot90(input, k=3, dims=[0, 1]) + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=0, dims=[0, 1]) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + out_ref = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + + self.assertTrue( + (out_np == out_ref).all(), + msg='rot90 output is wrong, out =' + str(out_np)) + + def test_static_k_2(self): + paddle.enable_static() + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=2, dims=[0, 1]) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + out_ref = np.array([[6, 5, 4], [3, 2, 1]]).astype(np.float32) + + self.assertTrue( + (out_np == out_ref).all(), + msg='rot90 output is wrong, out =' + str(out_np)) + + def test_static_k_3(self): + paddle.enable_static() + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=3, dims=[0, 1]) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + out_ref = np.array([[4, 1], [5, 2], [6, 3]]).astype(np.float32) + + self.assertTrue( + (out_np == out_ref).all(), + msg='rot90 output is wrong, out =' + str(out_np)) - def test_neg_k(self): + def test_static_neg_k_1(self): paddle.enable_static() input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=-1, dims=[0, 1]) - output = paddle.rot90(input, k=-2, dims=[0, 1]) - output = paddle.rot90(input, k=-3, dims=[0, 1]) + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=-1, dims=[0, 1]) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + out_ref = np.array([[4, 1], [5, 2], [6, 3]]).astype(np.float32) + + self.assertTrue( + (out_np == out_ref).all(), + msg='rot90 output is wrong, out =' + str(out_np)) + + def test_static_neg_k_2(self): + paddle.enable_static() + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=-2, dims=[0, 1]) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + out_ref = np.array([[6, 5, 4], [3, 2, 1]]).astype(np.float32) + + self.assertTrue( + (out_np == out_ref).all(), + msg='rot90 output is wrong, out =' + str(out_np)) + + def test_static_neg_k_3(self): + paddle.enable_static() + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + startup_program = fluid.Program() + train_program = fluid.Program() + with fluid.program_guard(train_program, startup_program): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.rot90(input, k=-3, dims=[0, 1]) + place = fluid.CPUPlace() + if fluid.core.is_compiled_with_cuda(): + place = fluid.CUDAPlace(0) + exe = fluid.Executor(place) + exe.run(startup_program) + + img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + res = exe.run(train_program, + feed={'input': img}, + fetch_list=[output]) + + out_np = np.array(res[0]) + out_ref = np.array([[3, 6], [2, 5], [1, 4]]).astype(np.float32) + + self.assertTrue( + (out_np == out_ref).all(), + msg='rot90 output is wrong, out =' + str(out_np)) def test_error_api(self): paddle.enable_static() From 6ca6380ff4732f942ac3c902b1f73b5d50a9daa4 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Mon, 29 Nov 2021 17:01:24 +0800 Subject: [PATCH 17/20] fix. test=develop --- python/paddle/tensor/manipulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index b8a080535678a..f481598bac45d 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -495,7 +495,7 @@ def flip(x, axis, name=None): return out -def rot90(x, k, dims, name=None): +def rot90(x, k=1, dims=[0, 1], name=None): """ Rotate a n-D tensor by 90 degrees in the plane specified by dims axis. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0. From 69f298f43f808ea8f4d93ce9393fa825df568ba8 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Mon, 29 Nov 2021 17:11:55 +0800 Subject: [PATCH 18/20] fix .test=develop --- python/paddle/tensor/manipulation.py | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index f481598bac45d..028c15c76d292 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -495,7 +495,7 @@ def flip(x, axis, name=None): return out -def rot90(x, k=1, dims=[0, 1], name=None): +def rot90(x, k=1, axes=[0, 1], name=None): """ Rotate a n-D tensor by 90 degrees in the plane specified by dims axis. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0. @@ -503,7 +503,7 @@ def rot90(x, k=1, dims=[0, 1], name=None): x (Tensor): The input Tensor(or LoDTensor). The data type of the input Tensor x should be float32, float64, int32, int64, bool. k (int): Number of times to rotate - dims (list|tuple): Axis to rotate + axes (list|tuple): Axis to rotate name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` . @@ -540,27 +540,27 @@ def rot90(x, k=1, dims=[0, 1], name=None): check_dtype(dtype, 'X', ['float16', 'float32', 'float64', 'int32', 'int64', 'bool'], 'rot90') - check_type(dims, 'dims', (list, tuple), 'rot90') + check_type(axes, 'axes', (list, tuple), 'rot90') input_total_dims = len(x.shape) - total_rot_dims = len(dims) + total_rot_dims = len(axes) if total_rot_dims != 2: - raise ValueError("expected total rotation dims == 2, but got dims = {}". + raise ValueError("expected total rotation axes == 2, but got axes = {}". format(total_rot_dims)) if input_total_dims < 2: raise ValueError("expected total dims >= 2, but got total dims = {}". format(input_total_dims)) - if not (dims[0] != dims[1] and abs(dims[0] - dims[1]) != input_total_dims): + if not (axes[0] != axes[1] and abs(axes[0] - axes[1]) != input_total_dims): raise ValueError( - "expected rotation dims to be different, but got dim0 = {}, and dim1 = {}". - format(dims[0], dims[1])) + "expected rotation axes to be different, but got axis0 = {}, and axis1 = {}". + format(axes[0], axes[1])) - if not (dims[0] < input_total_dims and dims[0] >= -input_total_dims): - raise ValueError("Rotation dim0 out of range, dim0 = {}".format(dims[ + if not (axes[0] < input_total_dims and axes[0] >= -input_total_dims): + raise ValueError("Rotation axis0 out of range, axis0 = {}".format(axes[ 0])) - if not (dims[1] < input_total_dims and dims[1] >= -input_total_dims): - raise ValueError("Rotation dim1 out of range, dim1 = {}".format(dims[ + if not (axes[1] < input_total_dims and axes[1] >= -input_total_dims): + raise ValueError("Rotation axis1 out of range, axis1 = {}".format(axes[ 1])) ## k % 4 @@ -568,16 +568,16 @@ def rot90(x, k=1, dims=[0, 1], name=None): if k == 0: return x if k == 2: - return flip(flip(x, dims[0]), dims[1]) + return flip(flip(x, axes[0]), axes[1]) axes_list = list(range(0, input_total_dims)) - (axes_list[dims[0]], axes_list[dims[1]]) = (axes_list[dims[1]], - axes_list[dims[0]]) + (axes_list[axes[0]], axes_list[axes[1]]) = (axes_list[axes[1]], + axes_list[axes[0]]) if k == 1: - return transpose(flip(x, dims[1]), axes_list) + return transpose(flip(x, axes[1]), axes_list) else: # k == 3 - return flip(transpose(x, axes_list), dims[1]) + return flip(transpose(x, axes_list), axes[1]) def flatten(x, start_axis=0, stop_axis=-1, name=None): From b82b1facd72e79c2ebeaea9648b52a3fab6d7971 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Mon, 29 Nov 2021 17:14:05 +0800 Subject: [PATCH 19/20] fix .test=develop --- .../fluid/tests/unittests/test_rot90_op.py | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_rot90_op.py b/python/paddle/fluid/tests/unittests/test_rot90_op.py index dc70d05d672a2..4ab7c4f14f96b 100644 --- a/python/paddle/fluid/tests/unittests/test_rot90_op.py +++ b/python/paddle/fluid/tests/unittests/test_rot90_op.py @@ -31,9 +31,9 @@ def test_static_graph(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=1, dims=[0, 1]) - output = paddle.rot90(output, k=1, dims=[0, 1]) - output = output.rot90(k=1, dims=[0, 1]) + output = paddle.rot90(input, k=1, axes=[0, 1]) + output = paddle.rot90(output, k=1, axes=[0, 1]) + output = output.rot90(k=1, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -59,7 +59,7 @@ def test_static_k_0(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=0, dims=[0, 1]) + output = paddle.rot90(input, k=0, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -85,7 +85,7 @@ def test_static_k_2(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=2, dims=[0, 1]) + output = paddle.rot90(input, k=2, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -111,7 +111,7 @@ def test_static_k_3(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=3, dims=[0, 1]) + output = paddle.rot90(input, k=3, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -137,7 +137,7 @@ def test_static_neg_k_1(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=-1, dims=[0, 1]) + output = paddle.rot90(input, k=-1, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -163,7 +163,7 @@ def test_static_neg_k_2(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=-2, dims=[0, 1]) + output = paddle.rot90(input, k=-2, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -189,7 +189,7 @@ def test_static_neg_k_3(self): train_program = fluid.Program() with fluid.program_guard(train_program, startup_program): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=-3, dims=[0, 1]) + output = paddle.rot90(input, k=-3, axes=[0, 1]) place = fluid.CPUPlace() if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) @@ -214,32 +214,32 @@ def test_error_api(self): ## dims error def run1(): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=1, dims=[0]) + output = paddle.rot90(input, k=1, axes=[0]) self.assertRaises(ValueError, run1) ## input dims error def run2(): input = fluid.data(name='input', dtype='float32', shape=[2]) - output = paddle.rot90(input, k=1, dims=[0, 1]) + output = paddle.rot90(input, k=1, axes=[0, 1]) self.assertRaises(ValueError, run2) def run3(): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=1, dims=[0, 0]) + output = paddle.rot90(input, k=1, axes=[0, 0]) self.assertRaises(ValueError, run3) def run4(): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=1, dims=[3, 1]) + output = paddle.rot90(input, k=1, axes=[3, 1]) self.assertRaises(ValueError, run4) def run5(): input = fluid.data(name='input', dtype='float32', shape=[2, 3]) - output = paddle.rot90(input, k=1, dims=[0, 3]) + output = paddle.rot90(input, k=1, axes=[0, 3]) self.assertRaises(ValueError, run5) @@ -248,9 +248,9 @@ def test_dygraph(self): with fluid.dygraph.guard(): inputs = fluid.dygraph.to_variable(img) - ret = paddle.rot90(inputs, k=1, dims=[0, 1]) - ret = ret.rot90(1, dims=[0, 1]) - ret = paddle.rot90(ret, k=1, dims=[0, 1]) + ret = paddle.rot90(inputs, k=1, axes=[0, 1]) + ret = ret.rot90(1, axes=[0, 1]) + ret = paddle.rot90(ret, k=1, axes=[0, 1]) out_ref = np.array([[4, 1], [5, 2], [6, 3]]).astype(np.float32) self.assertTrue( From 2faa22030917c6c2374794303cf0aba534cd0fa0 Mon Sep 17 00:00:00 2001 From: zmxdream Date: Fri, 3 Dec 2021 16:27:23 +0800 Subject: [PATCH 20/20] fix doc. test=develop --- python/paddle/tensor/manipulation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 028c15c76d292..f48e5a3a764cd 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -517,6 +517,7 @@ def rot90(x, k=1, axes=[0, 1], name=None): Examples: .. code-block:: python + import paddle import numpy as np