-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【PaddlePaddle Hackathon 4】No.63 add fp16 and bf16 for eye and frame #51819
Changes from 2 commits
cb6168f
7b62afe
ccf743f
a142e90
fa5f56c
56818c0
14ca4e2
f06c074
2bfd867
1d61d3c
e362416
e48f422
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,34 +16,46 @@ | |
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 | ||
|
||
|
||
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))} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个我看报错,应该也没必要convert了 |
||
|
||
def test_check_output(self): | ||
place = core.CUDAPlace(0) | ||
self.check_output_with_place(place) | ||
|
||
|
||
if __name__ == "__main__": | ||
paddle.enable_static() | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修改类名TestFrameBF16为TestFrameBF16OP |
||
def setUp(self): | ||
self.op_type = "frame" | ||
self.python_api = paddle.signal.frame | ||
self.__class__.op_type = self.op_type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个应该不用在这里设置,OpTest框架里有对应的设置 |
||
self.shape, self.dtype, self.attrs = self.initTestCase() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个好像没设置self.dtype=uint16? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ZzSean 有的,initTestCase()会返回 np.uint16 |
||
x = np.random.random(size=self.shape).astype(np.float32) | ||
out = frame_from_librosa(x, **self.attrs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 与diagonal问题类似,这里也需要加个.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__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修改类名TestEyeBF16为TestEyeBF16OP