From 95c100c122e4c3344ad8599998cafa177ee19cb4 Mon Sep 17 00:00:00 2001 From: TeslaZhao Date: Thu, 23 Sep 2021 11:23:14 +0800 Subject: [PATCH] op:transpose_op supports bool type (#35886) (#35926) * Pass compat of conv_transpose_bias_mkldnn_fuse_pass * Fix a bug of strided_slice op, about the axes parameter access memory out of bounds * Fix a bug of transpose op, about accessing memory out of bounds of the perm param * op:transpose_op supports bool type --- paddle/fluid/operators/math/math_function.cu | 1 + paddle/fluid/operators/transpose_op.cc | 8 +- paddle/fluid/operators/transpose_op.cu | 4 + python/paddle/fluid/layers/nn.py | 6 +- .../tests/unittests/test_transpose_op.py | 97 ++++++++++++++++++- 5 files changed, 109 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/operators/math/math_function.cu b/paddle/fluid/operators/math/math_function.cu index 46dd58562a..cfdfa456e3 100644 --- a/paddle/fluid/operators/math/math_function.cu +++ b/paddle/fluid/operators/math/math_function.cu @@ -44,6 +44,7 @@ template struct SetConstant>; #define DEFINE_GPU_TRANS(RANK) \ + template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ diff --git a/paddle/fluid/operators/transpose_op.cc b/paddle/fluid/operators/transpose_op.cc index 2b02d76d08..35dcb25f6b 100644 --- a/paddle/fluid/operators/transpose_op.cc +++ b/paddle/fluid/operators/transpose_op.cc @@ -350,7 +350,8 @@ REGISTER_OPERATOR( REGISTER_OPERATOR(transpose_grad, ops::TransposeOpGrad); REGISTER_OP_CPU_KERNEL( - transpose, ops::TransposeKernel, + transpose, ops::TransposeKernel, + ops::TransposeKernel, ops::TransposeKernel, ops::TransposeKernel>, @@ -358,6 +359,7 @@ REGISTER_OP_CPU_KERNEL( paddle::platform::complex>); REGISTER_OP_CPU_KERNEL( transpose_grad, + ops::TransposeGradKernel, ops::TransposeGradKernel, ops::TransposeGradKernel, ops::TransposeGradKernel); REGISTER_OP_CPU_KERNEL( - transpose2, ops::TransposeKernel, + transpose2, ops::TransposeKernel, + ops::TransposeKernel, ops::TransposeKernel, ops::TransposeKernel, ops::TransposeKernel, @@ -383,6 +386,7 @@ REGISTER_OP_CPU_KERNEL( paddle::platform::complex>); REGISTER_OP_CPU_KERNEL( transpose2_grad, + ops::TransposeGradKernel, ops::TransposeGradKernel, ops::TransposeGradKernel, ops::TransposeGradKernel, diff --git a/paddle/fluid/operators/transpose_op.cu b/paddle/fluid/operators/transpose_op.cu index 383fc6a5b9..5bcfbee5c5 100644 --- a/paddle/fluid/operators/transpose_op.cu +++ b/paddle/fluid/operators/transpose_op.cu @@ -83,6 +83,7 @@ namespace plat = paddle::platform; REGISTER_OP_CUDA_KERNEL( transpose, + ops::TransposeGPUKernel, ops::TransposeGPUKernel, ops::TransposeGPUKernel, ops::TransposeGPUKernel, @@ -92,6 +93,7 @@ REGISTER_OP_CUDA_KERNEL( paddle::platform::complex>); REGISTER_OP_CUDA_KERNEL( transpose_grad, + ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel, ops::TransposeGPUKernel, ops::TransposeGPUKernel, ops::TransposeGPUKernel, @@ -114,6 +117,7 @@ REGISTER_OP_CUDA_KERNEL( paddle::platform::complex>); REGISTER_OP_CUDA_KERNEL( transpose2_grad, + ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel, diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 6a1320e65a..8167a0c443 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -5499,12 +5499,12 @@ def transpose(x, perm, name=None): perm[i]-th dimension of `input`. Args: - x (Tensor): 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 bool, 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: - Tensor: A transposed n-D Tensor, with data type being float32, float64, int32, int64. + Tensor: A transposed n-D Tensor, with data type being bool, float32, float64, int32, int64. For Example: @@ -5546,7 +5546,7 @@ def transpose(x, perm, name=None): return out check_variable_and_dtype( - x, 'x', ['float16', 'float32', 'float64', 'int32', 'int64'], + x, 'x', ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], 'transpose') check_type(perm, 'perm', (list, tuple), 'transpose') if isinstance(perm, tuple): diff --git a/python/paddle/fluid/tests/unittests/test_transpose_op.py b/python/paddle/fluid/tests/unittests/test_transpose_op.py index 59b4afdf8b..e255350fd6 100644 --- a/python/paddle/fluid/tests/unittests/test_transpose_op.py +++ b/python/paddle/fluid/tests/unittests/test_transpose_op.py @@ -113,6 +113,99 @@ class TestCase9(TestTransposeOp): self.axis = (6, 1, 3, 5, 0, 2, 4, 7) +class TestTransposeOpBool(TestTransposeOp): + def test_check_grad(self): + pass + + +class TestTransposeOpBool1D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (100, ) + self.axis = (0, ) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool2D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (3, 40) + self.axis = (1, 0) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool3D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (3, 4, 10) + self.axis = (0, 2, 1) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool4D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (2, 3, 4, 5) + self.axis = (0, 2, 3, 1) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool5D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (2, 3, 4, 5, 6) + self.axis = (4, 2, 3, 1, 0) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool6D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (2, 3, 4, 5, 6, 1) + self.axis = (4, 2, 3, 1, 0, 5) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool7D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (2, 3, 2, 3, 2, 4, 3) + self.axis = (0, 1, 3, 2, 4, 5, 6) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + +class TestTransposeOpBool8D(TestTransposeOpBool): + def initTestCase(self): + self.shape = (2, 3, 2, 3, 2, 4, 3, 3) + self.axis = (6, 1, 3, 5, 0, 2, 4, 7) + self.inputs = {'X': np.random.random(self.shape).astype("bool")} + self.outputs = { + 'XShape': np.random.random(self.shape).astype("bool"), + 'Out': self.inputs['X'].transpose(self.axis) + } + + class TestTransposeOpError(unittest.TestCase): def test_errors(self): paddle.enable_static() @@ -126,9 +219,9 @@ class TestTransposeOpError(unittest.TestCase): self.assertRaises(TypeError, test_x_Variable_check) def test_x_dtype_check(): - # the Input(x)'s dtype must be one of [float16, float32, float64, int32, int64] + # the Input(x)'s dtype must be one of [bool, float16, float32, float64, int32, int64] x1 = fluid.layers.data( - name='x1', shape=[10, 5, 3], dtype='bool') + name='x1', shape=[10, 5, 3], dtype='int8') fluid.layers.transpose(x1, perm=[1, 0, 2]) self.assertRaises(TypeError, test_x_dtype_check) -- GitLab