test_fake_dequantize_op.py 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

15 16
from __future__ import print_function

17 18 19
import unittest
import numpy as np
import math
20
from op_test import OpTest
21 22


23
def quantize_max_abs(x, max_range):
24
    scale = np.max(np.abs(x).flatten())
25
    y = np.round(x / scale * max_range)
26 27 28
    return y, scale


29 30
def dequantize_max_abs(x, scale, max_range):
    y = (scale / max_range) * x
31 32 33
    return y


34 35
def channel_wise_quantize_max_abs(x, quant_bit=8, quant_axis=0):
    assert quant_axis in [0, 1], "The quant_axis should be 0 or 1."
Z
Zhen Wang 已提交
36
    scales = []
37 38 39
    y = x.copy()
    max_range = math.pow(2, quant_bit - 1) - 1
    if quant_axis == 0:
40
        for i in range(x.shape[0]):
41 42 43 44 45 46 47 48
            scale = np.max(np.abs(x[i])).astype("float32")
            scales.append(scale)
            y[i] = np.round(x[i] * max_range / scale)
    elif quant_axis == 1:
        for i in range(x.shape[1]):
            scale = np.max(np.abs(x[:, i])).astype("float32")
            scales.append(scale)
            y[:, i] = np.round(x[:, i] * max_range / scale)
Z
Zhen Wang 已提交
49 50 51
    return y, scales


52 53 54
def channel_wise_dequantize_max_abs(x,
                                    scales,
                                    quant_bits,
55
                                    quant_axis,
56
                                    activation_scale=None):
57 58 59 60
    assert quant_axis in [0, 1], "The quant_axis should be 0 or 1."

    if isinstance(quant_bits, list):
        max_range = math.pow(2, quant_bits[0] - 1) - 1
61
    else:
62 63 64
        max_range = math.pow(2, quant_bits - 1) - 1
    y = x.copy()
    if quant_axis == 0:
65
        for i in range(x.shape[0]):
66 67 68 69 70 71 72
            y[i] = x[i] * scales[i] / max_range
    elif quant_axis == 1:
        for i in range(x.shape[1]):
            y[:, i] = x[:, i] * scales[i] / max_range

    if activation_scale is not None:
        y = y * activation_scale / (math.pow(2, quant_bits[1] - 1) - 1)
Z
Zhen Wang 已提交
73 74 75
    return y


76
class TestFakeChannelWiseDequantizeMaxAbsOpTwoScales(OpTest):
Z
Zhen Wang 已提交
77
    def set_args(self):
78
        self.quant_bits = [8, 8]
Z
Zhen Wang 已提交
79
        self.data_type = "float32"
80
        self.activation_scale = 0.7861
Z
Zhen Wang 已提交
81 82 83 84 85

    def setUp(self):
        self.set_args()
        self.op_type = "fake_channel_wise_dequantize_max_abs"
        x = np.random.randn(4, 3, 64, 64).astype(self.data_type)
86 87
        yq, scales = channel_wise_quantize_max_abs(x, self.quant_bits[0], 1)
        ydq = channel_wise_dequantize_max_abs(yq, scales, self.quant_bits, 1,
88
                                              self.activation_scale)
Z
Zhen Wang 已提交
89 90 91

        self.inputs = {
            'X': yq,
92
            'Scales': [("scales0", np.array(scales).astype(self.data_type)),
93 94
                       ("scales1", np.array(
                           [self.activation_scale]).astype(self.data_type))]
Z
Zhen Wang 已提交
95
        }
96
        self.attrs = {'quant_bits': self.quant_bits}
Z
Zhen Wang 已提交
97 98 99 100 101 102
        self.outputs = {'Out': ydq}

    def test_check_output(self):
        self.check_output()


103
class TestFakeChannelWiseDequantizeMaxAbsOpOneScale(OpTest):
Z
Zhen Wang 已提交
104
    def set_args(self):
105
        self.quant_bits = [8]
Z
Zhen Wang 已提交
106
        self.data_type = "float32"
107
        self.quant_axis = 0
Z
Zhen Wang 已提交
108 109 110 111 112

    def setUp(self):
        self.set_args()
        self.op_type = "fake_channel_wise_dequantize_max_abs"
        x = np.random.randn(4, 3, 64, 64).astype(self.data_type)
113 114 115 116
        yq, scales = channel_wise_quantize_max_abs(x, self.quant_bits[0],
                                                   self.quant_axis)
        ydq = channel_wise_dequantize_max_abs(yq, scales, self.quant_bits,
                                              self.quant_axis)
Z
Zhen Wang 已提交
117 118 119

        self.inputs = {
            'X': yq,
120
            'Scales': [("scales0", np.array(scales).astype(self.data_type))]
Z
Zhen Wang 已提交
121
        }
122 123 124 125
        self.attrs = {
            'quant_bits': self.quant_bits,
            'quant_axis': self.quant_axis
        }
Z
Zhen Wang 已提交
126 127 128 129 130 131
        self.outputs = {'Out': ydq}

    def test_check_output(self):
        self.check_output()


132 133 134 135 136 137 138 139
class TestFakeChannelWiseDequantizeMaxAbsOpOneScale1(
        TestFakeChannelWiseDequantizeMaxAbsOpOneScale):
    def set_args(self):
        self.quant_bits = [8]
        self.data_type = "float32"
        self.quant_axis = 1


140 141 142
class TestFakeDequantizeMaxAbsOp(OpTest):
    def set_args(self):
        self.num_bits = 8
143 144
        self.max_range = math.pow(2, self.num_bits - 1) - 1
        self.data_type = "float32"
145 146 147 148

    def setUp(self):
        self.set_args()
        self.op_type = "fake_dequantize_max_abs"
149 150 151
        x = np.random.randn(31, 65).astype(self.data_type)
        yq, scale = quantize_max_abs(x, self.max_range)
        ydq = dequantize_max_abs(yq, scale, self.max_range)
152

153 154
        self.inputs = {'X': yq, 'Scale': np.array(scale).astype(self.data_type)}
        self.attrs = {'max_range': self.max_range}
155 156 157 158 159 160
        self.outputs = {'Out': ydq}

    def test_check_output(self):
        self.check_output()


161 162 163 164 165 166 167 168
class TestFakeDequantizeMaxAbsOpDouble(TestFakeDequantizeMaxAbsOp):
    def set_args(self):
        self.num_bits = 8
        self.max_range = math.pow(2, self.num_bits - 1) - 1
        self.data_type = "float64"


class TestFakeDequantizeMaxAbsOp5Bits(TestFakeDequantizeMaxAbsOp):
169 170
    def set_args(self):
        self.num_bits = 5
171 172
        self.max_range = math.pow(2, self.num_bits - 1) - 1
        self.data_type = "float32"
173 174 175 176


if __name__ == "__main__":
    unittest.main()