1#!/bin/bash 2 3# 4# Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. 5# SPDX-License-Identifier: MIT 6# 7 8# Script which builds Arm NN and ACL 9# setup-armnn.sh must be executed in the same directory, before running this script 10 11set -o nounset # Catch references to undefined variables. 12set -o pipefail # Catch non zero exit codes within pipelines. 13set -o errexit # Catch and propagate non zero exit codes. 14 15rel_path=$(dirname "$0") # relative path from where script is executed to script location 16 17build_acl() 18{ 19 cd "$ACL_SRC" 20 21 # $acl_scons_params are additional options provided by the user and will overwrite any previously defined args 22 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 $acl_scons_params" 23 24 if [ "$flag_debug" -eq 1 ]; then 25 acl_params="$acl_params debug=1 asserts=1" 26 fi 27 28 local native_flag="" 29 if [ "$NATIVE_BUILD" ]; then 30 native_flag="build=native" 31 fi 32 33 # Force -fPIC so that ACL is suitable for inclusion in Arm NN library 34 local extra_cxx_flags="extra_cxx_flags='-fPIC'" 35 36 local compile_flags="" 37 local acl_arch="" 38 39 case "$TARGET_ARCH" in 40 "aarch64") 41 compile_flags+="$AARCH64_COMPILER_FLAGS" 42 acl_arch="arch=arm64-v8a" 43 ;; 44 45 "x86_64") 46 acl_arch="arch=x86_64" 47 ;; 48 esac 49 50 echo -e "\n***** Building ACL for $TARGET_ARCH *****" 51 52 if [ "$flag_clean" -eq 1 ]; then 53 echo -e "\n***** Clean flag detected: removing existing ACL build *****" 54 rm -rf "$ACL_BUILD_TARGET" 55 fi 56 57 mkdir -p "$ACL_BUILD_TARGET" 58 59 eval "$compile_flags" \ 60 scons "$native_flag" \ 61 "$acl_arch" \ 62 "$acl_params" \ 63 build_dir="$ACL_BUILD_TARGET" \ 64 "$extra_cxx_flags" \ 65 -j "$NUM_THREADS" 66 67 echo -e "\n***** Built ACL for $TARGET_ARCH *****" 68 69 return 0 70} 71 72build_armnn() 73{ 74 mkdir -p "$ARMNN_BUILD_TARGET" 75 cd "$ARMNN_BUILD_TARGET" 76 77 local build_type="Release" 78 if [ "$flag_debug" -eq 1 ]; then 79 build_type="Debug" 80 fi 81 82 local compile_flags="" 83 84 case "$TARGET_ARCH" in 85 "aarch64") 86 compile_flags+="$AARCH64_COMPILER_FLAGS" 87 ;; 88 esac 89 90 if [ "$flag_clean" -eq 1 ]; then 91 echo -e "\n***** Clean flag detected: removing existing Arm NN build *****" 92 rm -rf "$ARMNN_BUILD_TARGET" 93 fi 94 95 echo -e "\n***** Building Arm NN for $TARGET_ARCH *****" 96 97 eval "$compile_flags" \ 98 cmake -DCMAKE_BUILD_TYPE="$build_type" \ 99 -DBUILD_CLASSIC_DELEGATE="$flag_tflite_delegate" \ 100 -DBUILD_TF_LITE_PARSER="$flag_tflite_parser" \ 101 -DBUILD_ONNX_PARSER="$flag_onnx_parser" \ 102 -DARMCOMPUTENEON="$flag_neon_backend" \ 103 -DARMCOMPUTECL="$flag_cl_backend" \ 104 -DARMNNREF="$flag_ref_backend" \ 105 -DARMCOMPUTE_ROOT="$ACL_SRC" \ 106 -DARMCOMPUTE_BUILD_DIR="$ACL_BUILD_TARGET" \ 107 -DTENSORFLOW_ROOT="$TENSORFLOW_SRC" \ 108 -DTF_LITE_SCHEMA_INCLUDE_PATH="$TFLITE_BUILD_ROOT" \ 109 -DTFLITE_LIB_ROOT="$TFLITE_BUILD_TARGET" \ 110 -DFLATBUFFERS_ROOT="$FLATBUFFERS_BUILD_TARGET" \ 111 -DFLATC_DIR="$FLATBUFFERS_BUILD_HOST" \ 112 -DONNX_GENERATED_SOURCES="$ONNX_BUILD_TARGET" \ 113 -DPROTOBUF_ROOT="$PROTOBUF_BUILD_HOST" \ 114 -DPROTOBUF_LIBRARY_DEBUG="$PROTOBUF_LIBRARY_TARGET" \ 115 -DPROTOBUF_LIBRARY_RELEASE="$PROTOBUF_LIBRARY_TARGET" \ 116 "$armnn_cmake_args" \ 117 "$ARMNN_SRC" 118 119 make -j "$NUM_THREADS" 120 121 # Copy protobuf library into Arm NN build directory, if ONNX Parser is enabled 122 if [ "$flag_onnx_parser" -eq 1 ]; then 123 cd "$ARMNN_BUILD_TARGET" 124 rm -f libprotobuf.so libprotobuf.so.23 libprotobuf.so.23.0.0 125 cp "$PROTOBUF_LIBRARY_TARGET" . 126 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so.23 127 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so 128 fi 129 130 # Copy Arm NN include directory into build output 131 cd "$ARMNN_BUILD_TARGET" 132 rm -rf include 133 cp -r "$SOURCE_DIR"/armnn/include . 134 135 echo -e "\n***** Built Arm NN for $TARGET_ARCH *****" 136 137 local tarball_path="$ROOT_DIR/armnn_$ARMNN_BUILD_DIR_NAME.tar.gz" 138 echo -e "\n***** Creating tarball of Arm NN build at $tarball_path *****" 139 140 cd "$ARMNN_BUILD_ROOT" 141 rm -f "$tarball_path" 142 tar -czf "$tarball_path" "$ARMNN_BUILD_DIR_NAME" 143 144 echo -e "\n***** Created tarball of Arm NN build at $ROOT_DIR/armnn_$ARMNN_BUILD_DIR_NAME.tar.gz *****" 145 echo -e "\n***** To extract tarball, run: tar -xzf armnn_$ARMNN_BUILD_DIR_NAME.tar.gz *****\n" 146 147 return 0 148} 149 150download_armnn() 151{ 152 cd "$SOURCE_DIR" 153 154 echo -e "\n***** Downloading Arm NN *****" 155 156 rm -rf "$ARMNN_SRC" 157 158 # Latest release branch of Arm NN is checked out by default 159 git clone https://github.com/ARM-software/armnn.git armnn 160 161 cd "$ARMNN_SRC" 162 local armnn_branch="$(git rev-parse --abbrev-ref HEAD)" 163 164 echo -e "\n***** Arm NN Downloaded: $armnn_branch *****" 165} 166 167download_acl() 168{ 169 # First get Arm NN branch so that we can download corresponding ACL tag 170 cd "$ARMNN_SRC" 171 local armnn_branch="$(git rev-parse --abbrev-ref HEAD)" 172 173 echo -e "\n***** Downloading corresponding ACL version using Arm NN branch: $armnn_branch *****" 174 175 cd "$SOURCE_DIR" 176 177 rm -rf "$ACL_SRC" 178 179 git clone https://github.com/ARM-software/ComputeLibrary.git acl 180 181 # Get corresponding release tag for ACL by parsing release branch number for Arm NN 182 local acl_tag="" 183 acl_tag="$(echo "$armnn_branch" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /./g')" 184 185 cd "$ACL_SRC" 186 git checkout v"$acl_tag" 187 188 echo -e "\n***** ACL Downloaded: $acl_tag *****" 189} 190 191usage() 192{ 193 cat <<EOF 194build-armnn.sh - Build Arm NN and ACL 195build-armnn.sh [OPTION]... 196 --tflite-delegate 197 build the Arm NN TF Lite Delegate component 198 --tflite-parser 199 build the Arm NN TF Lite Parser component 200 --onnx-parser 201 build the Arm NN ONNX parser component 202 --all 203 build all Arm NN components listed above 204 --target-arch=[aarch64|x86_64] 205 specify a target architecture (mandatory) 206 --neon-backend 207 build Arm NN with the NEON backend (CPU acceleration from ACL) 208 --cl-backend 209 build Arm NN with the OpenCL backend (GPU acceleration from ACL) 210 --ref-backend 211 build Arm NN with the reference backend (Should be used for verification purposes only. Does not provide any performance acceleration.) 212 --clean 213 remove previous Arm NN and ACL build prior to script execution (optional: defaults to off) 214 --debug 215 build Arm NN (and ACL) with debug turned on (optional: defaults to off) 216 --armnn-cmake-args=<ARG LIST STRING> 217 provide additional comma-separated CMake arguments string for building Arm NN (optional) 218 --acl-scons-params=<PARAM LIST STRING> 219 provide additional comma-separated scons parameters string for building ACL (optional) 220 --num-threads=<INTEGER> 221 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host) 222 -h, --help 223 print brief usage information and exit 224 -x 225 enable shell tracing in this script 226 227At least one component (i.e. --tflite-delegate, --tflite-parser, --onnx-parser) must be provided or else provide --all to build all Arm NN components. 228At least one backend (i.e. --neon-backend, --cl-backend, --ref-backend) must be chosen. 229This script must be executed from the same root directory in which setup-armnn.sh was executed from. 230 231The first execution of this script will download the latest release branches of Arm NN and ACL, by default. 232Alternatively, place custom/modified repositories named "armnn" and (optionally) "acl" in <ROOT_DIR>/source. 233Providing custom "acl" repo is optional since it is only required if backend flags --neon-backend or --cl-backend are chosen. 234 235By default, a tarball tar.gz archive of the Arm NN build will be created in the directory from which this script is called from. 236 237Examples: 238Build for aarch64 with all Arm NN components, NEON enabled and OpenCL enabled: 239 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --all --neon-backend --cl-backend 240Build for aarch64 with TF Lite Delegate, OpenCL enabled and additional ACL scons params: 241 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --tflite-delegate --cl-backend --acl-scons-params='compress_kernels=1,benchmark_examples=1' 242Setup for aarch64 with all Arm NN dependencies, OpenCL enabled and additional Arm NN cmake args: 243 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --all --cl-backend --armnn-cmake-args='-DBUILD_SAMPLE_APP=1,-DBUILD_UNIT_TESTS=0' 244EOF 245} 246 247# This will catch in validation.sh if not set 248target_arch="" 249 250# Default flag values 251flag_tflite_delegate=0 252flag_tflite_parser=0 253flag_onnx_parser=0 254flag_neon_backend=0 255flag_cl_backend=0 256flag_ref_backend=0 257flag_clean=0 258flag_debug=0 259 260# Empty strings for optional additional args by default 261armnn_cmake_args="" 262acl_scons_params="" 263 264# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used 265num_threads=0 266 267name=$(basename "$0") 268 269# If no options provided, show help 270if [ $# -eq 0 ]; then 271 usage 272 exit 1 273fi 274 275args=$(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" -- "$@") 276eval set -- "$args" 277while [ $# -gt 0 ]; do 278 if [ -n "${opt_prev:-}" ]; then 279 eval "$opt_prev=\$1" 280 opt_prev= 281 shift 1 282 continue 283 elif [ -n "${opt_append:-}" ]; then 284 if [ -n "$1" ]; then 285 eval "$opt_append=\"\${$opt_append:-} \$1\"" 286 fi 287 opt_append= 288 shift 1 289 continue 290 fi 291 case $1 in 292 --tflite-parser) 293 flag_tflite_parser=1 294 ;; 295 296 --tflite-delegate) 297 flag_tflite_delegate=1 298 ;; 299 300 --onnx-parser) 301 flag_onnx_parser=1 302 ;; 303 304 --all) 305 flag_tflite_delegate=1 306 flag_tflite_parser=1 307 flag_onnx_parser=1 308 ;; 309 310 --target-arch) 311 opt_prev=target_arch 312 ;; 313 314 --neon-backend) 315 flag_neon_backend=1 316 ;; 317 318 --cl-backend) 319 flag_cl_backend=1 320 ;; 321 322 --ref-backend) 323 flag_ref_backend=1 324 ;; 325 326 --clean) 327 flag_clean=1 328 ;; 329 330 --debug) 331 flag_debug=1 332 ;; 333 334 --armnn-cmake-args) 335 opt_prev=armnn_cmake_args 336 ;; 337 338 --acl-scons-params) 339 opt_prev=acl_scons_params 340 ;; 341 342 --num-threads) 343 opt_prev=num_threads 344 ;; 345 346 -h | --help) 347 usage 348 exit 0 349 ;; 350 351 -x) 352 set -x 353 ;; 354 355 --) 356 shift 357 break 2 358 ;; 359 360 esac 361 shift 1 362done 363 364# shellcheck source=common.sh 365source "$rel_path"/common.sh 366 367# Validation of chosen Arm NN backends 368if [ "$flag_neon_backend" -eq 0 ] && [ "$flag_cl_backend" -eq 0 ] && [ "$flag_ref_backend" -eq 0 ]; then 369 echo -e "\n$name: at least one of flags --neon-backend, --cl-backend or --ref-backend must be set." 370 exit 1 371fi 372 373if [ "$target_arch" == "x86_64" ]; then 374 if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then 375 echo "$name: Accelerated backends --neon-backend and --cl-backend are supported on Arm targets only (x86_64 chosen)." 376 exit 1 377 fi 378fi 379 380# Verify that root source and build directories are present (post execution of setup-armnn.sh) 381if [ ! -d "$SOURCE_DIR" ]; then 382 echo -e "\nERROR: Root source directory does not exist at $SOURCE_DIR" 383 echo "Please check that:" 384 echo "1. setup-armnn.sh was executed successfully prior to running this script" 385 echo "2. This script is being executed in the same directory as setup-armnn.sh" 386 387 exit 1 388fi 389 390if [ ! -d "$BUILD_DIR" ]; then 391 echo -e "\nERROR: Root build directory does not exist at $BUILD_DIR" 392 echo "Please check that:" 393 echo "1. setup-armnn.sh was executed successfully prior to running this script" 394 echo "2. This script is being executed in the same directory as setup-armnn.sh" 395 396 exit 1 397fi 398 399# Download Arm NN if not done already in a previous execution of this script 400# Check if Arm NN source directory exists AND that it is a repository (not empty) 401if [ -d "$ARMNN_SRC" ] && check_if_repository "$ARMNN_SRC"; then 402 echo -e "\n***** Arm NN source repository already located at $ARMNN_SRC. Skipping cloning of Arm NN. *****" 403else 404 # Download latest release branch of Arm NN 405 download_armnn 406fi 407 408# Download ACL if not done already in a previous execution of this script 409# Only download ACL if backend options --neon-backend and --cl-backend are chosen 410if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then 411 # Check if Arm NN source directory exists AND that it is a repository (not empty) 412 if [ -d "$ACL_SRC" ] && check_if_repository "$ACL_SRC"; then 413 echo -e "\n***** ACL source repository already located at $ACL_SRC. Skipping cloning of ACL. *****" 414 else 415 # Download latest release branch of ACL 416 download_acl 417 fi 418else 419 echo -e "\n***** Backend options --neon-backend and --cl-backend not selected - skipping cloning of ACL *****" 420fi 421 422# Adjust output build directory names for Arm NN and ACL if debug is enabled 423DEBUG_POSTFIX="" 424if [ "$flag_debug" -eq 1 ]; then 425 DEBUG_POSTFIX="_debug" 426fi 427 428# Replace commas with spaces in additional Arm NN / ACL build args 429# shellcheck disable=SC2001 430armnn_cmake_args="$(echo "$armnn_cmake_args" | sed 's/,/ /g')" 431 432# shellcheck disable=SC2001 433acl_scons_params="$(echo "$acl_scons_params" | sed 's/,/ /g')" 434 435# Directories for Arm NN and ACL build outputs 436ARMNN_BUILD_ROOT="$BUILD_DIR"/armnn 437ARMNN_BUILD_DIR_NAME="$TARGET_ARCH"_build"$DEBUG_POSTFIX" 438ARMNN_BUILD_TARGET="$ARMNN_BUILD_ROOT"/"$ARMNN_BUILD_DIR_NAME" 439ACL_BUILD_TARGET="$BUILD_DIR"/acl/"$TARGET_ARCH"_build"$DEBUG_POSTFIX" 440 441echo -e "\nINFO: Displaying configuration information before execution of $name" 442echo " target-arch: $TARGET_ARCH" 443echo " host-arch: $HOST_ARCH" 444echo " tflite-delegate: $flag_tflite_delegate" 445echo " tflite-parser: $flag_tflite_parser" 446echo " onnx-parser: $flag_onnx_parser" 447echo " neon-backend: $flag_neon_backend" 448echo " cl-backend: $flag_cl_backend" 449echo " ref-backend: $flag_ref_backend" 450echo " clean: $flag_clean" 451echo " debug: $flag_debug" 452echo "armnn-cmake-args: $armnn_cmake_args" 453echo "acl-scons-params: $acl_scons_params" 454echo " num-threads: $NUM_THREADS" 455echo " root directory: $ROOT_DIR" 456echo "source directory: $SOURCE_DIR" 457echo " build directory: $BUILD_DIR" 458echo " armnn build dir: $ARMNN_BUILD_TARGET" 459echo -e "\nScript execution will begin in 10 seconds..." 460 461sleep 10 462 463if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then 464 build_acl 465else 466 echo -e "\n***** Skipping ACL build: --neon-backend and --cl-backend not set in options. *****" 467fi 468 469build_armnn 470 471exit 0 472