ArmNN
 22.08
BuildGuideCrossCompilation.md
Go to the documentation of this file.
1 # How to Cross-Compile Arm NN on x86_64 for arm64
2 
3 - [Introduction](#introduction)
4 - [Cross-compiling ToolChain](#cross-compiling-toolchain)
5 - [Install Cmake](#build-cmake)
6 - [Build and install Google's Protobuf library](#build-and-install-google-s-protobuf-library)
7 - [Download Arm NN](#download-arm-nn)
8 - [Build Arm Compute Library](#build-arm-compute-library)
9 - [Build Flatbuffer](#build-flatbuffer)
10 - [Build Onnx](#build-onnx)
11 - [Build TfLite](#build-tflite)
12 - [Build Arm NN](#build-armnn)
13 - [Generate TF Lite Schema](#generate-tflite-schema)
14 - [Build Standalone Sample Dynamic Backend](#build-standalone-sample-dynamic-backend)
15 - [Run Unit Tests](#run-unit-tests)
16 - [Troubleshooting and Errors:](#troubleshooting-and-errors-)
17 
18 ### NOTE: This build guide is being replaced by the [Arm NN Build Tool](build-tool/README.md) and will be removed soon. Please use the [Arm NN Build Tool](build-tool/README.md) for a user-friendly way to build Arm NN and its dependencies.
19 
20 ## Introduction
21 These are the step by step instructions on Cross-Compiling Arm NN under an x86_64 system to target an Arm64 Ubuntu Linux system. This build flow has been tested with Ubuntu 18.04 and 20.04 and it depends on the same version of Ubuntu or Debian being installed on both the build host and target machines. The instructions assume you are using a bash shell and show how to build the Arm NN core library, Protobuf, Tflite, Flatbuffer and Compute Libraries.
22 Start by creating a directory to contain all components:
23 
24 ```
25 mkdir $HOME/armnn-devenv
26 cd $HOME/armnn-devenv
27 ```
28 
29 ## Cross-compiling ToolChain
30 * Install the standard cross-compilation libraries for arm64:
31 ```
32 sudo apt install crossbuild-essential-arm64
33 ```
34 
35 ## Install Cmake
36 Cmake 3.19rc3 is required to build TF Lite Delegate.
37 
38 ```
39 sudo apt-get install libssl-dev
40 wget https://github.com/Kitware/CMake/releases/download/v3.19.0-rc3/cmake-3.19.0-rc3.tar.gz
41 tar -zxvf cmake-3.19.0-rc3.tar.gz
42 cd cmake-3.19.0-rc3
43 ./bootstrap --prefix=$HOME/armnn-devenv/cmake/install
44 make all install
45 cd..
46 ```
47 
48 
49 ## Build and install Google's Protobuf library
50 
51 We support protobuf version 3.12.0
52 * Get protobuf from here: https://github.com/protocolbuffers/protobuf:
53  (Requires Git if not previously installed: `sudo apt install git`)
54 ```bash
55 git clone -b v3.12.0 https://github.com/google/protobuf.git protobuf
56 cd protobuf
57 git submodule update --init --recursive
58 ./autogen.sh
59 ```
60 * Build a native (x86_64) version of the protobuf libraries and compiler (protoc):
61  (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: sudo apt install curl autoconf libtool build-essential g++)
62 ```
63 mkdir x86_64_build
64 cd x86_64_build
65 ../configure --prefix=$HOME/armnn-devenv/google/x86_64_pb_install
66 make install -j16
67 cd ..
68 ```
69 * Build the arm64 version of the protobuf libraries:
70 ```
71 mkdir arm64_build
72 cd arm64_build
73 CC=aarch64-linux-gnu-gcc \
74 CXX=aarch64-linux-gnu-g++ \
75 ../configure --host=aarch64-linux \
76 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
77 --with-protoc=$HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc
78 make install -j16
79 cd ..
80 ```
81 
82 ## Download Arm NN
83 * Clone Arm NN:
84 ```bash
85 cd $HOME/armnn-devenv
86 git clone https://github.com/ARM-software/armnn.git
87 ```
88 
89 * Checkout Arm NN branch:
90 ```bash
91 cd armnn
92 git checkout <branch_name>
93 git pull
94 ```
95 For example, if you want to check out the 21.11 release branch:
96 ```bash
97 git checkout branches/armnn_21_11
98 git pull
99 ```
100 
101 ## Build Arm Compute Library
102 * Clone Arm Compute Library:
103 
104 ```bash
105 cd $HOME/armnn-devenv
106 git clone https://github.com/ARM-software/ComputeLibrary.git
107 ```
108 * Checkout Arm Compute Library release tag:
109 ```bash
110 cd ComputeLibrary
111 git checkout <tag_name>
112 ```
113 Arm NN and Arm Compute Library are developed closely together. If you would like to use the Arm NN 21.11 release you will need the 21.11 release of ACL too. For example, if you want to checkout the 21.11 release tag:
114 ```bash
115 git checkout v21.11
116 ```
117 Arm NN provides a script that downloads the version of Arm Compute Library that Arm NN was tested with:
118 ```bash
119 git checkout $(../armnn/scripts/get_compute_library.sh -p)
120 ```
121 * Build the Arm Compute Library:
122  (Requires SCons if not previously installed: `sudo apt install scons`)
123 ```bash
124 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j4
125 ```
126 
127 ## Build Flatbuffer
128 * Building Flatbuffer version 1.12.0
129 ```bash
130 cd $HOME/armnn-devenv
131 wget -O flatbuffers-1.12.0.tar.gz https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz
132 tar xf flatbuffers-1.12.0.tar.gz
133 cd flatbuffers-1.12.0
134 rm -f CMakeCache.txt
135 mkdir build
136 cd build
137 cmake .. -DFLATBUFFERS_BUILD_FLATC=1 \
138  -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers \
139  -DFLATBUFFERS_BUILD_TESTS=0
140 make all install
141 ```
142 
143 * Build arm64 version of flatbuffer
144 ```bash
145 cd ..
146 mkdir build-arm64
147 cd build-arm64
148 # Add -fPIC to allow us to use the libraries in shared objects.
149 CXXFLAGS="-fPIC" cmake .. -DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc \
150  -DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++ \
151  -DFLATBUFFERS_BUILD_FLATC=1 \
152  -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers-arm64 \
153  -DFLATBUFFERS_BUILD_TESTS=0
154 make all install
155 ```
156 
157 ## Build Onnx
158 * Building Onnx
159 ```bash
160 cd $HOME/armnn-devenv
161 git clone https://github.com/onnx/onnx.git
162 cd onnx
163 git fetch https://github.com/onnx/onnx.git 553df22c67bee5f0fe6599cff60f1afc6748c635 && git checkout FETCH_HEAD
164 LD_LIBRARY_PATH=$HOME/armnn-devenv/google/x86_64_pb_install/lib:$LD_LIBRARY_PATH \
165 $HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc \
166 onnx/onnx.proto --proto_path=. --proto_path=../google/x86_64_pb_install/include --cpp_out $HOME/armnn-devenv/onnx
167 ```
168 
169 ## Build TfLite
170 * Arm NN provides a script, armnn/scripts/get_tensorflow.sh, that can be used to check out the version of TensorFlow that Arm NN was tested with:
171 ```bash
172 cd $HOME/armnn-devenv
173 git clone https://github.com/tensorflow/tensorflow.git
174 cd tensorflow/
175 git checkout $(../armnn/scripts/get_tensorflow.sh -p) # Checks out the latest tested version of TF
176 cd ..
177 ```
178 
179 * You will need to download gcc-arm-8.3-2019.03 toolchain and continue building TF Lite as following:
180 ```
181 curl -LO https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz
182 mkdir tflite-toolchains
183 tar xvf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz -C tflite-toolchains
184 mkdir -p tflite/build
185 cd tflite/build
186 ARMCC_PREFIX=$HOME/armnn-devenv/tflite-toolchains/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu- \
187 ARMCC_FLAGS="-funsafe-math-optimizations" \
188 cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
189  -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
190  -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
191  -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_SYSTEM_NAME=Linux \
192  -DTFLITE_ENABLE_XNNPACK=OFF \
193  -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
194  $HOME/armnn-devenv/tensorflow/tensorflow/lite/ \
195 cmake --build .
196 ```
197 
198 ## Generate TF Lite Schema
199 ```
200 cd $HOME/armnn-devenv/tflite
201 cp $HOME/armnn-devenv/tensorflow/tensorflow/lite/schema/schema.fbs .
202 ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs
203 ```
204 
205 ## Build Arm NN
206 * Compile Arm NN for arm64:
207 ```bash
208 cd $HOME/armnn-devenv/armnn
209 mkdir build
210 cd build
211 ```
212 
213 * Use CMake to configure your build environment, update the following script and run it from the armnn/build directory to set up the Arm NN build:
214 ```bash
215 #!/bin/bash
216 CXX=aarch64-linux-gnu-g++ CC=aarch64-linux-gnu-gcc cmake .. \
217 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary \
218 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build/ \
219 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
220 -DONNX_GENERATED_SOURCES=$HOME/armnn-devenv/onnx \
221 -DBUILD_ONNX_PARSER=1 \
222 -DBUILD_TF_LITE_PARSER=1 \
223 -DTENSORFLOW_ROOT=$HOME/armnn-devenv/tensorflow \
224 -DTF_LITE_SCHEMA_INCLUDE_PATH=$WORKING_DIR/tflite \
225 -DFLATBUFFERS_ROOT=$HOME/armnn-devenv/flatbuffers-arm64 \
226 -DFLATC_DIR=$HOME/armnn-devenv/flatbuffers-1.12.0/build \
227 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install \
228 -DPROTOBUF_LIBRARY_DEBUG=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0 \
229 -DPROTOBUF_LIBRARY_RELEASE=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0
230 ```
231 
232 * If you want to include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:
233 ```bash
234 -DSAMPLE_DYNAMIC_BACKEND=1 \
235 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
236 ```
237 * If you want to build Arm NN TF Lite Delegate, add the arguments:
238 ```bash
239 -DTFLITE_LIB_ROOT=$HOME/armnn-devenv/tflite/build \
240 -DBUILD_ARMNN_TFLITE_DELEGATE=1
241 ```
242 * Run the build
243 ```bash
244 make -j32
245 ```
246 
247 ## Build Standalone Sample Dynamic Backend
248 * The sample dynamic backend is located in armnn/src/dynamic/sample
249 ```bash
250 cd $HOME/armnn-devenv/armnn/src/dynamic/sample
251 mkdir build
252 cd build
253 ```
254 
255 * Use CMake to configure your build environment, update the following script and run it from the armnn/src/dynamic/sample/build directory to set up the Arm NN build:
256 ```bash
257 #!/bin/bash
258 CXX=aarch64-linux-gnu-g++ CC=aarch64-linux-gnu-gcc cmake .. \
259 -DCMAKE_CXX_FLAGS=--std=c++14 \
260 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so
261 ```
262 
263 * Run the build
264 ```bash
265 make
266 ```
267 
268 ## Run Unit Tests
269 * Copy the build folder to an arm64 linux machine
270 * Copy the libprotobuf.so.23.0.0 library file to the build folder
271 * If you enable the standalone sample dynamic tests, also copy libArm_SampleDynamic_backend.so library file to the folder specified as $SAMPLE_DYNAMIC_BACKEND_PATH when you build Arm NN
272 * cd to the build folder on your arm64 machine and set your LD_LIBRARY_PATH to its current location:
273 
274 ```bash
275 cd build/
276 ```
277 
278 * Create a symbolic link to libprotobuf.so.23.0.0:
279 
280 ```bash
281 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so.23
282 ```
283 
284 * Run the UnitTests:
285 
286 ```bash
287 LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./UnitTests
288 [doctest] doctest version is "2.4.6"
289 [doctest] run with "--help" for options
290 ===============================================================================
291 [doctest] test cases: 4817 | 4817 passed | 0 failed | 0 skipped
292 [doctest] assertions: 807634 | 807634 passed | 0 failed |
293 [doctest] Status: SUCCESS!
294 ```
295 
296 * Run the Delegate UnitTests:
297 
298 ```bash
299 LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./delegate/DelegateUnitTests
300 ```
301 
302 ## Troubleshooting and Errors:
303 ### Missing xxd
304 * When Cross-compiling Arm NN if you are getting error
305 ```bash
306 [ 62%] Generating SchemaText.cpp
307 /bin/sh: 1: xxd: not found
308 make[2]: *** [armnn/CMakeFiles/UnitTests.dir/build.make:63: armnn/SchemaText.cpp] Error 127
309 make[1]: *** [CMakeFiles/Makefile2:674: armnn/CMakeFiles/UnitTests.dir/all] Error 2
310 make[1]: *** Waiting for unfinished jobs....
311 ```
312 * Missing xxd, this can be fixed by installing it:
313 ```bash
314 sudo apt-get install xxd
315 ```
316 
317 <br><br>
318 
319 ### Missing libz.so.1
320 * When compiling armNN:
321 ```bash
322 /usr/lib/gcc-cross/aarch64-linux-gnu/5/../../../../aarch64-linux-gnu/bin/ld: warning: libz.so.1, needed by /home/<username>/armNN/usr/lib64/libprotobuf.so.23.0.0, not found (try using -rpath or -rpath-link)
323 ```
324 
325 * Missing arm64 libraries for libz.so.1, these can be added by adding a second architecture to dpkg and explicitly installing them:
326 ```bash
327 sudo dpkg --add-architecture arm64
328 sudo apt-get install zlib1g:arm64
329 sudo apt-get update
330 sudo ldconfig
331 ```
332 * If apt-get update returns 404 errors for arm64 repos refer to section 5 below.
333 * Alternatively the missing arm64 version of libz.so.1 can be downloaded and installed from a .deb package here:
334  https://launchpad.net/ubuntu/wily/arm64/zlib1g/1:1.2.8.dfsg-2ubuntu4
335 ```bash
336 sudo dpkg -i zlib1g_1.2.8.dfsg-2ubuntu4_arm64.deb
337 ```
338 <br><br>
339 
340 ### Unable to install arm64 packages after adding arm64 architecture
341 * Using sudo apt-get update should add all of the required repos for arm64 but if it does not or you are getting 404 errors the following instructions can be used to add the repos manually:
342 * From stackoverflow:
343 https://askubuntu.com/questions/430705/how-to-use-apt-get-to-download-multi-arch-library/430718
344 * Open /etc/apt/sources.list with your preferred text editor.
345 
346 * Mark all the current (default) repos as \[arch=<current_os_arch>], e.g.
347 ```bash
348 deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ xenial main restricted
349 ```
350 * Then add the following:
351 ```bash
352 deb [arch=arm64] http://ports.ubuntu.com/ xenial main restricted
353 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates main restricted
354 deb [arch=arm64] http://ports.ubuntu.com/ xenial universe
355 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates universe
356 deb [arch=arm64] http://ports.ubuntu.com/ xenial multiverse
357 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates multiverse
358 deb [arch=arm64] http://ports.ubuntu.com/ xenial-backports main restricted universe multiverse
359 ```
360 * Update and install again:
361 ```bash
362 sudo apt-get install zlib1g:arm64
363 sudo apt-get update
364 sudo ldconfig
365 ```
366 <br><br>
367 
368 ### Undefined references to google::protobuf:: functions
369 * Missing or out of date protobuf compilation libraries.
370  Use the command 'protoc --version' to check which version of protobuf is available (version 3.12.0 is required).
371  Follow the instructions above to install protobuf 3.12.0
372 <br><br>
373 
374 ### Errors on strict-aliasing rules when compiling the Compute Library
375 * When compiling the Compute Library there are multiple errors on strict-aliasing rules:
376  ```
377 cc1plus: error: unrecognized command line option ‘-Wno-implicit-fallthrough’ [-Werror]
378  ```
379 * Add Werror=0 to the scons command:
380 ```
381 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j8 Werror=0
382 ```