class paddle.quantization. PTQ ( config: paddle.quantization.config.QuantConfig ) [source]

Applying post training quantization to the model.

quantize ( model: paddle.nn.layer.layers.Layer, inplace=False ) [source]


Create a model for post-training quantization.

The quantization configuration will be propagated in the model. And it will insert observers into the model to collect and compute quantization parameters.

  • model (Layer) –

  • inplace (bool) –

Return: The prepared model for post-training quantization.

Examples: .. code-block:: python

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/quantization/ of paddle.quantization.ptq.PTQ.quantize, line 16)

Unexpected indentation.

from paddle.quantization import PTQ, QuantConfig from paddle.quantization.observers import AbsmaxObserver from import LeNet

observer = AbsmaxObserver() q_config = QuantConfig(activation=observer, weight=observer) ptq = PTQ(q_config) model = LeNet() model.eval() quant_model = ptq.quantize(model) print(quant_model)

convert ( model: paddle.nn.layer.layers.Layer, inplace=False )


Convert the quantization model to onnx style. And the converted model can be saved as inference model by calling :param model: :type model: Layer :param inplace: :type inplace: bool

Return: The converted model

Examples: .. code-block:: python

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/quantization/ of paddle.quantization.quantize.Quantization.convert, line 12)

Unexpected indentation.

import paddle from paddle.quantization import QAT, QuantConfig from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver from import LeNet

quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) q_config = QuantConfig(activation=quanter, weight=quanter) qat = QAT(q_config) model = LeNet() quantized_model = qat.quantize(model) converted_model = qat.convert(quantized_model) dummy_data = paddle.rand([1, 1, 32, 32], dtype=”float32”), “./quant_deploy”, [dummy_data])