ONNX model quantization algorithm#
- quark.onnx.algorithm.interface.apply_CLE(float_model: ModelProto, op_types_to_quantize: list[str] | None = None, nodes_to_quantize: list[str] = [], nodes_to_exclude: list[str] = [], extra_options: dict[str, Any] = {}) ModelProto[source]#
This algorithm is referenced from the cross-layer equalization proposed in the following paper: “Markus Nagel et al., Data-Free Quantization Through Weight Equalization and Bias Correction, arXiv:1906.04721, 2019.”
- Parameters:
float_model (onnx.ModelProto) – ONNX model for the transform.
op_types_to_quantize (Optional[List[str]]) – Specify the types of operators to quantize. It quantizes all supported operators by default.
nodes_to_quantize (List[str]) – List of nodes names to quantize. When this list is not
Noneand empty only the nodes in this list.nodes_to_exclude (List[str]) – List of nodes names to exclude. The nodes in this list will be excluded from quantization when it is not
Noneand empty.extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The transformed model.
- quark.onnx.algorithm.interface.apply_SmoothQuant(float_model: ModelProto, data_reader: CalibrationDataReader, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}) ModelProto[source]#
This algorithm is referenced from the Smooth Quant proposed in the following paper: “Guangxuan Xiao et al., SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language Models, arXiv:2211.10438, 2022.”
- Parameters:
float_model (onnx.ModelProto) – ONNX model for the transform.
data_reader (CalibrationDataReader) – Data reader for the algorithm.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The transformed model.
- quark.onnx.algorithm.interface.apply_QuaRot(float_model: ModelProto, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}) ModelProto[source]#
This algorithm is referenced from the QuaRot proposed in the following paper: “Saleh Ashkboos et al., QuaRot: Outlier-Free 4-Bit Inference in Rotated LLMs, arXiv:2404.00456, 2024.”
- Parameters:
float_model (onnx.ModelProto) – ONNX model for the transform.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The transformed model.
- quark.onnx.algorithm.interface.apply_pre_quant_algorithms(float_model: ModelProto, data_reader: CalibrationDataReader | None = None, op_types_to_quantize: list[str] | None = None, nodes_to_quantize: list[str] = [], nodes_to_exclude: list[str] = [], use_external_data_format: bool = False, extra_options: dict[str, Any] | None = {}, algorithm: str = 'CLE') ModelProto[source]#
This is an interface function used for applying pre-quant algorithms on the float model.
- Parameters:
float_model (onnx.ModelProto) – ONNX model for the transform.
data_reader (CalibrationDataReader) – Data reader for some algorithms.
op_types_to_quantize (Optional[List[str]]) – Specify the types of operators to quantize. It quantizes all supported operators by default.
nodes_to_quantize (List[str]) – List of nodes names to quantize. When this list is not
Noneand empty only the nodes in this list.nodes_to_exclude (List[str]) – List of nodes names to exclude. The nodes in this list will be excluded from quantization when it is not
Noneand empty.use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Extra options for quantization, which contains additional options for the algorithms.
algorithm (str) – The algorithm to apply, the valid options are ‘CLE’, ‘SmoothQuant’ and ‘QuaRot’.
- Returns:
The transformed model.
- quark.onnx.algorithm.interface.apply_BiasCorrection(float_model: ModelProto, quant_model: ModelProto, data_reader: CalibrationDataReader, calibrate_method: CalibrationMethod = CalibrationMethod.MinMax, activation_type: QuantType = QuantType.QInt8, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}) ModelProto[source]#
This algorithm is referenced from the bias correction proposed in the following paper: “Markus Nagel et al., Data-Free Quantization Through Weight Equalization and Bias Correction, arXiv:1906.04721, 2019.”
Based on the original algorithm, we have made improvements to deliver better accuracy and applicability.
- Parameters:
float_model (onnx.ModelProto) – The float model for reference.
quant_model (onnx.ModelProto) – The quantized model to be optimized.
data_reader (CalibrationDataReader) – Data reader for the algorithm.
calibrate_method (CalibrationMethod) – Calibration method, the default is CalibrationMethod.MinMax.
activation_type (QuantType) – The quantization type to mix in activation tensors.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The quantized model that has been optimized.
- quark.onnx.algorithm.interface.apply_AutoMixedPrecision(float_model: ModelProto, quant_model: ModelProto, data_reader: CalibrationDataReader, tensors_range: TensorsData, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}) ModelProto[source]#
This function applies auto mixed precision to balance efficiency and performance.
- Parameters:
float_model (onnx.ModelProto) – The float model for reference.
quant_model (onnx.ModelProto) – The quantized model to be optimized.
data_reader (CalibrationDataReader) – Data reader for the algorithm.
tensors_range (TensorsData) – Tensors range for the algorithm.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The quantized model that has been optimized.
- quark.onnx.algorithm.interface.apply_FastFinetune(float_model: str | Path | ModelProto, quant_model: str | Path | ModelProto, data_reader: CalibrationDataReader, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}) ModelProto[source]#
This function applies two widely used PTQ algorithms, AdaRound and AdaQuant.
The AdaRound is referenced from the following paper: “Markus Nagel et al., Up or Down? Adaptive Rounding for Post-Training Quantization, arXiv:2004.10568, 2020.”
The AdaQuant is referenced from the following paper: “Itay Hubara et al., Improving Post Training Neural Quantization: Layer-wise Calibration and Integer Programming, arXiv:2006.10518, 2020.”
Based on the original algorithms, we have made improvements to deliver better accuracy and applicability.
- Parameters:
float_model (str | Path | onnx.ModelProto) – The float model for reference.
quant_model (str | Path | onnx.ModelProto) – The quantized model to be optimized.
data_reader (CalibrationDataReader) – Data reader for the algorithm.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The quantized model that has been optimized.
- quark.onnx.algorithm.interface.apply_GPTQ(float_model: ModelProto, quant_model: ModelProto, data_reader: CalibrationDataReader, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}) ModelProto[source]#
This algorithm is referenced from the GPTQ proposed in the following paper: “Elias Frantar et al., GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers, arXiv:2210.17323, 2022.”
- Parameters:
float_model (onnx.ModelProto) – The float model for reference.
quant_model (onnx.ModelProto) – The quantized model to be optimized.
data_reader (CalibrationDataReader) – Data reader for the algorithm.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
- Returns:
The quantized model that has been optimized.
- quark.onnx.algorithm.interface.apply_post_quant_algorithms(float_model: ModelProto, quant_model: ModelProto, data_reader: CalibrationDataReader, tensors_range: TensorsData | None = None, calibrate_method: CalibrationMethod = CalibrationMethod.MinMax, activation_type: QuantType = QuantType.QInt8, weight_type: QuantType = QuantType.QInt8, use_external_data_format: bool = False, extra_options: dict[str, Any] = {}, algorithm: str = 'FastFinetune') ModelProto[source]#
This is an interface function used for applying post-quant algorithms on the quantized model.
- Parameters:
float_model (onnx.ModelProto) – The float model for reference.
quant_model (onnx.ModelProto) – The quantized model to be optimized.
data_reader (CalibrationDataReader) – Data reader for the algorithm.
tensors_range (TensorsData) – Tensors range for the algorithm.
calibrate_method (CalibrationMethod) – Calibration method, the default is CalibrationMethod.MinMax.
activation_type (QuantType) – The quantization type to mix in activation tensors.
weight_type (QuantType) – The quantization type to mix in weight tensors.
use_external_data_format (bool) – Option used for large size (>2GB) model.
extra_options (Dict[str, Any]) – Options for the algorithm.
algorithm (str) – The algorithm to apply, the valid options are ‘BiasCorrection’, ‘AutoMixprecision’, ‘FastFinetune’ and ‘GPTQ’.
- Returns:
The quantized model that has been optimized.