blob: 10f41e8cc73ce2f4de25ee2d6cc8ddaf6da54a54 [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
129 echo -e "\n***** Built Arm NN for $TARGET_ARCH *****"
130
131 local tarball_path="$ROOT_DIR/armnn_$ARMNN_BUILD_DIR_NAME.tar.gz"
132 echo -e "\n***** Creating tarball of Arm NN build at $tarball_path *****"
133
134 cd "$ARMNN_BUILD_ROOT"
135 rm -f "$tarball_path"
136 tar -czf "$tarball_path" "$ARMNN_BUILD_DIR_NAME"
137
138 echo -e "\n***** Created tarball of Arm NN build at $ROOT_DIR/armnn_$ARMNN_BUILD_DIR_NAME.tar.gz *****"
139 echo -e "\n***** To extract tarball, run: tar -xzf armnn_$ARMNN_BUILD_DIR_NAME.tar.gz *****\n"
140
141 return 0
142}
143
James Conroy210897d2022-08-04 16:55:05 +0100144download_armnn()
145{
146 cd "$SOURCE_DIR"
147
148 echo -e "\n***** Downloading Arm NN *****"
149
150 rm -rf "$ARMNN_SRC"
151
152 # Latest release branch of Arm NN is checked out by default
153 git clone https://github.com/ARM-software/armnn.git armnn
154
155 cd "$ARMNN_SRC"
156 armnn_branch="$(git rev-parse --abbrev-ref HEAD)"
157
158 echo -e "\n***** Arm NN Downloaded: $armnn_branch *****"
159}
160
161download_acl()
162{
163 cd "$SOURCE_DIR"
164
165 echo -e "\n***** Downloading ACL *****"
166
167 rm -rf "$ACL_SRC"
168
169 git clone https://github.com/ARM-software/ComputeLibrary.git acl
170
171 # Get corresponding release tag for ACL by parsing release branch number for Arm NN
172 local acl_tag=""
173 acl_tag="$(echo "$armnn_branch" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /./g')"
174
175 cd "$ACL_SRC"
176 git checkout v"$acl_tag"
177
178 echo -e "\n***** ACL Downloaded: $acl_tag *****"
179}
180
James Conroy919ec712022-07-13 12:57:53 +0100181usage()
182{
183 cat <<EOF
184build-armnn.sh - Build Arm NN and ACL
185build-armnn.sh [OPTION]...
186 --tflite-delegate
187 build the Arm NN TF Lite Delegate component
188 --tflite-parser
189 build the Arm NN TF Lite Parser component
190 --onnx-parser
191 build the Arm NN ONNX parser component
192 --all
193 build all Arm NN components listed above
James Conroye6f30ad2022-09-08 12:04:26 +0100194 --target-arch=[aarch64|x86_64]
James Conroy919ec712022-07-13 12:57:53 +0100195 specify a target architecture (mandatory)
196 --neon-backend
197 build Arm NN with the NEON backend (CPU acceleration from ACL)
198 --cl-backend
199 build Arm NN with the OpenCL backend (GPU acceleration from ACL)
200 --ref-backend
201 build Arm NN with the reference backend (Should be used for verification purposes only. Does not provide any performance acceleration.)
202 --clean
203 remove previous Arm NN and ACL build prior to script execution (optional: defaults to off)
204 --debug
205 build Arm NN (and ACL) with debug turned on (optional: defaults to off)
206 --armnn-cmake-args=<ARG LIST STRING>
James Conroye6f30ad2022-09-08 12:04:26 +0100207 provide additional space-separated CMake arguments string for building Arm NN (optional)
James Conroy919ec712022-07-13 12:57:53 +0100208 --acl-scons-params=<PARAM LIST STRING>
James Conroye6f30ad2022-09-08 12:04:26 +0100209 provide additional space-separated scons parameters string for building ACL (optional)
James Conroy919ec712022-07-13 12:57:53 +0100210 --num-threads=<INTEGER>
211 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host)
212 -h, --help
213 print brief usage information and exit
214 -x
215 enable shell tracing in this script
216
217At least one component (i.e. --tflite-delegate, --tflite-parser, --onnx-parser) must be provided or else provide --all to build all Arm NN components.
218At least one backend (i.e. --neon-backend, --cl-backend, --ref-backend) must be chosen.
219This script must be executed from the same root directory in which setup-armnn.sh was executed from.
220
James Conroy210897d2022-08-04 16:55:05 +0100221The first execution of this script will download the latest release branches of Arm NN and ACL, by default.
James Conroy919ec712022-07-13 12:57:53 +0100222Alternatively, place custom/modified repositories named "armnn" and "acl" in <ROOT_DIR>/source.
James Conroy210897d2022-08-04 16:55:05 +0100223If providing custom repos, both Arm NN and ACL must be provided. The ACL repo will not be used if flags --neon-backend or --cl-backend are not selected.
James Conroy919ec712022-07-13 12:57:53 +0100224
225By default, a tarball tar.gz archive of the Arm NN build will be created in the directory from which this script is called from.
226
227Examples:
228Build for aarch64 with all Arm NN components, NEON enabled and OpenCL enabled:
229 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --all --neon-backend --cl-backend
230Build for aarch64 with TF Lite Delegate, OpenCL enabled and additional ACL scons params:
James Conroye6f30ad2022-09-08 12:04:26 +0100231 <PATH_TO>/build-armnn.sh --target-arch=aarch64 --tflite-delegate --cl-backend --acl-scons-params='compress_kernels=1 benchmark_examples=1'
232Setup for aarch64 with all Arm NN dependencies, OpenCL enabled and additional Arm NN cmake args:
233 <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 +0100234EOF
235}
236
237# This will catch in validation.sh if not set
238target_arch=""
239
240# Default flag values
241flag_tflite_delegate=0
242flag_tflite_parser=0
243flag_onnx_parser=0
244flag_neon_backend=0
245flag_cl_backend=0
246flag_ref_backend=0
247flag_clean=0
248flag_debug=0
249
250# Empty strings for optional additional args by default
251armnn_cmake_args=""
252acl_scons_params=""
253
254# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used
255num_threads=0
256
257name=$(basename "$0")
258
259# If no options provided, show help
260if [ $# -eq 0 ]; then
261 usage
262 exit 1
263fi
264
265args=$(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" -- "$@")
266eval set -- "$args"
267while [ $# -gt 0 ]; do
268 if [ -n "${opt_prev:-}" ]; then
269 eval "$opt_prev=\$1"
270 opt_prev=
271 shift 1
272 continue
273 elif [ -n "${opt_append:-}" ]; then
274 if [ -n "$1" ]; then
275 eval "$opt_append=\"\${$opt_append:-} \$1\""
276 fi
277 opt_append=
278 shift 1
279 continue
280 fi
281 case $1 in
282 --tflite-parser)
283 flag_tflite_parser=1
284 ;;
285
286 --tflite-delegate)
287 flag_tflite_delegate=1
288 ;;
289
290 --onnx-parser)
291 flag_onnx_parser=1
292 ;;
293
294 --all)
295 flag_tflite_delegate=1
296 flag_tflite_parser=1
297 flag_onnx_parser=1
298 ;;
299
300 --target-arch)
301 opt_prev=target_arch
302 ;;
303
304 --neon-backend)
305 flag_neon_backend=1
306 ;;
307
308 --cl-backend)
309 flag_cl_backend=1
310 ;;
311
312 --ref-backend)
313 flag_ref_backend=1
314 ;;
315
316 --clean)
317 flag_clean=1
318 ;;
319
320 --debug)
321 flag_debug=1
322 ;;
323
324 --armnn-cmake-args)
325 opt_prev=armnn_cmake_args
326 ;;
327
328 --acl-scons-params)
329 opt_prev=acl_scons_params
330 ;;
331
332 --num-threads)
333 opt_prev=num_threads
334 ;;
335
336 -h | --help)
337 usage
338 exit 0
339 ;;
340
341 -x)
342 set -x
343 ;;
344
345 --)
346 shift
347 break 2
348 ;;
349
350 esac
351 shift 1
352done
353
354# shellcheck source=common.sh
355source "$rel_path"/common.sh
356
357# Validation of chosen Arm NN backends
358if [ "$flag_neon_backend" -eq 0 ] && [ "$flag_cl_backend" -eq 0 ] && [ "$flag_ref_backend" -eq 0 ]; then
359 echo -e "\n$name: at least one of flags --neon-backend, --cl-backend or --ref-backend must be set."
360 exit 1
361fi
362
363if [ "$target_arch" == "x86_64" ]; then
364 if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then
365 echo "$name: Accelerated backends --neon-backend and --cl-backend are supported on Arm targets only (x86_64 chosen)."
366 exit 1
367 fi
368fi
369
370# Verify that root source and build directories are present (post execution of setup-armnn.sh)
371if [ ! -d "$SOURCE_DIR" ]; then
372 echo -e "\nERROR: Root source directory does not exist at $SOURCE_DIR"
373 echo "Please check that:"
374 echo "1. setup-armnn.sh was executed successfully prior to running this script"
375 echo "2. This script is being executed in the same directory as setup-armnn.sh"
376
377 exit 1
378fi
379
380if [ ! -d "$BUILD_DIR" ]; then
381 echo -e "\nERROR: Root build directory does not exist at $BUILD_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
James Conroy210897d2022-08-04 16:55:05 +0100389# Download Arm NN and ACL if not done already in a previous execution of this script
390# Check if Arm NN source directory exists AND that it is a repository (not empty)
391if [ -d "$ARMNN_SRC" ] && check_if_repository "$ARMNN_SRC"; then
392 echo -e "\n***** Arm NN source repository already located at $ARMNN_SRC. Skipping cloning of Arm NN. *****"
James Conroy919ec712022-07-13 12:57:53 +0100393
James Conroy210897d2022-08-04 16:55:05 +0100394 # ACL repo must also be present if Arm NN repo is present
395 if [ -d "$ACL_SRC" ] && check_if_repository "$ACL_SRC"; then
396 echo -e "\n***** ACL source repository already located at $ACL_SRC. Skipping cloning of ACL. *****"
397 else
398 echo -e "\nERROR: ACL source repository must be provided at $ACL_SRC if Arm NN source is provided. *****"
James Conroy919ec712022-07-13 12:57:53 +0100399 exit 1
400 fi
James Conroy210897d2022-08-04 16:55:05 +0100401else
402 # Download latest release branches of Arm NN and ACL
403 download_armnn
404 download_acl
James Conroy919ec712022-07-13 12:57:53 +0100405fi
406
407# Adjust output build directory names for Arm NN and ACL if debug is enabled
408DEBUG_POSTFIX=""
409if [ "$flag_debug" -eq 1 ]; then
410 DEBUG_POSTFIX="_debug"
411fi
412
413# Directories for Arm NN and ACL build outputs
414ARMNN_BUILD_ROOT="$BUILD_DIR"/armnn
415ARMNN_BUILD_DIR_NAME="$TARGET_ARCH"_build"$DEBUG_POSTFIX"
416ARMNN_BUILD_TARGET="$ARMNN_BUILD_ROOT"/"$ARMNN_BUILD_DIR_NAME"
417ACL_BUILD_TARGET="$BUILD_DIR"/acl/"$TARGET_ARCH"_build"$DEBUG_POSTFIX"
418
419echo -e "\nINFO: Displaying configuration information before execution of $name"
420echo " target-arch: $TARGET_ARCH"
421echo " host-arch: $HOST_ARCH"
422echo " tflite-delegate: $flag_tflite_delegate"
423echo " tflite-parser: $flag_tflite_parser"
424echo " onnx-parser: $flag_onnx_parser"
425echo " neon-backend: $flag_neon_backend"
426echo " cl-backend: $flag_cl_backend"
427echo " ref-backend: $flag_ref_backend"
428echo " clean: $flag_clean"
429echo " debug: $flag_debug"
430echo "armnn-cmake-args: $armnn_cmake_args"
431echo "acl-scons-params: $acl_scons_params"
432echo " num-threads: $NUM_THREADS"
433echo " root directory: $ROOT_DIR"
434echo "source directory: $SOURCE_DIR"
435echo " build directory: $BUILD_DIR"
436echo " armnn build dir: $ARMNN_BUILD_TARGET"
437echo -e "\nScript execution will begin in 10 seconds..."
438
439sleep 10
440
441if [ "$flag_neon_backend" -eq 1 ] || [ "$flag_cl_backend" -eq 1 ]; then
442 build_acl
443else
444 echo -e "\n***** Skipping ACL build: --neon-backend and --cl-backend not set in options. *****"
445fi
446
447build_armnn
448
449exit 0