From 0f84cc3f7900ddbbe98506273c151dcd2d5562fd Mon Sep 17 00:00:00 2001 From: TeslaZhao Date: Tue, 17 Nov 2020 18:53:11 +0800 Subject: [PATCH 1/3] Fix two english api documents, transpose and strided_slice --- python/paddle/fluid/layers/nn.py | 5 ++--- python/paddle/tensor/manipulation.py | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 2feca60430dc04..a11cdc1de27c3a 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -5450,7 +5450,6 @@ def transpose(x, perm, name=None): """ :alias_main: paddle.transpose :alias: paddle.transpose,paddle.tensor.transpose,paddle.tensor.linalg.transpose,paddle.tensor.manipulation.transpose - :old_api: paddle.fluid.layers.transpose Permute the data dimensions of `input` according to `perm`. @@ -5458,12 +5457,12 @@ def transpose(x, perm, name=None): perm[i]-th dimension of `input`. Args: - x (Variable): The input Tensor. It is a N-D Tensor of data types float32, float64, int32. + x (Tensor): The input Tensor. It is a N-D Tensor of data types float32, float64, int32. perm (list|tuple): Permute the input according to the data of perm. name (str): The name of this layer. It is optional. Returns: - Variable: A transposed n-D Tensor, with data type being float32, float64, int32, int64. + Tensor: A transposed n-D Tensor, with data type being float32, float64, int32, int64. For Example: diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index bdda90315ac9c7..69ba4264e3e213 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -1503,6 +1503,7 @@ def strided_slice(x, axes, starts, ends, strides, name=None): strides = [1, 3] Then: result = [ [2], ] + Args: x (Tensor): An N-D ``Tensor``. The data type is ``float32``, ``float64``, ``int32`` or ``int64``. axes (list|tuple): The data type is ``int32`` . Axes that `starts` and `ends` apply to. @@ -1534,7 +1535,7 @@ def strided_slice(x, axes, starts, ends, strides, name=None): # sliced_1 is x[:, 1:3:1, 0:2:1, 2:4:1]. # example 2: # attr starts is a list which contain tensor Tensor. - minus_3 = paddle.fill_constant([1], "int32", -3) + minus_3 = paddle.full(shape=[1], fill_value=-3, dtype='int32') sliced_2 = paddle.strided_slice(x, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2) # sliced_2 is x[:, 1:3:1, 0:2:1, 2:4:2]. """ From d327cae6277dc03ba3bac83b782e093044df790b Mon Sep 17 00:00:00 2001 From: TeslaZhao Date: Thu, 19 Nov 2020 10:49:47 +0800 Subject: [PATCH 2/3] delete nouse comments --- python/paddle/fluid/layers/nn.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index a11cdc1de27c3a..cafb965d406d93 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -5448,9 +5448,6 @@ def ctc_greedy_decoder(input, def transpose(x, perm, name=None): """ - :alias_main: paddle.transpose - :alias: paddle.transpose,paddle.tensor.transpose,paddle.tensor.linalg.transpose,paddle.tensor.manipulation.transpose - Permute the data dimensions of `input` according to `perm`. The `i`-th dimension of the returned tensor will correspond to the From f741806cb808e5b7b5221f2405485374812b4b99 Mon Sep 17 00:00:00 2001 From: TeslaZhao Date: Mon, 7 Jun 2021 12:36:54 +0800 Subject: [PATCH 3/3] OP:strided_slice_op supports bool type inputs --- paddle/fluid/operators/strided_slice_op.cc | 2 + paddle/fluid/operators/strided_slice_op.cu | 4 +- python/paddle/fluid/layers/nn.py | 4 +- .../tests/unittests/test_strided_slice_op.py | 65 +++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/operators/strided_slice_op.cc b/paddle/fluid/operators/strided_slice_op.cc index d71be60e1f5c22..f8272d550b9991 100644 --- a/paddle/fluid/operators/strided_slice_op.cc +++ b/paddle/fluid/operators/strided_slice_op.cc @@ -324,6 +324,7 @@ REGISTER_OPERATOR(strided_slice_grad, ops::StridedSliceOpGrad, REGISTER_OP_CPU_KERNEL( strided_slice, + ops::StridedSliceKernel, ops::StridedSliceKernel, ops::StridedSliceKernel, ops::StridedSliceKernel, @@ -335,6 +336,7 @@ REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL( strided_slice_grad, + ops::StridedSliceGradKernel, ops::StridedSliceGradKernel, ops::StridedSliceGradKernel, ops::StridedSliceGradKernel, diff --git a/paddle/fluid/operators/strided_slice_op.cu b/paddle/fluid/operators/strided_slice_op.cu index 68a8312f0818d4..f88605fbfc86dc 100644 --- a/paddle/fluid/operators/strided_slice_op.cu +++ b/paddle/fluid/operators/strided_slice_op.cu @@ -18,6 +18,7 @@ limitations under the License. */ namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL( strided_slice, + ops::StridedSliceKernel, ops::StridedSliceKernel, ops::StridedSliceKernel, ops::StridedSliceKernel, @@ -29,7 +30,8 @@ REGISTER_OP_CUDA_KERNEL( REGISTER_OP_CUDA_KERNEL( strided_slice_grad, - ops::StridedSliceGradKernel, + ops::StridedSliceGradKernel, + ops::StridedSliceGradKernel, ops::StridedSliceGradKernel, ops::StridedSliceGradKernel, ops::StridedSliceGradKernel, diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index ee08cb8654ec13..d7c95dc4669beb 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -11093,7 +11093,7 @@ def strided_slice(input, axes, starts, ends, strides): Then: result = [ [2], ] Args: - input (Variable): An N-D ``Tensor`` or ``LoDTensor`` . The data type is ``float32``, ``float64``, ``int32`` or ``int64``. + input (Variable): An N-D ``Tensor`` or ``LoDTensor`` . The data type is ``bool``, ``float32``, ``float64``, ``int32`` or ``int64``. axes (list|tuple): The data type is ``int32`` . Axes that `starts` and `ends` apply to. It's optional. If it is not provides, it will be treated as :math:`[0,1,...,len(starts)-1]`. starts (list|tuple|Variable): The data type is ``int32`` . If ``starts`` is a list or tuple, the elements of @@ -11144,7 +11144,7 @@ def strided_slice(input, axes, starts, ends, strides): helper = LayerHelper('strided_slice', **locals()) check_variable_and_dtype(input, 'input', - ['float32', 'float64', 'int32', 'int64'], + ['bool', 'float32', 'float64', 'int32', 'int64'], 'strided_slice') check_type(axes, 'axes', (list, tuple), 'strided_slice') check_type(starts, 'starts', (list, tuple, Variable), 'strided_slice') diff --git a/python/paddle/fluid/tests/unittests/test_strided_slice_op.py b/python/paddle/fluid/tests/unittests/test_strided_slice_op.py index 71550c8f24753c..ebf7c01e2cae5f 100644 --- a/python/paddle/fluid/tests/unittests/test_strided_slice_op.py +++ b/python/paddle/fluid/tests/unittests/test_strided_slice_op.py @@ -216,6 +216,71 @@ def initTestCase(self): self.infer_flags = [1, 1, 1, 1, 1] +class TestStrideSliceOpBool(TestStrideSliceOp): + def test_check_grad(self): + pass + + +class TestStrideSliceOpBool1D(TestStrideSliceOpBool): + def initTestCase(self): + self.input = np.random.rand(100).astype("bool") + self.axes = [0] + self.starts = [3] + self.ends = [8] + self.strides = [1] + self.infer_flags = [1] + + +class TestStrideSliceOpBool2D(TestStrideSliceOpBool): + def initTestCase(self): + self.input = np.random.rand(10, 10).astype("bool") + self.axes = [0, 1] + self.starts = [1, 0] + self.ends = [2, 2] + self.strides = [1, 1] + self.infer_flags = [1, 1] + + +class TestStrideSliceOpBool3D(TestStrideSliceOpBool): + def initTestCase(self): + self.input = np.random.rand(3, 4, 10).astype("bool") + self.axes = [0, 1, 2] + self.starts = [0, -1, 0] + self.ends = [2, -3, 5] + self.strides = [1, -1, 1] + self.infer_flags = [1, 1, 1] + + +class TestStrideSliceOpBool4D(TestStrideSliceOpBool): + def initTestCase(self): + self.input = np.random.rand(3, 3, 3, 4).astype("bool") + self.axes = [0, 1, 2, 3] + self.starts = [1, 0, 0, 0] + self.ends = [2, 2, 3, 4] + self.strides = [1, 1, 1, 2] + self.infer_flags = [1, 1, 1, 1] + + +class TestStrideSliceOpBool5D(TestStrideSliceOpBool): + def initTestCase(self): + self.input = np.random.rand(3, 3, 3, 4, 5).astype("bool") + self.axes = [0, 1, 2, 3, 4] + self.starts = [1, 0, 0, 0, 0] + self.ends = [2, 2, 3, 4, 4] + self.strides = [1, 1, 1, 1, 1] + self.infer_flags = [1, 1, 1, 1] + + +class TestStrideSliceOpBool6D(TestStrideSliceOpBool): + def initTestCase(self): + self.input = np.random.rand(3, 3, 3, 6, 7, 8).astype("bool") + self.axes = [0, 1, 2, 3, 4, 5] + self.starts = [1, 0, 0, 0, 1, 2] + self.ends = [2, 2, 3, 1, 2, 8] + self.strides = [1, 1, 1, 1, 1, 2] + self.infer_flags = [1, 1, 1, 1, 1] + + class TestStridedSliceOp_starts_ListTensor(OpTest): def setUp(self): self.op_type = "strided_slice"