Convert to QNN for Linux Host on Linux / Android / QNX Target¶
Note
This is Part 2 of the Convert to QNN tutorial for Linux host machines. If you have not completed Part 1, please do so here.
Step 3. Model Build on Linux Host for Linux Target¶
Once the model has been converted into QNN format, the next step is to build it so it can run on the target device’s operating system with qnn-model-lib-generator.
Based on the operating system and architecture of your target device, choose one of the following build instructions.
Warning
For cases where the “host machine” and “target device” are the same, you will need to adapt the steps to handle files locally instead of transferring them to a remote device.
Note
Please continue to use the same terminal you were using on your host machine from part 1.
Create a directory on your host machine where your newly built files will live by running:
mkdir -p /tmp/qnn_tmp
Navigate to the new directory:
cd /tmp/qnn_tmp
Copy over the QNN
.cppand.binmodel files to/tmp/qnn_tmp/:cp "${CONVERTED_MODEL_PATH}.cpp" "${CONVERTED_MODEL_PATH}.bin" /tmp/qnn_tmp/
Choose the most relevant supported target architecture from the following list:
Warning
If you don’t know which one to choose, you can run the following commands on your target device to get more information:
uname -a,cat /etc/os-release, andgcc --version.Target Architecture
Build Instruction
64-bit Linux targets
x86_64-linux-clang64-bit Android devices
aarch64-androidHexagon Tensor Processor (HTP)
x86_64-linux-clangQualcomm’s QNX OS
aarch64-qnx- Note: This architecture is not supported by default in the QNN SDK.OpenEmbedded Linux (GCC 11.2)
aarch64-oe-linux-gcc11.2OpenEmbedded Linux (GCC 9.3)
aarch64-oe-linux-gcc9.3OpenEmbedded Linux (GCC 8.2)
aarch64-oe-linux-gcc8.2Ubuntu Linux (GCC 9.4)
aarch64-ubuntu-gcc9.4Ubuntu Linux (GCC 7.5)
aarch64-ubuntu-gcc7.5On your host machine, set the target architecture of your target device by setting
QNN_TARGET_ARCHto your device’s target architecture:export QNN_TARGET_ARCH="your-target-architecture-from-above"
For example:
export QNN_TARGET_ARCH="x86_64-linux-clang"
Run the following command on your host machine to generate the model library:
python3 "${QNN_SDK_ROOT}/bin/x86_64-linux-clang/qnn-model-lib-generator" \ -c "${CONVERTED_MODEL_PATH##*/}.cpp" \ -b "${CONVERTED_MODEL_PATH##*/}.bin" \ -o model_libs \ -t ${QNN_TARGET_ARCH}
c- This indicates the path to the.cppQNN model file.b- This indicates the path to the.binQNN model file. (bis optional, but at runtime, the.cppfile could fail if it needs the.binfile, so it is recommended).o- The path to the output folder.t- Indicate which architecture to build for.
Run
ls /tmp/qnn_tmp/model_libs/${QNN_TARGET_ARCH}and verify that an output file ending in.sois inside. - You will use the.sofile on the target device to execute inferences. - The output.sofile will be located in themodel_libsdirectory, named according to the target architecture.For example:
model_libs/x64/Inception_v3.soormodel_libs/aarch64/Inception_v3.so.
Once you’ve located your relevant .so file, save the full path of the file to an environment variable:
export QNN_MODEL_PATH="/tmp/qnn_tmp/model_libs/<path to .so>"
Step 4. Use the built model on specific processors¶
Now that you have an executable version of your model, the next step is to transfer the built model and all necessary files to the target processor, then to run inferences on it.
Install all necessary dependencies from Setup.
Follow the below SSH setup instructions.
Follow the instructions for each specific processor you want to run your model on.
Sub-Step 1: Ensure that you follow the processor-specific Setup instructions for your host machine :doc:`here<../setup>`.
Sub-Step 2: Set up SSH on the target device.
Here we use
OpenSSHto copy files withscplater on and run scripts on the target device viassh. If that does not work for your target device, feel free to use any other method of transferring the files over. (Ex.adbfor Android debugging or USB with manual terminal commands on the target device)
- Ensure that both the host device and the target device are on the same network for this setup.
Otherwise,
OpenSSHrequires port-forwarding to connect.
- On your target device, install
OpenSSH Serverif it is not already installed.
Example for an Ubuntu device:
# Update package lists sudo apt update # Install OpenSSH server sudo apt install openssh-server # Check SSH service status sudo systemctl status ssh # Start SSH service if it's not running sudo systemctl start ssh # Enable SSH service to start on boot sudo systemctl enable sshNote
You can turn off the OpenSSH Server service later by running
sudo systemctl stop sshif you want to.
On your target device, run
ifconfigto get the IP address of your target device.On your host machine, set a console variable for your target device’s
inet addrfrom above to see itsipv4address (replacing127.0.0.1below).export TARGET_IP="127.0.0.1"
Also set the username of the desired account on your target device (you can find it by running
whoamion your target device if you are logged into the desired account).export TARGET_USER="your-linux-account-username"
On your host machine, install
OpenSSH Clientby running the following commands:sudo apt update sudo apt install openssh-client sudo systemctl status ssh sudo systemctl start ssh # Only if the service is not running sudo systemctl enable sshNote
From this point on you should be able to connect to
sshfrom the terminal. You may need to open another terminal to do so though.
Sub-Step 3: Follow the steps below for whichever processor you would like to run your model on.