test_flatten_mkldnn_op.py 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2021 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.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
16

17
import numpy as np
18

19
import paddle
20
from paddle.fluid import core
21
from paddle.fluid.tests.unittests.eager_op_test import (
22 23 24 25
    OpTest,
    OpTestTool,
    convert_float_to_uint16,
)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98


@OpTestTool.skip_if_not_cpu_bf16()
class TestFlattenOneDNNOp(OpTest):
    def setUp(self):
        self.set_op_type()
        self.init_test_case()
        self.set_inputs()
        self.attrs = {"axis": self.axis, 'use_mkldnn': True}
        self.ori_shape = self.inputs['X'].shape
        self.outputs = {"Out": self.inputs["X"].copy().reshape(self.new_shape)}

    def set_inputs(self):
        self.inputs = {"X": np.random.random(self.in_shape).astype("float32")}

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

    def test_check_output(self):
        self.check_output_with_place(core.CPUPlace())

    def test_check_grad(self):
        self.check_grad_with_place(core.CPUPlace(), ["X"], "Out")

    def init_test_case(self):
        self.in_shape = (3, 2, 2, 10)
        self.axis = 1
        self.new_shape = (3, 40)


class TestFlattenOneDNNOp1(TestFlattenOneDNNOp):
    def init_test_case(self):
        self.in_shape = (3, 2, 2, 10)
        self.axis = 0
        self.new_shape = (1, 120)


class TestFlattenOneDNNOpSixDims(TestFlattenOneDNNOp):
    def init_test_case(self):
        self.in_shape = (3, 2, 3, 2, 4, 4)
        self.axis = 4
        self.new_shape = (36, 16)


class TestFlatten2OneDNNOp(TestFlattenOneDNNOp):
    def set_op_type(self):
        self.op_type = "flatten2"


class TestFlatten2OneDNNOp1(TestFlattenOneDNNOp1):
    def set_op_type(self):
        self.op_type = "flatten2"


class TestFlatten2OneDNNOpSixDims(TestFlattenOneDNNOpSixDims):
    def set_op_type(self):
        self.op_type = "flatten2"


#   BF16 TESTS
def create_flatten_bf16_test_classes(parent):
    class TestFlatten2BF16OneDNNOp(parent):
        def set_inputs(self):
            self.dtype = np.uint16
            self.inputs = {
                "X": np.random.random(self.in_shape).astype("uint16")
            }

        def calculate_grads(self):
            self.dout = self.outputs['Out']
            self.dx = np.reshape(self.dout, self.ori_shape)

        def test_check_output(self):
99 100 101
            self.check_output_with_place(
                core.CPUPlace(), no_check_set=["XShape"]
            )
102 103 104

        def test_check_grad(self):
            self.calculate_grads()
105 106 107 108 109 110 111
            self.check_grad_with_place(
                core.CPUPlace(),
                ["X"],
                "Out",
                user_defined_grads=[self.dx],
                user_defined_grad_outputs=[self.dout],
            )
112

113
    cls_name = "{}_{}".format(parent.__name__, "Flatten2_BF16")
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    TestFlatten2BF16OneDNNOp.__name__ = cls_name
    globals()[cls_name] = TestFlatten2BF16OneDNNOp

    class TestFlattenBF16OneDNNOp(parent):
        def set_op_type(self):
            self.dtype = np.uint16
            self.op_type = "flatten"

        def set_inputs(self):
            self.dtype = np.uint16
            self.inputs = {
                "X": np.random.random(self.in_shape).astype("uint16")
            }

        def set_outputs(self):
            self.outputs = {"Out": self.x.reshape(self.new_shape)}

        def calculate_grads(self):
            self.dout = self.outputs['Out']
            self.dx = np.reshape(self.dout, self.ori_shape)

        def test_check_output(self):
            self.check_output_with_place(core.CPUPlace())

        def test_check_grad(self):
            self.calculate_grads()
            self.check_grad_with_place(
141 142
                core.CPUPlace(),
                ["X"],
143 144
                "Out",
                user_defined_grads=[self.dx],
145 146
                user_defined_grad_outputs=[convert_float_to_uint16(self.dout)],
            )
147

148
    cls_name = "{}_{}".format(parent.__name__, "Flatten_BF16")
149 150 151 152 153 154 155 156 157 158 159
    TestFlattenBF16OneDNNOp.__name__ = cls_name
    globals()[cls_name] = TestFlattenBF16OneDNNOp


create_flatten_bf16_test_classes(TestFlatten2OneDNNOp)
create_flatten_bf16_test_classes(TestFlatten2OneDNNOp1)
create_flatten_bf16_test_classes(TestFlatten2OneDNNOpSixDims)

if __name__ == "__main__":
    paddle.enable_static()
    unittest.main()