xref: /aosp_15_r20/external/android-nn-driver/docs/IntegratorGuide.md (revision 3e777be0405cee09af5d5785ff37f7cfb5bee59a)
1Integration guide
2=================
3
4This document describes how to integrate the Arm NN Android NNAPI driver into an Android source tree.
5
6### Prerequisites
7
81. Android source tree for Android Q (we have tested against Android Q version 10.0.0_r39), in the directory `<ANDROID_ROOT>`
92. Android source tree for Android R (we have tested against Android R version 11.0.0_r3), in the directory `<ANDROID_ROOT>`
103. Android source tree for Android S (we have tested against Android S version 12.0.0_r1), in the directory `<ANDROID_ROOT>`
114. Android source tree for Android T (we have tested against Android T pre-release tag - TP1A.220624.003), in the directory `<ANDROID_ROOT>`
125. Mali OpenCL driver integrated into the Android source tree
13
14### Procedure
15
161. Place this source directory at `<ANDROID_ROOT>/vendor/arm/android-nn-driver`
172. Run setup.sh
183. Update the Android build environment to add the Arm NN driver. This ensures that the driver service
19is built and copied to the `system/vendor/bin/hw` directory in the Android image.
20To update the build environment, add to the contents of the variable `PRODUCT_PACKAGES`
21within the device-specific makefile that is located in the `<ANDROID_ROOT>/device/<manufacturer>/<product>`
22directory. This file is normally called `device.mk`:
23
24`Android.mk` contains the module definition of all versions (1.1, 1.2 and 1.3) of the Arm NN driver.
25
26For Android Q, a new version of the NN API is available (1.2),
27thus the following should be added to `device.mk` instead:
28<pre>
29PRODUCT_PACKAGES += [email protected]
30</pre>
31
32For Android R, S and T, new version of the NN API is available (1.3),
33thus the following should be added to `device.mk` instead:
34<pre>
35PRODUCT_PACKAGES += [email protected]
36</pre>
37
38Similarly, the Neon, CL or Reference backend can be enabled/disabled by setting ARMNN_COMPUTE_CL_ENABLE,
39ARMNN_COMPUTE_NEON_ENABLE or ARMNN_REF_ENABLE in `device.mk`:
40<pre>
41ARMNN_COMPUTE_CL_ENABLE := 1
42</pre>
43
44For all Android versions the vendor manifest.xml requires the Neural Network HAL information.
45For Android Q use HAL version 1.2 as below. For later Android versions substitute 1.3 where necessary.
46```xml
47<hal format="hidl">
48    <name>android.hardware.neuralnetworks</name>
49    <transport>hwbinder</transport>
50    <version>1.2</version>
51    <interface>
52        <name>IDevice</name>
53        <instance>armnn</instance>
54    </interface>
55    <fqname>@1.2::IDevice/armnn</fqname>
56</hal>
57```
58
594. Build Android as normal (https://source.android.com/setup/build/building)
605. To confirm that the Arm NN driver has been built, check for the driver service executable at
61
62Android Q
63<pre>
64<ANDROID_ROOT>/out/target/product/<product>/vendor/bin/hw
65</pre>
66
67### Testing
68
691. Run the Arm NN driver service executable in the background.
70Use the corresponding version of the driver for the Android version you are running.
71i.e
72[email protected] for Android Q and
73[email protected] for Android R, S and T
74<pre>
75It is also possible to use a specific backend by using the -c option.
76The following is an example of using the CpuAcc backend for Android Q:
77adb shell /system/vendor/bin/hw/android.hardware.neuralnetworks@1.2-service-armnn -c CpuAcc &
78</pre>
792. Run some code that exercises the Android Neural Networks API, for example Android's
80`NeuralNetworksTest` unit tests (note this is an optional component that must be built).
81<pre>
82adb shell /data/nativetest/NeuralNetworksTest_static/NeuralNetworksTest_static > NeuralNetworkTest.log
83</pre>
843. To confirm that the Arm NN driver is being used to service the Android Neural Networks API requests,
85check 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
87### Using the GPU tuner
88
89The GPU tuner is a feature of the Compute Library that finds optimum values for GPU acceleration tuning parameters.
90There are three levels of tuning: exhaustive, normal and rapid.
91Exhaustive means that all lws values are tested.
92Normal 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.
93Rapid means that only 3 lws values should be tested for each kernel.
94The 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
961. Run the Arm NN driver service executable in tuning mode. The path to the tuning data must be writable by the service.
97The following examples assume that the 1.2 version of the driver is being used:
98<pre>
99adb shell /system/vendor/bin/hw/android.hardware.neuralnetworks@1.2-service-armnn --cl-tuned-parameters-file &lt;PATH_TO_TUNING_DATA&gt; --cl-tuned-parameters-mode UpdateTunedParameters --cl-tuning-level exhaustive &
100</pre>
1012. 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.
1023. Stop the service.
1034. Deploy the tuned parameters file to a location readable by the Arm NN driver service (for example, to a location within /vendor/etc).
1045. 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<pre>
106adb shell /system/vendor/bin/hw/android.hardware.neuralnetworks@1.2-service-armnn --cl-tuned-parameters-file &lt;PATH_TO_TUNING_DATA&gt; &
107</pre>
108