r"""Quantize the input float model with post training static quantization. First it will prepare the model for calibration, then it calls `run_fn` which will run the calibration step, after that we will convert the model to a quantized model. Args: model: input float model
(model, run_fn, run_args, mapping=None, inplace=False)
| 356 | _remove_activation_post_process(module) |
| 357 | |
| 358 | def quantize(model, run_fn, run_args, mapping=None, inplace=False): |
| 359 | r"""Quantize the input float model with post training static quantization. |
| 360 | |
| 361 | First it will prepare the model for calibration, then it calls |
| 362 | `run_fn` which will run the calibration step, after that we will |
| 363 | convert the model to a quantized model. |
| 364 | |
| 365 | Args: |
| 366 | model: input float model |
| 367 | run_fn: a calibration function for calibrating the prepared model |
| 368 | run_args: positional arguments for `run_fn` |
| 369 | inplace: carry out model transformations in-place, the original module is mutated |
| 370 | mapping: correspondence between original module types and quantized counterparts |
| 371 | |
| 372 | Return: |
| 373 | Quantized model. |
| 374 | """ |
| 375 | torch._C._log_api_usage_once("quantization_api.quantize.quantize") |
| 376 | if mapping is None: |
| 377 | mapping = get_default_static_quant_module_mappings() |
| 378 | if not inplace: |
| 379 | model = copy.deepcopy(model) |
| 380 | model.eval() |
| 381 | prepare(model, inplace=True) |
| 382 | run_fn(model, *run_args) |
| 383 | convert(model, mapping, inplace=True) |
| 384 | return model |
| 385 | |
| 386 | def quantize_dynamic(model, qconfig_spec=None, dtype=torch.qint8, |
| 387 | mapping=None, inplace=False): |
searching dependent graphs…