test_elementwise_sub_mkldnn_op.py 9.4 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
from paddle import enable_static
20
from paddle.fluid import core
21
from paddle.fluid.framework import _current_expected_place
22
from paddle.fluid.tests.unittests.eager_op_test import (
23 24 25 26
    OpTest,
    OpTestTool,
    convert_float_to_uint16,
)
27 28


29 30 31 32
@OpTestTool.skip_if(
    not (isinstance(_current_expected_place(), core.CPUPlace)),
    "GPU is not supported",
)
33 34 35 36 37 38 39 40 41
class TestMKLDNNElementwiseSubOp(OpTest):
    def setUp(self):
        self.op_type = "elementwise_sub"
        self.init_dtype()
        self.init_input_output()
        self.init_kernel_type()
        self.init_axis()
        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
42
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
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
        }
        self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
        self.outputs = {'Out': self.out}

    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)

    def test_check_grad_normal(self):
        self.check_grad(['X', 'Y'], 'Out')

    def test_check_grad_ignore_x(self):
        self.check_grad(['Y'], 'Out', no_grad_set=set("X"))

    def test_check_grad_ignore_y(self):
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'))

    def init_axis(self):
        self.axis = -1

    def init_kernel_type(self):
        self.use_mkldnn = True

    def init_dtype(self):
        self.dtype = np.float32

    def test_check_output(self):
        self.check_output()


class TestMKLDNNElementwiseSubOp2(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
76 77
        self.x = np.random.random((100,)).astype(self.dtype)
        self.y = np.random.random((100,)).astype(self.dtype)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        self.out = np.subtract(self.x, self.y)


class TestMKLDNNElementwiseSubOp3(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)


class TestMKLDNNElementwiseSubOp4(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.random.uniform(1, 2, [2, 3, 4, 32]).astype(self.dtype)
        self.y = np.random.uniform(1, 2, [4, 32]).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)


95
class TestMKLDNNElementwiseSubOp5(TestMKLDNNElementwiseSubOp):
96
    def init_input_output(self):
97 98
        self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
        self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
99 100 101
        self.out = np.subtract(self.x, self.y)


102 103 104 105 106
class TestMKLDNNElementwiseSubOp6(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.random.uniform(0.1, 2, [180, 1]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [1, 256]).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)
107 108


109
class TestMKLDNNElementwiseSubOp7(TestMKLDNNElementwiseSubOp):
110
    def init_input_output(self):
111 112
        self.x = np.random.uniform(0.1, 2, [1, 180]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [256, 1]).astype(self.dtype)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        self.out = np.subtract(self.x, self.y)


class TestMKLDNNElementwiseSubOp_broadcast(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.random.rand(2, 10, 12, 3).astype(self.dtype)
        self.y = np.random.rand(10, 12).astype(self.dtype)
        self.out = self.x - self.y.reshape(1, 10, 12, 1)

    def init_axis(self):
        self.axis = 1


class TestElementwiseSubOp_xsize_lessthan_ysize_sub(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.random.rand(10, 12).astype(self.dtype)
        self.y = np.random.rand(2, 2, 10, 12).astype(self.dtype)
        self.out = self.x - self.y

    def init_axis(self):
        self.axis = 2


136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
class TestMKLDNNElementwiseSubOpZeroDim(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.random.random((100,)).astype(self.dtype)
        self.y = np.array(3.0).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ignore_x(self):
        pass

    def test_check_grad_ignore_y(self):
        pass


class TestMKLDNNElementwiseSubOpZeroDim2(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.array(3.0).astype(self.dtype)
        self.y = np.random.random((100,)).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ignore_x(self):
        pass

    def test_check_grad_ignore_y(self):
        pass


class TestMKLDNNElementwiseSubOpZeroDim3(TestMKLDNNElementwiseSubOp):
    def init_input_output(self):
        self.x = np.array(3.0).astype(self.dtype)
        self.y = np.array(3.0).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ignore_x(self):
        pass

    def test_check_grad_ignore_y(self):
        pass


184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
@OpTestTool.skip_if_not_cpu_bf16()
class TestBf16(TestMKLDNNElementwiseSubOp):
    def setUp(self):
        self.op_type = "elementwise_sub"
        self.init_dtype()
        self.init_input_output()
        self.init_kernel_type()
        self.init_axis()

        self.x_bf16 = convert_float_to_uint16(self.x)
        self.y_bf16 = convert_float_to_uint16(self.y)
        self.inputs = {'X': self.x_bf16, 'Y': self.y_bf16}
        self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
        self.outputs = {'Out': convert_float_to_uint16(self.out)}

    def init_dtype(self):
        self.dtype = np.float32
        self.mkldnn_data_type = "bfloat16"

    def init_input_output(self):
204 205 206 207 208 209
        self.x = np.random.random(
            100,
        ).astype(self.dtype)
        self.y = np.random.random(
            100,
        ).astype(self.dtype)
210 211 212 213 214 215
        self.out = np.subtract(self.x, self.y)

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

    def test_check_grad_normal(self):
216 217 218 219 220 221 222
        self.check_grad_with_place(
            core.CPUPlace(),
            ["X", "Y"],
            "Out",
            user_defined_grads=[self.x, -self.x],
            user_defined_grad_outputs=[self.x_bf16],
        )
223 224

    def test_check_grad_ignore_x(self):
225 226 227 228 229 230 231
        self.check_grad_with_place(
            core.CPUPlace(),
            ["Y"],
            "Out",
            user_defined_grads=[-self.y],
            user_defined_grad_outputs=[self.y_bf16],
        )
232 233

    def test_check_grad_ignore_y(self):
234 235 236 237 238 239 240
        self.check_grad_with_place(
            core.CPUPlace(),
            ["X"],
            "Out",
            user_defined_grads=[self.x],
            user_defined_grad_outputs=[self.x_bf16],
        )
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256


class TestBf16Broadcasting(TestBf16):
    def init_input_output(self):
        self.x = np.random.uniform(1, 2, [2, 3, 4, 100]).astype(self.dtype)
        self.y = np.random.uniform(1, 2, [100]).astype(self.dtype)
        self.out = np.subtract(self.x, self.y)

    def compute_reduced_gradients(self, out_grads):
        part_sum = np.add.reduceat(out_grads, [0], axis=0)
        part_sum = np.add.reduceat(part_sum, [0], axis=1)
        part_sum = np.add.reduceat(part_sum, [0], axis=2)
        return -part_sum.flatten()

    def test_check_grad_normal(self):
        self.check_grad_with_place(
257 258
            core.CPUPlace(),
            ["X", "Y"],
259
            "Out",
260 261 262
            user_defined_grads=[self.x, self.compute_reduced_gradients(self.x)],
            user_defined_grad_outputs=[self.x_bf16],
        )
263 264 265

    def test_check_grad_ignore_x(self):
        self.check_grad_with_place(
266 267
            core.CPUPlace(),
            ["Y"],
268 269
            "Out",
            user_defined_grads=[self.compute_reduced_gradients(self.x)],
270 271
            user_defined_grad_outputs=[self.x_bf16],
        )
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308


class TestInt8(TestMKLDNNElementwiseSubOp):
    def init_kernel_type(self):
        self.use_mkldnn = True
        self._cpu_only = True

    def init_dtype(self):
        self.dtype = np.int8

    def init_input_output(self):
        self.x = np.random.randint(0, 3, (12, 9)).astype("int8")
        self.y = np.random.randint(0, 3, (12, 9)).astype("int8")
        self.out = np.subtract(self.x, self.y)

    def init_scales(self):
        self.attrs['Scale_x'] = 1.0
        self.attrs['Scale_y'] = 1.0
        self.attrs['Scale_out'] = 1.0

    def test_check_output(self):
        self.init_scales()
        self.check_output()

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ignore_x(self):
        pass

    def test_check_grad_ignore_y(self):
        pass


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