部署 - 3.5 简体中文

Vitis AI 用户指南 (UG1414)

Document ID
UG1414
Release Date
2023-09-28
Version
3.5 简体中文

本节描述了如何在 graph_runner API 中部署具有自定义运算符的 PyTorch 模型。graph_runner API支持 C++ 和 Python。您可参考 Vitis-AI/examples/vai_library/samples/graph_runner 中的 graph_runner 样本:

  1. 创建一个新目录来存放测试代码。
  2. 为此样本创建源文件并实现以下功能:
    1. 参数解析和初始化
    2. 预处理
    3. 模型运行
    4. 后处理
    此样本的基本流程如下图所示。
    图 1. 此样本的基本流程
  3. 构建程序。
    1. 如果您的工程比较简单,其中包含一个或若干个 .cpp 源文件,那么您可轻松设置构建进程,只需从 Vitis-AI/examples/vai_library/samples/graph_runner 复制任意现有的 build.sh 脚本,并根据具体需求对其进行调整即可。自定义 build.sh 脚本后,请执行以下命令来构建程序:
      cd <your sample folder>
      bash build.sh
      下图显示了 resnet50_graph_runner 样本的 build.sh。
    2. 如果工程比较复杂,例如该样本,那么使用 CMakeLists.txt 来轻松完成编译更好。如需了解有关 CMakeLists.txt 的更多详情,请参阅 Vitis-AI/examples/custom_operator/pytorch_example/deployment/cpp/pointpillars_graph_runner/CMakeLists.txt
      然后,运行以下命令以构建程序:
      cd <your sample folder>
      mkdir build
      cd build
      cmake ..
      make

    编译成功后,<your sample folder> 下将生成可执行程序 sample_pointpillars_graph_runner

  4. 测试该程序。
    测试程序前,请将 XMODEL、测试图像、自定义 OP 库 libvart_op_imp_PPScatterV2.sosample_pointpillars_graph_runner 可执行程序复制到开发板上。将自定义运算符库置于 /usr/lib 下。然后,运行以下命令以进行测试:
    ./sample_pointpillars_graph_runner ./pointpillars_full_customer_op.xmodel sample_pointpillars.bin

    此样本的运行结果快照如下所示:

    root@xilinx-zcu102-2021_2:~/pointpillars_graph_runner# ./sample_pointpillars_graph_runner pointpillars_op.xmodel sample_pointpillars.bin
    WARNING: Logging before InitGoogleLogging() is written to STDERR
    W1202 05:59:20.517452 1307 tool_function.cpp:177] [UNILOG][WARNING] The operator named VoxelNet__VoxelNet_input_4, type: PPScatterV2, is not defined in XIR. XIR creates the definition of this operator automatically. You should specify the shape and the data_type of the output tensor of this operation by set_attr("shape", std::vector) and set_attr("data_type", std::string)
    result: 0
    0 18.541065 3.999999 -1.732742 1.703191 4.419279 1.465484 1.679375 0.880797
    0 34.522400 1.505865 -1.515198 1.503061 3.550991 1.420396 1.710625 0.851953
    0 10.917599 4.705865 -1.622433 1.650789 4.350764 1.634866 1.632500 0.851953
    1 21.338514 -2.400001 -1.681677 0.600000 1.963422 1.784916 4.742843 0.777300
    0 57.891731 -4.188268 -1.536627 1.575194 3.780010 1.512004 2.007500 0.679179
如果您要剖析该自定义运算符样本,请使用 DEEPHI_PROFILING=1 环境变量:
env DEEPHI_PROFILING=1 ./sample_pointpillars_graph_runner ./pointpillars_full_customer_op.xmodel sample_pointpillars.bin
剖析结果如下所示:
I1130 01:29:53.038476 15571 cpu_task.cpp:163] CPU_UPDATE_INPUT : 5us
I1130 01:29:53.038684 15571 cpu_task.cpp:166] CPU_UPDATE_OUTPUT : 55us
I1130 01:29:53.038872 15571 cpu_task.cpp:169] CPU_SYNC_FOR_READ : 46us
I1130 01:29:53.039050 15571 cpu_task.cpp:181] CPU_OP_EXEC : 32us
I1130 01:29:53.039232 15571 cpu_task.cpp:181] CPU_OP_EXEC : 36us
I1130 01:29:53.039597 15571 cpu_task.cpp:181] CPU_OP_EXEC : 232us
I1130 01:29:53.066352 15571 cpu_task.cpp:181] CPU_OP_EXEC : 26575us
I1130 01:29:53.066745 15571 cpu_task.cpp:195] CPU_SYNC_FOR_WRITE : 1us