From 1e6047f19001df82c241fd4e2360c7ac44f11a1a Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Fri, 11 Feb 2022 17:35:16 +0800 Subject: [PATCH] [bf16] add bf16 kernel: transpose & unbind (#39457) * add transpose unbind * add unittest * refine transpose unittest --- paddle/fluid/operators/transpose_op.cc | 16 ++++++--- paddle/fluid/operators/transpose_op.cu | 9 +++++ paddle/fluid/operators/unbind_op.cc | 3 +- paddle/fluid/operators/unbind_op.cu.cc | 3 +- .../tests/unittests/test_transpose_op.py | 36 ++++++++++++++++++- .../fluid/tests/unittests/test_unbind_op.py | 28 ++++++++++++++- 6 files changed, 87 insertions(+), 8 deletions(-) diff --git a/paddle/fluid/operators/transpose_op.cc b/paddle/fluid/operators/transpose_op.cc index 35dcb25f6b7..7320c1314c6 100644 --- a/paddle/fluid/operators/transpose_op.cc +++ b/paddle/fluid/operators/transpose_op.cc @@ -356,7 +356,9 @@ REGISTER_OP_CPU_KERNEL( ops::TransposeKernel>, ops::TransposeKernel>); + paddle::platform::complex>, + ops::TransposeKernel); REGISTER_OP_CPU_KERNEL( transpose_grad, ops::TransposeGradKernel, @@ -365,7 +367,9 @@ REGISTER_OP_CPU_KERNEL( ops::TransposeGradKernel>, ops::TransposeGradKernel>); + paddle::platform::complex>, + ops::TransposeGradKernel); REGISTER_OPERATOR(transpose2, ops::Transpose2Op, ops::Transpose2OpMaker, ops::Transpose2GradMaker, @@ -383,7 +387,9 @@ REGISTER_OP_CPU_KERNEL( ops::TransposeKernel>, ops::TransposeKernel>); + paddle::platform::complex>, + ops::TransposeKernel); REGISTER_OP_CPU_KERNEL( transpose2_grad, ops::TransposeGradKernel, @@ -394,4 +400,6 @@ REGISTER_OP_CPU_KERNEL( ops::TransposeGradKernel>, ops::TransposeGradKernel>); + paddle::platform::complex>, + ops::TransposeGradKernel); diff --git a/paddle/fluid/operators/transpose_op.cu b/paddle/fluid/operators/transpose_op.cu index 5bcfbee5c53..02e224549a5 100644 --- a/paddle/fluid/operators/transpose_op.cu +++ b/paddle/fluid/operators/transpose_op.cu @@ -14,6 +14,7 @@ limitations under the License. */ #include "paddle/fluid/operators/transpose_op.cu.h" #include "paddle/fluid/operators/transpose_op.h" +#include "paddle/fluid/platform/bfloat16.h" #include "paddle/fluid/platform/float16.h" namespace paddle { @@ -87,6 +88,8 @@ REGISTER_OP_CUDA_KERNEL( ops::TransposeGPUKernel, ops::TransposeGPUKernel, ops::TransposeGPUKernel, + ops::TransposeGPUKernel, ops::TransposeGPUKernel>, ops::TransposeGPUKernel, ops::TransposeGradGPUKernel, + ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel>, ops::TransposeGradGPUKernel, ops::TransposeGPUKernel, ops::TransposeGPUKernel, + ops::TransposeGPUKernel, ops::TransposeGPUKernel>, ops::TransposeGPUKernel, ops::TransposeGradGPUKernel, + ops::TransposeGradGPUKernel, ops::TransposeGradGPUKernel>, ops::TransposeGradGPUKernel, ops::UnbindOpKernel, ops::UnbindOpKernel, - ops::UnbindOpKernel); + ops::UnbindOpKernel, + ops::UnbindOpKernel); diff --git a/paddle/fluid/operators/unbind_op.cu.cc b/paddle/fluid/operators/unbind_op.cu.cc index 15701b54b46..cec7058d3cf 100644 --- a/paddle/fluid/operators/unbind_op.cu.cc +++ b/paddle/fluid/operators/unbind_op.cu.cc @@ -20,4 +20,5 @@ REGISTER_OP_CUDA_KERNEL( ops::UnbindOpKernel, ops::UnbindOpKernel, ops::UnbindOpKernel, - ops::UnbindOpKernel); + ops::UnbindOpKernel, + ops::UnbindOpKernel); diff --git a/python/paddle/fluid/tests/unittests/test_transpose_op.py b/python/paddle/fluid/tests/unittests/test_transpose_op.py index 4270bc8de0b..0fc56726c5d 100644 --- a/python/paddle/fluid/tests/unittests/test_transpose_op.py +++ b/python/paddle/fluid/tests/unittests/test_transpose_op.py @@ -16,10 +16,11 @@ from __future__ import print_function import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, convert_float_to_uint16 import paddle import paddle.fluid as fluid from paddle.fluid import Program, program_guard +import paddle.fluid.core as core paddle.enable_static() @@ -113,6 +114,39 @@ class TestCase9(TestTransposeOp): self.axis = (6, 1, 3, 5, 0, 2, 4, 7) +class TestTransposeBF16Op(OpTest): + def setUp(self): + self.init_op_type() + self.initTestCase() + self.dtype = np.uint16 + x = np.random.random(self.shape).astype("float32") + + self.inputs = {'X': convert_float_to_uint16(x)} + self.attrs = { + 'axis': list(self.axis), + 'use_mkldnn': self.use_mkldnn, + } + self.outputs = { + 'XShape': convert_float_to_uint16( + np.random.random(self.shape).astype("float32")), + 'Out': self.inputs['X'].transpose(self.axis) + } + + def init_op_type(self): + self.op_type = "transpose2" + self.use_mkldnn = False + + def test_check_output(self): + self.check_output(no_check_set=['XShape']) + + def test_check_grad(self): + pass + + def initTestCase(self): + self.shape = (3, 2) + self.axis = (1, 0) + + class TestTransposeOpBool(TestTransposeOp): def test_check_grad(self): pass diff --git a/python/paddle/fluid/tests/unittests/test_unbind_op.py b/python/paddle/fluid/tests/unittests/test_unbind_op.py index 4a50fa97e6e..e16fb6ddaac 100644 --- a/python/paddle/fluid/tests/unittests/test_unbind_op.py +++ b/python/paddle/fluid/tests/unittests/test_unbind_op.py @@ -16,7 +16,7 @@ from __future__ import print_function import unittest import numpy as np -from op_test import OpTest +from op_test import OpTest, convert_float_to_uint16 import paddle.fluid as fluid import paddle.tensor as tensor from paddle.fluid import compiler, Program, program_guard, core @@ -154,6 +154,32 @@ class TestUnbindOp4(TestUnbindOp): self.out[1] = self.out[1].reshape((3, 2)) +class TestUnbindBF16Op(OpTest): + def setUp(self): + self._set_op_type() + self.dtype = self.get_dtype() + self.axis = 0 + self.num = 3 + x = np.arange(12).reshape(3, 2, 2).astype(self.dtype) + self.out = np.split(x, self.num, self.axis) + self.inputs = {'X': convert_float_to_uint16(x)} + self.attrs = {'axis': self.axis} + self.outputs = {'Out': [('out%d' % i, convert_float_to_uint16(self.out[i])) \ + for i in range(len(self.out))]} + + def get_dtype(self): + return np.uint16 + + def _set_op_type(self): + self.op_type = "unbind" + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + pass + + class TestUnbindAxisError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): -- GitLab