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