Pytorch Version - 1.4 English

Vitis AI User Guide (UG1414)

Document ID
Release Date
1.4 English

vai_q_pytorch is designed to work as a Pytorch plugin. Xilinx provides the simplest APIs to introduce the FPGA-friendly quantization feature. vai_q_pytorch package is already installed in docker image "vitis-ai-pytorch" environment. A Resnet18 example is in the open-source repo. Use the following steps to quantize a Resnet18 model.

  1. Prepare a floating-point model for Resnet-18. You can download one from Pytorch official site.
    wget -O resnet18.pth
  2. Prepare the calibration dataset used by vai_q_pytorch. You can download 100 to 1000 images of ImageNet dataset from or
  3. Copy float model resnet18.pth, example code, and calibration images to docker image and modify default data_dir and model_dir in accordingly.
  4. Activate the Pytorch running environment:
    conda activate vitis-ai-pytorch
  5. Evaluate float model
    python --quant_mode 0
  6. Quantize, using a subset (200 images) of validation data for calibration. Because it is in quantize calibration process, the displayed loss and accuracy are meaningless.
    python --quant_mode 1 --subset_len 200
  7. Evaluate quantized model and generate xmodel file for compiler.
    python --quant_mode 2
    A ResNet_int.xmodelfile will be generated under folder quantize_result. It could be fed to VAI compiler for following compilation process.