test_fake_quantize_op.py 21.7 KB
Newer Older
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
视言's avatar
视言 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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

视言's avatar
视言 已提交
17
import unittest
18
import itertools
视言's avatar
视言 已提交
19
import numpy as np
20
import math
21
from op_test import OpTest
视言's avatar
视言 已提交
22 23


24 25 26 27 28
def round_c_single_element(val):
    dtype = type(val)
    if val >= 0:
        return dtype(np.floor(val + 0.5))
    return dtype(np.ceil(val - 0.5))
29 30


31
# rounding to nearest ties away from zero
32 33 34
round_c = np.vectorize(round_c_single_element)


35 36 37 38 39
def get_compute_type(dtype):
    assert dtype in [np.float16, np.float32, np.float64]
    if dtype == np.float16:
        return np.float32
    return dtype
40

Z
Zhen Wang 已提交
41

42
class TestFakeQuantizeAbsMaxOp(OpTest):
43

44
    def setUp(self):
45
        self.op_type = 'fake_quantize_abs_max'
46 47
        self.attrs = {'bit_length': 8}

48 49 50 51 52
    def _fake_quantize_abs_max(self,
                               dtype,
                               input_shape,
                               distribution,
                               round_type='TiesToEven'):
53 54 55 56
        input_data = distribution(input_shape).astype(dtype)
        compute_type = get_compute_type(dtype)
        scale = np.max(np.abs(input_data))
        bnt = (1 << (self.attrs['bit_length'] - 1)) - 1
57
        inv_scale = 1.0 / (scale + 1e-6) if scale < 1e-30 else 1.0 / scale
58 59 60 61 62 63 64 65 66
        if round_type == 'TiesToEven':
            round_out = np.round(
                input_data.astype(compute_type) * inv_scale * bnt)
            self.attrs['round_type'] = 0
        else:
            round_out = round_c(
                input_data.astype(compute_type) * inv_scale * bnt)
            self.attrs['round_type'] = 1
        output_data = np.clip(round_out, -bnt - 1, bnt)
67 68 69
        self.inputs = {'X': input_data}
        self.outputs = {'Out': output_data, 'OutScale': scale}
        self.dtype = dtype
70 71
        self.check_output()

72 73
    def test_fake_quantize_abs_max(self):
        self._fake_quantize_abs_max(np.float32, (124, 240), np.random.random)
74

75 76 77 78 79
    def test_fake_quantize_abs_max_round1(self):
        self._fake_quantize_abs_max(np.float32, (124, 240),
                                    np.random.random,
                                    round_type='TiesAwayFromZero')

80 81
    def test_fake_quantize_abs_max_float16(self):
        self._fake_quantize_abs_max(np.float16, (124, 240), np.random.random)
82

83 84
    def test_fake_quantize_abs_max_underflow(self):
        self._fake_quantize_abs_max(np.float32, (10, 10), np.zeros)
85

86 87 88
    def test_fake_quantize_abs_max_underflow2(self):
        self._fake_quantize_abs_max(np.float32, (10, 10),
                                    lambda shape: np.full(shape, 1e-40))
Z
Zhen Wang 已提交
89

90

91
class TestFakeChannelWiseQuantizeAbsMaxOp(OpTest):
92

93 94 95
    def setUp(self):
        self.op_type = 'fake_channel_wise_quantize_abs_max'
        self.attrs = {'bit_length': 8}
96

97 98 99 100 101 102
    def _fake_channel_wise_quantize_abs_max(self,
                                            dtype,
                                            input_shape,
                                            quant_axis,
                                            distribution,
                                            round_type='TiesToEven'):
103 104 105 106
        assert quant_axis in [0, 1], 'quant_axis should be 0 or 1.'
        input_data = distribution(input_shape).astype(dtype)
        compute_type = get_compute_type(dtype)
        bnt = (1 << (self.attrs['bit_length'] - 1)) - 1
107 108
        compute_axis = tuple(i for i in range(len(input_shape))
                             if i != quant_axis)
109
        scale_broadcast = np.amax(input_data, axis=compute_axis, keepdims=True)
110 111 112 113 114 115 116 117 118
        if round_type == 'TiesToEven':
            round_out = np.round(
                input_data.astype(compute_type) / scale_broadcast * bnt)
            self.attrs['round_type'] = 0
        else:
            round_out = round_c(
                input_data.astype(compute_type) / scale_broadcast * bnt)
            self.attrs['round_type'] = 1
        output_data = np.clip(round_out, -bnt - 1, bnt)
119 120 121 122 123 124 125 126
        if quant_axis == 1:
            scale_broadcast = np.transpose(scale_broadcast,
                                           (1, ) + compute_axis)
        scale = scale_broadcast.reshape(input_shape[quant_axis], -1)[:, 0]
        self.inputs = {'X': input_data}
        self.outputs = {'Out': output_data, 'OutScale': scale}
        self.dtype = dtype
        self.attrs['quant_axis'] = quant_axis
Z
Zhen Wang 已提交
127
        self.check_output()
128

129 130 131
    def test_fake_channel_wise_quantize_abs_max(self):
        dtype_options = [np.float32, np.float16]
        input_shape_quant_axis_options = [[(20, 15, 6, 6), 0],
132 133 134 135 136 137
                                          [(20, 15, 6, 6), 1], [(30, 30), 0],
                                          [(30, 30), 1]]
        round_type_options = ['TiesToEven', 'TiesAwayFromZero']
        for dtype, input_shape_quant_axis, round_type in itertools.product(
                dtype_options, input_shape_quant_axis_options,
                round_type_options):
138
            input_shape, quant_axis = input_shape_quant_axis
139 140
            with self.subTest(dtype=dtype,
                              input_shape=input_shape,
141 142
                              quant_axis=quant_axis,
                              round_type=round_type):
143
                self._fake_channel_wise_quantize_abs_max(
144 145
                    dtype, input_shape, quant_axis, np.random.random,
                    round_type)
146 147


148
class TestFakeQuantizeRangeAbsMaxOp(OpTest):
149

150
    def setUp(self):
151 152 153 154 155 156 157
        self.op_type = 'fake_quantize_range_abs_max'
        self.attrs = {'bit_length': 5, 'window_size': 1}

    def _fake_quantize_range_abs_max(self,
                                     dtype,
                                     input_shape,
                                     distribution,
158 159
                                     is_test=False,
                                     round_type='TiesToEven'):
160 161 162 163 164 165 166 167
        input_data = distribution(input_shape).astype(dtype)
        compute_type = get_compute_type(dtype)
        bnt = (1 << (self.attrs['bit_length'] - 1)) - 1
        in_scale = np.zeros(1).astype(dtype)
        out_scale = np.zeros(self.attrs['window_size']).astype(dtype)
        out_scale[0] = np.max(np.abs(input_data))
        if is_test:
            out_scale[0] = in_scale[0] = out_scale[0] - 1.0
168 169 170 171
        if round_type == 'TiesToEven':
            round_out = np.round(
                input_data.astype(compute_type) / out_scale[0] * bnt)
            self.attrs['round_type'] = 0
172
        else:
173 174 175 176
            round_out = round_c(
                input_data.astype(compute_type) / out_scale[0] * bnt)
            self.attrs['round_type'] = 1
        output_data = np.clip(round_out, -bnt - 1, bnt)
视言's avatar
视言 已提交
177
        self.inputs = {
178 179 180
            'X': input_data,
            'Iter': np.zeros(1).astype(np.int64),
            'InScale': in_scale
视言's avatar
视言 已提交
181 182
        }
        self.outputs = {
183 184 185
            'Out': output_data,
            'OutScale': out_scale[0],
            'OutScales': out_scale
视言's avatar
视言 已提交
186
        }
187 188
        self.dtype = dtype
        self.attrs['is_test'] = is_test
视言's avatar
视言 已提交
189 190
        self.check_output()

191
    def test_fake_quantize_range_abs_max(self):
192
        dtype_options = [np.float16, np.float32]
193
        is_test_options = [False, True]
194 195 196
        round_type_options = ['TiesToEven', 'TiesAwayFromZero']
        for dtype, is_test, round_type in itertools.product(
                dtype_options, is_test_options, round_type_options):
197
            self.attrs['bit_length'] = 8 if is_test else 5
198 199 200
            with self.subTest(dtype=dtype,
                              is_test=is_test,
                              round_type=round_type):
201
                self._fake_quantize_range_abs_max(
202 203 204 205
                    dtype, (8, 16, 6, 6),
                    lambda shape: (np.random.random(shape) - 0.4) * 10,
                    is_test=is_test,
                    round_type=round_type)
206 207


Z
Zhen Wang 已提交
208
class TestMovingAverageAbsMaxScaleOp(OpTest):
209

Z
Zhen Wang 已提交
210
    def setUp(self):
211
        self.op_type = 'moving_average_abs_max_scale'
Z
Zhen Wang 已提交
212 213
        self.attrs = {'moving_rate': float(0.9), 'is_test': False}

214 215 216 217 218 219 220
    def _moving_average_abs_max_scale(self, dtype, input_shape, distribution):
        input_data = distribution(input_shape).astype(dtype)
        in_accum = np.ones(1).astype(dtype)
        in_state = np.ones(1).astype(dtype)
        out_accum = self.attrs['moving_rate'] * in_accum[0] + np.max(
            np.abs(input_data))
        out_state = self.attrs['moving_rate'] * in_state[0] + 1.0
Z
Zhen Wang 已提交
221
        out_scale = out_accum / out_state
222 223 224 225 226
        self.inputs = {
            'X': input_data,
            'InAccum': in_accum,
            'InState': in_state
        }
Z
Zhen Wang 已提交
227
        self.outputs = {
228
            'Out': input_data,
Z
Zhen Wang 已提交
229 230
            'OutAccum': out_accum,
            'OutState': out_state,
231
            'OutScale': out_scale
Z
Zhen Wang 已提交
232
        }
233
        self.dtype = dtype
Z
Zhen Wang 已提交
234 235
        self.check_output()

236 237 238
    def test_moving_average_abs_max(self):
        self._moving_average_abs_max_scale(np.float32, (8, 16, 7, 7),
                                           np.random.random)
Z
Zhen Wang 已提交
239

240

241
class TestFakeQuantizeMovingAverageAbsMaxOp(OpTest):
242

243
    def setUp(self):
244 245 246 247 248 249 250 251
        self.op_type = 'fake_quantize_moving_average_abs_max'
        self.attrs = {'bit_length': 5, 'moving_rate': 0.9, 'is_test': False}

    def _fake_quantize_moving_average_abs_max(self,
                                              dtype,
                                              input_shape,
                                              distribution,
                                              dequantize=False,
252 253
                                              with_gradient=False,
                                              round_type='TiesToEven'):
254 255 256 257 258 259 260 261 262 263 264 265 266
        input_data = distribution(input_shape).astype(dtype)
        compute_type = get_compute_type(dtype)
        bnt = (1 << (self.attrs['bit_length'] - 1)) - 1
        in_accum = np.ones(1).astype(dtype)
        in_state = np.ones(1).astype(dtype)
        in_scale = np.array([0.001]).astype(dtype)
        out_accum = np.zeros(1).astype(dtype)
        out_state = np.zeros(1).astype(dtype)
        out_scale = np.zeros(1).astype(dtype)
        out_accum[0] = self.attrs['moving_rate'] * in_accum[0] + np.max(
            np.abs(input_data))
        out_state[0] = self.attrs['moving_rate'] * in_state[0] + 1.0
        out_scale = out_accum / out_state
267 268 269 270 271 272 273 274 275
        if round_type == 'TiesToEven':
            round_out = np.round(
                input_data.astype(compute_type) / out_scale * bnt)
            self.attrs['round_type'] = 0
        else:
            round_out = round_c(
                input_data.astype(compute_type) / out_scale * bnt)
            self.attrs['round_type'] = 1
        quant_data = np.clip(round_out, -bnt - 1, bnt)
276
        if dequantize:
277
            output_data = (quant_data * out_scale / bnt).astype(dtype)
278 279
            self.op_type = 'fake_quantize_dequantize_moving_average_abs_max'
        else:
280
            output_data = quant_data.astype(dtype)
281
        self.inputs = {
282 283 284 285
            'X': input_data,
            'InScale': in_scale,
            'InAccum': in_accum,
            'InState': in_state
286 287
        }
        self.outputs = {
288
            'Out': output_data,
289 290
            'OutAccum': out_accum,
            'OutState': out_state,
291
            'OutScale': out_scale
292
        }
293
        self.dtype = dtype
294
        self.check_output()
295 296 297 298 299
        if with_gradient:
            gradient = [
                np.ones(input_data.shape) / np.product(input_data.shape)
            ]
            self.check_grad(['X'], 'Out', user_defined_grads=gradient)
300

301 302 303
    def test_fake_quantize_moving_average_abs_max(self):
        self._fake_quantize_moving_average_abs_max(np.float32, (8, 16, 7, 7),
                                                   np.random.random)
304

305 306 307
    def test_fake_quantize_moving_average_abs_max_float16(self):
        self._fake_quantize_moving_average_abs_max(np.float16, (8, 16, 7, 7),
                                                   np.random.random)
308

309 310 311 312 313 314
    def test_fake_quantize_moving_average_abs_max_round1(self):
        self._fake_quantize_moving_average_abs_max(
            np.float32, (8, 16, 7, 7),
            np.random.random,
            round_type='TiesAwayFromZero')

315
    def test_fake_quantize_dequantize_moving_average_abs_max(self):
316 317 318 319
        self._fake_quantize_moving_average_abs_max(np.float32, (8, 16, 7, 7),
                                                   np.random.random,
                                                   dequantize=True,
                                                   with_gradient=True)
320

321

322
class TestFakeQuantizeDequantizeAbsMaxOp(OpTest):
323

324
    def setUp(self):
325
        self.op_type = 'fake_quantize_dequantize_abs_max'
326
        self.attrs = {'bit_length': 8}
327

328 329 330 331 332
    def _fake_quantize_dequantize_abs_max(self,
                                          dtype,
                                          input_shape,
                                          distribution,
                                          round_type='TiesToEven'):
333 334 335
        input_data = distribution(input_shape).astype(dtype)
        scale = np.max(np.abs(input_data)).astype(dtype)
        bnt = (1 << (self.attrs['bit_length'] - 1)) - 1
336 337 338 339 340 341 342
        if round_type == 'TiesToEven':
            round_out = np.round(input_data / scale * bnt)
            self.attrs['round_type'] = 0
        else:
            round_out = round_c(input_data / scale * bnt)
            self.attrs['round_type'] = 1
        output_data = np.clip(round_out, -bnt - 1, bnt) * scale / bnt
343
        self.inputs = {'X': input_data}
344
        self.outputs = {
345 346
            'Out': output_data,
            'OutScale': np.array(scale).astype(dtype)
347
        }
348
        self.dtype = dtype
349
        self.check_output()
350 351
        gradient = [np.ones(input_data.shape) / np.product(input_data.shape)]
        self.check_grad(['X'], 'Out', user_defined_grads=gradient)
352

353 354 355
    def test_fake_quantize_dequantize_abs_max(self):
        self._fake_quantize_dequantize_abs_max(np.float32, (124, 240),
                                               np.random.random)
356

357 358 359 360 361
    def test_fake_quantize_dequantize_abs_max_round1(self):
        self._fake_quantize_dequantize_abs_max(np.float32, (124, 240),
                                               np.random.random,
                                               round_type='TiesAwayFromZero')

362

363
class TestChannelWiseFakeQuantizeDequantizeAbsMaxOp(OpTest):
364

H
huangxu96 已提交
365
    def setUp(self):
366 367
        self.op_type = 'fake_channel_wise_quantize_dequantize_abs_max'
        self.attrs = {'bit_length': 8}
H
huangxu96 已提交
368

369 370 371
    def _fake_channel_wise_quantize_dequantize_abs_max(self,
                                                       dtype,
                                                       input_shape,
372
                                                       quant_axis,
373 374
                                                       distribution,
                                                       round_type='TiesToEven'):
375 376 377 378 379
        assert quant_axis in [0, 1], 'quant_axis should be 0 or 1.'
        input_data = distribution(input_shape).astype(dtype)
        compute_type = get_compute_type(dtype)
        bnt = (1 << (self.attrs['bit_length'] - 1)) - 1
        output_data = input_data.copy().astype(compute_type)
380 381
        compute_axis = tuple(i for i in range(len(input_shape))
                             if i != quant_axis)
382
        scale_broadcast = np.amax(input_data, axis=compute_axis, keepdims=True)
383 384 385 386 387 388 389
        if round_type == 'TiesToEven':
            round_out = np.round(bnt * output_data / scale_broadcast)
            self.attrs['round_type'] = 0
        else:
            round_out = round_c(bnt * output_data / scale_broadcast)
            self.attrs['round_type'] = 1
        output_data = np.clip(round_out, -bnt - 1, bnt) * scale_broadcast / bnt
390 391 392 393 394 395 396 397
        if quant_axis == 1:
            scale_broadcast = np.transpose(scale_broadcast,
                                           (1, ) + compute_axis)
        scale = scale_broadcast.reshape(input_shape[quant_axis], -1)[:, 0]
        self.inputs = {'X': input_data}
        self.outputs = {'Out': output_data, 'OutScale': scale}
        self.dtype = dtype
        self.attrs['quant_axis'] = quant_axis
H
huangxu96 已提交
398
        self.check_output()
399 400
        gradient = [np.ones(input_data.shape) / np.product(input_data.shape)]
        self.check_grad(['X'], 'Out', user_defined_grads=gradient)
H
huangxu96 已提交
401

402
    def test_channel_wise_fake_quant_dequant_abs_max(self):
403 404 405
        input_shape_quant_axis_options = [[(3, 4, 64, 64), 0],
                                          [(15, 20, 5, 5), 1], [(30, 15), 0],
                                          [(30, 15), 1]]
406 407 408 409 410 411 412
        round_type_options = ['TiesToEven', 'TiesAwayFromZero']
        for input_shape_quant_axis, round_type in itertools.product(
                input_shape_quant_axis_options, round_type_options):
            input_shape, quant_axis = input_shape_quant_axis
            with self.subTest(input_shape=input_shape,
                              quant_axis=quant_axis,
                              round_type=round_type):
413
                self._fake_channel_wise_quantize_dequantize_abs_max(
414 415 416 417 418
                    np.float32,
                    input_shape,
                    quant_axis,
                    np.random.random,
                    round_type=round_type)
H
huangxu96 已提交
419 420


421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
def quantize_max_abs(x, max_range):
    scale = np.max(np.abs(x).flatten())
    y = np.round(x / scale * max_range)
    return y, scale


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."
    scales = []
    y = x.copy()
    max_range = math.pow(2, quant_bit - 1) - 1
    if quant_axis == 0:
        for i in range(x.shape[0]):
            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)
    return y, scales


class TestChannelWiseQuantizeOp(OpTest):
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    def set_args(self):
        self.bit_length = 8
        self.data_type = "float32"
        self.quant_axis = 0

    def setUp(self):
        self.set_args()
        self.op_type = "quantize_linear"
        x = np.random.randn(4, 3, 64, 64).astype(self.data_type)
        yq, scale = channel_wise_quantize_max_abs(x, self.bit_length,
                                                  self.quant_axis)
        scale = np.array(scale).astype(self.data_type)
        zero_point = np.zeros(scale.shape, dtype="int32")

        self.inputs = {'X': x, 'Scale': scale, 'ZeroPoint': zero_point}
        self.attrs = {
            'bit_length': self.bit_length,
            'quant_axis': self.quant_axis
        }
        self.outputs = {'Y': yq}

    def test_check_output(self):
        self.check_output()


class TestChannelWiseQuantizeOp1(TestChannelWiseQuantizeOp):
473

474 475 476 477 478 479 480
    def set_args(self):
        self.bit_length = 8
        self.data_type = "float32"
        self.quant_axis = 1


class TestChannelWiseQuantizeOpTrain(OpTest):
481

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    def set_args(self):
        self.bit_length = 8
        self.data_type = "float32"
        self.quant_axis = 0
        self.is_test = False

    def setUp(self):
        self.set_args()
        self.op_type = "quantize_linear"
        x = np.random.randn(4, 3, 64, 64).astype(self.data_type)
        yq, scale = channel_wise_quantize_max_abs(x, self.bit_length,
                                                  self.quant_axis)
        scale = np.array(scale).astype(self.data_type)
        zero_point = np.zeros(scale.shape, dtype="int32")

        self.inputs = {'X': x, 'Scale': scale, 'ZeroPoint': zero_point}
        self.attrs = {
            'bit_length': self.bit_length,
            'quant_axis': self.quant_axis,
            'is_test': self.is_test
        }
        self.outputs = {'Y': yq, 'OutScale': scale}

    def test_check_output(self):
        self.check_output()


class TestquantizeOp(OpTest):
510

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    def set_args(self):
        self.bit_length = 8
        self.quant_axis = -1
        self.max_range = math.pow(2, self.bit_length - 1) - 1
        self.data_type = "float32"

    def setUp(self):
        self.set_args()
        self.op_type = "quantize_linear"
        x = np.random.randn(31, 65).astype(self.data_type)
        yq, scale = quantize_max_abs(x, self.max_range)
        scale = np.array(scale).astype(self.data_type)
        zero_point = np.zeros(scale.shape, dtype="int32")

        self.inputs = {'X': x, 'Scale': scale, 'ZeroPoint': zero_point}
        self.attrs = {
            'bit_length': self.bit_length,
            'quant_axis': self.quant_axis,
        }
        self.outputs = {'Y': yq}

    def test_check_output(self):
        self.check_output()


class TestquantizeOpTrain(TestquantizeOp):
537

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    def set_args(self):
        self.bit_length = 8
        self.quant_axis = -1
        self.max_range = math.pow(2, self.bit_length - 1) - 1
        self.data_type = "float32"
        self.is_test = False

    def setUp(self):
        self.set_args()
        self.op_type = "quantize_linear"
        x = np.random.randn(31, 65).astype(self.data_type)
        yq, scale = quantize_max_abs(x, self.max_range)
        scale = np.array(scale).astype(self.data_type)
        zero_point = np.zeros(scale.shape, dtype="int32")

        self.inputs = {'X': x, 'Scale': scale, 'ZeroPoint': zero_point}
        self.attrs = {
            'bit_length': self.bit_length,
            'quant_axis': self.quant_axis,
            'is_test': self.is_test
        }
        self.outputs = {'Y': yq, 'OutScale': scale}

    def test_check_output(self):
        self.check_output()


565
if __name__ == '__main__':
视言's avatar
视言 已提交
566
    unittest.main()