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