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