test_elementwise_mul_mkldnn_op.py 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#  Copyright (c) 2018 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.tests.unittests.eager_op_test import skip_check_grad_ci
21 22 23
from paddle.fluid.tests.unittests.test_elementwise_mul_op import (
    ElementwiseMulOp,
)
24 25


26 27 28 29 30
class TestMKLDNNElementwiseMulOp(ElementwiseMulOp):
    def init_kernel_type(self):
        self.use_mkldnn = True

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

33 34 35

class TestMKLDNNElementwiseMulOp2(TestMKLDNNElementwiseMulOp):
    def init_input_output(self):
36 37
        self.x = np.random.random((100,)).astype(self.dtype)
        self.y = np.random.random((100,)).astype(self.dtype)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        self.out = np.multiply(self.x, self.y)


class TestMKLDNNElementwiseMulOp3(TestMKLDNNElementwiseMulOp):
    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.multiply(self.x, self.y)


class TestMKLDNNElementwiseMulOp4(TestMKLDNNElementwiseMulOp):
    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.multiply(self.x, self.y)

    # TODO(jczaja): Enable when grad is ready
    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_y(self):
        pass


62 63 64 65 66 67
class TestMKLDNNElementwiseMulOp5(TestMKLDNNElementwiseMulOp):
    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.multiply(self.x, self.y)

68 69 70 71 72 73 74 75 76 77
    # TODO(jczaja): Enable when grad is ready
    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
class TestMKLDNNElementwiseMulOpZeroDim(TestMKLDNNElementwiseMulOp):
    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.multiply(self.x, self.y)

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

    def test_check_grad_ingore_x(self):
        pass


class TestMKLDNNElementwiseMulOpZeroDim2(TestMKLDNNElementwiseMulOp):
    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.multiply(self.x, self.y)

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

    def test_check_grad_ingore_x(self):
        pass


class TestMKLDNNElementwiseMulOpZeroDim3(TestMKLDNNElementwiseMulOp):
    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.multiply(self.x, self.y)

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_y(self):
        pass

    def test_check_grad_ingore_x(self):
        pass


127 128 129 130
''' INT8 Tests '''


@skip_check_grad_ci(
131 132
    reason="oneDNN's int8 elementwise_ops don't implemend grad kernel."
)
133
class TestInt8(ElementwiseMulOp):
134 135
    def init_kernel_type(self):
        self.use_mkldnn = True
136 137 138 139 140 141 142 143 144
        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.multiply(self.x, self.y)
145

146 147 148 149
    def init_scales(self):
        self.attrs['Scale_x'] = 1.0
        self.attrs['Scale_y'] = 1.0
        self.attrs['Scale_out'] = 1.0
150 151

    def test_check_output(self):
152 153
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.init_scales()
154
        self.check_output(check_dygraph=(not self.use_mkldnn))
155 156 157 158 159 160 161 162 163

    def test_check_grad_normal(self):
        pass

    def test_check_grad_ingore_x(self):
        pass

    def test_check_grad_ingore_y(self):
        pass
164

165

166 167 168 169 170 171 172 173
class TestInt8Scales(TestInt8):
    def quantize(self, tensor, dt="int8"):
        max_int = 127.0 if dt == "int8" else 255.0
        scale = max_int / np.abs(np.amax(tensor))
        quantized = np.round(scale * tensor).astype(dt)
        return scale, quantized

    def init_input_output(self):
174 175
        self.x_f = np.random.random((100,)).astype("float")
        self.y_f = np.random.random((100,)).astype("float")
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        self.out_f = np.multiply(self.x_f, self.y_f)

        self.scale_x, self.x = self.quantize(self.x_f)
        self.scale_y, self.y = self.quantize(self.y_f)
        self.scale_o, self.out = self.quantize(self.out_f)

    def init_scales(self):
        self.attrs['Scale_x'] = self.scale_x
        self.attrs['Scale_y'] = self.scale_y
        self.attrs['Scale_out'] = self.scale_o

    def test_check_output(self):
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.init_scales()
        int_atol = 1  # different quantization techniques
191
        self.check_output(check_dygraph=(not self.use_mkldnn), atol=int_atol)
192 193 194 195


class TestUint8Scales(TestInt8Scales):
    def init_input_output(self):
196 197
        self.x_f = np.random.random((100,)).astype("float")
        self.y_f = np.random.random((100,)).astype("float")
198 199 200 201 202 203 204 205 206 207
        self.out_f = np.multiply(self.x_f, self.y_f)

        self.scale_x, self.x = self.quantize(self.x_f, "uint8")
        self.scale_y, self.y = self.quantize(self.y_f, "uint8")
        self.scale_o, self.out = self.quantize(self.out_f, "uint8")

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


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