Skip to content
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

reshape onednn test reimplemented #48850

Merged
merged 11 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,54 +19,30 @@
import paddle
import paddle.fluid.core as core
from paddle.fluid.tests.unittests.op_test import (
OpTest,
OpTestTool,
convert_float_to_uint16,
)
from paddle.fluid.tests.unittests.test_reshape_op import TestReshapeOp

paddle.enable_static()

@OpTestTool.skip_if(
core.is_compiled_with_cuda(),
"CUDA has to be skipped because it forces dygraph",
)
class TestReshape2OneDNNOp(OpTest):

class TestReshape2OneDNNOp(TestReshapeOp):
def setUp(self):
self.init_data()
self.set_op_type()
self.x = np.random.random(self.ori_shape).astype("float32")
self.set_inputs()
super().setUp()
self.x = self.inputs["X"]
self.attrs['use_mkldnn'] = True
self.set_additional_inputs()
self.set_attrs()
self.set_outputs()

def set_op_type(self):
self.op_type = "reshape2"

def set_inputs(self):
self.inputs = {"X": self.x}
def init_dtype(self):
self.dtype = np.float32

def set_additional_inputs(self):
pass

def set_attrs(self):
self.attrs = {"shape": self.new_shape, 'use_mkldnn': True}

def set_outputs(self):
self.outputs = {
"Out": self.inputs["X"].reshape(self.infered_shape),
'XShape': np.random.random(self.ori_shape).astype("float32"),
}

def init_data(self):
self.ori_shape = (2, 60)
self.new_shape = (12, 10)
self.infered_shape = (12, 10)

def test_check_output(self):
self.check_output(no_check_set=['XShape'])

def test_check_grad(self):
self.check_grad(["X"], "Out")
pass


class TestReshape2OneDNNOpDimInfer1(TestReshape2OneDNNOp):
Expand All @@ -80,14 +56,14 @@ class TestReshape2OneDNNOpDimInfer2(TestReshape2OneDNNOp):
def init_data(self):
self.ori_shape = (6, 20)
self.new_shape = (0, -1, 20)
self.actual_shape = (2, 3, 20)
self.infered_shape = (2, 3, 20)

def set_additional_inputs(self):
self.inputs["Shape"] = np.array(self.actual_shape, dtype="int32")
self.inputs["Shape"] = np.array(self.infered_shape, dtype="int32")

def set_outputs(self):
self.outputs = {
"Out": self.inputs["X"].reshape(self.actual_shape),
"Out": self.inputs["X"].reshape(self.infered_shape),
'XShape': np.random.random(self.ori_shape).astype("float32"),
}

Expand All @@ -96,9 +72,6 @@ class TestReshape2OneDNNOp_attr_OnlyShape(TestReshape2OneDNNOp):
def set_additional_inputs(self):
self.inputs["Shape"] = np.array(self.new_shape, dtype="int32")

def set_attrs(self):
self.attrs = {'use_mkldnn': True}

def set_outputs(self):
self.outputs = {
"Out": self.inputs["X"].reshape(self.infered_shape),
Expand Down Expand Up @@ -153,7 +126,8 @@ def set_additional_inputs(self):


class TestReshapeOneDNNOp(TestReshape2OneDNNOp):
def set_op_type(self):
def setUp(self):
super().setUp()
self.op_type = "reshape"

def set_outputs(self):
Expand All @@ -171,7 +145,8 @@ def init_data(self):


class TestReshapeOneDNNOp_attr_OnlyShape(TestReshape2OneDNNOp_attr_OnlyShape):
def set_op_type(self):
def setUp(self):
super().setUp()
self.op_type = "reshape"

def set_outputs(self):
Expand All @@ -195,9 +170,11 @@ def init_data(self):
def create_reshape_bf16_test_classes(parent):
@OpTestTool.skip_if_not_cpu_bf16()
class TestReshape2BF16OneDNNOp(parent):
def set_inputs(self):
def setUp(self):
super().setUp()
self.dtype = np.uint16
self.inputs = {"X": convert_float_to_uint16(self.x)}
self.attrs['use_mkldnn'] = True

def calculate_grads(self):
self.dout = self.outputs['Out']
Expand All @@ -223,9 +200,9 @@ def test_check_grad(self):
globals()[cls_name] = TestReshape2BF16OneDNNOp

class TestReshapeBF16OneDNNOp(TestReshape2BF16OneDNNOp):
def set_op_type(self):
def setUp(self):
super().setUp()
self.dtype = np.uint16
self.op_type = "reshape"

def set_outputs(self):
self.outputs = {"Out": self.x.reshape(self.new_shape)}
Expand All @@ -252,5 +229,4 @@ def test_check_grad(self):
create_reshape_bf16_test_classes(TestReshape2OneDNNOpDimInfer1)

if __name__ == "__main__":
paddle.enable_static()
unittest.main()
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/test_reshape_op.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down