From 16929a2b432232f7a34fcbd1f1b0fe1212500206 Mon Sep 17 00:00:00 2001 From: James Conroy Date: Wed, 13 Jul 2022 12:57:53 +0100 Subject: IVGCVSW-6776 Add Arm NN build-tool scripts * Initial scripts which download and build Arm NN dependencies (setup-armnn.sh) and build both Arm NN and ACL (build-armnn.sh). * Also added common.sh for handling common variables between scripts and validation.sh for common command line arg validation. Signed-off-by: James Conroy Change-Id: I6a61149d048f618b4cc9770cc9dd6ec2e5f64ea5 --- build-tool/scripts/build-armnn.sh | 414 ++++++++++++++++++++++++++++++++++ build-tool/scripts/common.sh | 75 +++++++ build-tool/scripts/setup-armnn.sh | 452 ++++++++++++++++++++++++++++++++++++++ build-tool/scripts/validation.sh | 51 +++++ 4 files changed, 992 insertions(+) create mode 100755 build-tool/scripts/build-armnn.sh create mode 100755 build-tool/scripts/common.sh create mode 100755 build-tool/scripts/setup-armnn.sh create mode 100755 build-tool/scripts/validation.sh diff --git a/build-tool/scripts/build-armnn.sh b/build-tool/scripts/build-armnn.sh new file mode 100755 index 0000000000..2b8217b313 --- /dev/null +++ b/build-tool/scripts/build-armnn.sh @@ -0,0 +1,414 @@ +#!/bin/bash +# +# Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +# SPDX-License-Identifier: MIT +# + +# Script which builds Arm NN and ACL +# setup-armnn.sh must be executed in the same directory, before running this script + +set -o nounset # Catch references to undefined variables. +set -o pipefail # Catch non zero exit codes within pipelines. +set -o errexit # Catch and propagate non zero exit codes. + +rel_path=$(dirname "$0") # relative path from where script is executed to script location + +build_acl() +{ + cd "$ACL_SRC" + + local acl_params="neon=$flag_neon_backend opencl=$flag_cl_backend Werror=0 embed_kernels=1 examples=0 validation_tests=0 benchmark_tests=0 benchmark_examples=0" + + if [ "$flag_debug" -eq 1 ]; then + acl_params="$acl_params debug=1 asserts=1" + fi + + local native_flag="" + if [ "$NATIVE_BUILD" ]; then + native_flag="build=native" + fi + + # Force -fPIC so that ACL is suitable for inclusion in Arm NN library + local extra_cxx_flags="extra_cxx_flags='-fPIC'" + + local compile_flags="" + local acl_arch="" + + case "$TARGET_ARCH" in + "aarch64") + compile_flags+="$AARCH64_COMPILER_FLAGS" + acl_arch="arch=arm64-v8a" + ;; + + "aarch32") + compile_flags+="$AARCH32_COMPILER_FLAGS" + acl_arch="arch=armv7a" + ;; + + "x86_64") + acl_arch="arch=x86_64" + ;; + esac + + echo -e "\n***** Building ACL for $TARGET_ARCH *****" + + if [ "$flag_clean" -eq 1 ]; then + echo -e "\n***** Clean flag detected: removing existing ACL build *****" + rm -rf "$ACL_BUILD_TARGET" + fi + + mkdir -p "$ACL_BUILD_TARGET" + + eval "$compile_flags" \ + scons "$native_flag" \ + "$acl_arch" \ + "$acl_params" \ + build_dir="$ACL_BUILD_TARGET" \ + "$extra_cxx_flags" \ + -j "$NUM_THREADS" + + echo -e "\n***** Built ACL for $TARGET_ARCH *****" + + return 0 +} + +build_armnn() +{ + mkdir -p "$ARMNN_BUILD_TARGET" + cd "$ARMNN_BUILD_TARGET" + + local build_type="Release" + if [ "$flag_debug" -eq 1 ]; then + build_type="Debug" + fi + + local compile_flags="" + + case "$TARGET_ARCH" in + "aarch64") + compile_flags+="$AARCH64_COMPILER_FLAGS" + ;; + + "aarch32") + compile_flags+="$AARCH32_COMPILER_FLAGS" + ;; + esac + + if [ "$flag_clean" -eq 1 ]; then + echo -e "\n***** Clean flag detected: removing existing Arm NN build *****" + rm -rf "$ARMNN_BUILD_TARGET" + fi + + echo -e "\n***** Building Arm NN for $TARGET_ARCH *****" + + eval "$compile_flags" \ + cmake -DCMAKE_BUILD_TYPE="$build_type" \ + -DBUILD_ARMNN_TFLITE_DELEGATE="$flag_tflite_delegate" \ + -DBUILD_TF_LITE_PARSER="$flag_tflite_parser" \ + -DBUILD_ONNX_PARSER="$flag_onnx_parser" \ + -DARMCOMPUTENEON="$flag_neon_backend" \ + -DARMCOMPUTECL="$flag_cl_backend" \ + -DARMNNREF="$flag_ref_backend" \ + -DARMCOMPUTE_ROOT="$ACL_SRC" \ + -DARMCOMPUTE_BUILD_DIR="$ACL_BUILD_TARGET" \ + -DTENSORFLOW_ROOT="$TENSORFLOW_SRC" \ + -DTF_LITE_SCHEMA_INCLUDE_PATH="$TFLITE_BUILD_ROOT" \ + -DTFLITE_LIB_ROOT="$TFLITE_BUILD_TARGET" \ + -DFLATBUFFERS_ROOT="$FLATBUFFERS_BUILD_TARGET" \ + -DFLATC_DIR="$FLATBUFFERS_BUILD_HOST" \ + -DONNX_GENERATED_SOURCES="$ONNX_BUILD_TARGET" \ + -DPROTOBUF_ROOT="$PROTOBUF_BUILD_HOST" \ + -DPROTOBUF_LIBRARY_DEBUG="$PROTOBUF_LIBRARY_TARGET" \ + -DPROTOBUF_LIBRARY_RELEASE="$PROTOBUF_LIBRARY_TARGET" \ + "$armnn_cmake_args" \ + "$ARMNN_SRC" + + make -j "$NUM_THREADS" + + # Copy protobuf library into Arm NN build directory, if ONNX Parser is enabled + if [ "$flag_onnx_parser" -eq 1 ]; then + cd "$ARMNN_BUILD_TARGET" + rm -f libprotobuf.so libprotobuf.so.23 libprotobuf.so.23.0.0 + cp "$PROTOBUF_LIBRARY_TARGET" . + ln -s libprotobuf.so.23.0.0 ./libprotobuf.so.23 + ln -s libprotobuf.so.23.0.0 ./libprotobuf.so + fi + + echo -e "\n***** Built Arm NN for $TARGET_ARCH *****" + + local tarball_path="$ROOT_DIR/armnn_$ARMNN_BUILD_DIR_NAME.tar.gz" + echo -e "\n***** Creating tarball of Arm NN build at $tarball_path *****" + + cd "$ARMNN_BUILD_ROOT" + rm -f "$tarball_path" + tar -czf "$tarball_path" "$ARMNN_BUILD_DIR_NAME" + + echo -e "\n***** Created tarball of Arm NN build at $ROOT_DIR/armnn_$ARMNN_BUILD_DIR_NAME.tar.gz *****" + echo -e "\n***** To extract tarball, run: tar -xzf armnn_$ARMNN_BUILD_DIR_NAME.tar.gz *****\n" + + return 0 +} + +usage() +{ + cat < + provide additional space-separated CMake arguments for building Arm NN (optional) + --acl-scons-params= + provide additional space-separated scons parameters for building ACL (optional) + --num-threads= + specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host) + -h, --help + print brief usage information and exit + -x + enable shell tracing in this script + +At least one component (i.e. --tflite-delegate, --tflite-parser, --onnx-parser) must be provided or else provide --all to build all Arm NN components. +At least one backend (i.e. --neon-backend, --cl-backend, --ref-backend) must be chosen. +This script must be executed from the same root directory in which setup-armnn.sh was executed from. + +This script will build using Arm NN and ACL repositories checked out in /source, downloaded using setup-armnn.sh. +Alternatively, place custom/modified repositories named "armnn" and "acl" in /source. + +By default, a tarball tar.gz archive of the Arm NN build will be created in the directory from which this script is called from. + +Examples: +Build for aarch64 with all Arm NN components, NEON enabled and OpenCL enabled: + /build-armnn.sh --target-arch=aarch64 --all --neon-backend --cl-backend +Build for aarch64 with TF Lite Delegate, OpenCL enabled and additional ACL scons params: + /build-armnn.sh --target-arch=aarch64 --tflite-delegate --cl-backend --acl-scons-params="compress_kernels=1 benchmark_examples=1" +Setup for aarch32 with all Arm NN dependencies, OpenCL enabled and additional Arm NN cmake args: + /build-armnn.sh --target-arch=aarch32 --all --cl-backend --armnn-cmake-args="-DBUILD_SAMPLE_APP=1 -DBUILD_UNIT_TESTS=0" +EOF +} + +# This will catch in validation.sh if not set +target_arch="" + +# Default flag values +flag_tflite_delegate=0 +flag_tflite_parser=0 +flag_onnx_parser=0 +flag_neon_backend=0 +flag_cl_backend=0 +flag_ref_backend=0 +flag_clean=0 +flag_debug=0 + +# Empty strings for optional additional args by default +armnn_cmake_args="" +acl_scons_params="" + +# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used +num_threads=0 + +name=$(basename "$0") + +# If no options provided, show help +if [ $# -eq 0 ]; then + usage + exit 1 +fi + +args=$(getopt -ohx -l tflite-delegate,tflite-parser,onnx-parser,all,target-arch:,neon-backend,cl-backend,ref-backend,clean,debug,armnn-cmake-args:,acl-scons-params:,num-threads:,help -n "$name" -- "$@") +eval set -- "$args" +while [ $# -gt 0 ]; do + if [ -n "${opt_prev:-}" ]; then + eval "$opt_prev=\$1" + opt_prev= + shift 1 + continue + elif [ -n "${opt_append:-}" ]; then + if [ -n "$1" ]; then + eval "$opt_append=\"\${$opt_append:-} \$1\"" + fi + opt_append= + shift 1 + continue + fi + case $1 in + --tflite-parser) + flag_tflite_parser=1 + ;; + + --tflite-delegate) + flag_tflite_delegate=1 + ;; + + --onnx-parser) + flag_onnx_parser=1 + ;; + + --all) + flag_tflite_delegate=1 + flag_tflite_parser=1 + flag_onnx_parser=1 + ;; + + --target-arch) + opt_prev=target_arch + ;; + + --neon-backend) + flag_neon_backend=1 + ;; + + --cl-backend) + flag_cl_backend=1 + ;; + + --ref-backend) + flag_ref_backend=1 + ;; + + --clean) + flag_clean=1 + ;; + + --debug) + flag_debug=1 + ;; + + --armnn-cmake-args) + opt_prev=armnn_cmake_args + ;; + + --acl-scons-params) + opt_prev=acl_scons_params + ;; + + --num-threads) + opt_prev=num_threads + ;; + + -h | --help) + usage + exit 0 + ;; + + -x) + set -x + ;; + + --) + shift + break 2 + ;; + + esac + shift 1 +done + +# shellcheck source=common.sh +source "$rel_path"/common.sh + +# Validation of chosen Arm NN backends +if [ "$flag_neon_backend" -eq 0 ] && [ "$flag_cl_backend" -eq 0 ] && [ "$flag_ref_backend" -eq 0 ]; then + echo -e "\n$name: at least one of flags --neon-backend, --cl-backend or --ref-backend must be set." + exit 1 +fi + +if [ "$target_arch" == "x86_64" ]; then + if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then + echo "$name: Accelerated backends --neon-backend and --cl-backend are supported on Arm targets only (x86_64 chosen)." + exit 1 + fi +fi + +# Verify that root source and build directories are present (post execution of setup-armnn.sh) +if [ ! -d "$SOURCE_DIR" ]; then + echo -e "\nERROR: Root source directory does not exist at $SOURCE_DIR" + echo "Please check that:" + echo "1. setup-armnn.sh was executed successfully prior to running this script" + echo "2. This script is being executed in the same directory as setup-armnn.sh" + + exit 1 +fi + +if [ ! -d "$BUILD_DIR" ]; then + echo -e "\nERROR: Root build directory does not exist at $BUILD_DIR" + echo "Please check that:" + echo "1. setup-armnn.sh was executed successfully prior to running this script" + echo "2. This script is being executed in the same directory as setup-armnn.sh" + + exit 1 +fi + +# Verify that Arm NN and ACL exist in correct paths prior to script execution +if [ ! -d "$ARMNN_SRC" ]; then + echo -e "\nERROR: Arm NN repo does not exist as expected at $ARMNN_SRC" + exit 1 +fi + +if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then + if [ ! -d "$ACL_SRC" ]; then + echo -e "\nERROR: ACL repo does not exist as expected at $ACL_SRC" + exit 1 + fi +fi + +# Adjust output build directory names for Arm NN and ACL if debug is enabled +DEBUG_POSTFIX="" +if [ "$flag_debug" -eq 1 ]; then + DEBUG_POSTFIX="_debug" +fi + +# Directories for Arm NN and ACL build outputs +ARMNN_BUILD_ROOT="$BUILD_DIR"/armnn +ARMNN_BUILD_DIR_NAME="$TARGET_ARCH"_build"$DEBUG_POSTFIX" +ARMNN_BUILD_TARGET="$ARMNN_BUILD_ROOT"/"$ARMNN_BUILD_DIR_NAME" +ACL_BUILD_TARGET="$BUILD_DIR"/acl/"$TARGET_ARCH"_build"$DEBUG_POSTFIX" + +echo -e "\nINFO: Displaying configuration information before execution of $name" +echo " target-arch: $TARGET_ARCH" +echo " host-arch: $HOST_ARCH" +echo " tflite-delegate: $flag_tflite_delegate" +echo " tflite-parser: $flag_tflite_parser" +echo " onnx-parser: $flag_onnx_parser" +echo " neon-backend: $flag_neon_backend" +echo " cl-backend: $flag_cl_backend" +echo " ref-backend: $flag_ref_backend" +echo " clean: $flag_clean" +echo " debug: $flag_debug" +echo "armnn-cmake-args: $armnn_cmake_args" +echo "acl-scons-params: $acl_scons_params" +echo " num-threads: $NUM_THREADS" +echo " root directory: $ROOT_DIR" +echo "source directory: $SOURCE_DIR" +echo " build directory: $BUILD_DIR" +echo " armnn build dir: $ARMNN_BUILD_TARGET" +echo -e "\nScript execution will begin in 10 seconds..." + +sleep 10 + +if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then + build_acl +else + echo -e "\n***** Skipping ACL build: --neon-backend and --cl-backend not set in options. *****" +fi + +build_armnn + +exit 0 \ No newline at end of file diff --git a/build-tool/scripts/common.sh b/build-tool/scripts/common.sh new file mode 100755 index 0000000000..522bfb68cd --- /dev/null +++ b/build-tool/scripts/common.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# +# Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +# SPDX-License-Identifier: MIT +# + +# Script which stores common variables and paths used by setup-armnn.sh and build-armnn.sh + +# shellcheck disable=SC2034 +# SC2034: false positives for variables appear unused - variables are used in setup-armnn.sh and build-armnn.sh + +set -o nounset # Catch references to undefined variables. +set -o pipefail # Catch non zero exit codes within pipelines. +set -o errexit # Catch and propagate non zero exit codes. + +# ROOT_DIR is the directory in which a script is called from +ROOT_DIR=$(pwd) +SOURCE_DIR="$ROOT_DIR"/source +BUILD_DIR="$ROOT_DIR"/build + +# Host architecture e.g. x86_64, aarch64 +HOST_ARCH=$(uname -m) + +# Number of online cores on host +NUM_THREADS=$(getconf _NPROCESSORS_ONLN) + +# Validate common user-defined options +# shellcheck source=validation.sh +source "$rel_path"/validation.sh + +# target_arch supplied as command line arg +TARGET_ARCH="$target_arch" + +NATIVE_BUILD=0 +if [ "$TARGET_ARCH" == "$HOST_ARCH" ]; then + NATIVE_BUILD=1 +fi + +AARCH64_COMPILER_FLAGS+="CC=/usr/bin/aarch64-linux-gnu-gcc CXX=/usr/bin/aarch64-linux-gnu-g++ " +AARCH32_COMPILER_FLAGS+="CC=/usr/bin/arm-linux-gnueabihf-gcc CXX=/usr/bin/arm-linux-gnueabihf-g++ " + +# Flatbuffers +FLATBUFFERS_VERSION=1.12.0 +FLATBUFFERS_SRC="$SOURCE_DIR"/flatbuffers-"$FLATBUFFERS_VERSION" +FLATBUFFERS_BUILD_ROOT="$BUILD_DIR"/flatbuffers +FLATBUFFERS_BUILD_TARGET="$FLATBUFFERS_BUILD_ROOT"/"$TARGET_ARCH"_build +FLATBUFFERS_BUILD_HOST="$FLATBUFFERS_BUILD_ROOT"/"$HOST_ARCH"_build # Location of flatc compiler + +# Tensorflow +TENSORFLOW_SRC="$SOURCE_DIR"/tensorflow +TFLITE_SRC="$TENSORFLOW_SRC"/tensorflow/lite +SCHEMA_SRC="$TFLITE_SRC"/schema/schema.fbs + +# TF Lite Schema +FLATC="$FLATBUFFERS_BUILD_HOST"/bin/flatc +TFLITE_BUILD_ROOT="$BUILD_DIR"/tflite # Generated TF Lite Schema location +TFLITE_BUILD_TARGET="$TFLITE_BUILD_ROOT"/"$TARGET_ARCH"_build + +# Protobuf +PROTOBUF_VERSION=3.12.0 +PROTOBUF_SRC="$SOURCE_DIR"/protobuf-"$PROTOBUF_VERSION" +PROTOBUF_BUILD_ROOT="$BUILD_DIR"/protobuf +PROTOBUF_BUILD_HOST="$PROTOBUF_BUILD_ROOT"/"$HOST_ARCH"_build +PROTOCOL_COMPILER_HOST="$PROTOBUF_BUILD_HOST"/bin/protoc +PROTOBUF_BUILD_TARGET="$PROTOBUF_BUILD_ROOT"/"$TARGET_ARCH"_build +PROTOBUF_LIBRARY_TARGET="$PROTOBUF_BUILD_TARGET"/lib/libprotobuf.so.23.0.0 + +# ONNX +ONNX_VERSION=1.6.0 +ONNX_SRC="$SOURCE_DIR"/onnx-"$ONNX_VERSION" +ONNX_BUILD_TARGET="$BUILD_DIR"/onnx/"$TARGET_ARCH"_build + +# Arm NN / ACL +ARMNN_SRC="$SOURCE_DIR"/armnn +ACL_SRC="$SOURCE_DIR"/acl \ No newline at end of file diff --git a/build-tool/scripts/setup-armnn.sh b/build-tool/scripts/setup-armnn.sh new file mode 100755 index 0000000000..6a9c22ebef --- /dev/null +++ b/build-tool/scripts/setup-armnn.sh @@ -0,0 +1,452 @@ +#!/bin/bash +# +# Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +# SPDX-License-Identifier: MIT +# + +# Script which downloads and builds Arm NN dependencies +# Perquisite to running build-armnn.sh + +set -o nounset # Catch references to undefined variables. +set -o pipefail # Catch non zero exit codes within pipelines. +set -o errexit # Catch and propagate non zero exit codes. + +rel_path=$(dirname "$0") # relative path from where script is executed to script location + +# Download an archive using wget and extract using tar +# Takes three arguments: +# 1. Name of dependency being downloaded e.g. Flatbuffers +# 2. Link to archive +# 3. Filename given to archive upon downloading +download_and_extract() +{ + cd "$SOURCE_DIR" + + echo -e "\n***** Downloading $1 *****" + wget -O "$3" "$2" + + echo -e "\n***** Extracting archive *****" + tar -xzf "$3" + + echo -e "\n***** Removing archive *****" + rm "$3" + + echo -e "\n***** $1 downloaded *****" +} + +download_protobuf() +{ + download_and_extract \ + "Protobuf" \ + "https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz" \ + "protobuf-all-$PROTOBUF_VERSION.tar.gz" +} + +build_protobuf() +{ + local native_build=$1 + local build_dir="$PROTOBUF_BUILD_TARGET" + local cmake_flags="" + local target_arch="$TARGET_ARCH" + local additional_cmds="" + + if [ "$native_build" -eq 0 ]; then + mkdir -p "$PROTOBUF_BUILD_TARGET" + additional_cmds+="--with-protoc=$PROTOCOL_COMPILER_HOST " + if [ "$TARGET_ARCH" == "aarch64" ]; then + cmake_flags+="$AARCH64_COMPILER_FLAGS" + additional_cmds+="--host=aarch64-linux " + elif [ "$TARGET_ARCH" == "aarch32" ]; then + cmake_flags+="$AARCH32_COMPILER_FLAGS" + additional_cmds+="--host=arm-linux " + fi + else + target_arch="$HOST_ARCH" + mkdir -p "$PROTOBUF_BUILD_HOST" + build_dir="$PROTOBUF_BUILD_HOST" + fi + + echo -e "\n***** Building Protobuf for $target_arch ***** " + + cd "$PROTOBUF_BUILD_ROOT" + + # Cleanup any previous cmake files, except actual builds which we keep + find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} + + + eval "$cmake_flags" \ + "$PROTOBUF_SRC"/configure --prefix="$build_dir" "$additional_cmds" + make install -j "$NUM_THREADS" + + echo -e "\n***** Protobuf built for $target_arch ***** " +} + +download_flatbuffers() +{ + download_and_extract \ + "Flatbuffers" \ + "https://github.com/google/flatbuffers/archive/v$FLATBUFFERS_VERSION.tar.gz" \ + "flatbuffers-$FLATBUFFERS_VERSION.tar.gz" +} + +build_flatbuffers() +{ + local native_build=$1 + local build_dir="$FLATBUFFERS_BUILD_TARGET" + local target_arch="$TARGET_ARCH" + + local cmake_flags="CXXFLAGS=-fPIC " + + if [ "$native_build" -eq 0 ]; then + mkdir -p "$FLATBUFFERS_BUILD_TARGET" + if [ "$TARGET_ARCH" == "aarch64" ]; then + cmake_flags+="$AARCH64_COMPILER_FLAGS" + elif [ "$TARGET_ARCH" == "aarch32" ]; then + cmake_flags+="$AARCH32_COMPILER_FLAGS" + fi + else + target_arch="$HOST_ARCH" + mkdir -p "$FLATBUFFERS_BUILD_HOST" + build_dir="$FLATBUFFERS_BUILD_HOST" + fi + + echo -e "\n***** Building flatbuffers for $target_arch *****" + + mkdir -p "$FLATBUFFERS_BUILD_ROOT" + cd "$FLATBUFFERS_BUILD_ROOT" + + # Cleanup any previous cmake files, except actual builds which we keep + find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} + + + eval "$cmake_flags" \ + cmake -DFLATBUFFERS_BUILD_FLATC=1 \ + -DCMAKE_INSTALL_PREFIX:PATH="$build_dir" \ + -DFLATBUFFERS_BUILD_TESTS=0 \ + "$FLATBUFFERS_SRC" + make all install -j "$NUM_THREADS" + + echo -e "\n***** Built flatbuffers for $target_arch *****" +} + +download_tensorflow() +{ + cd "$SOURCE_DIR" + + echo -e "\n***** Downloading TensorFlow *****" + git clone https://github.com/tensorflow/tensorflow.git + cd "$TENSORFLOW_SRC" + + git checkout "$("$ARMNN_SRC"/scripts/get_tensorflow.sh -p)" + echo -e "\n***** TensorFlow downloaded *****" +} + +build_tflite() +{ + mkdir -p "$TFLITE_BUILD_TARGET" + cd "$TFLITE_BUILD_TARGET" + + local target_arch_cmd="" # default is native, no command needed + local cmake_flags="" + + case "$TARGET_ARCH" in + "aarch64") + cmake_flags+="$AARCH64_COMPILER_FLAGS" + target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=aarch64 " + + if [ "$NATIVE_BUILD" -eq 0 ]; then + cmake_flags+="ARMCC_FLAGS='-funsafe-math-optimizations' " + fi + ;; + + "aarch32") + cmake_flags+="$AARCH32_COMPILER_FLAGS" + target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=armv7 " + + if [ "$NATIVE_BUILD" -eq 0 ]; then + cmake_flags+="ARMCC_FLAGS='-march=armv7-a -mfpu=neon-vfpv4 -funsafe-math-optimizations -mfp16-format=ieee' " + fi + ;; + esac + + echo -e "\n***** Building TF Lite for $TARGET_ARCH *****" + + # Cleanup any previous cmake files, except actual builds which we keep + find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} + + + eval "$cmake_flags" \ + cmake -DTFLITE_ENABLE_XNNPACK=OFF \ + "$target_arch_cmd" \ + "$TFLITE_SRC" + cmake --build . -j "$NUM_THREADS" + + echo -e "\n***** Built TF Lite for $TARGET_ARCH *****" +} + +generate_tflite_schema() +{ + echo -e "\n***** Generating TF Lite Schema *****" + mkdir -p "$TFLITE_BUILD_ROOT" + cd "$TFLITE_BUILD_ROOT" + + cp "$SCHEMA_SRC" . + + $FLATC -c --gen-object-api --reflect-types --reflect-names schema.fbs + + echo -e "\n***** Generated TF Lite Schema *****" +} + +download_onnx() +{ + download_and_extract \ + "ONNX" \ + "https://github.com/onnx/onnx/releases/download/v$ONNX_VERSION/onnx-$ONNX_VERSION.tar.gz" \ + "onnx-$ONNX_VERSION.tar.gz" +} + +generate_onnx_sources() +{ + mkdir -p "$ONNX_BUILD_TARGET" + cd "$ONNX_SRC" + + echo -e "\n***** Generating ONNX sources for $TARGET_ARCH *****" + + export LD_LIBRARY_PATH="$PROTOBUF_BUILD_HOST"/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} + + eval "$PROTOCOL_COMPILER_HOST" onnx/onnx.proto \ + --proto_path=. \ + --proto_path="$ONNX_SRC" \ + --proto_path="$PROTOBUF_BUILD_HOST"/include \ + --cpp_out "$ONNX_BUILD_TARGET" + + echo -e "\n***** Generated ONNX sources for $TARGET_ARCH *****" +} + +download_armnn() +{ + cd "$SOURCE_DIR" + + echo -e "\n***** Downloading Arm NN *****" + + # Latest release branch of Arm NN is checked out by default + git clone https://github.com/ARM-software/armnn.git armnn + + cd "$ARMNN_SRC" + armnn_branch="$(git rev-parse --abbrev-ref HEAD)" + + echo -e "\n***** Arm NN Downloaded: $armnn_branch *****" +} + +download_acl() +{ + cd "$SOURCE_DIR" + + echo -e "\n***** Downloading ACL *****" + + git clone https://github.com/ARM-software/ComputeLibrary.git acl + + # Get corresponding release tag for ACL by parsing release branch number for Arm NN + local acl_tag="" + acl_tag="$(echo "$armnn_branch" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /./g')" + + cd "$ACL_SRC" + git checkout v"$acl_tag" + + echo -e "\n***** ACL Downloaded: $acl_tag *****" +} + +usage() +{ + cat < + specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host) + -h, --help + print brief usage information and exit + -x + enable shell tracing in this script + +At least one dependency flag (e.g. --tflite-delegate) must be provided or else provide --all to setup all dependencies. +Directories called "source" and "build" will be generated in the current directory (ROOT_DIR) from which this script is called. +It's recommended to call this script in a directory outside of this Arm NN source repo, to avoid nested repositories. + +This script will download the latest release branches of Arm NN and ACL, by default. +Alternatively, manually create a "source" directory in ROOT_DIR and place custom repositories there. +Custom repositories in "source" must be named "armnn" and "acl". + +Examples: +Setup for aarch64 with all Arm NN dependencies: + /setup-armnn.sh --target-arch=aarch64 --all +Setup for aarch64 with TF Lite Delegate and TF Lite Parser dependencies only: + /setup-armnn.sh --target-arch=aarch64 --tflite-delegate --tflite-parser +Setup for aarch32 with all Arm NN dependencies: + /setup-armnn.sh --target-arch=aarch32 --all +EOF +} + +# This will catch in validation.sh if not set +target_arch="" + +# Default flag values +flag_tflite_delegate=0 +flag_tflite_parser=0 +flag_onnx_parser=0 + +# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used +num_threads=0 + +name=$(basename "$0") + +# If no options provided, show help +if [ $# -eq 0 ]; then + usage + exit 1 +fi + +args=$(getopt -ohx -l tflite-delegate,tflite-parser,onnx-parser,all,target-arch:,num-threads:,help -n "$name" -- "$@") +eval set -- "$args" +while [ $# -gt 0 ]; do + if [ -n "${opt_prev:-}" ]; then + eval "$opt_prev=\$1" + opt_prev= + shift 1 + continue + elif [ -n "${opt_append:-}" ]; then + if [ -n "$1" ]; then + eval "$opt_append=\"\${$opt_append:-} \$1\"" + fi + opt_append= + shift 1 + continue + fi + case $1 in + --tflite-parser) + flag_tflite_parser=1 + ;; + + --tflite-delegate) + flag_tflite_delegate=1 + ;; + + --onnx-parser) + flag_onnx_parser=1 + ;; + + --all) + flag_tflite_delegate=1 + flag_tflite_parser=1 + flag_onnx_parser=1 + ;; + + --target-arch) + opt_prev=target_arch + ;; + + --num-threads) + opt_prev=num_threads + ;; + + -h | --help) + usage + exit 0 + ;; + + -x) + set -x + ;; + + --) + shift + break 2 + ;; + + esac + shift 1 +done + +# shellcheck source=common.sh +source "$rel_path"/common.sh + +echo -e "\nINFO: Displaying configuration information before execution of $name" +echo " target-arch: $TARGET_ARCH" +echo " host-arch: $HOST_ARCH" +echo " tflite-delegate: $flag_tflite_delegate" +echo " tflite-parser: $flag_tflite_parser" +echo " onnx-parser: $flag_onnx_parser" +echo " num-threads: $NUM_THREADS" +echo " root directory: $ROOT_DIR" +echo "source directory: $SOURCE_DIR" +echo " build directory: $BUILD_DIR" + +if [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]; then + echo -e "\n***** WARNING: Running script inside a git repository. To avoid nested repos, call this script from outside of this repo. *****" +fi + +echo -e "\nScript execution will begin in 10 seconds..." + +sleep 10 + +mkdir -p "$SOURCE_DIR" +mkdir -p "$BUILD_DIR" + +if [ -d "$ARMNN_SRC" ]; then + echo -e "\n***** Arm NN source repository already located at $ARMNN_SRC. Skipping cloning of Arm NN. *****" +else + download_armnn +fi + +if [ -d "$ACL_SRC" ]; then + echo -e "\n***** ACL source repository already located at $ACL_SRC. Skipping cloning of ACL. *****" +else + download_acl +fi + +if [ "$flag_tflite_delegate" -eq 1 ] || [ "$flag_tflite_parser" -eq 1 ]; then + download_flatbuffers + + # Host build + build_flatbuffers 1 + + # Target build for cross compile + if [ "$NATIVE_BUILD" -eq 0 ]; then + build_flatbuffers 0 + fi + + download_tensorflow +fi + +if [ "$flag_tflite_parser" -eq 1 ]; then + generate_tflite_schema +fi + +if [ "$flag_tflite_delegate" -eq 1 ]; then + build_tflite +fi + +if [ "$flag_onnx_parser" -eq 1 ]; then + download_protobuf + + # Host build + build_protobuf 1 + + # Target build for cross compile + if [ "$NATIVE_BUILD" -eq 0 ]; then + build_protobuf 0 + fi + + download_onnx + generate_onnx_sources +fi + +echo -e "\n***** Arm NN setup complete. Now build with build-armnn.sh. *****\n" + +exit 0 \ No newline at end of file diff --git a/build-tool/scripts/validation.sh b/build-tool/scripts/validation.sh new file mode 100755 index 0000000000..785c1b8f83 --- /dev/null +++ b/build-tool/scripts/validation.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# +# Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +# SPDX-License-Identifier: MIT +# + +# Common validation of command line arguments provided to setup-armnn.sh and build-armnn.sh + +# shellcheck disable=SC2034,SC2154 +# SC2034: false positives for variables appear unused - variables are used in setup-armnn.sh and build-armnn.sh +# SC2154: false positives for variables referenced but not assigned - variables are assigned in setup-armnn.sh and build-armnn.sh + +set -o nounset # Catch references to undefined variables. +set -o pipefail # Catch non zero exit codes within pipelines. +set -o errexit # Catch and propagate non zero exit codes. + +# Host and target architecture validation +if [ "$target_arch" == "" ]; then + echo "$name: --target-arch is not set. Example usage: --target-arch=aarch64" + exit 1 +fi + +if [ "$target_arch" != "aarch64" ] && [ "$target_arch" != "aarch32" ] && [ "$target_arch" != "x86_64" ]; then + echo "$name: --target-arch is not valid. Valid options are: aarch64, aarch32, x86_64" + exit 1 +fi + +if [ "$HOST_ARCH" == "aarch64" ]; then + if [ "$target_arch" != "aarch64" ]; then + echo "$name: aarch64 is the only supported --target_arch when host is aarch64" + exit 1 + fi +fi + +if [ "$target_arch" == "aarch32" ]; then + if [ "$HOST_ARCH" != "x86_64" ]; then + echo "$name: aarch32 is the only supported --target_arch when host is x86_64 (cross compile only)" + exit 1 + fi +fi + +# Validation of chosen Arm NN dependencies +if [ "$flag_tflite_delegate" -eq 0 ] && [ "$flag_tflite_parser" -eq 0 ] && [ "$flag_onnx_parser" -eq 0 ]; then + echo "$name: at least one of flags --tflite-delegate, --tflite-parser or --onnx-parser must be set (or --all)." + exit 1 +fi + +# If --num-threads is set, overwrite default NUM_THREADS with user-defined value +if [ ! "$num_threads" -eq 0 ]; then + NUM_THREADS="$num_threads" +fi \ No newline at end of file -- cgit v1.2.1