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 17from abc import ABC, abstractmethod 18from enum import Enum 19 20from ..._checkparam import Validator 21 22__all__ = ["OptimizeOption"] 23 24 25class OptimizeOption(Enum): 26 r""" 27 An enum for the model quantization optimize option, currently only support `QAT` and `LEARNED_SCALE`. 28 """ 29 # using quantization aware training 30 QAT = "QAT" 31 32 # using the learned scale quantization 33 LEARNED_SCALE = "LEARNED_SCALE" 34 35 def __str__(self): 36 return self.value 37 38 39class Quantizer(ABC): 40 """ 41 Base class of Quantizer. You can implement different kind of quantizer to get different quantization result. 42 43 Notes: 44 This class is an abstract class. 45 46 Args: 47 optimize_option (OptimizeOption, list or tuple): Specifies the quant algorithm and options. Default: 48 OptimizeOption.QAT. 49 """ 50 def __init__(self, 51 optimize_option=OptimizeOption.QAT): 52 if not isinstance(optimize_option, list) and not isinstance(optimize_option, tuple): 53 optimize_option = [optimize_option] 54 for option in optimize_option: 55 option = Validator.check_isinstance("optimize_option", option, OptimizeOption) 56 self.optimize_option = optimize_option 57 58 @abstractmethod 59 def quantize(self, network): 60 """ 61 Quant API to convert input network to a quantization aware training network 62 Args: 63 network (Cell): network to be quantized. 64 """ 65