Convert the prepared model to a quantized model. Args: model (torch.nn.Module): torch model quant_config (BaseConfig, optional): path to quantization config, only required when model is not prepared. inplace (bool, optional): It will change the given model in-place if Tr
(
model: torch.nn.Module,
quant_config: BaseConfig = None,
inplace: bool = True,
**kwargs,
)
| 251 | |
| 252 | @log_process(mode=Mode.CONVERT) |
| 253 | def convert( |
| 254 | model: torch.nn.Module, |
| 255 | quant_config: BaseConfig = None, |
| 256 | inplace: bool = True, |
| 257 | **kwargs, |
| 258 | ): |
| 259 | """Convert the prepared model to a quantized model. |
| 260 | |
| 261 | Args: |
| 262 | model (torch.nn.Module): torch model |
| 263 | quant_config (BaseConfig, optional): path to quantization config, only required when model is not prepared. |
| 264 | inplace (bool, optional): It will change the given model in-place if True. |
| 265 | |
| 266 | Returns: |
| 267 | The quantized model. |
| 268 | """ |
| 269 | if isinstance(model, _AutoRoundModelReference): |
| 270 | if quant_config is None: |
| 271 | quant_config = model.quant_config |
| 272 | else: |
| 273 | logger.warning("quant_config will be ignored since the model has been prepared.") |
| 274 | model = model.model_reference |
| 275 | |
| 276 | q_model = model if inplace else copy.deepcopy(model) |
| 277 | |
| 278 | assert ( |
| 279 | getattr(model, "is_prepared", False) or quant_config is not None |
| 280 | ), "Please pass quant_config to convert function." |
| 281 | |
| 282 | if getattr(model, "is_prepared", False): |
| 283 | if quant_config is None: |
| 284 | quant_config = model.quant_config |
| 285 | else: |
| 286 | logger.warning("quant_config will be ignored since the model has been prepared.") |
| 287 | example_inputs = model.example_inputs if getattr(model, "is_prepared", False) else None |
| 288 | |
| 289 | registered_configs = config_registry.get_cls_configs() |
| 290 | if isinstance(quant_config, dict): |
| 291 | quant_config = ComposableConfig.from_dict(quant_config, config_registry=registered_configs[FRAMEWORK_NAME]) |
| 292 | logger.info(f"Parsed a config dict to construct the quantization config: {quant_config}.") |
| 293 | else: |
| 294 | assert isinstance( |
| 295 | quant_config, BaseConfig |
| 296 | ), f"Please pass a dict or config instance as the quantization configuration, but got {type(quant_config)}." |
| 297 | if hasattr(model, "dpq_quantized") and model.dpq_quantized and type(quant_config) == FP8Config: |
| 298 | quant_config = HybridGPTQConfig.convert_from_fp8(quant_config) |
| 299 | logger.debug("Convert model with config:") |
| 300 | logger.debug(quant_config.to_dict()) |
| 301 | |
| 302 | # select quantization algo according to config |
| 303 | if is_ipex_available and ( |
| 304 | isinstance(quant_config, INT8StaticQuantConfig) or isinstance(quant_config, SmoothQuantConfig) |
| 305 | ): |
| 306 | model_info = quant_config.get_model_info(q_model, example_inputs) |
| 307 | else: |
| 308 | model_info = quant_config.get_model_info(model=q_model) |
| 309 | configs_mapping = quant_config.to_config_mapping(model_info=model_info) |
| 310 | logger.debug(configs_mapping) |
searching dependent graphs…