From 8f397a1efed11e17e9f8cb12b53a72b7e32ab978 Mon Sep 17 00:00:00 2001 From: Sadik Armagan Date: Fri, 17 Jun 2022 15:38:22 +0100 Subject: IVGCVSW-6989 "Merged experimental/armnn_shim_sl" * Updated Serializer CMakeLists.txt to build armnnSerializerObj * Added constant tensors as input support to SL Signed-off-by: Sadik Armagan Change-Id: I22f6cf50147d99a01f7fe70d7446b114a4c57af3 --- shim/BuildGuideShimSupportLibrary.md | 162 +++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 shim/BuildGuideShimSupportLibrary.md (limited to 'shim/BuildGuideShimSupportLibrary.md') diff --git a/shim/BuildGuideShimSupportLibrary.md b/shim/BuildGuideShimSupportLibrary.md new file mode 100644 index 0000000000..dc69f8f77c --- /dev/null +++ b/shim/BuildGuideShimSupportLibrary.md @@ -0,0 +1,162 @@ +# How to use the Android NDK to build Arm NN + +- [Introduction](#introduction) +- [Prerequisites](#prerequisites) +- [Download Arm NN](#download-arm-nn) +- [Build Arm Compute Library](#build-arm-compute-library) +- [Build Arm NN](#build-arm-nn) +- [Build Arm NN Support Library](#build-arm-nn-support-library) +- [Build Arm NN Shim](#build-arm-nn-shim) + + +## Introduction +These are step by step instructions for building the Arm NN shim and support library for NNAPI. +This work is currently in an experimental phase. + +## Prerequisites + +The following are required to build the Arm NN support library +* Android NDK r20b + * Detailed setup can be found in [BuildGuideAndroidNDK.md](../BuildGuideAndroidNDK.md) +* Flatbuffer version 1.12.0 + * Detailed setup can be found in [BuildGuideCrossCompilation.md](../BuildGuideCrossCompilation.md) + +The following is required to build the Arm NN shim +* AOSP Source (Android Open Source Project) + * Download the source from the [official website](https://source.android.com/setup/build/downloading) + * This guide will use release tag `android12-s1-release` + + +Set environment variables +```bash +export WORKING_DIR= +export AOSP_ROOT= +export AOSP_MODULES_ROOT= +export ARMNN_BUILD_DIR= +export NDK=android-ndk-r20b +export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64 +export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH +export FLATBUFFERS_ANDROID_BUILD= +export FLATBUFFERS_X86_BUILD= +``` + +## Download Arm NN +If the user only wishes to build the Support Library with the NDK, the Arm NN repo can be cloned into any folder. +If the user also wishes to build the Arm NN Shim, for this the Arm NN repo will need to reside within +the Android tree in order for armnn/shim/Android.bp to be picked up by the Soong (Android) build system. +For example $AOSP_ROOT/vendor/arm/armnn + + +* Clone Arm NN: + (Requires Git if not previously installed: `sudo apt install git`) + +```bash +cd $WORKING_DIR +git clone https://github.com/ARM-software/armnn.git +``` + +## Build Arm Compute Library + +Arm NN provides a script that downloads the version of Arm Compute Library that Arm NN was tested with: +```bash +${WORKING_DIR}/armnn/scripts/get_compute_library.sh +``` +* Build the Arm Compute Library: + (Requires SCons if not previously installed: `sudo apt install scons`) +```bash +cd ${WORKING_DIR}/clframework + +scons arch=arm64-v8a \ +toolchain_prefix=aarch64-linux-android- \ +compiler_prefix=aarch64-linux-android29- \ +neon=1 opencl=1 \ +embed_kernels=1 \ +build_dir=android-arm64v8a \ +extra_cxx_flags="-Wno-parentheses-equality -Wno-missing-braces -fPIC" \ +Werror=0 embed_kernels=1 examples=0 \ +validation_tests=0 benchmark_tests=0 benchmark_examples=0 os=android -j16 +``` + +## Build Arm NN and Serializer + +* Build Arm NN: + (Requires CMake if not previously installed: `sudo apt install cmake`) +```bash +cd $ARMNN_BUILD_DIR +CXX=aarch64-linux-android29-clang++ \ +CC=aarch64-linux-android29-clang \ +CXX_FLAGS="-fPIE -fPIC" cmake ${WORKING_DIR}/armnn \ +-DCMAKE_ANDROID_NDK=$NDK \ +-DCMAKE_SYSTEM_NAME=Android \ +-DCMAKE_SYSTEM_VERSION=29 \ +-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \ +-DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \ +-DARMCOMPUTE_ROOT=$WORKING_DIR/clframework/ \ +-DARMCOMPUTE_BUILD_DIR=$WORKING_DIR/clframework/build/android-arm64v8a/ \ +-DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \ +-DFLATBUFFERS_ROOT=$FLATBUFFERS_ANDROID_BUILD \ +-DFLATC_DIR=$FLATBUFFERS_X86_BUILD \ +-DBUILD_ARMNN_SERIALIZER=1 -DBUILD_GATORD_MOCK=0 -DBUILD_BASE_PIPE_SERVER=0 +``` + + * Run the build +```bash +make -j16 +``` + +## Build Arm NN Support Library + +Building the support library requires building some AOSP libraries via the NDK. +It should be possible to use $AOSP_ROOT instead of $AOSP_MODULES_ROOT. + +However this example will instead clone the necessary AOSP repos outside of the Android tree and apply some minor patches +which were required to get it to build with the Android version used in this guide. + +```bash +# Call a script which will clone the necessary AOSP repos (do not clone them into Android tree) +${WORKING_DIR}/armnn/shim/sl/scripts/clone_aosp_libs.sh $AOSP_MODULES_ROOT + +# Modify the repos by applying patches +${WORKING_DIR}/armnn/shim/sl/scripts/modify_aosp_libs.sh $AOSP_MODULES_ROOT + +# Build the Support Library +CMARGS="$CMARGS \ +-DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \ +-DANDROID_ABI=arm64-v8a \ +-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \ +-DCMAKE_ANDROID_NDK=$NDK \ +-DANDROID_PLATFORM=android-29 \ +-DAOSP_MODULES_ROOT=$AOSP_MODULES_ROOT \ +-DARMNN_SOURCE_DIR=$WORKING_DIR/armnn \ +-DArmnn_DIR=$ARMNN_BUILD_DIR " + +mkdir ${WORKING_DIR}/armnn/shim/sl/build +cd ${WORKING_DIR}/armnn/shim/sl/build + +CXX=aarch64-linux-android29-clang++ \ +CC=aarch64-linux-android29-clang \ +cmake $CMARGS ../ +make +``` + +## Build Arm NN Shim + +By default the Arm NN shim Android.bp is not enabled. +Enable it by editing armnn/shim/Android.bp and setting `enabled: true` + +```bash +cd $AOSP_ROOT +source build/envsetup.sh +lunch -eng +cd vendor/arm/armnn/shim +export ARMNN_ANDROID_MK_ENABLE=0 +mm +``` + +The built libraries and manifest file can be found here: +$AOSP_ROOT/out/target/product//vendor/lib64/libarmnn_support_library.so +$AOSP_ROOT/out/target/product//vendor/bin/hw/android.hardware.neuralnetworks-shim-service-armnn +$AOSP_ROOT/out/target/product//vendor/etc/vintf/manifest/android.hardware.neuralnetworks-shim-service-armnn.xml + +Currently the Arm NN libraries are shared libraries and therefore will need to be pushed to the device: +$ARMNN_BUILD_DIR/libarmnn.so -- cgit v1.2.1