-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
【Hackathon No57】add_bf16_fp16 unittest for conv3d & conv3d_transpose #52195
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b8d4b35
add test+conv3d_transpose_part2
Difers 5172c6c
fix some merge error
Difers 2bd3be2
fix codestyle
Difers 6f661ce
fix typo
Difers 6d68270
fix codestyle
Difers 574ffba
fix some error
Difers 43451f8
add redef float2uint
Difers 4d11f4d
fix conv3d and conv3d_transpose
Difers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,25 @@ | |
import paddle | ||
|
||
paddle.enable_static() | ||
from eager_op_test import OpTest | ||
from eager_op_test import OpTest, copy_bits_from_float_to_uint16 | ||
|
||
from paddle.fluid import core | ||
|
||
|
||
def convert_float_to_uint16(float_list, data_format="NCHW"): | ||
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. 这里为啥要重写一个? 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. 用原先的,对NCHW dataf_format维度变换是np.transpose(float_list, [0, 3, 1, 2]),但这里data_format是NCDHW,维度变换应该是5维的[0, 4, 1, 2, 3] |
||
if data_format == "NHWC": | ||
float_list = np.transpose(float_list, [0, 4, 1, 2, 3]) | ||
|
||
new_output = [] | ||
for x in np.nditer(float_list): | ||
new_output.append(np.uint16(copy_bits_from_float_to_uint16(x))) | ||
new_output = np.reshape(new_output, float_list.shape).view(np.uint16) | ||
|
||
if data_format == "NHWC": | ||
new_output = np.transpose(new_output, [0, 2, 3, 4, 1]) | ||
return new_output | ||
|
||
|
||
def conv3dtranspose_forward_naive(input_, filter_, attrs): | ||
padding_algorithm = attrs['padding_algorithm'] | ||
if padding_algorithm not in ["SAME", "VALID", "EXPLICIT"]: | ||
|
@@ -134,6 +148,86 @@ def _get_padding_with_SAME(input_shape, kernel_size, kernel_stride): | |
return out | ||
|
||
|
||
def create_test_cudnn_fp16_class(parent, grad_check=True): | ||
@unittest.skipIf( | ||
not core.is_compiled_with_cuda(), "core is not compiled with CUDA" | ||
) | ||
class TestConv3DTransposeCUDNNFP16(parent): | ||
def init_kernel_type(self): | ||
self.use_cudnn = True | ||
self.dtype = np.float16 | ||
|
||
def test_check_output(self): | ||
if core.is_compiled_with_cuda(): | ||
place = core.CUDAPlace(0) | ||
if core.is_float16_supported(place): | ||
self.check_output_with_place(place, atol=2e-2) | ||
|
||
def test_check_grad_no_filter(self): | ||
place = core.CUDAPlace(0) | ||
if core.is_float16_supported(place) and grad_check: | ||
self.check_grad_with_place( | ||
place, ['Input'], 'Output', no_grad_set={'Filter'} | ||
) | ||
|
||
def test_check_grad_no_input(self): | ||
place = core.CUDAPlace(0) | ||
if core.is_float16_supported(place) and grad_check: | ||
self.check_grad_with_place( | ||
place, ['Filter'], 'Output', no_grad_set={'Input'} | ||
) | ||
|
||
cls_name = "{}_{}".format(parent.__name__, "CUDNNFP16OP") | ||
TestConv3DTransposeCUDNNFP16.__name__ = cls_name | ||
globals()[cls_name] = TestConv3DTransposeCUDNNFP16 | ||
|
||
|
||
def create_test_cudnn_bf16_class(parent): | ||
@unittest.skipIf( | ||
not core.is_compiled_with_cuda() | ||
or not core.is_bfloat16_supported(core.CUDAPlace(0)), | ||
"core is not compiled with CUDA and do not support bfloat16", | ||
) | ||
class TestConv3DTransposeCUDNNBF16(parent): | ||
def init_kernel_type(self): | ||
self.use_cudnn = True | ||
self.dtype = np.uint16 | ||
|
||
def test_check_output(self): | ||
place = core.CUDAPlace(0) | ||
self.check_output_with_place(place) | ||
|
||
def test_check_grad(self): | ||
place = core.CUDAPlace(0) | ||
self.check_grad_with_place( | ||
place, | ||
{'Input', 'Filter'}, | ||
'Output', | ||
) | ||
|
||
def test_check_grad_no_filter(self): | ||
place = core.CUDAPlace(0) | ||
self.check_grad_with_place( | ||
place, | ||
['Input'], | ||
'Output', | ||
no_grad_set={'Filter'}, | ||
) | ||
|
||
def test_check_grad_no_input(self): | ||
place = core.CUDAPlace(0) | ||
self.check_grad_with_place( | ||
place, | ||
['Filter'], | ||
'Output', | ||
no_grad_set={'Input'}, | ||
) | ||
|
||
cls_name = "{}_{}".format(parent.__name__, "CUDNNBF16OP") | ||
TestConv3DTransposeCUDNNBF16.__name__ = cls_name | ||
globals()[cls_name] = TestConv3DTransposeCUDNNBF16 | ||
|
||
|
||
def conv3d_transpose_wrapper( | ||
x, | ||
weight, | ||
|
@@ -172,12 +266,16 @@ def setUp(self): | |
self.pad = [0, 0, 0] | ||
self.padding_algorithm = "EXPLICIT" | ||
self.init_op_type() | ||
self.init_kernel_type() | ||
self.init_test_case() | ||
|
||
input_ = np.random.random(self.input_size).astype("float32") | ||
filter_ = np.random.random(self.filter_size).astype("float32") | ||
if self.is_bfloat16_op(): | ||
input = np.random.random(self.input_size).astype(np.float32) | ||
filter = np.random.random(self.filter_size).astype(np.float32) | ||
else: | ||
input = np.random.random(self.input_size).astype(self.dtype) | ||
filter = np.random.random(self.filter_size).astype(self.dtype) | ||
|
||
self.inputs = {'Input': input_, 'Filter': filter_} | ||
self.attrs = { | ||
'strides': self.stride, | ||
'paddings': self.pad, | ||
|
@@ -189,9 +287,21 @@ def setUp(self): | |
} | ||
|
||
output = conv3dtranspose_forward_naive( | ||
input_, filter_, self.attrs | ||
input, filter, self.attrs | ||
).astype("float32") | ||
|
||
if self.is_bfloat16_op(): | ||
self.inputs = { | ||
'Input': convert_float_to_uint16(input), | ||
'Filter': convert_float_to_uint16(filter), | ||
} | ||
else: | ||
self.inputs = { | ||
'Input': input, | ||
'Filter': filter, | ||
} | ||
output = output.astype(self.dtype) | ||
|
||
self.outputs = {'Output': output} | ||
|
||
def test_check_output(self): | ||
|
@@ -264,6 +374,9 @@ def init_op_type(self): | |
self.op_type = "conv3d_transpose" | ||
self.python_api = conv3d_transpose_wrapper | ||
|
||
def init_kernel_type(self): | ||
self.dtype = np.float32 | ||
|
||
|
||
class TestWithSymmetricPad(TestConv3DTransposeOp): | ||
def init_test_case(self): | ||
|
@@ -596,6 +709,30 @@ def init_op_type(self): | |
self.python_api = conv3d_transpose_wrapper | ||
|
||
|
||
# ----------------Conv3DTransposeCUDNN fp16---------------- | ||
create_test_cudnn_fp16_class(TestConv3DTransposeOp) | ||
create_test_cudnn_fp16_class(TestWithSymmetricPad) | ||
create_test_cudnn_fp16_class(TestWithAsymmetricPad) | ||
create_test_cudnn_fp16_class(TestWithSAMEPad) | ||
create_test_cudnn_fp16_class(TestWithVALIDPad) | ||
create_test_cudnn_fp16_class(TestWithStride) | ||
create_test_cudnn_fp16_class(TestWithGroups) | ||
create_test_cudnn_fp16_class(TestWithDilation) | ||
create_test_cudnn_fp16_class(Test_NHWC) | ||
|
||
|
||
# ----------------Conv3DTransposeCUDNN bf16---------------- | ||
create_test_cudnn_bf16_class(TestConv3DTransposeOp) | ||
create_test_cudnn_bf16_class(TestWithSymmetricPad) | ||
create_test_cudnn_bf16_class(TestWithAsymmetricPad) | ||
create_test_cudnn_bf16_class(TestWithSAMEPad) | ||
create_test_cudnn_bf16_class(TestWithVALIDPad) | ||
create_test_cudnn_bf16_class(TestWithStride) | ||
create_test_cudnn_bf16_class(TestWithGroups) | ||
create_test_cudnn_bf16_class(TestWithDilation) | ||
create_test_cudnn_bf16_class(Test_NHWC) | ||
|
||
|
||
class TestConv3dTranspose(unittest.TestCase): | ||
def error_weight_input(self): | ||
array = np.array([1], dtype=np.float32) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里跟之前的错误类似,当uint16的时候顺序不太对,应该先初始化为float32,计算完结果后将输入输出convert