quant.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from paddle.utils import try_import
  18. from paddlex.ppdet.core.workspace import register, serializable
  19. from paddlex.ppdet.utils.logger import setup_logger
  20. logger = setup_logger(__name__)
  21. @register
  22. @serializable
  23. class QAT(object):
  24. def __init__(self, quant_config, print_model):
  25. super(QAT, self).__init__()
  26. self.quant_config = quant_config
  27. self.print_model = print_model
  28. def __call__(self, model):
  29. paddleslim = try_import('paddleslim')
  30. self.quanter = paddleslim.dygraph.quant.QAT(config=self.quant_config)
  31. if self.print_model:
  32. logger.info("Model before quant:")
  33. logger.info(model)
  34. self.quanter.quantize(model)
  35. if self.print_model:
  36. logger.info("Quantized model:")
  37. logger.info(model)
  38. return model
  39. def save_quantized_model(self, layer, path, input_spec=None, **config):
  40. self.quanter.save_quantized_model(
  41. model=layer, path=path, input_spec=input_spec, **config)