xref: /aosp_15_r20/external/armnn/delegate/DelegateQuickStartGuide.md (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1# TfLite Delegate Quick Start Guide
2If you have downloaded the Arm NN Github binaries or built the TfLite delegate yourself, then this tutorial will show you how you can
3integrate it into TfLite to run models using python.
4
5Here is an example python script showing how to do this. In this script we are making use of the
6[external adaptor](https://www.tensorflow.org/lite/performance/implementing_delegate#option_2_leverage_external_delegate)
7tool of TfLite that allows you to load delegates at runtime.
8```python
9import numpy as np
10import tflite_runtime.interpreter as tflite
11
12# Load TFLite model and allocate tensors.
13# (if you are using the complete tensorflow package you can find load_delegate in tf.experimental.load_delegate)
14armnn_delegate = tflite.load_delegate( library="<path-to-armnn-binaries>/libarmnnDelegate.so",
15                                       options={"backends": "CpuAcc,GpuAcc,CpuRef", "logging-severity":"info"})
16# Delegates/Executes all operations supported by Arm NN to/with Arm NN
17interpreter = tflite.Interpreter(model_path="<your-armnn-repo-dir>/delegate/python/test/test_data/mock_model.tflite",
18                                 experimental_delegates=[armnn_delegate])
19interpreter.allocate_tensors()
20
21# Get input and output tensors.
22input_details = interpreter.get_input_details()
23output_details = interpreter.get_output_details()
24
25# Test model on random input data.
26input_shape = input_details[0]['shape']
27input_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8)
28interpreter.set_tensor(input_details[0]['index'], input_data)
29
30interpreter.invoke()
31
32# Print out result
33output_data = interpreter.get_tensor(output_details[0]['index'])
34print(output_data)
35```
36
37# Prepare the environment
38Pre-requisites:
39 * Dynamically build Arm NN Delegate library or download the Arm NN binaries
40 * python3 (Depends on TfLite version)
41 * virtualenv
42 * numpy (Depends on TfLite version)
43 * tflite_runtime (>=2.5, depends on Arm NN Delegate)
44
45If you haven't built the delegate yet then take a look at the [build guide](./BuildGuideNative.md). Otherwise, you can download the binaries [here](https://github.com/ARM-software/armnn/releases/). Set the following environment variable to the location of the .so binary files:
46
47```bash
48export LD_LIBRARY_PATH=<path_to_so_binary_files>
49```
50
51We recommend creating a virtual environment for this tutorial. For the following code to work python3 is needed. Please
52also check the documentation of the TfLite version you want to use. There might be additional prerequisites for the python
53version. We will use Tensorflow Lite 2.5.0 for this guide.
54```bash
55# Install python3 (We ended up with python3.5.3) and virtualenv
56sudo apt-get install python3-pip
57sudo pip3 install virtualenv
58
59# create a virtual environment
60cd your/tutorial/dir
61# creates a directory myenv at the current location
62virtualenv -p python3 myenv
63# activate the environment
64source myenv/bin/activate
65```
66
67Now that the environment is active we can install additional packages we need for our example script. As you can see
68in the python script at the start of this page, this tutorial uses the `tflite_runtime` rather than the whole tensorflow
69package. The `tflite_runtime` is a package that wraps the TfLite Interpreter. Therefore it can only be used to run inferences of
70TfLite models. But since Arm NN is only an inference engine itself this is a perfect match. The
71`tflite_runtime` is also much smaller than the whole tensorflow package and better suited to run models on
72mobile and embedded devices.
73
74The TfLite [website](https://www.tensorflow.org/lite/guide/python) shows you two methods to download the `tflite_runtime`  package.
75In our experience, the use of the pip command works for most systems including debian. However, if you're using an older version of Tensorflow,
76you may need to build the pip package from source. You can find more information [here](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tools/pip_package/README.md).
77But in our case, with Tensorflow Lite 2.5.0, we can install through:
78
79```
80pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime==2.5.0
81```
82
83Your virtual environment is now all setup. Copy the final python script into a python file e.g.
84`ExternalDelegatePythonTutorial.py`. Modify the python script above and replace `<path-to-armnn-binaries>` and
85`<your-armnn-repo-dir>` with the directories you have set up. If you've been using the [native build guide](./BuildGuideNative.md)
86this will be `$BASEDIR/armnn/build` and `$BASEDIR/armnn`.
87
88Finally, execute the script:
89```bash
90python ExternalDelegatePythonTutorial.py
91```
92The output should look similar to this:
93```bash
94Info: Arm NN v28.0.0
95
96Info: Initialization time: 0.56 ms
97
98INFO: TfLiteArmnnDelegate: Created TfLite Arm NN delegate.
99[[ 12 123  16  12  11  14  20  16  20  12]]
100Info: Shutdown time: 0.28 ms
101```
102
103For more details of the kind of options you can pass to the Arm NN delegate please check the parameters of function tflite_plugin_create_delegate.
104
105You can also test the functionality of the external delegate adaptor by running some unit tests:
106```bash
107pip install pytest
108cd armnn/delegate/python/test
109# You can deselect tests that require backends that your hardware doesn't support using markers e.g. -m "not GpuAccTest"
110pytest --delegate-dir="<path-to-armnn-binaries>/libarmnnDelegate.so" -m "not GpuAccTest"
111```
112