> ## 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` 工具，用于使用委托测量硬件上的模型执行性能。该工具与其他 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="在目标设备上创建 artifacts 目录">
    ```shell theme={null}
    ssh ubuntu@<IP_ADDRESS_OF_TARGET_DEVICE>
    ```

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

    ```shell theme={null}
    mkdir -p /home/ubuntu/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 ubuntu@<IP_ADDRESS_OF_TARGET_DEVICE>:/home/ubuntu/artifacts
    ```
  </Step>
</Steps>

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

## 在 CPU 上进行基准测试

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

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

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

```shell theme={null}
cd /home/ubuntu/artifacts
```

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

<Frame caption="LiteRT CPU 基准测试的示例输出">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/AI-Developer-Workflow-Ubuntu/_images/benchmark-litert-cpu-results.png" alt="LiteRT CPU 基准测试的示例输出" />
</Frame>

## 在 GPU 上进行基准测试

要使用 GPU 委托进行基准测试：

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

```shell theme={null}
cd /home/ubuntu/artifacts
```

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

<Frame caption="LiteRT GPU 基准测试的示例输出">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/AI-Developer-Workflow-Ubuntu/_images/litert-gpu-performance-benchmark.png" alt="LiteRT GPU 基准测试的示例输出" />
</Frame>

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

Qualcomm AI Runtime 委托使用 Qualcomm AI Runtime API 及其后端，在 Adreno GPU 和 Hexagon 张量处理器上加速模型。

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

* `libQnnTFLiteDelegate.so` — QNN 委托库
* Qualcomm AI Engine Direct SDK 中的库

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

* `libQnnGpu.so` — 在 GPU 上运行 QNN 委托
* `libQnnHtp.so` — 在 Hexagon 张量处理器上运行 QNN 委托
* `libQnnDsp.so` — 在 DSP 上运行 QNN 委托

要使用 QNN 外部委托在 Hexagon 张量处理器上进行基准测试：

<Tabs>
  <Tab title="QCS6490/QCS5430、IQ-9075 和 QCS8275">
    ```shell theme={null}
    benchmark_model --graph=/home/ubuntu/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>

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

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

<Frame caption="benchmark_model 工具统计信息">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/AI-Developer-Workflow-Ubuntu/_images/tool-statistics-benchmark-model.jpeg" alt="benchmark_model 工具输出，显示委托状态、推理时间和内存占用" />
</Frame>
