> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 对 LiteRT 模型进行基准测试

> 使用 benchmark_model 工具在 Qualcomm Dragonwing IoT 平台的 CPU、GPU 和 NPU 上对 LiteRT 模型性能进行基准测试。

LiteRT 开源框架提供了 `benchmark_model` 工具，用于测量模型通过 delegate 在硬件上执行的性能。此工具与其他 LiteRT 构件一起安装在目标设备上。

该工具测量并报告以下性能指标：

* 初始化时间
* 推理时间（预热和稳定状态）
* 初始化期间的内存使用量
* 总体内存使用量

## 前提条件

在运行基准测试之前，请确保具备以下条件：

* 一台 Ubuntu 22.04 主机
* 一套 Qualcomm 开发套件

### 设置模型文件

1. 下载示例模型、标签文件和一张测试图片：

   * [BMP 测试图片](https://github.com/sourcecode369/tensorflow-1/tree/master/tensorflow/lite/examples/label_image/testdata/)
   * [MobileNet LiteRT 模型](https://github.com/emgucv/models/blob/master/mobilenet_v1_1.0_224_float_2017_11_08/mobilenet_v1_1.0_224.tflite)

2. 在主机上，下载并解压 MobileNet 模型归档文件：

   ```shell theme={null}
   wget http://download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224_quant.tgz
   ```

   ```shell theme={null}
   tar -xvf mobilenet_v1_1.0_224_quant.tgz
   ```

   ```shell theme={null}
   wget https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_1.0_224_frozen.tgz
   ```

   ```shell theme={null}
   tar -xvf mobilenet_v1_1.0_224_frozen.tgz
   ```

3. 在目标设备上，创建构件目录：

   ```shell theme={null}
   ssh root@<IP_ADDRESS_OF_TARGET_DEVICE>
   ```

   ```shell theme={null}
   mount -o remount,rw /
   ```

   ```shell theme={null}
   mkdir -p /etc/artifacts
   ```

   ```shell theme={null}
   exit
   ```

4. 在主机上，将模型、图片和标签文件复制到设备：

   ```shell theme={null}
   scp mobilenet_v1_1.0_224_quant.tflite grace_hopper.bmp mobilenet_v1_1.0_224/labels.txt mobilenet_v1_1.0_224.tflite root@<IP_ADDRESS_OF_TARGET_DEVICE>:/etc/artifacts
   ```

5. 在目标设备上，设置 GPU 库：

   ```shell theme={null}
   export OCL_ICD_FILENAMES=/usr/lib/libOpenCL_adreno.so.1
   ```

   ```shell theme={null}
   ln -sf /usr/lib/libOpenCL.so.1 /usr/lib/libOpenCL.so
   ```

示例应用程序使用 MobileNet v1 模型（在包含 1000 个类别的 ImageNet 数据集上训练）作为分类模型示例。

## 在 CPU 上进行基准测试

`label_image` 示例应用程序已使用 LiteRT 库交叉编译并安装在目标设备上。源代码可在 [TensorFlow GitHub 仓库](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/label_image)中获取。

要在 CPU 上使用 XNNPACK delegate 进行基准测试：

```shell theme={null}
ssh root@<IP_ADDRESS_OF_TARGET_DEVICE>
```

```shell theme={null}
cd /etc/artifacts
```

```shell theme={null}
benchmark_model --graph=/etc/artifacts/mobilenet_v1_1.0_224_quant.tflite \
                --enable_op_profiling=true \
                --use_xnnpack=true \
                --num_threads=4 \
                --max_secs=300 \
                --profiling_output_csv_file=/etc/artifacts/mobilenet_v1_1.0_224_quant_xnnpack_performance.csv
```

<Frame caption="LiteRT CPU 基准测试示例输出">
  <img src="https://mintcdn.com/qualcomm-prod/Sb9VrG0-ITL9uwLF/Key-Documents/AI-Developer-Workflow/_images/benchmark-litert-cpu-results.png?fit=max&auto=format&n=Sb9VrG0-ITL9uwLF&q=85&s=0b823c2b9541577f82e233dbd58fff24" alt="LiteRT CPU 基准测试示例输出" width="1892" height="640" data-path="Key-Documents/AI-Developer-Workflow/_images/benchmark-litert-cpu-results.png" />
</Frame>

## 在 GPU 上进行基准测试

要使用 GPU delegate 进行基准测试：

```shell theme={null}
ssh root@<IP_ADDRESS_OF_TARGET_DEVICE>
```

```shell theme={null}
cd /etc/artifacts
```

```shell theme={null}
benchmark_model --graph=/etc/artifacts/mobilenet_v1_1.0_224_quant.tflite \
                --enable_op_profiling=true \
                --use_gpu=true \
                --num_runs=100 \
                --warmup_runs=10 \
                --max_secs=300 \
                --profiling_output_csv_file=/etc/artifacts/mobilenet_v1_1.0_224_GPU_Delegate_performance.csv
```

<Frame caption="LiteRT GPU 基准测试示例输出">
  <img src="https://mintcdn.com/qualcomm-prod/L-jqwrTTz49ZAgVX/Key-Documents/AI-Developer-Workflow/_images/litert-gpu-performance-benchmark.png?fit=max&auto=format&n=L-jqwrTTz49ZAgVX&q=85&s=8194512e7ea469a7dadf150bc484f14d" alt="LiteRT GPU 基准测试示例输出" width="1881" height="477" data-path="Key-Documents/AI-Developer-Workflow/_images/litert-gpu-performance-benchmark.png" />
</Frame>

## 使用 QAIRT delegate 在 NPU 上进行基准测试

Qualcomm AI Runtime delegate 使用 Qualcomm AI Runtime API 及其后端在 Adreno GPU 和 Hexagon Tensor Processor 上加速模型。

要使用 QAIRT 外部 delegate，请确保设备上具有以下库：

* `libQnnTFLiteDelegate.so` —— QNN delegate 库
* 来自 Qualcomm AI Engine Direct SDK 的库

您可以通过外部 delegate 选项自定义模型执行，以使用特定后端：

* `libQnnGpu.so` —— 在 GPU 上运行 QNN delegate
* `libQnnHtp.so` —— 在 Hexagon Tensor Processor 上运行 QNN delegate
* `libQnnDsp.so` —— 在 DSP 上运行 QNN delegate

要使用 QNN 外部 delegate 在 Hexagon Tensor Processor 上进行基准测试：

<Tabs>
  <Tab title="QCS6490/QCS5430、IQ-9075 和 QCS8275">
    ```shell theme={null}
    benchmark_model --graph=/etc/artifacts/mobilenet_v1_1.0_224_quant.tflite \
                    --external_delegate_path=/usr/lib/libQnnTFLiteDelegate.so \
                    --external_delegate_options='backend_type:htp;library_path:/usr/lib/libQnnHtp.so;skel_library_dir:/usr/lib/rfsa/adsp;htp_precision:0;htp_performance_mode:2'
    ```
  </Tab>

  <Tab title="IQ-615">
    ```shell theme={null}
    benchmark_model --graph=/usr/share/label_image/mobilenet_v1_1.0_224_quant.tflite \
                    --external_delegate_path=libQnnTFLiteDelegate.so \
                    --external_delegate_options='backend_type:dsp;library_path:/usr/lib/libQnnDsp.so;skel_library_dir:/usr/lib/dsp/adsp'
    ```

    <Note>
      目前在 IQ-615 上基准测试会失败。
    </Note>
  </Tab>
</Tabs>

基准测试输出包括以下统计信息：

* Delegate 创建状态
* 使用该 delegate 在硬件上的平均推理时间
* 模型执行的内存占用

<Frame caption="benchmark_model 工具统计信息">
  <img src="https://mintcdn.com/qualcomm-prod/WwC9kmcnKl9Ef7de/Key-Documents/AI-Developer-Workflow/_images/tool-statistics-benchmark-model.jpeg?fit=max&auto=format&n=WwC9kmcnKl9Ef7de&q=85&s=7d5913aa8f2482063a2bb94f5c9520a0" alt="benchmark_model 工具输出，显示 delegate 状态、推理时间和内存占用" width="1920" height="819" data-path="Key-Documents/AI-Developer-Workflow/_images/tool-statistics-benchmark-model.jpeg" />
</Frame>
