From cb6168f15bf5608ab847f3b3cfd2b8983445b14a Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sat, 18 Mar 2023 22:07:19 +0800 Subject: [PATCH 1/7] add fp16 and bf16 for eye and frame --- paddle/phi/kernels/gpu/eye_kernel.cu | 4 +- paddle/phi/kernels/gpu/frame_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/frame_kernel.cu | 2 + .../fluid/tests/unittests/test_eye_op.py | 53 ++++++++++++-- .../fluid/tests/unittests/test_frame_op.py | 72 ++++++++++++++++--- 5 files changed, 114 insertions(+), 19 deletions(-) diff --git a/paddle/phi/kernels/gpu/eye_kernel.cu b/paddle/phi/kernels/gpu/eye_kernel.cu index 5cd8e5caabe98..04735aaa228a6 100644 --- a/paddle/phi/kernels/gpu/eye_kernel.cu +++ b/paddle/phi/kernels/gpu/eye_kernel.cu @@ -13,7 +13,6 @@ // limitations under the License. #include "paddle/phi/kernels/eye_kernel.h" - #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/impl/eye_kernel_impl.h" @@ -26,4 +25,5 @@ PD_REGISTER_KERNEL(eye, double, int64_t, int, - phi::dtype::float16) {} + phi::dtype::float16, + phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/gpu/frame_grad_kernel.cu b/paddle/phi/kernels/gpu/frame_grad_kernel.cu index 7deb9ff04cdb3..f7b5d441f5c93 100644 --- a/paddle/phi/kernels/gpu/frame_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/frame_grad_kernel.cu @@ -13,7 +13,6 @@ // limitations under the License. #include "paddle/phi/kernels/frame_grad_kernel.h" - #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/complex.h" #include "paddle/phi/core/kernel_registry.h" @@ -28,5 +27,6 @@ PD_REGISTER_KERNEL(frame_grad, float, double, phi::dtype::float16, + phi::dtype::bfloat16, phi::dtype::complex, phi::dtype::complex) {} diff --git a/paddle/phi/kernels/gpu/frame_kernel.cu b/paddle/phi/kernels/gpu/frame_kernel.cu index 2506cd714be09..58e60b1ed33b1 100644 --- a/paddle/phi/kernels/gpu/frame_kernel.cu +++ b/paddle/phi/kernels/gpu/frame_kernel.cu @@ -28,5 +28,7 @@ PD_REGISTER_KERNEL(frame, float, double, phi::dtype::float16, + phi::dtype::bfloat16, + phi::dtype::float16, phi::dtype::complex, phi::dtype::complex) {} diff --git a/python/paddle/fluid/tests/unittests/test_eye_op.py b/python/paddle/fluid/tests/unittests/test_eye_op.py index 43754fe6afd7a..c1c039b4b8fdc 100644 --- a/python/paddle/fluid/tests/unittests/test_eye_op.py +++ b/python/paddle/fluid/tests/unittests/test_eye_op.py @@ -16,11 +16,12 @@ import unittest import numpy as np -from eager_op_test import OpTest +from eager_op_test import OpTest, convert_float_to_uint16 from test_attribute_var import UnittestBase import paddle import paddle.fluid as fluid +import paddle.fluid.core as core import paddle.fluid.framework as framework from paddle.fluid.framework import Program, program_guard @@ -28,22 +29,33 @@ class TestEyeOp(OpTest): def setUp(self): ''' - Test eye op with specified shape + Test eye op with default shape ''' self.python_api = paddle.eye self.op_type = "eye" + self.init_dtype() + self.init_attrs() self.inputs = {} self.attrs = { - 'num_rows': 219, - 'num_columns': 319, - 'dtype': framework.convert_np_dtype_to_dtype_(np.int32), + 'num_rows': self.num_columns, + 'num_columns': self.num_columns, + 'dtype': framework.convert_np_dtype_to_dtype_(self.dtype), + } + self.outputs = { + 'Out': np.eye(self.num_rows, self.num_columns, dtype=self.dtype) } - self.outputs = {'Out': np.eye(219, 319, dtype=np.int32)} def test_check_output(self): self.check_output() + def init_dtype(self): + self.dtype = np.int32 + + def init_attrs(self): + self.num_rows = 319 + self.num_columns = 319 + class TestEyeOp1(OpTest): def setUp(self): @@ -178,6 +190,35 @@ def test_error(self): paddle.eye(-1) +class TestEyeFP16OP(TestEyeOp): + '''Test eye op with specified dtype''' + + def init_dtype(self): + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not complied with CUDA and not support the bfloat16", +) +class TestEyeBF16(OpTest): + def setUp(self): + self.op_type = "eye" + self.dtype = np.uint16 + self.python_api = paddle.eye + self.inputs = {} + self.attrs = { + 'num_rows': 219, + 'num_columns': 319, + } + self.outputs = {'Out': convert_float_to_uint16(np.eye(219, 319))} + + def test_check_output(self): + place = core.CUDAPlace(0) + self.check_output_with_place(place) + + if __name__ == "__main__": paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_frame_op.py b/python/paddle/fluid/tests/unittests/test_frame_op.py index ba37ad4556261..22c9a6cefe398 100644 --- a/python/paddle/fluid/tests/unittests/test_frame_op.py +++ b/python/paddle/fluid/tests/unittests/test_frame_op.py @@ -15,10 +15,11 @@ import unittest import numpy as np -from eager_op_test import OpTest +from eager_op_test import OpTest, convert_float_to_uint16 from numpy.lib.stride_tricks import as_strided import paddle +import paddle.fluid.core as core def frame_from_librosa(x, frame_length, hop_length, axis=-1): @@ -48,23 +49,28 @@ class TestFrameOp(OpTest): def setUp(self): self.op_type = "frame" self.python_api = paddle.signal.frame - self.shape, self.type, self.attrs = self.initTestCase() - self.inputs = { - 'X': np.random.random(size=self.shape).astype(self.type), - } + + self.init_dtype() + self.init_shape() + self.init_attrs() + + self.inputs = {'X': np.random.random(size=self.shape)} self.outputs = { 'Out': frame_from_librosa(x=self.inputs['X'], **self.attrs) } - def initTestCase(self): - input_shape = (150,) - input_type = 'float64' - attrs = { + def init_dtype(self): + self.dtype = 'float64' + + def init_shape(self): + self.shape = (150,) + + def init_attrs(self): + self.attrs = { 'frame_length': 50, 'hop_length': 15, 'axis': -1, } - return input_shape, input_type, attrs def test_check_output(self): paddle.enable_static() @@ -137,5 +143,51 @@ def initTestCase(self): return input_shape, input_type, attrs +class TestFrameFP16OP(TestFrameOp): + def init_dtype(self): + self.dtype = np.float16 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or not core.is_bfloat16_supported(core.CUDAPlace(0)), + "core is not complied with CUDA and not support the bfloat16", +) +class TestFrameBF16(OpTest): + def setUp(self): + self.op_type = "frame" + self.python_api = paddle.signal.frame + self.__class__.op_type = self.op_type + self.shape, self.dtype, self.attrs = self.initTestCase() + x = np.random.random(size=self.shape).astype(np.float32) + out = frame_from_librosa(x, **self.attrs) + self.inputs = { + 'X': convert_float_to_uint16(x), + } + self.outputs = {'Out': convert_float_to_uint16(out)} + + def initTestCase(self): + input_shape = (150,) + input_dtype = np.uint16 + attrs = { + 'frame_length': 50, + 'hop_length': 15, + 'axis': -1, + } + return input_shape, input_dtype, attrs + + def test_check_output(self): + paddle.enable_static() + place = core.CUDAPlace(0) + self.check_output_with_place(place) + paddle.disable_static() + + def test_check_grad_normal(self): + paddle.enable_static() + place = core.CUDAPlace(0) + self.check_grad_with_place(place, ['X'], 'Out') + paddle.disable_static() + + if __name__ == '__main__': unittest.main() From 7b62afed86f35bcfde51cc42719b8bd701e70999 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sun, 19 Mar 2023 14:58:34 +0800 Subject: [PATCH 2/7] fix bug --- paddle/phi/kernels/gpu/frame_kernel.cu | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/phi/kernels/gpu/frame_kernel.cu b/paddle/phi/kernels/gpu/frame_kernel.cu index 58e60b1ed33b1..153e450576459 100644 --- a/paddle/phi/kernels/gpu/frame_kernel.cu +++ b/paddle/phi/kernels/gpu/frame_kernel.cu @@ -29,6 +29,5 @@ PD_REGISTER_KERNEL(frame, double, phi::dtype::float16, phi::dtype::bfloat16, - phi::dtype::float16, phi::dtype::complex, phi::dtype::complex) {} From a142e90b404e2b6ee5ab4b0006575f7684d18033 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Tue, 21 Mar 2023 14:44:26 +0800 Subject: [PATCH 3/7] fix bug --- python/paddle/fluid/tests/unittests/test_eye_op.py | 2 +- python/paddle/fluid/tests/unittests/test_frame_op.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_eye_op.py b/python/paddle/fluid/tests/unittests/test_eye_op.py index c1c039b4b8fdc..d37d2c62403ad 100644 --- a/python/paddle/fluid/tests/unittests/test_eye_op.py +++ b/python/paddle/fluid/tests/unittests/test_eye_op.py @@ -202,7 +202,7 @@ def init_dtype(self): or not core.is_bfloat16_supported(core.CUDAPlace(0)), "core is not complied with CUDA and not support the bfloat16", ) -class TestEyeBF16(OpTest): +class TestEyeBF16OP(OpTest): def setUp(self): self.op_type = "eye" self.dtype = np.uint16 diff --git a/python/paddle/fluid/tests/unittests/test_frame_op.py b/python/paddle/fluid/tests/unittests/test_frame_op.py index 22c9a6cefe398..2154c5fa49d17 100644 --- a/python/paddle/fluid/tests/unittests/test_frame_op.py +++ b/python/paddle/fluid/tests/unittests/test_frame_op.py @@ -153,11 +153,10 @@ def init_dtype(self): or not core.is_bfloat16_supported(core.CUDAPlace(0)), "core is not complied with CUDA and not support the bfloat16", ) -class TestFrameBF16(OpTest): +class TestFrameBF16OP(OpTest): def setUp(self): self.op_type = "frame" self.python_api = paddle.signal.frame - self.__class__.op_type = self.op_type self.shape, self.dtype, self.attrs = self.initTestCase() x = np.random.random(size=self.shape).astype(np.float32) out = frame_from_librosa(x, **self.attrs) From 56818c022c102190edb67511a8500979eb081713 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sun, 26 Mar 2023 23:00:07 +0800 Subject: [PATCH 4/7] fix bug --- python/paddle/fluid/tests/unittests/test_eye_op.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_eye_op.py b/python/paddle/fluid/tests/unittests/test_eye_op.py index 166ec020b54eb..c542f00007dbf 100644 --- a/python/paddle/fluid/tests/unittests/test_eye_op.py +++ b/python/paddle/fluid/tests/unittests/test_eye_op.py @@ -20,9 +20,8 @@ from test_attribute_var import UnittestBase import paddle - from paddle import fluid -from paddle.fluid import framework +from paddle.fluid import core, framework from paddle.fluid.framework import Program, program_guard From 14ca4e29d556d8ae21a3637f778a33dbb68cbda2 Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Mon, 27 Mar 2023 14:35:52 +0800 Subject: [PATCH 5/7] Update test_frame_op.py fix code style --- python/paddle/fluid/tests/unittests/test_frame_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_frame_op.py b/python/paddle/fluid/tests/unittests/test_frame_op.py index 2154c5fa49d17..163170680054b 100644 --- a/python/paddle/fluid/tests/unittests/test_frame_op.py +++ b/python/paddle/fluid/tests/unittests/test_frame_op.py @@ -19,7 +19,7 @@ from numpy.lib.stride_tricks import as_strided import paddle -import paddle.fluid.core as core +from paddle.fluid import core def frame_from_librosa(x, frame_length, hop_length, axis=-1): From 1d61d3cc0fa900f39d4b6d584a493a75cbaa0fc2 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Tue, 28 Mar 2023 21:43:55 +0800 Subject: [PATCH 6/7] fix bug --- python/paddle/fluid/tests/unittests/test_eye_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_eye_op.py b/python/paddle/fluid/tests/unittests/test_eye_op.py index c542f00007dbf..b7ef848b42fad 100644 --- a/python/paddle/fluid/tests/unittests/test_eye_op.py +++ b/python/paddle/fluid/tests/unittests/test_eye_op.py @@ -16,7 +16,7 @@ import unittest import numpy as np -from eager_op_test import OpTest, convert_float_to_uint16 +from eager_op_test import OpTest from test_attribute_var import UnittestBase import paddle @@ -211,7 +211,7 @@ def setUp(self): 'num_rows': 219, 'num_columns': 319, } - self.outputs = {'Out': convert_float_to_uint16(np.eye(219, 319))} + self.outputs = {'Out': np.eye(219, 319)} def test_check_output(self): place = core.CUDAPlace(0) From e362416f2a9949d06c2134f58f5867a30259fbc1 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Tue, 4 Apr 2023 11:43:37 +0800 Subject: [PATCH 7/7] fix bug --- python/paddle/fluid/tests/unittests/test_frame_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_frame_op.py b/python/paddle/fluid/tests/unittests/test_frame_op.py index 163170680054b..e2a5b6fb8e92e 100644 --- a/python/paddle/fluid/tests/unittests/test_frame_op.py +++ b/python/paddle/fluid/tests/unittests/test_frame_op.py @@ -159,7 +159,7 @@ def setUp(self): self.python_api = paddle.signal.frame self.shape, self.dtype, self.attrs = self.initTestCase() x = np.random.random(size=self.shape).astype(np.float32) - out = frame_from_librosa(x, **self.attrs) + out = frame_from_librosa(x, **self.attrs).copy() self.inputs = { 'X': convert_float_to_uint16(x), }