test_reduce_bf16_mkldnn_op.py 8.0 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 43 44
        self.attrs = {'use_mkldnn': self.use_mkldnn}

    def test_check_output(self):
        self.check_output(check_dygraph=False)

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 87
            user_defined_grad_outputs=[convert_float_to_uint16(self.grad_Out)],
        )
88 89


90
class TestReduceSum4DReduceAllDimAttributeBF16OneDNNOp(
91 92
    TestReduceDefaultWithGradBF16OneDNNOp
):
93 94 95
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
96 97 98 99 100
        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']))}
101 102


103
class TestReduceSum4DReduceAllWithoutReduceAllAttributeNegativeDimsBF16OneDNNOp(
104 105
    TestReduceDefaultWithGradBF16OneDNNOp
):
106 107 108
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
109 110 111 112 113
        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']))}
114 115


116
class TestReduceSum5DReduceAllKeepDimsBF16OneDNNOp(
117 118
    TestReduceDefaultWithGradBF16OneDNNOp
):
119 120 121
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
122 123 124
        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}
125
        self.attrs = {'reduce_all': True, 'keep_dim': True, 'use_mkldnn': True}
126
        self.outputs = {'Out': self.x_fp32.sum(keepdims=self.attrs['keep_dim'])}
127 128


129 130 131
class TestReduceSum4DReduceAllBF16OneDNNOp(
    TestReduceDefaultWithGradBF16OneDNNOp
):
132 133 134
    def setUp(self):
        self.op_type = "reduce_sum"
        self.use_mkldnn = True
135 136 137
        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}
138
        self.attrs = {'reduce_all': True, 'use_mkldnn': self.use_mkldnn}
139
        self.outputs = {'Out': self.x_fp32.sum()}
140 141 142 143


@skip_check_grad_ci(
    reason="reduce_max is discontinuous non-derivable function,"
144 145
    " its gradient check is not supported by unittest framework."
)
146
class TestReduceMax3DBF16OneDNNOp(TestReduceSumDefaultBF16OneDNNOp):
147 148 149 150 151
    """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
152 153 154
        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}
155
        self.attrs = {'dim': [-1], 'use_mkldnn': self.use_mkldnn}
156
        self.outputs = {'Out': self.x_fp32.max(axis=tuple(self.attrs['dim']))}
157 158 159 160


@skip_check_grad_ci(
    reason="reduce_max is discontinuous non-derivable function,"
161 162
    " its gradient check is not supported by unittest framework."
)
163
class TestReduceMax4DNegativeAndPositiveDimsBF16OneDNNOp(
164 165
    TestReduceSumDefaultBF16OneDNNOp
):
166 167 168 169 170
    """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
171 172 173
        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}
174
        self.attrs = {'dim': [-1, 0, 1], 'use_mkldnn': self.use_mkldnn}
175
        self.outputs = {'Out': self.x_fp32.max(axis=tuple(self.attrs['dim']))}
176 177 178 179


@skip_check_grad_ci(
    reason="reduce_min is discontinuous non-derivable function,"
180 181
    " its gradient check is not supported by unittest framework."
)
182
class TestReduceMin3DBF16OneDNNOp(TestReduceSumDefaultBF16OneDNNOp):
183 184 185 186 187
    """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
188 189 190
        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}
191
        self.attrs = {'dim': [2], 'use_mkldnn': self.use_mkldnn}
192
        self.outputs = {'Out': self.x_fp32.min(axis=tuple(self.attrs['dim']))}
193 194


195
class TestReduceMean3DBF16OneDNNOp(TestReduceDefaultWithGradBF16OneDNNOp):
196 197 198
    def setUp(self):
        self.op_type = "reduce_mean"
        self.use_mkldnn = True
199 200 201
        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}
202
        self.attrs = {'use_mkldnn': self.use_mkldnn}
203 204 205 206 207 208 209 210 211 212 213 214
        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 = {
215 216
            'Out': self.x_fp32.sum(axis=tuple(self.attrs['dim']))
            / (self.x_fp32.shape[0] * self.x_fp32.shape[1])
217
        }
218 219 220 221 222


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