slim_quant.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import paddle
  2. import numpy as np
  3. import os
  4. import paddle.nn as nn
  5. import paddleslim
  6. class PACT(paddle.nn.Layer):
  7. def __init__(self):
  8. super(PACT, self).__init__()
  9. alpha_attr = paddle.ParamAttr(
  10. name=self.full_name() + ".pact",
  11. initializer=paddle.nn.initializer.Constant(value=20),
  12. learning_rate=1.0,
  13. regularizer=paddle.regularizer.L2Decay(2e-5))
  14. self.alpha = self.create_parameter(
  15. shape=[1], attr=alpha_attr, dtype='float32')
  16. def forward(self, x):
  17. out_left = paddle.nn.functional.relu(x - self.alpha)
  18. out_right = paddle.nn.functional.relu(-self.alpha - x)
  19. x = x - out_left + out_right
  20. return x
  21. quant_config = {
  22. # weight preprocess type, default is None and no preprocessing is performed.
  23. 'weight_preprocess_type': None,
  24. # activation preprocess type, default is None and no preprocessing is performed.
  25. 'activation_preprocess_type': None,
  26. # weight quantize type, default is 'channel_wise_abs_max'
  27. 'weight_quantize_type': 'channel_wise_abs_max',
  28. # activation quantize type, default is 'moving_average_abs_max'
  29. 'activation_quantize_type': 'moving_average_abs_max',
  30. # weight quantize bit num, default is 8
  31. 'weight_bits': 8,
  32. # activation quantize bit num, default is 8
  33. 'activation_bits': 8,
  34. # data type after quantization, such as 'uint8', 'int8', etc. default is 'int8'
  35. 'dtype': 'int8',
  36. # window size for 'range_abs_max' quantization. default is 10000
  37. 'window_size': 10000,
  38. # The decay coefficient of moving average, default is 0.9
  39. 'moving_rate': 0.9,
  40. # for dygraph quantization, layers of type in quantizable_layer_type will be quantized
  41. 'quantizable_layer_type': ['Conv2D', 'Linear'],
  42. }