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