blob: 2751fc2bee1ae1ba4fb321fb87ffaea6ae608470 [file] [log] [blame]
James Conroy919ec712022-07-13 12:57:53 +01001#!/bin/bash
Teresa Charlinad1b3d72023-03-14 12:10:28 +00002
James Conroy919ec712022-07-13 12:57:53 +01003#
Teresa Charlinad1b3d72023-03-14 12:10:28 +00004# Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
James Conroy919ec712022-07-13 12:57:53 +01005# 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
James Conroye6f30ad2022-09-08 12:04:26 +010021 # $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"
James Conroy919ec712022-07-13 12:57:53 +010023
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
James Conroy919ec712022-07-13 12:57:53 +010045 "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 ;;
James Conroy919ec712022-07-13 12:57:53 +010088 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" \
Nikhil Raj542c8482023-04-26 16:22:10 +010099 -DBUILD_CLASSIC_DELEGATE="$flag_tflite_classic_delegate" \
100 -DBUILD_OPAQUE_DELEGATE="$flag_tflite_opaque_delegate" \
James Conroy919ec712022-07-13 12:57:53 +0100101 -DBUILD_TF_LITE_PARSER="$flag_tflite_parser" \
Nikhil Raj0d294532023-04-20 15:19:05 +0100102 -DBUILD_DELEGATE_JNI_INTERFACE="$flag_jni" \
James Conroy919ec712022-07-13 12:57:53 +0100103 -DBUILD_ONNX_PARSER="$flag_onnx_parser" \
104 -DARMCOMPUTENEON="$flag_neon_backend" \
105 -DARMCOMPUTECL="$flag_cl_backend" \
106 -DARMNNREF="$flag_ref_backend" \
107 -DARMCOMPUTE_ROOT="$ACL_SRC" \
108 -DARMCOMPUTE_BUILD_DIR="$ACL_BUILD_TARGET" \
109 -DTENSORFLOW_ROOT="$TENSORFLOW_SRC" \
110 -DTF_LITE_SCHEMA_INCLUDE_PATH="$TFLITE_BUILD_ROOT" \
111 -DTFLITE_LIB_ROOT="$TFLITE_BUILD_TARGET" \
112 -DFLATBUFFERS_ROOT="$FLATBUFFERS_BUILD_TARGET" \
113 -DFLATC_DIR="$FLATBUFFERS_BUILD_HOST" \
114 -DONNX_GENERATED_SOURCES="$ONNX_BUILD_TARGET" \
115 -DPROTOBUF_ROOT="$PROTOBUF_BUILD_HOST" \
116 -DPROTOBUF_LIBRARY_DEBUG="$PROTOBUF_LIBRARY_TARGET" \
117 -DPROTOBUF_LIBRARY_RELEASE="$PROTOBUF_LIBRARY_TARGET" \
118 "$armnn_cmake_args" \
119 "$ARMNN_SRC"
120
121 make -j "$NUM_THREADS"
122
123 # Copy protobuf library into Arm NN build directory, if ONNX Parser is enabled
124 if [ "$flag_onnx_parser" -eq 1 ]; then
125 cd "$ARMNN_BUILD_TARGET"
126 rm -f libprotobuf.so libprotobuf.so.23 libprotobuf.so.23.0.0
127 cp "$PROTOBUF_LIBRARY_TARGET" .
128 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so.23
129 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so
130 fi
131
James Conroy3106c7f2022-12-07 10:30:19 +0000132 # Copy Arm NN include directory into build output
133 cd "$ARMNN_BUILD_TARGET"
134 rm -rf include
135 cp -r "$SOURCE_DIR"/armnn/include .
136
James Conroy919ec712022-07-13 12:57:53 +0100137 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
James Conroy210897d2022-08-04 16:55:05 +0100152download_armnn()
153{
154 cd "$SOURCE_DIR"
155
156 echo -e "\n***** Downloading Arm NN *****"
157
158 rm -rf "$ARMNN_SRC"
159
160 # Latest release branch of Arm NN is checked out by default
161 git clone https://github.com/ARM-software/armnn.git armnn
162
163 cd "$ARMNN_SRC"
James Conroyc4fbbec2022-09-22 16:40:00 +0100164 local armnn_branch="$(git rev-parse --abbrev-ref HEAD)"
James Conroy210897d2022-08-04 16:55:05 +0100165
166 echo -e "\n***** Arm NN Downloaded: $armnn_branch *****"
167}
168
169download_acl()
170{
James Conroyc4fbbec2022-09-22 16:40:00 +0100171 # First get Arm NN branch so that we can download corresponding ACL tag
172 cd "$ARMNN_SRC"
173 local armnn_branch="$(git rev-parse --abbrev-ref HEAD)"
James Conroy210897d2022-08-04 16:55:05 +0100174
James Conroyc4fbbec2022-09-22 16:40:00 +0100175 echo -e "\n***** Downloading corresponding ACL version using Arm NN branch: $armnn_branch *****"
176
177 cd "$SOURCE_DIR"
James Conroy210897d2022-08-04 16:55:05 +0100178
179 rm -rf "$ACL_SRC"
180
181 git clone https://github.com/ARM-software/ComputeLibrary.git acl
182
183 # Get corresponding release tag for ACL by parsing release branch number for Arm NN
184 local acl_tag=""
185 acl_tag="$(echo "$armnn_branch" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /./g')"
186
187 cd "$ACL_SRC"
188 git checkout v"$acl_tag"
189
190 echo -e "\n***** ACL Downloaded: $acl_tag *****"
191}
192
James Conroy919ec712022-07-13 12:57:53 +0100193usage()
194{
195 cat <<EOF
196build-armnn.sh - Build Arm NN and ACL
197build-armnn.sh [OPTION]...
Nikhil Raj542c8482023-04-26 16:22:10 +0100198 --tflite-classic-delegate
199 build the existing Arm NN TF Lite Delegate component
200 --tflite-opaque-delegate
201 build the new Arm NN opaque delegate component
James Conroy919ec712022-07-13 12:57:53 +0100202 --tflite-parser
203 build the Arm NN TF Lite Parser component
204 --onnx-parser
205 build the Arm NN ONNX parser component
206 --all
207 build all Arm NN components listed above
James Conroye6f30ad2022-09-08 12:04:26 +0100208 --target-arch=[aarch64|x86_64]
James Conroy919ec712022-07-13 12:57:53 +0100209 specify a target architecture (mandatory)
210 --neon-backend
211 build Arm NN with the NEON backend (CPU acceleration from ACL)
212 --cl-backend
213 build Arm NN with the OpenCL backend (GPU acceleration from ACL)
214 --ref-backend
215 build Arm NN with the reference backend (Should be used for verification purposes only. Does not provide any performance acceleration.)
216 --clean
217 remove previous Arm NN and ACL build prior to script execution (optional: defaults to off)
218 --debug
219 build Arm NN (and ACL) with debug turned on (optional: defaults to off)
220 --armnn-cmake-args=<ARG LIST STRING>
James Conroyc8d7a662022-11-18 10:17:27 +0000221 provide additional comma-separated CMake arguments string for building Arm NN (optional)
James Conroy919ec712022-07-13 12:57:53 +0100222 --acl-scons-params=<PARAM LIST STRING>
James Conroyc8d7a662022-11-18 10:17:27 +0000223 provide additional comma-separated scons parameters string for building ACL (optional)
James Conroy919ec712022-07-13 12:57:53 +0100224 --num-threads=<INTEGER>
225 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host)
226 -h, --help
227 print brief usage information and exit
228 -x
229 enable shell tracing in this script
230
Nikhil Raj542c8482023-04-26 16:22:10 +0100231At least one component (i.e. --tflite-classic-delegate, --tflite-opaque-delegate, --tflite-parser, --onnx-parser) must be provided or else provide --all to build all Arm NN components.
James Conroy919ec712022-07-13 12:57:53 +0100232At least one backend (i.e. --neon-backend, --cl-backend, --ref-backend) must be chosen.
233This script must be executed from the same root directory in which setup-armnn.sh was executed from.
234
James Conroy210897d2022-08-04 16:55:05 +0100235The first execution of this script will download the latest release branches of Arm NN and ACL, by default.
James Conroyc4fbbec2022-09-22 16:40:00 +0100236Alternatively, place custom/modified repositories named "armnn" and (optionally) "acl" in <ROOT_DIR>/source.
237Providing custom "acl" repo is optional since it is only required if backend flags --neon-backend or --cl-backend are chosen.
James Conroy919ec712022-07-13 12:57:53 +0100238
239By default, a tarball tar.gz archive of the Arm NN build will be created in the directory from which this script is called from.
240
241Examples:
242Build for aarch64 with all Arm NN components, NEON enabled and OpenCL enabled:
243 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --all --neon-backend --cl-backend
244Build for aarch64 with TF Lite Delegate, OpenCL enabled and additional ACL scons params:
Nikhil Raj542c8482023-04-26 16:22:10 +0100245 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --tflite-classic-delegate --cl-backend --acl-scons-params='compress_kernels=1,benchmark_examples=1'
James Conroye6f30ad2022-09-08 12:04:26 +0100246Setup for aarch64 with all Arm NN dependencies, OpenCL enabled and additional Arm NN cmake args:
James Conroyc8d7a662022-11-18 10:17:27 +0000247 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --all --cl-backend --armnn-cmake-args='-DBUILD_SAMPLE_APP=1,-DBUILD_UNIT_TESTS=0'
James Conroy919ec712022-07-13 12:57:53 +0100248EOF
249}
250
251# This will catch in validation.sh if not set
252target_arch=""
253
254# Default flag values
Nikhil Raj542c8482023-04-26 16:22:10 +0100255flag_tflite_classic_delegate=0
256flag_tflite_opaque_delegate=0
James Conroy919ec712022-07-13 12:57:53 +0100257flag_tflite_parser=0
258flag_onnx_parser=0
259flag_neon_backend=0
260flag_cl_backend=0
261flag_ref_backend=0
262flag_clean=0
263flag_debug=0
Nikhil Raj0d294532023-04-20 15:19:05 +0100264flag_jni=0
James Conroy919ec712022-07-13 12:57:53 +0100265
266# Empty strings for optional additional args by default
267armnn_cmake_args=""
268acl_scons_params=""
269
270# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used
271num_threads=0
272
273name=$(basename "$0")
274
275# If no options provided, show help
276if [ $# -eq 0 ]; then
277 usage
278 exit 1
279fi
280
Nikhil Raj542c8482023-04-26 16:22:10 +0100281args=$(getopt -ohx -l tflite-classic-delegate,tflite-opaque-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" -- "$@")
James Conroy919ec712022-07-13 12:57:53 +0100282eval set -- "$args"
283while [ $# -gt 0 ]; do
284 if [ -n "${opt_prev:-}" ]; then
285 eval "$opt_prev=\$1"
286 opt_prev=
287 shift 1
288 continue
289 elif [ -n "${opt_append:-}" ]; then
290 if [ -n "$1" ]; then
291 eval "$opt_append=\"\${$opt_append:-} \$1\""
292 fi
293 opt_append=
294 shift 1
295 continue
296 fi
297 case $1 in
298 --tflite-parser)
299 flag_tflite_parser=1
300 ;;
301
Nikhil Raj542c8482023-04-26 16:22:10 +0100302 --tflite-classic-delegate)
Nikhil Raj365fa4b2023-05-03 09:39:56 +0100303 flag_tflite_classic_delegate=1
James Conroy919ec712022-07-13 12:57:53 +0100304 ;;
305
Nikhil Raj542c8482023-04-26 16:22:10 +0100306 --tflite-opaque-delegate)
307 flag_tflite_opaque_delegate=1
308 ;;
309
James Conroy919ec712022-07-13 12:57:53 +0100310 --onnx-parser)
311 flag_onnx_parser=1
312 ;;
313
314 --all)
Nikhil Raj542c8482023-04-26 16:22:10 +0100315 flag_tflite_classic_delegate=1
316 flag_tflite_opaque_delegate=1
James Conroy919ec712022-07-13 12:57:53 +0100317 flag_tflite_parser=1
318 flag_onnx_parser=1
319 ;;
320
321 --target-arch)
322 opt_prev=target_arch
323 ;;
324
325 --neon-backend)
326 flag_neon_backend=1
327 ;;
328
329 --cl-backend)
330 flag_cl_backend=1
331 ;;
332
333 --ref-backend)
334 flag_ref_backend=1
335 ;;
336
337 --clean)
338 flag_clean=1
339 ;;
340
341 --debug)
342 flag_debug=1
343 ;;
344
345 --armnn-cmake-args)
346 opt_prev=armnn_cmake_args
347 ;;
348
349 --acl-scons-params)
350 opt_prev=acl_scons_params
351 ;;
352
353 --num-threads)
354 opt_prev=num_threads
355 ;;
356
357 -h | --help)
358 usage
359 exit 0
360 ;;
361
362 -x)
363 set -x
364 ;;
365
366 --)
367 shift
368 break 2
369 ;;
370
371 esac
372 shift 1
373done
374
375# shellcheck source=common.sh
376source "$rel_path"/common.sh
377
378# Validation of chosen Arm NN backends
379if [ "$flag_neon_backend" -eq 0 ] && [ "$flag_cl_backend" -eq 0 ] && [ "$flag_ref_backend" -eq 0 ]; then
380 echo -e "\n$name: at least one of flags --neon-backend, --cl-backend or --ref-backend must be set."
381 exit 1
382fi
383
384if [ "$target_arch" == "x86_64" ]; then
385 if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then
386 echo "$name: Accelerated backends --neon-backend and --cl-backend are supported on Arm targets only (x86_64 chosen)."
387 exit 1
388 fi
389fi
390
391# Verify that root source and build directories are present (post execution of setup-armnn.sh)
392if [ ! -d "$SOURCE_DIR" ]; then
393 echo -e "\nERROR: Root source directory does not exist at $SOURCE_DIR"
394 echo "Please check that:"
395 echo "1. setup-armnn.sh was executed successfully prior to running this script"
396 echo "2. This script is being executed in the same directory as setup-armnn.sh"
397
398 exit 1
399fi
400
401if [ ! -d "$BUILD_DIR" ]; then
402 echo -e "\nERROR: Root build directory does not exist at $BUILD_DIR"
403 echo "Please check that:"
404 echo "1. setup-armnn.sh was executed successfully prior to running this script"
405 echo "2. This script is being executed in the same directory as setup-armnn.sh"
406
407 exit 1
408fi
409
James Conroyc4fbbec2022-09-22 16:40:00 +0100410# Download Arm NN if not done already in a previous execution of this script
James Conroy210897d2022-08-04 16:55:05 +0100411# Check if Arm NN source directory exists AND that it is a repository (not empty)
412if [ -d "$ARMNN_SRC" ] && check_if_repository "$ARMNN_SRC"; then
413 echo -e "\n***** Arm NN source repository already located at $ARMNN_SRC. Skipping cloning of Arm NN. *****"
James Conroyc4fbbec2022-09-22 16:40:00 +0100414else
415 # Download latest release branch of Arm NN
416 download_armnn
417fi
James Conroy919ec712022-07-13 12:57:53 +0100418
James Conroyc4fbbec2022-09-22 16:40:00 +0100419# Download ACL if not done already in a previous execution of this script
420# Only download ACL if backend options --neon-backend and --cl-backend are chosen
421if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then
422 # Check if Arm NN source directory exists AND that it is a repository (not empty)
James Conroy210897d2022-08-04 16:55:05 +0100423 if [ -d "$ACL_SRC" ] && check_if_repository "$ACL_SRC"; then
424 echo -e "\n***** ACL source repository already located at $ACL_SRC. Skipping cloning of ACL. *****"
425 else
James Conroyc4fbbec2022-09-22 16:40:00 +0100426 # Download latest release branch of ACL
427 download_acl
James Conroy919ec712022-07-13 12:57:53 +0100428 fi
James Conroy210897d2022-08-04 16:55:05 +0100429else
James Conroyc4fbbec2022-09-22 16:40:00 +0100430 echo -e "\n***** Backend options --neon-backend and --cl-backend not selected - skipping cloning of ACL *****"
James Conroy919ec712022-07-13 12:57:53 +0100431fi
432
433# Adjust output build directory names for Arm NN and ACL if debug is enabled
434DEBUG_POSTFIX=""
435if [ "$flag_debug" -eq 1 ]; then
436 DEBUG_POSTFIX="_debug"
437fi
438
James Conroyc8d7a662022-11-18 10:17:27 +0000439# Replace commas with spaces in additional Arm NN / ACL build args
440# shellcheck disable=SC2001
441armnn_cmake_args="$(echo "$armnn_cmake_args" | sed 's/,/ /g')"
442
443# shellcheck disable=SC2001
444acl_scons_params="$(echo "$acl_scons_params" | sed 's/,/ /g')"
445
James Conroy919ec712022-07-13 12:57:53 +0100446# Directories for Arm NN and ACL build outputs
447ARMNN_BUILD_ROOT="$BUILD_DIR"/armnn
448ARMNN_BUILD_DIR_NAME="$TARGET_ARCH"_build"$DEBUG_POSTFIX"
449ARMNN_BUILD_TARGET="$ARMNN_BUILD_ROOT"/"$ARMNN_BUILD_DIR_NAME"
450ACL_BUILD_TARGET="$BUILD_DIR"/acl/"$TARGET_ARCH"_build"$DEBUG_POSTFIX"
451
452echo -e "\nINFO: Displaying configuration information before execution of $name"
Nikhil Raj542c8482023-04-26 16:22:10 +0100453echo " target-arch: $TARGET_ARCH"
454echo " host-arch: $HOST_ARCH"
455echo "tflite-classic-delegate: $flag_tflite_classic_delegate"
456echo "tflite-opaque-delegate : $flag_tflite_opaque_delegate"
457echo " tflite-parser: $flag_tflite_parser"
458echo " onnx-parser: $flag_onnx_parser"
459echo " neon-backend: $flag_neon_backend"
460echo " cl-backend: $flag_cl_backend"
461echo " ref-backend: $flag_ref_backend"
462echo " clean: $flag_clean"
463echo " debug: $flag_debug"
464echo " armnn-cmake-args: $armnn_cmake_args"
465echo " acl-scons-params: $acl_scons_params"
466echo " num-threads: $NUM_THREADS"
467echo " root directory: $ROOT_DIR"
468echo " source directory: $SOURCE_DIR"
469echo " build directory: $BUILD_DIR"
470echo " armnn build dir: $ARMNN_BUILD_TARGET"
James Conroy919ec712022-07-13 12:57:53 +0100471echo -e "\nScript execution will begin in 10 seconds..."
472
473sleep 10
474
475if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then
476 build_acl
477else
478 echo -e "\n***** Skipping ACL build: --neon-backend and --cl-backend not set in options. *****"
479fi
480
481build_armnn
482
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000483exit 0