Directly converting a model to INT8 involves using tools that support quantization-aware training or post-training quantization. For Intel Xeon processors, especially those with Intel® Deep Learning Boost (DL Boost), INT8 is indeed optimized and can significantly enhance performance due to the presence of Vector Neural Network Instructions (VNNI).
Steps to Convert a Model to INT8 Using OpenVINO
Assuming you have already converted your PyTorch model to ONNX, you can use OpenVINO's Post-Training Optimization Tool (POT) to quantize it directly to INT8.
Step 1: Prepare Your ONNX Model
Ensure your model is saved in the ONNX format. We'll use the POT tool to convert and optimize it directly to INT8.
Step 2: Create a Calibration Dataset
A calibration dataset is necessary for POT to perform quantization effectively while preserving model accuracy. Gather representative samples of your dataset that the model will encounter during inference.
Step 3: Configure the POT Tool
Create a configuration file for POT quantization. This file contains settings that describe your model and how the optimization should be carried out. Here's an example configuration named quantization_config.json:
{
"model": {
"model_name": "my_int8_model",
"model": "/path/to/your/model.onnx"
},
"engine": {
"type": "simplified",
"data_source": "/path/to/calibration/dataset",
"device": "CPU"
},
"compression": {
"algorithms": [
{
"name": "DefaultQuantization",
"params": {
"preset": "performance",
"target_device": "CPU"
}
}
]
}
}
engine.type: "simplified" is used for the basic approach without extra inference engine requirements.
data_source: Path to your calibration data.
preset: Either "performance" for an aggressive performance-oriented quantization or "mixed" to retain better accuracy.
Step 4: Run the POT to Quantize the Model
Now use the OpenVINO POT command to perform the quantization:
pot -c quantization_config.json -o /path/to/output/int8_model -d /path/to/quantized_model_directory
-c: Specifies your configuration file.
-o: The output directory for saving the quantized INT8 model.
Step 5: Verify INT8 Optimization
After quantization, verify if the model is using INT8 by inspecting the model’s layers or running inference and checking performance.
INT8 Optimization on Intel Xeon
Intel Xeon processors, especially those from the Cascade Lake family onwards, support VNNI instructions for INT8, providing significant acceleration for deep learning tasks. The OpenVINO toolkit is designed to leverage these instructions:
-
Performance Gains: Significant speed-up compared to FP32, particularly for large batch sizes or models that are compute-bound.
-
Utility of DL Boost: Ensures efficient handling of compact data through vector processing capabilities, optimizing tensor operations.
By following these steps, you can effectively convert and run your model directly in INT8 on Intel Xeon processors, maximizing performance benefits from OpenVINO optimizations.