diff --git a/paddle/fluid/operators/math/math_function.cu b/paddle/fluid/operators/math/math_function.cu index 46dd58562aebdc8d1606225e4b0de399d144e5f0..cfdfa456e39eac6f09a7f0d261d228f7f98b2da8 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 2b02d76d08e2de7b3a88b7032d6f601b9a6ca003..35dcb25f6b784dec698a37ab6012671be66d8b88 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 383fc6a5b9b32ca9d8f06802fd62cb89e0e85afc..5bcfbee5c53bc9a3b01f21ebe5a360fae2e23372 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 6a1320e65aba95e07d7b9e831544450cdfaa0bba..8167a0c44373ca7e2ec34ec3bc6c4f8a82699ad8 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 59b4afdf8b02d286728a8f801959e3d08f840e97..e255350fd66186df5c7d0d41404122a575c8fc74 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)