Professional Documents
Culture Documents
Abstract
This lab illustrates the Vitis™ AI quantizer and Vitis AI compiler flow for the TensorFlow2 and
PyTorch frameworks. You will explore the required options for these tools and then run the
tools.
This lab should take approximately 45-60 minutes.
CloudShare users: You may face some delays in running this lab depending on your region. This
lab is supported only from the Amsterdam region—other regions are not supported.
Objectives
After completing this lab, you will be able to:
• Describe the AI quantizer flow and AI compiler flow for the TensorFlow2 and PyTorch
frameworks
• Quantize a pre-trained model
• Compile the quantized model for the DPUCZDX8G architecture
• Verify the files generated after quantization and compilation for the TensorFlow 2 and
PyTorch model networks
Introduction
The Vitis AI toolchain provides an innovative workflow to deploy deep learning inference
applications on the DPU with the following four steps:
• Quantize the neural network model
• Compile the neural network model
• Program with the Vitis AI programming interface
• Run and evaluate the deployed DPU application
www.amd.com 1
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
Compiling
The Vitis AI compiler provides a unified interface to all the necessary tools for developing DPUs.
Each compiler maps a network model into a highly optimized DPU instruction sequence.
The XIR-based compiler takes the quantized TensorFlow or PyTorch model as the input. The
TensorFlow model is transformed to XIR format by xcompiler. However, for PyTorch, the
quantizer will output an XIR format xmodel.
First, it transforms the input models into the XIR format as the foundation of the following
processes. Most of the variations among different frameworks are eliminated and transferred to
a unified representation in XIR.
The compiler then applies various optimizations to the graph and breaks up the graph into
several subgraphs based on whether the operation can be executed on the DPU. More
2 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
architecture-aware optimizations are applied for each subgraph, as required. For the DPU
subgraph, the compiler generates the instruction stream and attaches to it.
Finally, the optimized graph with the necessary information and instructions for VART is
serialized into a compiled xmodel file.
You can find the arch.json files for those platforms in /opt/vitis_ai/compiler/arch.
You will focus on the AI quantizer and AI compiler for TensorFlow2 and PyTorch in this lab.
Understanding the Lab Environment
The labs and demos provided in this course are designed to run on a Linux platform.
One environment variable is required: TRAINING_PATH, which points to where the lab files are
located. This variable comes configured in the CloudShare/CustEd_VM environments.
Some tools can use this environment variable directly (that is, $TRAINING_PATH will be
expanded), and some tools require manual expansion (/home/xilinx/training for the
CloudShare/CustEd_VM environments). The lab instructions describe what to do for each tool.
Both the Vivado Design Suite and the Vitis platform offer a Tcl environment that is used by
many labs. When the tool is launched, it starts with a clean Tcl environment with none of the
procs or variables remaining from a previous launch of the tools.
If you sourced a Tcl script or manually set any Tcl variables and you closed the tool, when you
reopen the tool, you will need to re-source the Tcl script and set any variables that the lab
requires. This is also true of terminal windows—any variable settings will be cleared when a new
terminal opens.
www.amd.com 3
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
Nomenclature
Formal nomenclature is used to explain how different arguments are used. The following are
some of the more commonly used symbols:
General Flow
Step 1: Step 2: Step 3: Step 4:
Reviewing Running Reviewing Running
the TF2 the TF2 the PyTorch the PyTorch
Arguments & Quantizer & Arguments & Quantizer &
Scripts Compiler Scripts Compiler
4 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
www.amd.com 5
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
1-1-1. Click the Terminal icon( ) in the toolbar or press <Ctrl + Alt + T> to open a Linux
terminal window.
1-1-2. Enter the following command to copy the files to the specified directory:
[host]$ cp -rf $TRAINING_PATH/vai_q_c_tf2_pt /home/xilinx/Vitis-AI/
vai_q_c_tf2_pt
Hint: If you are using the CustEd VM or CloudShare, the tilde symbol ('~') represents
/home/xilinx. You can use this as a shortcut to reduce your typing.
You will be using the following scripts and text file for running the TF2 quantizer and
compiler.
o 1_tf2_quantize.sh
▪ This script calls the vitis-ai quantizer for TensorFlow2.
o 4_tf2_compile_for_mpsoc.sh
▪ This script calls the vitis-ai compiler for TensorFlow2.
o val.txt
▪ This is the evaluation image list file used during quantization.
6 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
label_offset Set to 1
1-2-3. After completing your review, press <Ctrl + Q> to close the editor.
Training set ImageNet training Dataset used for training the model
Validation set ImageNet validation Dataset used for verifying the model
Images and labels from ImageNet have been downloaded and placed in the
vai_q_c_tf2_pt/images directory so that calibration and evaluation during the
quantization phase can be conducted.
www.amd.com 7
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
8 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
2-1. [All users other than CloudShare] Verify if your machine supports the avx
flag.
2-1-1. [Optional] Press <Ctrl + Alt + T> to open a Linux terminal window.
2-1-2. Enter the following command (1):
[host] # lscpu
Alternatively, you can enter:
lscpu | grep -c "avx"
This counts the number of times avx is exactly matched in the output of the lscpu
command. As long as it returns at least one match, then you can continue.
2-1-3. Locate the Flags field at the end of the report (2).
If you see avx listed among the flags, then your processor supports TensorFlow2 (3).
Note: If your machine does NOT support this flag, you will see an "Illegal
instruction (core dumped)" message.
If your machine cannot run TensorFlow2, skip to the "Reviewing the PyTorch AI Quantizer
Arguments and Training Scripts" step, where you can continue with PyTorch.
2-1-4. If you opened a new terminal window to run this test, enter the following to close this
terminal window:
exit
www.amd.com 9
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
Note that this approach specifically loads Vitis AI environment 2.5. This lab is tested against
this version and *MAY* work with other versions. When developing your own projects, you
can use the latest tag to pull the most recent version of the tools.
Supported tags can be found at
https://hub.docker.com/r/xilinx/vitis-ai-cpu/tags?page=1&ordering=last_updated.
For example: [host]$ ./docker_run.sh xilinx/vitis-ai-cpu:<Docker_tag>
Selecting the latest version: [host]$ ./docker_run.sh
xilinx/vitis-ai-cpu:latest
Note: Accept the terms and agreements by pressing any key until you are told to
specifically click the 'y' key to accept the final condition.
It is worthwhile noting that the environment uses its own directory hierarchy, which is
now /workspace.
10 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
Note: From this point forward, the rather lengthy prompt will be shown as [VAI]>.
Note: You may see warnings about not being able to load dynamic libraries. These
messages relate to CUDA drivers that are used in GPU implementations and can be
safely ignored here.
After successful execution of the quantizer script, the quantized file quantized.h5 is
generated in the output directory
(.../tf2_resnet50_imagenet_224_224_7.76G_2.5/vai_q_output).
For TensorFlow2.x, the quantizer generates the quantized model in the h5 format.
www.amd.com 11
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
2-4-3. After completing your review, press <Ctrl + Q> to close the editor.
2-4-4. [Optional] Enter exit in the terminal window to close the terminal.
12 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
You should see the following output after compilation has completed.
www.amd.com 13
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
14 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
o The argument list is parsed, and the necessary information is extracted in the lines
following the call to argparse.ArgumentParser():
▪ --data_dir: Dataset directory, when quant_mode=calib, it is for calibration,
and when quant_mode=test, it is for evaluation.
▪ --model_dir: Trained model file path.
▪ --subset_len: subset_len to evaluate the model, using the whole
validation dataset if it is not set.
▪ --quant_mode: Quantization mode {float, calib, test}:
➢ float: No quantization, evaluate float model.
➢ calib: Quantize.
➢ test: Evaluate the quantized model.
▪ --finetune: Finetune the model before calibration.
o The main() program starts at line no. 293:
▪ At near line no. 295, the model name is set to resnet50.
▪ At near line no. 299, verify the quant_mode argument that is passed as calib in
the 5_pytorch_quantize.sh script.
▪ At near line no. 311, the function quantization has been called with the
following arguments:
➢ title: resnet50 with optimization
➢ model_name: resnet50
➢ file_path: pt_resnet50.pth
➢ quant_mode: calib
➢ finetune: Set to default
3-2-2. After completing your review, press <Ctrl + Q> to close the editor.
www.amd.com 15
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
Note that this approach specifically loads Vitis AI environment 2.5. This lab is tested against
this version and *MAY* work with other versions. When developing your own projects, you
can use the latest tag to pull the most recent version of the tools.
Supported tags can be found at
https://hub.docker.com/r/xilinx/vitis-ai-cpu/tags?page=1&ordering=last_updated.
For example: [host]$ ./docker_run.sh xilinx/vitis-ai-cpu:<Docker_tag>
Selecting the latest version: [host]$ ./docker_run.sh
xilinx/vitis-ai-cpu:latest
Note: Accept the terms and agreements by pressing any key until you are told to
specifically click the 'y' key to accept the final condition.
It is worthwhile noting that the environment uses its own directory hierarchy, which is
now /workspace.
16 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
www.amd.com 17
© Copyright 2022 Advanced Micro Devices, Inc.
错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。 Lab Workbook
4-3-3. After completing your review, press <Ctrl + Q> to close the editor.
4-3-4. [Optional] Enter exit in the terminal window to close the terminal.
18 www.amd.com
© Copyright 2022 Advanced Micro Devices, Inc.
Lab Workbook 错误!使用“开始”选项卡将 Heading 1 应用于要在此处显示的文字。
Summary
You just completed exploring the process of quantizing, compiling, and verifying TensorFlow2
and PyTorch models. These are mandatory steps in the development process of deploying
trained models.
Other labs will continue the development and deployment process.
www.amd.com 19
© Copyright 2022 Advanced Micro Devices, Inc.