test_mul_int8_mkldnn_op.py 4.8 KB
Newer Older
P
Physher 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2019 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

P
Physher 已提交
17
import numpy as np
18

19
import paddle
20
from paddle.fluid import core
21 22 23 24
from paddle.fluid.tests.unittests.eager_op_test import (
    OpTest,
    skip_check_grad_ci,
)
25

P
Physher 已提交
26 27 28 29 30
'''
 test case for s8 * s8
'''


31
@skip_check_grad_ci(
32
    reason="mul_mkldnn_op does not implement grad operator, check_grad is not required."
33
)
P
Physher 已提交
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
class TestMKLDNNMulOpS8S8(OpTest):
    def setUp(self):
        self.op_type = "mul"
        self.init_kernel_type()
        self.init_data_type()
        self.init_data()
        self.attrs = {
            "use_mkldnn": self.use_mkldnn,
            "scale_x": self.scale_x,
            "scale_y": self.scale_y,
            "scale_out": self.scale_out,
            "force_fp32_output": self.force_fp32,
        }

    def init_kernel_type(self):
        self.use_mkldnn = True
        self.force_fp32 = True

    def init_data_type(self):
        self.srctype = np.uint8
        self.dsttype = np.float32 if self.force_fp32 else np.int8

    def init_data(self):
        self.scale_x = 0.6
        self.scale_y = [0.8]
        self.scale_out = 1.0

        # limit random range inside |-127, 127| to avoid overflow on SKL
        if self.srctype == np.int8:
63
            A_data = np.random.randint(-127, 127, (20, 5)).astype(np.int8)
P
Physher 已提交
64
        else:
65
            A_data = np.random.randint(0, 127, (20, 5)).astype(np.uint8)
P
Physher 已提交
66

67
        B_data = np.random.uniform(-127, 127, (5, 20)).astype(np.float32)
P
Physher 已提交
68

69
        quant_B = np.round(B_data * self.scale_y[0]).astype(np.int_)
P
Physher 已提交
70 71
        output = np.dot(A_data, quant_B)

72
        scale_output_shift = (self.scale_out) / (self.scale_x * self.scale_y[0])
P
Physher 已提交
73

74
        if self.force_fp32:
P
Physher 已提交
75 76 77 78 79 80 81 82
            output = (output * scale_output_shift).astype(self.dsttype)
        else:
            output = np.round(output * scale_output_shift).astype(self.dsttype)

        self.inputs = {'X': A_data, 'Y': B_data}
        self.outputs = {'Out': output}

    def test_check_output(self):
83
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
84 85 86
        self.check_output_with_place(
            core.CPUPlace(), atol=0, check_dygraph=False
        )
P
Physher 已提交
87 88 89


'''
90
 test case for  s8 * u8
P
Physher 已提交
91 92 93 94 95 96 97 98 99 100
'''


class TestMKLDNNMulOpS8U8(TestMKLDNNMulOpS8S8):
    def init_data_type(self):
        self.srctype = np.uint8
        self.dsttype = np.float32 if self.force_fp32 else np.int8


'''
101
 test case for  s8 * s8
P
Physher 已提交
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 127 128 129 130 131
'''


class TestMKLDNNMulOpS8S8WithFlatten(TestMKLDNNMulOpS8S8):
    def setUp(self):
        self.op_type = "mul"
        self.init_kernel_type()
        self.init_data_type()
        self.init_data()
        self.attrs = {
            "use_mkldnn": self.use_mkldnn,
            "scale_x": self.scale_x,
            "scale_y": self.scale_y,
            "scale_out": self.scale_out,
            "force_fp32_output": self.force_fp32,
            "x_num_col_dims": 2,
            "y_num_col_dims": 2,
        }

    def init_data(self):
        self.scale_x = 0.6
        self.scale_y = [0.8]
        self.scale_out = 1.0

        # limit random range inside |-127, 127| to avoid overflow on SKL
        if self.srctype == np.int8:
            A_data = np.random.randint(-127, 127, (3, 4, 4, 3)).astype(np.int8)
        else:
            A_data = np.random.randint(0, 127, (3, 4, 4, 3)).astype(np.uint8)

132 133 134
        B_data = np.random.uniform(-127, 127, (2, 6, 1, 2, 3)).astype(
            np.float32
        )
P
Physher 已提交
135 136 137 138

        A_data_reshape = A_data.reshape(3 * 4, 4 * 3)
        B_data_reshape = B_data.reshape(2 * 6, 1 * 2 * 3)

139
        quant_B = np.round(B_data_reshape * self.scale_y[0]).astype(np.int_)
P
Physher 已提交
140 141
        output = np.dot(A_data_reshape, quant_B)

142
        scale_output_shift = (self.scale_out) / (self.scale_x * self.scale_y[0])
P
Physher 已提交
143

144
        if self.force_fp32:
P
Physher 已提交
145 146 147 148 149 150 151 152 153 154 155
            output = (output * scale_output_shift).astype(self.dsttype)
        else:
            output = np.round(output * scale_output_shift).astype(self.dsttype)

        output = output.reshape(3, 4, 1, 2, 3)

        self.inputs = {'X': A_data, 'Y': B_data}
        self.outputs = {'Out': output}


'''
156
 test case for  s8 * u8
P
Physher 已提交
157 158 159 160 161 162 163 164 165 166
'''


class TestMKLDNNMulOpS8U8WithFlatten(TestMKLDNNMulOpS8S8WithFlatten):
    def init_data_type(self):
        self.srctype = np.uint8
        self.dsttype = np.float32 if self.force_fp32 else np.int8


if __name__ == '__main__':
167
    paddle.enable_static()
P
Physher 已提交
168
    unittest.main()