You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

quantizer.py 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """Base Class of Quantizer."""
  16. from abc import ABC, abstractmethod
  17. from enum import Enum
  18. from ..._checkparam import Validator
  19. __all__ = ["OptimizeOption"]
  20. class OptimizeOption(Enum):
  21. r"""
  22. An enum for the model quantization optimize option, currently only support `QAT`.
  23. """
  24. # using quantization aware training
  25. QAT = "QAT"
  26. # using the learned scale quantization
  27. LEARNED_SCALE = "LEARNED_SCALE"
  28. def __str__(self):
  29. return self.value
  30. class Quantizer(ABC):
  31. """
  32. Base class of Quantizer. You can implement different kind of quantizer to get different quantization result.
  33. Notes:
  34. This class is an abstract class.
  35. Args:
  36. optimize_option (OptimizeOption, list or tuple): Specifies the quant algorithm and options. Default:
  37. OptimizeOption.QAT.
  38. """
  39. def __init__(self,
  40. optimize_option=OptimizeOption.QAT):
  41. if not isinstance(optimize_option, list) and not isinstance(optimize_option, tuple):
  42. optimize_option = [optimize_option]
  43. for option in optimize_option:
  44. option = Validator.check_isinstance("optimize_option", option, OptimizeOption)
  45. self.optimize_option = optimize_option
  46. @abstractmethod
  47. def quantize(self, network):
  48. pass