1*89c4ff92SAndroid Build Coastguard Worker# Delegate Build Guide 2*89c4ff92SAndroid Build Coastguard Worker 3*89c4ff92SAndroid Build Coastguard WorkerThis guide assumes that Arm NN has been built with the Arm NN TF Lite Delegate with the [Arm NN Build Tool](../build-tool/README.md).<br> 4*89c4ff92SAndroid Build Coastguard WorkerThe Arm NN TF Lite Delegate can also be obtained from downloading the [Pre-Built Binaries on the GitHub homepage](../README.md). 5*89c4ff92SAndroid Build Coastguard Worker 6*89c4ff92SAndroid Build Coastguard Worker**Table of Contents:** 7*89c4ff92SAndroid Build Coastguard Worker- [Running DelegateUnitTests](#running-delegateunittests) 8*89c4ff92SAndroid Build Coastguard Worker- [Run the TF Lite Benchmark Tool](#run-the-tflite-model-benchmark-tool) 9*89c4ff92SAndroid Build Coastguard Worker - [Download the TFLite Model Benchmark Tool](#download-the-tflite-model-benchmark-tool) 10*89c4ff92SAndroid Build Coastguard Worker - [Execute the benchmarking tool with the Arm NN TF Lite Delegate](#execute-the-benchmarking-tool-with-the-arm-nn-tf-lite-delegate) 11*89c4ff92SAndroid Build Coastguard Worker- [Integrate the Arm NN TfLite Delegate into your project](#integrate-the-arm-nn-tflite-delegate-into-your-project) 12*89c4ff92SAndroid Build Coastguard Worker 13*89c4ff92SAndroid Build Coastguard Worker 14*89c4ff92SAndroid Build Coastguard Worker## Running DelegateUnitTests 15*89c4ff92SAndroid Build Coastguard Worker 16*89c4ff92SAndroid Build Coastguard WorkerTo ensure that the build was successful you can run the unit tests for the delegate that can be found in 17*89c4ff92SAndroid Build Coastguard Workerthe build directory for the delegate. [Doctest](https://github.com/onqtam/doctest) was used to create those tests. Using test filters you can 18*89c4ff92SAndroid Build Coastguard Workerfilter out tests that your build is not configured for. In this case, we run all test suites that have `CpuAcc` in their name. 19*89c4ff92SAndroid Build Coastguard Worker```bash 20*89c4ff92SAndroid Build Coastguard Workercd <PATH_TO_ARMNN_BUILD_DIRECTORY>/delegate/build 21*89c4ff92SAndroid Build Coastguard Worker./DelegateUnitTests --test-suite=*CpuAcc* 22*89c4ff92SAndroid Build Coastguard Worker``` 23*89c4ff92SAndroid Build Coastguard WorkerIf you have built for Gpu acceleration as well you might want to change your test-suite filter: 24*89c4ff92SAndroid Build Coastguard Worker```bash 25*89c4ff92SAndroid Build Coastguard Worker./DelegateUnitTests --test-suite=*CpuAcc*,*GpuAcc* 26*89c4ff92SAndroid Build Coastguard Worker``` 27*89c4ff92SAndroid Build Coastguard Worker 28*89c4ff92SAndroid Build Coastguard Worker## Run the TFLite Model Benchmark Tool 29*89c4ff92SAndroid Build Coastguard Worker 30*89c4ff92SAndroid Build Coastguard WorkerThe [TFLite Model Benchmark](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/benchmark) Tool has a useful command line interface to test the TF Lite Delegate. 31*89c4ff92SAndroid Build Coastguard WorkerWe can use this to demonstrate the use of the Arm NN TF Lite Delegate and its options. 32*89c4ff92SAndroid Build Coastguard Worker 33*89c4ff92SAndroid Build Coastguard WorkerSome examples of this can be viewed in this [YouTube demonstration](https://www.youtube.com/watch?v=NResQ1kbm-M&t=920s). 34*89c4ff92SAndroid Build Coastguard Worker 35*89c4ff92SAndroid Build Coastguard Worker### Download the TFLite Model Benchmark Tool 36*89c4ff92SAndroid Build Coastguard Worker 37*89c4ff92SAndroid Build Coastguard WorkerBinary builds of the benchmarking tool for various platforms are available [here](https://www.tensorflow.org/lite/performance/measurement#native_benchmark_binary). In this example I will target an aarch64 Linux environment. I will also download a sample uint8 tflite model from the [Arm ML Model Zoo](https://github.com/ARM-software/ML-zoo). 38*89c4ff92SAndroid Build Coastguard Worker 39*89c4ff92SAndroid Build Coastguard Worker```bash 40*89c4ff92SAndroid Build Coastguard Workermkdir $BASEDIR/benchmarking 41*89c4ff92SAndroid Build Coastguard Workercd $BASEDIR/benchmarking 42*89c4ff92SAndroid Build Coastguard Worker# Get the benchmarking binary. 43*89c4ff92SAndroid Build Coastguard Workerwget https://storage.googleapis.com/tensorflow-nightly-public/prod/tensorflow/release/lite/tools/nightly/latest/linux_aarch64_benchmark_model -O benchmark_model 44*89c4ff92SAndroid Build Coastguard Worker# Make it executable. 45*89c4ff92SAndroid Build Coastguard Workerchmod +x benchmark_model 46*89c4ff92SAndroid Build Coastguard Worker# and a sample model from model zoo. 47*89c4ff92SAndroid Build Coastguard Workerwget https://github.com/ARM-software/ML-zoo/blob/master/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8/mobilenet_v2_1.0_224_quantized_1_default_1.tflite?raw=true -O mobilenet_v2_1.0_224_quantized_1_default_1.tflite 48*89c4ff92SAndroid Build Coastguard Worker``` 49*89c4ff92SAndroid Build Coastguard Worker 50*89c4ff92SAndroid Build Coastguard Worker### Execute the benchmarking tool with the Arm NN TF Lite Delegate 51*89c4ff92SAndroid Build Coastguard WorkerYou are already at $BASEDIR/benchmarking from the previous stage. 52*89c4ff92SAndroid Build Coastguard Worker```bash 53*89c4ff92SAndroid Build Coastguard WorkerLD_LIBRARY_PATH=<PATH_TO_ARMNN_BUILD_DIRECTORY> ./benchmark_model --graph=mobilenet_v2_1.0_224_quantized_1_default_1.tflite --external_delegate_path="<PATH_TO_ARMNN_BUILD_DIRECTORY>/delegate/libarmnnDelegate.so" --external_delegate_options="backends:CpuAcc;logging-severity:info" 54*89c4ff92SAndroid Build Coastguard Worker``` 55*89c4ff92SAndroid Build Coastguard WorkerThe "external_delegate_options" here are specific to the Arm NN delegate. They are used to specify a target Arm NN backend or to enable/disable various options in Arm NN. A full description can be found in the parameters of function tflite_plugin_create_delegate. 56*89c4ff92SAndroid Build Coastguard Worker 57*89c4ff92SAndroid Build Coastguard Worker## Integrate the Arm NN TfLite Delegate into your project 58*89c4ff92SAndroid Build Coastguard Worker 59*89c4ff92SAndroid Build Coastguard WorkerThe delegate can be integrated into your c++ project by creating a TfLite Interpreter and 60*89c4ff92SAndroid Build Coastguard Workerinstructing it to use the Arm NN delegate for the graph execution. This should look similar 61*89c4ff92SAndroid Build Coastguard Workerto the following code snippet. 62*89c4ff92SAndroid Build Coastguard Worker```objectivec 63*89c4ff92SAndroid Build Coastguard Worker// Create TfLite Interpreter 64*89c4ff92SAndroid Build Coastguard Workerstd::unique_ptr<Interpreter> armnnDelegateInterpreter; 65*89c4ff92SAndroid Build Coastguard WorkerInterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) 66*89c4ff92SAndroid Build Coastguard Worker (&armnnDelegateInterpreter) 67*89c4ff92SAndroid Build Coastguard Worker 68*89c4ff92SAndroid Build Coastguard Worker// Create the Arm NN Delegate 69*89c4ff92SAndroid Build Coastguard WorkerarmnnDelegate::DelegateOptions delegateOptions(backends); 70*89c4ff92SAndroid Build Coastguard Workerstd::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> 71*89c4ff92SAndroid Build Coastguard Worker theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), 72*89c4ff92SAndroid Build Coastguard Worker armnnDelegate::TfLiteArmnnDelegateDelete); 73*89c4ff92SAndroid Build Coastguard Worker 74*89c4ff92SAndroid Build Coastguard Worker// Instruct the Interpreter to use the armnnDelegate 75*89c4ff92SAndroid Build Coastguard WorkerarmnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()); 76*89c4ff92SAndroid Build Coastguard Worker``` 77*89c4ff92SAndroid Build Coastguard Worker 78*89c4ff92SAndroid Build Coastguard WorkerFor further information on using TfLite Delegates please visit the [TensorFlow website](https://www.tensorflow.org/lite/guide). 79*89c4ff92SAndroid Build Coastguard Worker 80*89c4ff92SAndroid Build Coastguard WorkerFor 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. 81