Quantizes a model using NNCF quantize_pt2e API. :param captured_model: The model to be quantized, represented as a torch.fx.GraphModule. :param calibration_dataset: A DataLoader containing calibration data for quantization. :param mode: Defines special quantization modes. -
(
captured_model: torch.fx.GraphModule,
calibration_dataset: torch.utils.data.DataLoader,
*,
mode: QuantizationMode = QuantizationMode.INT8_SYM,
subset_size: int = 300,
fast_bias_correction: Optional[bool] = True,
smooth_quant: bool = False,
transform_fn: Optional[Callable[[Any], Any]] = None,
extra_quantizer_options: Optional[Dict[str, Any]] = None,
**kwargs,
)
| 617 | |
| 618 | |
| 619 | def quantize_model( |
| 620 | captured_model: torch.fx.GraphModule, |
| 621 | calibration_dataset: torch.utils.data.DataLoader, |
| 622 | *, |
| 623 | mode: QuantizationMode = QuantizationMode.INT8_SYM, |
| 624 | subset_size: int = 300, |
| 625 | fast_bias_correction: Optional[bool] = True, |
| 626 | smooth_quant: bool = False, |
| 627 | transform_fn: Optional[Callable[[Any], Any]] = None, |
| 628 | extra_quantizer_options: Optional[Dict[str, Any]] = None, |
| 629 | **kwargs, |
| 630 | ) -> torch.fx.GraphModule: |
| 631 | """ |
| 632 | Quantizes a model using NNCF quantize_pt2e API. |
| 633 | |
| 634 | :param captured_model: The model to be quantized, represented as a torch.fx.GraphModule. |
| 635 | :param calibration_dataset: A DataLoader containing calibration data for quantization. |
| 636 | :param mode: Defines special quantization modes. |
| 637 | - INT8_SYM: INT8 symmetric quantization for both activations and weights. |
| 638 | - INT8_MIXED: INT8 asymmetric quantization for activations, symmetric for weights. |
| 639 | - INT8_TRANSFORMER: Optimized INT8 quantization for transformer-based models |
| 640 | Default value is INT8_SYM. |
| 641 | :param subset_size: Size of a subset to calculate activations |
| 642 | statistics used for quantization. |
| 643 | :param fast_bias_correction: Setting this option to `False` enables a different |
| 644 | bias correction method which is more accurate, in general, and takes |
| 645 | more time but requires less memory. None disables the bias correction algorithm. |
| 646 | :param smooth_quant: Setting this option to `True` enables the SmoothQuant algorithm. |
| 647 | :param extra_quantizer_options: A dictionary containing additional configuration options |
| 648 | for the OpenVINOQuantizer. |
| 649 | :param kwargs: The keyword arguments for the nncf quantize_pt2e function. |
| 650 | :return: The quantized model as a torch.fx.GraphModule. |
| 651 | """ |
| 652 | extra_quantizer_options = extra_quantizer_options or {} |
| 653 | if "mode" in extra_quantizer_options: |
| 654 | print( |
| 655 | f'Ignoring "mode" from the quantizer_config. Using parameter mode = {mode}' |
| 656 | ) |
| 657 | del extra_quantizer_options["mode"] |
| 658 | |
| 659 | quantizer = OpenVINOQuantizer(mode=mode, **extra_quantizer_options) |
| 660 | |
| 661 | print("PTQ: Quantize the model") |
| 662 | |
| 663 | if "fold_quantize" not in kwargs: |
| 664 | kwargs["fold_quantize"] = False |
| 665 | |
| 666 | quantized_model = nncf_fx.quantize_pt2e( |
| 667 | captured_model, |
| 668 | quantizer, |
| 669 | subset_size=subset_size, |
| 670 | calibration_dataset=nncf.Dataset(calibration_dataset, transform_fn), |
| 671 | fast_bias_correction=fast_bias_correction, |
| 672 | smooth_quant=smooth_quant, |
| 673 | **kwargs, |
| 674 | ) |
| 675 | return quantized_model |
no test coverage detected