1*3e777be0SXin LiIntegration guide 2*3e777be0SXin Li================= 3*3e777be0SXin Li 4*3e777be0SXin LiThis document describes how to integrate the Arm NN Android NNAPI driver into an Android source tree. 5*3e777be0SXin Li 6*3e777be0SXin Li### Prerequisites 7*3e777be0SXin Li 8*3e777be0SXin Li1. Android source tree for Android Q (we have tested against Android Q version 10.0.0_r39), in the directory `<ANDROID_ROOT>` 9*3e777be0SXin Li2. Android source tree for Android R (we have tested against Android R version 11.0.0_r3), in the directory `<ANDROID_ROOT>` 10*3e777be0SXin Li3. Android source tree for Android S (we have tested against Android S version 12.0.0_r1), in the directory `<ANDROID_ROOT>` 11*3e777be0SXin Li4. Android source tree for Android T (we have tested against Android T pre-release tag - TP1A.220624.003), in the directory `<ANDROID_ROOT>` 12*3e777be0SXin Li5. Mali OpenCL driver integrated into the Android source tree 13*3e777be0SXin Li 14*3e777be0SXin Li### Procedure 15*3e777be0SXin Li 16*3e777be0SXin Li1. Place this source directory at `<ANDROID_ROOT>/vendor/arm/android-nn-driver` 17*3e777be0SXin Li2. Run setup.sh 18*3e777be0SXin Li3. Update the Android build environment to add the Arm NN driver. This ensures that the driver service 19*3e777be0SXin Liis built and copied to the `system/vendor/bin/hw` directory in the Android image. 20*3e777be0SXin LiTo update the build environment, add to the contents of the variable `PRODUCT_PACKAGES` 21*3e777be0SXin Liwithin the device-specific makefile that is located in the `<ANDROID_ROOT>/device/<manufacturer>/<product>` 22*3e777be0SXin Lidirectory. This file is normally called `device.mk`: 23*3e777be0SXin Li 24*3e777be0SXin Li`Android.mk` contains the module definition of all versions (1.1, 1.2 and 1.3) of the Arm NN driver. 25*3e777be0SXin Li 26*3e777be0SXin LiFor Android Q, a new version of the NN API is available (1.2), 27*3e777be0SXin Lithus the following should be added to `device.mk` instead: 28*3e777be0SXin Li<pre> 29*3e777be0SXin LiPRODUCT_PACKAGES += [email protected] 30*3e777be0SXin Li</pre> 31*3e777be0SXin Li 32*3e777be0SXin LiFor Android R, S and T, new version of the NN API is available (1.3), 33*3e777be0SXin Lithus the following should be added to `device.mk` instead: 34*3e777be0SXin Li<pre> 35*3e777be0SXin LiPRODUCT_PACKAGES += [email protected] 36*3e777be0SXin Li</pre> 37*3e777be0SXin Li 38*3e777be0SXin LiSimilarly, the Neon, CL or Reference backend can be enabled/disabled by setting ARMNN_COMPUTE_CL_ENABLE, 39*3e777be0SXin LiARMNN_COMPUTE_NEON_ENABLE or ARMNN_REF_ENABLE in `device.mk`: 40*3e777be0SXin Li<pre> 41*3e777be0SXin LiARMNN_COMPUTE_CL_ENABLE := 1 42*3e777be0SXin Li</pre> 43*3e777be0SXin Li 44*3e777be0SXin LiFor all Android versions the vendor manifest.xml requires the Neural Network HAL information. 45*3e777be0SXin LiFor Android Q use HAL version 1.2 as below. For later Android versions substitute 1.3 where necessary. 46*3e777be0SXin Li```xml 47*3e777be0SXin Li<hal format="hidl"> 48*3e777be0SXin Li <name>android.hardware.neuralnetworks</name> 49*3e777be0SXin Li <transport>hwbinder</transport> 50*3e777be0SXin Li <version>1.2</version> 51*3e777be0SXin Li <interface> 52*3e777be0SXin Li <name>IDevice</name> 53*3e777be0SXin Li <instance>armnn</instance> 54*3e777be0SXin Li </interface> 55*3e777be0SXin Li <fqname>@1.2::IDevice/armnn</fqname> 56*3e777be0SXin Li</hal> 57*3e777be0SXin Li``` 58*3e777be0SXin Li 59*3e777be0SXin Li4. Build Android as normal (https://source.android.com/setup/build/building) 60*3e777be0SXin Li5. To confirm that the Arm NN driver has been built, check for the driver service executable at 61*3e777be0SXin Li 62*3e777be0SXin LiAndroid Q 63*3e777be0SXin Li<pre> 64*3e777be0SXin Li<ANDROID_ROOT>/out/target/product/<product>/vendor/bin/hw 65*3e777be0SXin Li</pre> 66*3e777be0SXin Li 67*3e777be0SXin Li### Testing 68*3e777be0SXin Li 69*3e777be0SXin Li1. Run the Arm NN driver service executable in the background. 70*3e777be0SXin LiUse the corresponding version of the driver for the Android version you are running. 71*3e777be0SXin Lii.e 72*3e777be0SXin Li[email protected] for Android Q and 73*3e777be0SXin Li[email protected] for Android R, S and T 74*3e777be0SXin Li<pre> 75*3e777be0SXin LiIt is also possible to use a specific backend by using the -c option. 76*3e777be0SXin LiThe following is an example of using the CpuAcc backend for Android Q: 77*3e777be0SXin Liadb shell /system/vendor/bin/hw/android.hardware.neuralnetworks@1.2-service-armnn -c CpuAcc & 78*3e777be0SXin Li</pre> 79*3e777be0SXin Li2. Run some code that exercises the Android Neural Networks API, for example Android's 80*3e777be0SXin Li`NeuralNetworksTest` unit tests (note this is an optional component that must be built). 81*3e777be0SXin Li<pre> 82*3e777be0SXin Liadb shell /data/nativetest/NeuralNetworksTest_static/NeuralNetworksTest_static > NeuralNetworkTest.log 83*3e777be0SXin Li</pre> 84*3e777be0SXin Li3. To confirm that the Arm NN driver is being used to service the Android Neural Networks API requests, 85*3e777be0SXin Licheck for messages in logcat with the `ArmnnDriver` tag. Please note that you need to add ARMNN_DRIVER_DEBUG := 1 to the 'device-vendor.mk' for the logcat to be visible. 86*3e777be0SXin Li 87*3e777be0SXin Li### Using the GPU tuner 88*3e777be0SXin Li 89*3e777be0SXin LiThe GPU tuner is a feature of the Compute Library that finds optimum values for GPU acceleration tuning parameters. 90*3e777be0SXin LiThere are three levels of tuning: exhaustive, normal and rapid. 91*3e777be0SXin LiExhaustive means that all lws values are tested. 92*3e777be0SXin LiNormal means that a reduced number of lws values are tested, but that generally is sufficient to have a performance close enough to the exhaustive approach. 93*3e777be0SXin LiRapid means that only 3 lws values should be tested for each kernel. 94*3e777be0SXin LiThe recommended way of using it with Arm NN is to generate the tuning data during development of the Android image for a device, and use it in read-only mode during normal operation: 95*3e777be0SXin Li 96*3e777be0SXin Li1. Run the Arm NN driver service executable in tuning mode. The path to the tuning data must be writable by the service. 97*3e777be0SXin LiThe following examples assume that the 1.2 version of the driver is being used: 98*3e777be0SXin Li<pre> 99*3e777be0SXin Liadb shell /system/vendor/bin/hw/android.hardware.neuralnetworks@1.2-service-armnn --cl-tuned-parameters-file <PATH_TO_TUNING_DATA> --cl-tuned-parameters-mode UpdateTunedParameters --cl-tuning-level exhaustive & 100*3e777be0SXin Li</pre> 101*3e777be0SXin Li2. Run a representative set of Android NNAPI testing loads. In this mode of operation, each NNAPI workload will be slow the first time it is executed, as the tuning parameters are being selected. Subsequent executions will use the tuning data which has been generated. 102*3e777be0SXin Li3. Stop the service. 103*3e777be0SXin Li4. Deploy the tuned parameters file to a location readable by the Arm NN driver service (for example, to a location within /vendor/etc). 104*3e777be0SXin Li5. During normal operation, pass the location of the tuning data to the driver service (this would normally be done by passing arguments via Android init in the service .rc definition): 105*3e777be0SXin Li<pre> 106*3e777be0SXin Liadb shell /system/vendor/bin/hw/android.hardware.neuralnetworks@1.2-service-armnn --cl-tuned-parameters-file <PATH_TO_TUNING_DATA> & 107*3e777be0SXin Li</pre> 108