test_fake_dequantize_op.py 4.5 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
def channel_wise_quantize_max_abs(x, quant_bit=8):
Z
Zhen Wang 已提交
35 36 37 38 39
    scales = []
    for i in range(x.shape[0]):
        scales.append(np.max(np.abs(x[i])).astype("float32"))

    y = x.copy()
40
    max_range = math.pow(2, quant_bit - 1) - 1
Z
Zhen Wang 已提交
41 42 43 44 45
    for i, scale in enumerate(scales):
        y[i] = np.round(y[i] / scale * max_range)
    return y, scales


46 47 48 49
def channel_wise_dequantize_max_abs(x,
                                    scales,
                                    quant_bits,
                                    activation_scale=None):
Z
Zhen Wang 已提交
50 51
    y = x.copy()
    for i in range(x.shape[0]):
52 53 54
        y[i] = (scales[i] / (math.pow(2, quant_bits[0] - 1) - 1)) * y[i]
    if activation_scale is not None:
        y *= activation_scale / (math.pow(2, quant_bits[1] - 1) - 1)
Z
Zhen Wang 已提交
55 56 57
    return y


58
class TestFakeChannelWiseDequantizeMaxAbsOpTwoScales(OpTest):
Z
Zhen Wang 已提交
59
    def set_args(self):
60
        self.quant_bits = [8, 8]
Z
Zhen Wang 已提交
61
        self.data_type = "float32"
62
        self.activation_scale = 0.7861
Z
Zhen Wang 已提交
63 64 65 66 67

    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)
68 69 70
        yq, scales = channel_wise_quantize_max_abs(x, self.quant_bits[0])
        ydq = channel_wise_dequantize_max_abs(yq, scales, self.quant_bits,
                                              self.activation_scale)
Z
Zhen Wang 已提交
71 72 73

        self.inputs = {
            'X': yq,
74
            'Scales': [("scales0", np.array(scales).astype(self.data_type)),
75 76
                       ("scales1", np.array(
                           [self.activation_scale]).astype(self.data_type))]
Z
Zhen Wang 已提交
77
        }
78
        self.attrs = {'quant_bits': self.quant_bits}
Z
Zhen Wang 已提交
79 80 81 82 83 84
        self.outputs = {'Out': ydq}

    def test_check_output(self):
        self.check_output()


85
class TestFakeChannelWiseDequantizeMaxAbsOpOneScale(OpTest):
Z
Zhen Wang 已提交
86
    def set_args(self):
87
        self.quant_bits = [8]
Z
Zhen Wang 已提交
88 89 90 91 92 93
        self.data_type = "float32"

    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)
94 95
        yq, scales = channel_wise_quantize_max_abs(x, self.quant_bits[0])
        ydq = channel_wise_dequantize_max_abs(yq, scales, self.quant_bits)
Z
Zhen Wang 已提交
96 97 98

        self.inputs = {
            'X': yq,
99
            'Scales': [("scales0", np.array(scales).astype(self.data_type))]
Z
Zhen Wang 已提交
100
        }
101
        self.attrs = {'quant_bits': self.quant_bits}
Z
Zhen Wang 已提交
102 103 104 105 106 107
        self.outputs = {'Out': ydq}

    def test_check_output(self):
        self.check_output()


108 109 110
class TestFakeDequantizeMaxAbsOp(OpTest):
    def set_args(self):
        self.num_bits = 8
111 112
        self.max_range = math.pow(2, self.num_bits - 1) - 1
        self.data_type = "float32"
113 114 115 116

    def setUp(self):
        self.set_args()
        self.op_type = "fake_dequantize_max_abs"
117 118 119
        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)
120

121 122
        self.inputs = {'X': yq, 'Scale': np.array(scale).astype(self.data_type)}
        self.attrs = {'max_range': self.max_range}
123 124 125 126 127 128
        self.outputs = {'Out': ydq}

    def test_check_output(self):
        self.check_output()


129 130 131 132 133 134 135 136
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):
137 138
    def set_args(self):
        self.num_bits = 5
139 140
        self.max_range = math.pow(2, self.num_bits - 1) - 1
        self.data_type = "float32"
141 142 143 144


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