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