未验证 提交 ae10133a 编写于 作者: L LoneRanger 提交者: GitHub

【PaddlePaddle Hackathon 4】No.63 add fp16 and bf16 for eye and frame (#51819)

* add fp16 and bf16 for eye and frame

* fix bug

* fix bug

* fix bug

* Update test_frame_op.py

fix code style

* fix bug

* fix bug
上级 2ad66a42
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
// limitations under the License. // limitations under the License.
#include "paddle/phi/kernels/eye_kernel.h" #include "paddle/phi/kernels/eye_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/eye_kernel_impl.h" #include "paddle/phi/kernels/impl/eye_kernel_impl.h"
...@@ -26,4 +25,5 @@ PD_REGISTER_KERNEL(eye, ...@@ -26,4 +25,5 @@ PD_REGISTER_KERNEL(eye,
double, double,
int64_t, int64_t,
int, int,
phi::dtype::float16) {} phi::dtype::float16,
phi::dtype::bfloat16) {}
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
// limitations under the License. // limitations under the License.
#include "paddle/phi/kernels/frame_grad_kernel.h" #include "paddle/phi/kernels/frame_grad_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/common/complex.h" #include "paddle/phi/common/complex.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
...@@ -28,5 +27,6 @@ PD_REGISTER_KERNEL(frame_grad, ...@@ -28,5 +27,6 @@ PD_REGISTER_KERNEL(frame_grad,
float, float,
double, double,
phi::dtype::float16, phi::dtype::float16,
phi::dtype::bfloat16,
phi::dtype::complex<float>, phi::dtype::complex<float>,
phi::dtype::complex<double>) {} phi::dtype::complex<double>) {}
...@@ -28,5 +28,6 @@ PD_REGISTER_KERNEL(frame, ...@@ -28,5 +28,6 @@ PD_REGISTER_KERNEL(frame,
float, float,
double, double,
phi::dtype::float16, phi::dtype::float16,
phi::dtype::bfloat16,
phi::dtype::complex<float>, phi::dtype::complex<float>,
phi::dtype::complex<double>) {} phi::dtype::complex<double>) {}
...@@ -21,29 +21,40 @@ from test_attribute_var import UnittestBase ...@@ -21,29 +21,40 @@ from test_attribute_var import UnittestBase
import paddle import paddle
from paddle import fluid from paddle import fluid
from paddle.fluid import framework from paddle.fluid import core, framework
from paddle.fluid.framework import Program, program_guard from paddle.fluid.framework import Program, program_guard
class TestEyeOp(OpTest): class TestEyeOp(OpTest):
def setUp(self): def setUp(self):
''' '''
Test eye op with specified shape Test eye op with default shape
''' '''
self.python_api = paddle.eye self.python_api = paddle.eye
self.op_type = "eye" self.op_type = "eye"
self.init_dtype()
self.init_attrs()
self.inputs = {} self.inputs = {}
self.attrs = { self.attrs = {
'num_rows': 219, 'num_rows': self.num_columns,
'num_columns': 319, 'num_columns': self.num_columns,
'dtype': framework.convert_np_dtype_to_dtype_(np.int32), '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): def test_check_output(self):
self.check_output() 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): class TestEyeOp1(OpTest):
def setUp(self): def setUp(self):
...@@ -178,6 +189,35 @@ class TestEyeRowsCol(UnittestBase): ...@@ -178,6 +189,35 @@ class TestEyeRowsCol(UnittestBase):
paddle.eye(-1) 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 TestEyeBF16OP(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': np.eye(219, 319)}
def test_check_output(self):
place = core.CUDAPlace(0)
self.check_output_with_place(place)
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static() paddle.enable_static()
unittest.main() unittest.main()
...@@ -15,10 +15,11 @@ ...@@ -15,10 +15,11 @@
import unittest import unittest
import numpy as np 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 from numpy.lib.stride_tricks import as_strided
import paddle import paddle
from paddle.fluid import core
def frame_from_librosa(x, frame_length, hop_length, axis=-1): def frame_from_librosa(x, frame_length, hop_length, axis=-1):
...@@ -48,23 +49,28 @@ class TestFrameOp(OpTest): ...@@ -48,23 +49,28 @@ class TestFrameOp(OpTest):
def setUp(self): def setUp(self):
self.op_type = "frame" self.op_type = "frame"
self.python_api = paddle.signal.frame self.python_api = paddle.signal.frame
self.shape, self.type, self.attrs = self.initTestCase()
self.inputs = { self.init_dtype()
'X': np.random.random(size=self.shape).astype(self.type), self.init_shape()
} self.init_attrs()
self.inputs = {'X': np.random.random(size=self.shape)}
self.outputs = { self.outputs = {
'Out': frame_from_librosa(x=self.inputs['X'], **self.attrs) 'Out': frame_from_librosa(x=self.inputs['X'], **self.attrs)
} }
def initTestCase(self): def init_dtype(self):
input_shape = (150,) self.dtype = 'float64'
input_type = 'float64'
attrs = { def init_shape(self):
self.shape = (150,)
def init_attrs(self):
self.attrs = {
'frame_length': 50, 'frame_length': 50,
'hop_length': 15, 'hop_length': 15,
'axis': -1, 'axis': -1,
} }
return input_shape, input_type, attrs
def test_check_output(self): def test_check_output(self):
paddle.enable_static() paddle.enable_static()
...@@ -137,5 +143,50 @@ class TestCase5(TestFrameOp): ...@@ -137,5 +143,50 @@ class TestCase5(TestFrameOp):
return input_shape, input_type, attrs 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 TestFrameBF16OP(OpTest):
def setUp(self):
self.op_type = "frame"
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).copy()
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__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册