test_reduce_bf16_mkldnn_op.py 8.1 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
T
tianshuo78520a 已提交
18
from eager_op_test import (
19
    OpTest,
20
    OpTestTool,
21
    convert_float_to_uint16,
22
    skip_check_grad_ci,
23
)
24

T
tianshuo78520a 已提交
25 26 27
import paddle
from paddle.fluid import core

A
arlesniak 已提交
28
paddle.enable_static()
29 30


A
arlesniak 已提交
31
@OpTestTool.skip_if_not_cpu_bf16()
32
class TestReduceSumDefaultBF16OneDNNOp(OpTest):
33 34 35
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
36 37 38 39
        self.x_fp32 = np.random.random((5, 6, 10)).astype("float32")
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
        self.outputs = {'Out': self.x_fp32.sum(axis=0)}
40 41 42
        self.attrs = {'use_mkldnn': self.use_mkldnn}

    def test_check_output(self):
43
        self.check_output(check_dygraph=False, check_new_ir=False)
44

45 46
    def calculate_grads(self):
        tmp_tensor = np.zeros(self.x_fp32.shape).astype("float32")
47

48 49
        prod_of_reduced_dims = self.inputs['X'].shape[0]
        axis = 0
50

51 52 53 54 55 56 57 58
        if "dim" in self.attrs:
            prod_of_reduced_dims = 1
            axis = tuple(self.attrs['dim'])
            for i in range(len(axis)):
                ax = axis[i]
                if axis[i] < 0:
                    ax = len(axis) + axis[i]
                prod_of_reduced_dims *= self.inputs['X'].shape[ax]
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        if 'reduce_all' in self.attrs:
            if self.attrs['reduce_all'] is True:
                axis = None
                prod_of_reduced_dims = np.asarray(self.inputs['X'].shape).prod()

        keepdim = False
        if 'keep_dim' in self.attrs:
            keepdim = True

        self.grad_Out = self.x_fp32.sum(axis=axis, keepdims=keepdim)
        self.grad_Out = np.atleast_1d(self.grad_Out)
        self.grad_X = tmp_tensor + self.grad_Out  # broadcast grad

        if self.op_type == 'reduce_mean':
            self.grad_X /= prod_of_reduced_dims


class TestReduceDefaultWithGradBF16OneDNNOp(TestReduceSumDefaultBF16OneDNNOp):
    def test_check_grad(self):
        self.calculate_grads()
        self.check_grad_with_place(
81 82
            core.CPUPlace(),
            ["X"],
83 84 85
            "Out",
            check_dygraph=False,
            user_defined_grads=[self.grad_X],
86
            user_defined_grad_outputs=[convert_float_to_uint16(self.grad_Out)],
87
            check_new_ir=False,
88
        )
89 90


91
class TestReduceSum4DReduceAllDimAttributeBF16OneDNNOp(
92 93
    TestReduceDefaultWithGradBF16OneDNNOp
):
94 95 96
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
97 98 99 100 101
        self.x_fp32 = np.random.normal(size=(2, 3, 5, 6)).astype('float32')
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
        self.attrs = {'use_mkldnn': self.use_mkldnn, 'dim': [0, 1, 2, 3]}
        self.outputs = {'Out': self.x_fp32.sum(axis=tuple(self.attrs['dim']))}
102 103


104
class TestReduceSum4DReduceAllWithoutReduceAllAttributeNegativeDimsBF16OneDNNOp(
105 106
    TestReduceDefaultWithGradBF16OneDNNOp
):
107 108 109
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
110 111 112 113 114
        self.x_fp32 = np.random.normal(size=(4, 7, 6, 6)).astype('float32')
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
        self.attrs = {'use_mkldnn': self.use_mkldnn, 'dim': [-1, -2, -3, -4]}
        self.outputs = {'Out': self.x_fp32.sum(axis=tuple(self.attrs['dim']))}
115 116


117
class TestReduceSum5DReduceAllKeepDimsBF16OneDNNOp(
118 119
    TestReduceDefaultWithGradBF16OneDNNOp
):
120 121 122
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
123 124 125
        self.x_fp32 = np.random.normal(size=(2, 5, 3, 2, 5)).astype('float32')
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
126
        self.attrs = {'reduce_all': True, 'keep_dim': True, 'use_mkldnn': True}
127
        self.outputs = {'Out': self.x_fp32.sum(keepdims=self.attrs['keep_dim'])}
128 129


130 131 132
class TestReduceSum4DReduceAllBF16OneDNNOp(
    TestReduceDefaultWithGradBF16OneDNNOp
):
133 134 135
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
136 137 138
        self.x_fp32 = np.random.normal(size=(4, 5, 4, 5)).astype('float32')
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
139
        self.attrs = {'reduce_all': True, 'use_mkldnn': self.use_mkldnn}
140
        self.outputs = {'Out': self.x_fp32.sum()}
141 142 143 144


@skip_check_grad_ci(
    reason="reduce_max is discontinuous non-derivable function,"
145 146
    " its gradient check is not supported by unittest framework."
)
147
class TestReduceMax3DBF16OneDNNOp(TestReduceSumDefaultBF16OneDNNOp):
148 149 150 151 152
    """Remove Max with subgradient from gradient check to confirm the success of CI."""

    def setUp(self):
        self.op_type = "reduce_max"
        self.use_mkldnn = True
153 154 155
        self.x_fp32 = np.random.random((5, 6, 10)).astype("float32")
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
156
        self.attrs = {'dim': [-1], 'use_mkldnn': self.use_mkldnn}
157
        self.outputs = {'Out': self.x_fp32.max(axis=tuple(self.attrs['dim']))}
158 159 160 161


@skip_check_grad_ci(
    reason="reduce_max is discontinuous non-derivable function,"
162 163
    " its gradient check is not supported by unittest framework."
)
164
class TestReduceMax4DNegativeAndPositiveDimsBF16OneDNNOp(
165 166
    TestReduceSumDefaultBF16OneDNNOp
):
167 168 169 170 171
    """Remove Max with subgradient from gradient check to confirm the success of CI."""

    def setUp(self):
        self.op_type = "reduce_max"
        self.use_mkldnn = True
172 173 174
        self.x_fp32 = np.random.random((5, 6, 10, 9)).astype("float32")
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
175
        self.attrs = {'dim': [-1, 0, 1], 'use_mkldnn': self.use_mkldnn}
176
        self.outputs = {'Out': self.x_fp32.max(axis=tuple(self.attrs['dim']))}
177 178 179 180


@skip_check_grad_ci(
    reason="reduce_min is discontinuous non-derivable function,"
181 182
    " its gradient check is not supported by unittest framework."
)
183
class TestReduceMin3DBF16OneDNNOp(TestReduceSumDefaultBF16OneDNNOp):
184 185 186 187 188
    """Remove Min with subgradient from gradient check to confirm the success of CI."""

    def setUp(self):
        self.op_type = "reduce_min"
        self.use_mkldnn = True
189 190 191
        self.x_fp32 = np.random.random((5, 6, 10)).astype("float32")
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
192
        self.attrs = {'dim': [2], 'use_mkldnn': self.use_mkldnn}
193
        self.outputs = {'Out': self.x_fp32.min(axis=tuple(self.attrs['dim']))}
194 195


196
class TestReduceMean3DBF16OneDNNOp(TestReduceDefaultWithGradBF16OneDNNOp):
197 198 199
    def setUp(self):
        self.op_type = "reduce_mean"
        self.use_mkldnn = True
200 201 202
        self.x_fp32 = np.random.random((5, 6, 10)).astype("float32")
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
203
        self.attrs = {'use_mkldnn': self.use_mkldnn}
204 205 206 207 208 209 210 211 212 213 214 215
        self.outputs = {'Out': self.x_fp32.sum(axis=0) / self.x_fp32.shape[0]}


class TestReduceMean4DBF16OneDNNOp(TestReduceDefaultWithGradBF16OneDNNOp):
    def setUp(self):
        self.op_type = "reduce_mean"
        self.use_mkldnn = True
        self.x_fp32 = np.random.random((5, 6, 3, 5)).astype("float32")
        self.x_bf16 = convert_float_to_uint16(self.x_fp32)
        self.inputs = {'X': self.x_bf16}
        self.attrs = {'use_mkldnn': self.use_mkldnn, 'dim': [0, 1]}
        self.outputs = {
216 217
            'Out': self.x_fp32.sum(axis=tuple(self.attrs['dim']))
            / (self.x_fp32.shape[0] * self.x_fp32.shape[1])
218
        }
219 220 221 222 223


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