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

# 对 LiteRT 模型进行基准测试

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

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

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

* 初始化时间
* 推理时间 (预热和稳态)
* 初始化期间的内存使用量
* 总体内存使用量

## 前提条件

在运行基准测试之前,请确保您具备以下条件:

* Ubuntu 22.04 主机
* Qualcomm 开发套件

### 设置模型文件

<Steps>
  <Step title="下载示例模型、标签文件和测试图像">
    * [BMP 测试图像](https://github.com/sourcecode369/tensorflow-1/tree/master/tensorflow/lite/examples/label_image/testdata/grace_hopper.bmp)
    * [MobileNet LiteRT 模型](https://github.com/emgucv/models/blob/master/mobilenet_v1_1.0_224_float_2017_11_08/mobilenet_v1_1.0_224.tflite)
  </Step>

  <Step title="下载并解压 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
    ```
  </Step>

  <Step title="在目标设备上,创建构件目录">
    ```shell theme={null}
    ssh root@<IP_ADDRESS_OF_TARGET_DEVICE>
    ```

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

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

    ```shell theme={null}
    exit
    ```
  </Step>

  <Step title="在主机上,将模型、图像和标签文件复制到设备">
    ```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>:~/artifacts
    ```
  </Step>

  <Step title="在目标设备上,设置 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
    ```
  </Step>
</Steps>

示例应用程序使用 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 ${HOME}/artifacts
```

```shell theme={null}
benchmark_model --graph=${HOME}/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=${HOME}/artifacts/mobilenet_v1_1.0_224_quant_xnnpack_performance.csv
```

<Frame caption="LiteRT CPU 基准测试的示例输出">
  <img src="https://mintcdn.com/qualcomm-staging/uWQLnBcS9sxlZhgB/Key-Documents/AI-Developer-Workflow/_images/benchmark-litert-cpu-results.png?fit=max&auto=format&n=uWQLnBcS9sxlZhgB&q=85&s=45b8c2faada8b5663ff97c3436a0a4eb" 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 ${HOME}/artifacts
```

```shell theme={null}
benchmark_model --graph=${HOME}/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=${HOME}/artifacts/mobilenet_v1_1.0_224_GPU_Delegate_performance.csv
```

<Frame caption="LiteRT GPU 基准测试的示例输出">
  <img src="https://mintcdn.com/qualcomm-staging/uWQLnBcS9sxlZhgB/Key-Documents/AI-Developer-Workflow/_images/litert-gpu-performance-benchmark.png?fit=max&auto=format&n=uWQLnBcS9sxlZhgB&q=85&s=40378f5d8ad53c30b242a2406d2bbe06" 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=${HOME}/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=${HOME}/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-staging/uWQLnBcS9sxlZhgB/Key-Documents/AI-Developer-Workflow/_images/tool-statistics-benchmark-model.jpeg?fit=max&auto=format&n=uWQLnBcS9sxlZhgB&q=85&s=2a08bb239d09ac374776687ccc2807ca" alt="benchmark_model 工具输出,显示 delegate 状态、推理时间和内存占用" width="1920" height="819" data-path="Key-Documents/AI-Developer-Workflow/_images/tool-statistics-benchmark-model.jpeg" />
</Frame>
