xref: /aosp_15_r20/external/executorch/examples/qualcomm/README.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker# ExecuTorch QNN Backend examples
2*523fa7a6SAndroid Build Coastguard Worker
3*523fa7a6SAndroid Build Coastguard WorkerThis directory contains examples for some AI models.
4*523fa7a6SAndroid Build Coastguard Worker
5*523fa7a6SAndroid Build Coastguard WorkerWe have seperated the example scripts into the following subfolders, please refer to [README.md](../../backends/qualcomm/README.md) for the example scripts' directory structure:
6*523fa7a6SAndroid Build Coastguard Worker
7*523fa7a6SAndroid Build Coastguard Worker1. executor_runner: This folder contains a general executor runner capable of running most of the models. As a rule of thumb, if a model does not have its own customized runner, execute the model using [executor_runner](./executor_runner/qnn_executor_runner.cpp). On the other hand, if a model has its own runner, such as [llama2](./oss_scripts/llama2/qnn_llama_runner.cpp), use the customized runner to execute the model. Customized runner should be located under the same folder as the model's python script.
8*523fa7a6SAndroid Build Coastguard Worker
9*523fa7a6SAndroid Build Coastguard Worker2. oss_scripts: OSS stands for Open Source Software. This folder contains python scripts for open source models. Some models under this folder might also have their own customized runner.
10*523fa7a6SAndroid Build Coastguard Worker   For example, [llama2](./oss_scripts/llama2/qnn_llama_runner.cpp) contains not only the python scripts to prepare the model but also a customized runner for executing the model.
11*523fa7a6SAndroid Build Coastguard Worker
12*523fa7a6SAndroid Build Coastguard Worker3. qaihub_scripts: QAIHub stands for [Qualcomm AI Hub](https://aihub.qualcomm.com/). On QAIHub, users can find pre-compiled context binaries, a format used by QNN to save its models. This provides users with a new option for model deployment. Different from oss_scripts & scripts, which the example scripts are converting a model from nn.Module to ExecuTorch .pte files, qaihub_scripts provides example scripts for converting pre-compiled context binaries to ExecuTorch .pte files. Additionaly, users can find customized example runners specific to the QAIHub models for execution. For example [qaihub_llama2_7b](./qaihub_scripts/llama2/qaihub_llama2_7b.py) is a script converting context binaries to ExecuTorch .pte files, and [qaihub_llama2_7b_runner](./qaihub_scripts/llama2/qaihub_llama2_7b_runner.cpp) is a customized example runner to execute llama2 .pte files. Please be aware that context-binaries downloaded from QAIHub are tied to a specific QNN SDK version.
13*523fa7a6SAndroid Build Coastguard WorkerBefore executing the scripts and runner, please ensure that you are using the QNN SDK version that is matching the context binary. Tutorial below will also cover how to check the QNN Version for a context binary.
14*523fa7a6SAndroid Build Coastguard Worker
15*523fa7a6SAndroid Build Coastguard Worker4. scripts: This folder contains scripts to build models provided by executorch.
16*523fa7a6SAndroid Build Coastguard Worker
17*523fa7a6SAndroid Build Coastguard Worker
18*523fa7a6SAndroid Build Coastguard Worker
19*523fa7a6SAndroid Build Coastguard WorkerPlease check helper of each examples for detailed arguments.
20*523fa7a6SAndroid Build Coastguard Worker
21*523fa7a6SAndroid Build Coastguard WorkerHere are some general information and limitations.
22*523fa7a6SAndroid Build Coastguard Worker
23*523fa7a6SAndroid Build Coastguard Worker## Prerequisite
24*523fa7a6SAndroid Build Coastguard Worker
25*523fa7a6SAndroid Build Coastguard WorkerPlease finish tutorial [Setting up executorch](https://pytorch.org/executorch/stable/getting-started-setup).
26*523fa7a6SAndroid Build Coastguard Worker
27*523fa7a6SAndroid Build Coastguard WorkerPlease finish [setup QNN backend](../../docs/source/build-run-qualcomm-ai-engine-direct-backend.md).
28*523fa7a6SAndroid Build Coastguard Worker
29*523fa7a6SAndroid Build Coastguard Worker## Environment
30*523fa7a6SAndroid Build Coastguard Worker
31*523fa7a6SAndroid Build Coastguard WorkerPlease set up `QNN_SDK_ROOT` environment variable.
32*523fa7a6SAndroid Build Coastguard WorkerNote that this version should be exactly same as building QNN backend.
33*523fa7a6SAndroid Build Coastguard WorkerPlease check [setup](../../docs/source/build-run-qualcomm-ai-engine-direct-backend.md).
34*523fa7a6SAndroid Build Coastguard Worker
35*523fa7a6SAndroid Build Coastguard WorkerPlease set up `LD_LIBRARY_PATH` to `$QNN_SDK_ROOT/lib/x86_64-linux-clang`.
36*523fa7a6SAndroid Build Coastguard WorkerOr, you could put QNN libraries to default search path of the dynamic linker.
37*523fa7a6SAndroid Build Coastguard Worker
38*523fa7a6SAndroid Build Coastguard Worker## Device
39*523fa7a6SAndroid Build Coastguard Worker
40*523fa7a6SAndroid Build Coastguard WorkerPlease connect an Android phone to the workstation. We use `adb` to communicate with the device.
41*523fa7a6SAndroid Build Coastguard Worker
42*523fa7a6SAndroid Build Coastguard WorkerIf the device is in a remote host, you might want to add `-H` to the `adb`
43*523fa7a6SAndroid Build Coastguard Workercommands in the `SimpleADB` class inside [utils.py](utils.py).
44*523fa7a6SAndroid Build Coastguard Worker
45*523fa7a6SAndroid Build Coastguard Worker## Please use python xxx.py --help for information of each examples.
46*523fa7a6SAndroid Build Coastguard Worker
47*523fa7a6SAndroid Build Coastguard WorkerSome CLI examples here. Please adjust according to your environment:
48*523fa7a6SAndroid Build Coastguard Worker
49*523fa7a6SAndroid Build Coastguard Worker#### First switch to following folder
50*523fa7a6SAndroid Build Coastguard Worker```bash
51*523fa7a6SAndroid Build Coastguard Workercd $EXECUTORCH_ROOT/examples/qualcomm/scripts
52*523fa7a6SAndroid Build Coastguard Worker```
53*523fa7a6SAndroid Build Coastguard Worker
54*523fa7a6SAndroid Build Coastguard Worker#### For MobileNet_v2
55*523fa7a6SAndroid Build Coastguard Worker```bash
56*523fa7a6SAndroid Build Coastguard Workerpython mobilenet_v2.py -s <device_serial> -m "SM8550" -b path/to/build-android/ -d /path/to/imagenet-mini/val
57*523fa7a6SAndroid Build Coastguard Worker```
58*523fa7a6SAndroid Build Coastguard Worker
59*523fa7a6SAndroid Build Coastguard Worker#### For DeepLab_v3
60*523fa7a6SAndroid Build Coastguard Worker```bash
61*523fa7a6SAndroid Build Coastguard Workerpython deeplab_v3.py -s <device_serial> -m "SM8550" -b path/to/build-android/ --download
62*523fa7a6SAndroid Build Coastguard Worker```
63*523fa7a6SAndroid Build Coastguard Worker
64*523fa7a6SAndroid Build Coastguard Worker#### Check context binary version
65*523fa7a6SAndroid Build Coastguard Worker```bash
66*523fa7a6SAndroid Build Coastguard Workercd ${QNN_SDK_ROOT}/bin/x86_64-linux-clang
67*523fa7a6SAndroid Build Coastguard Worker./qnn-context-binary-utility --context_binary ${PATH_TO_CONTEXT_BINARY} --json_file ${OUTPUT_JSON_NAME}
68*523fa7a6SAndroid Build Coastguard Worker```
69*523fa7a6SAndroid Build Coastguard WorkerAfter retreiving the json file, search in the json file for the field "buildId" and ensure it matches the ${QNN_SDK_ROOT} you are using for the environment variable.
70*523fa7a6SAndroid Build Coastguard WorkerIf you run into the following error, that means the ${QNN_SDK_ROOT} that you are using is older than the context binary QNN SDK version. In this case, please download a newer QNN SDK version.
71*523fa7a6SAndroid Build Coastguard Worker```
72*523fa7a6SAndroid Build Coastguard WorkerError: Failed to get context binary info.
73*523fa7a6SAndroid Build Coastguard Worker```
74*523fa7a6SAndroid Build Coastguard Worker
75*523fa7a6SAndroid Build Coastguard Worker## Additional Dependency
76*523fa7a6SAndroid Build Coastguard Worker
77*523fa7a6SAndroid Build Coastguard WorkerThe mobilebert multi-class text classification example requires `pandas` and `sklearn`.
78*523fa7a6SAndroid Build Coastguard WorkerPlease install them by something like
79*523fa7a6SAndroid Build Coastguard Worker
80*523fa7a6SAndroid Build Coastguard Worker```bash
81*523fa7a6SAndroid Build Coastguard Workerpip install scikit-learn pandas
82*523fa7a6SAndroid Build Coastguard Worker```
83*523fa7a6SAndroid Build Coastguard Worker
84*523fa7a6SAndroid Build Coastguard Worker## Limitation
85*523fa7a6SAndroid Build Coastguard Worker
86*523fa7a6SAndroid Build Coastguard Worker1. QNN 2.24 is used for all examples. Newer or older QNN might work,
87*523fa7a6SAndroid Build Coastguard Workerbut the performance and accuracy number can differ.
88*523fa7a6SAndroid Build Coastguard Worker
89*523fa7a6SAndroid Build Coastguard Worker2. The mobilebert example is on QNN HTP fp16, which is only supported by a limited
90*523fa7a6SAndroid Build Coastguard Workerset of SoCs. Please check QNN documents for details.
91*523fa7a6SAndroid Build Coastguard Worker
92*523fa7a6SAndroid Build Coastguard Worker3. The mobilebert example needs to train the last classifier layer a bit, so it takes
93*523fa7a6SAndroid Build Coastguard Workertime to run.
94*523fa7a6SAndroid Build Coastguard Worker
95*523fa7a6SAndroid Build Coastguard Worker4. [**Important**] Due to the numerical limits of FP16, other use cases leveraging mobileBert wouldn't
96*523fa7a6SAndroid Build Coastguard Workerguarantee to work.
97