Return a quantized Keras model according to the given configuration. Args: model (keras.Model): FP32 Keras model to be quantized. quant_config (BaseConfig): Quantization configuration. calib_function (Callable, optional): Function used for model calibration, required for
(
model: keras.Model,
quant_config: BaseConfig,
calib_function: Callable = None,
inplace: bool = True
)
| 41 | # fmt: off |
| 42 | @log_process(mode=Mode.QUANTIZE) |
| 43 | def quantize_model( |
| 44 | model: keras.Model, |
| 45 | quant_config: BaseConfig, |
| 46 | calib_function: Callable = None, |
| 47 | inplace: bool = True |
| 48 | ): |
| 49 | """Return a quantized Keras model according to the given configuration. |
| 50 | |
| 51 | Args: |
| 52 | model (keras.Model): FP32 Keras model to be quantized. |
| 53 | quant_config (BaseConfig): Quantization configuration. |
| 54 | calib_function (Callable, optional): Function used for model calibration, required for static quantization. |
| 55 | inplace (bool): When True, the original model is modified in-place and should not be used afterward. False creates a copy of original model |
| 56 | |
| 57 | Returns: |
| 58 | keras.Model: The quantized model. |
| 59 | """ |
| 60 | # fmt: on |
| 61 | check_backend() |
| 62 | if not inplace: |
| 63 | model = clone_model(model) |
| 64 | |
| 65 | model_info = quant_config.get_model_info(model) |
| 66 | configs_mapping = quant_config.to_config_mapping(model_info=model_info) |
| 67 | for algo_name, algo_func in algos_mapping.items(): |
| 68 | if need_apply(configs_mapping, algo_name): |
| 69 | logger.info(f"Start to apply {algo_name} on the model.") |
| 70 | model = algo_func(model, configs_mapping, quant_config, calib_function) |
| 71 | return model |
searching dependent graphs…