ArmNN
 21.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 - [Build and install Google's Protobuf library](#build-and-install-google-s-protobuf-library)
6 - [Build Boost library for arm64](#build-boost-library-for-arm64)
7 - [Build Compute Library](#build-compute-library)
8 - [Download ArmNN](#download-armnn)
9 - [Build Flatbuffer](#build-flatbuffer)
10 - [Build Onnx](#build-onnx)
11 - [Build TfLite](#build-tflite)
12 - [Build Arm NN](#build-armnn)
13 - [Build Standalone Sample Dynamic Backend](#build-standalone-sample-dynamic-backend)
14 - [Run Unit Tests](#run-unit-tests)
15 - [Troubleshooting and Errors:](#troubleshooting-and-errors-)
16 
17 
18 ## Introduction
19 These are the step by step instructions on Cross-Compiling Arm NN under an x86_64 system to target an Arm64 system. This build flow has been tested with Ubuntu 16.04.
20 The instructions assume you are using a bash shell and show how to build the Arm NN core library, Boost, 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 #####Note: We are currently in the process of removing boost as a dependency to Arm NN. This process is finished for everything apart from our unit tests. This means you don't need boost to build and use Arm NN but you need it to execute our unit tests. Boost will soon be removed from Arm NN entirely. We also are deprecating support for Caffe and Tensorflow parsers in 21.02. This will be removed in 21.05.
29 
30 ## Cross-compiling ToolChain
31 * Install the standard cross-compilation libraries for arm64:
32 ```
33 sudo apt install crossbuild-essential-arm64
34 ```
35 
36 ## Build and install Google's Protobuf library
37 
38 We support protobuf version 3.12.0
39 * Get protobuf from here: https://github.com/protocolbuffers/protobuf :
40 ```bash
41 git clone -b v3.12.0 https://github.com/google/protobuf.git protobuf
42 cd protobuf
43 git submodule update --init --recursive
44 ./autogen.sh
45 ```
46 * Build a native (x86_64) version of the protobuf libraries and compiler (protoc):
47  (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: sudo apt install curl autoconf libtool build-essential g++)
48 ```
49 mkdir x86_64_build
50 cd x86_64_build
51 ../configure --prefix=$HOME/armnn-devenv/google/x86_64_pb_install
52 make install -j16
53 cd ..
54 ```
55 * Build the arm64 version of the protobuf libraries:
56 ```
57 mkdir arm64_build
58 cd arm64_build
59 CC=aarch64-linux-gnu-gcc \
60 CXX=aarch64-linux-gnu-g++ \
61 ../configure --host=aarch64-linux \
62 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
63 --with-protoc=$HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc
64 make install -j16
65 cd ..
66 ```
67 
68 ## Build Boost library for arm64
69 * Build Boost library for arm64
70  Download Boost version 1.64 from http://www.boost.org/doc/libs/1_64_0/more/getting_started/unix-variants.html
71  Using any version of Boost greater than 1.64 will fail to build Arm NN, due to different dependency issues.
72 ```bash
73 cd $HOME/armnn-devenv
74 tar -zxvf boost_1_64_0.tar.gz
75 cd boost_1_64_0
76 echo "using gcc : arm : aarch64-linux-gnu-g++ ;" > user_config.jam
77 ./bootstrap.sh --prefix=$HOME/armnn-devenv/boost_arm64_install
78 ./b2 install toolset=gcc-arm link=static cxxflags=-fPIC --with-test --with-log --with-program_options -j32 --user-config=user_config.jam
79 ```
80 
81 ## Build Compute Library
82 * Building the Arm Compute Library:
83 ```bash
84 cd $HOME/armnn-devenv
85 git clone https://github.com/ARM-software/ComputeLibrary.git
86 cd ComputeLibrary/
87 git checkout <tag_name>
88 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j4 internal_only=0
89 ```
90 
91 For example, if you want to checkout release tag of 21.02:
92 ```bash
93 git checkout v21.02
94 ```
95 
96 ## Download ArmNN
97 
98 ```bash
99 cd $HOME/armnn-devenv
100 git clone https://github.com/ARM-software/armnn.git
101 cd armnn
102 git checkout <branch_name>
103 git pull
104 ```
105 
106 For example, if you want to checkout release branch of 21.02:
107 ```bash
108 git checkout branches/armnn_21_02
109 git pull
110 ```
111 
112 ## Build Flatbuffer
113 * Building Flatbuffer version 1.12.0
114 ```bash
115 cd $HOME/armnn-devenv
116 wget -O flatbuffers-1.12.0.tar.gz https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz
117 tar xf flatbuffers-1.12.0.tar.gz
118 cd flatbuffers-1.12.0
119 rm -f CMakeCache.txt
120 mkdir build
121 cd build
122 cmake .. -DFLATBUFFERS_BUILD_FLATC=1 \
123  -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers \
124  -DFLATBUFFERS_BUILD_TESTS=0
125 make all install
126 ```
127 
128 * Build arm64 version of flatbuffer
129 ```bash
130 cd ..
131 mkdir build-arm64
132 cd build-arm64
133 # Add -fPIC to allow us to use the libraries in shared objects.
134 CXXFLAGS="-fPIC" cmake .. -DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc \
135  -DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++ \
136  -DFLATBUFFERS_BUILD_FLATC=1 \
137  -DCMAKE_INSTALL_PREFIX:PATH=$HOME/armnn-devenv/flatbuffers-arm64 \
138  -DFLATBUFFERS_BUILD_TESTS=0
139 make all install
140 ```
141 
142 ## Build Onnx
143 * Building Onnx
144 ```bash
145 cd $HOME/armnn-devenv
146 git clone https://github.com/onnx/onnx.git
147 cd onnx
148 git fetch https://github.com/onnx/onnx.git 553df22c67bee5f0fe6599cff60f1afc6748c635 && git checkout FETCH_HEAD
149 LD_LIBRARY_PATH=$HOME/armnn-devenv/google/x86_64_pb_install/lib:$LD_LIBRARY_PATH \
150 $HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc \
151 onnx/onnx.proto --proto_path=. --proto_path=../google/x86_64_pb_install/include --cpp_out $HOME/armnn-devenv/onnx
152 ```
153 
154 ## Build TfLite
155 * Building TfLite (Tensorflow version 2.3.1)
156 ```bash
157 cd $HOME/armnn-devenv
158 git clone https://github.com/tensorflow/tensorflow.git
159 cd tensorflow/
160 git checkout fcc4b966f1265f466e82617020af93670141b009
161 mkdir tflite
162 cd tflite
163 cp ../tensorflow/tensorflow/lite/schema/schema.fbs .
164 ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs
165 ```
166 
167 ## Build Arm NN
168 * Compile Arm NN for arm64:
169 ```bash
170 cd $HOME/armnn-devenv/armnn
171 mkdir build
172 cd build
173 ```
174 
175 * 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:
176 ```bash
177 #!/bin/bash
178 CXX=aarch64-linux-gnu-g++ CC=aarch64-linux-gnu-gcc cmake .. \
179 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary \
180 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build/ \
181 -DBOOST_ROOT=$HOME/armnn-devenv/boost_arm64_install/ \
182 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
183 -DONNX_GENERATED_SOURCES=$HOME/armnn-devenv/onnx \
184 -DBUILD_ONNX_PARSER=1 \
185 -DBUILD_TF_LITE_PARSER=1 \
186 -DTF_LITE_GENERATED_PATH=$HOME/armnn-devenv/tflite \
187 -DFLATBUFFERS_ROOT=$HOME/armnn-devenv/flatbuffers-arm64 \
188 -DFLATC_DIR=$HOME/armnn-devenv/flatbuffers-1.12.0/build \
189 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install \
190 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install/ \
191 -DPROTOBUF_LIBRARY_DEBUG=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0 \
192 -DPROTOBUF_LIBRARY_RELEASE=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0
193 ```
194 
195 * 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:
196 ```bash
197 -DSAMPLE_DYNAMIC_BACKEND=1 \
198 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
199 ```
200 * Run the build
201 ```bash
202 make -j32
203 ```
204 
205 ## Build Standalone Sample Dynamic Backend
206 * The sample dynamic backend is located in armnn/src/dynamic/sample
207 ```bash
208 cd $HOME/armnn-devenv/armnn/src/dynamic/sample
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/src/dynamic/sample/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 -DCMAKE_CXX_FLAGS=--std=c++14 \
218 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so
219 ```
220 
221 * Run the build
222 ```bash
223 make
224 ```
225 
226 ## Run Unit Tests
227 * Copy the build folder to an arm64 linux machine
228 * Copy the libprotobuf.so.23.0.0 library file to the build folder
229 * 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
230 * cd to the build folder on your arm64 machine and set your LD_LIBRARY_PATH to its current location:
231 
232 ```bash
233 cd build/
234 ```
235 
236 * Create a symbolic link to libprotobuf.so.23.0.0:
237 
238 ```bash
239 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so.23
240 ```
241 
242 * Run the UnitTests:
243 
244 ```bash
245 LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./UnitTests
246 Running 4493 test cases...
247 
248 *** No errors detected
249 ```
250 
251 ## Troubleshooting and Errors:
252 ### Error adding symbols: File in wrong format
253 * When building Arm NN:
254 ```bash
255 /usr/local/lib/libboost_log.a: error adding symbols: File in wrong format
256 collect2: error: ld returned 1 exit status
257 CMakeFiles/armnn.dir/build.make:4028: recipe for target 'libarmnn.so' failed
258 make[2]: *** [libarmnn.so] Error 1
259 CMakeFiles/Makefile2:105: recipe for target 'CMakeFiles/armnn.dir/all' failed
260 make[1]: *** [CMakeFiles/armnn.dir/all] Error 2
261 Makefile:127: recipe for target 'all' failed
262 make: *** [all] Error 2
263 ```
264 * Boost libraries are not compiled for the correct architecture, try recompiling for arm64
265 <br><br>
266 
267 ### Virtual memory exhausted
268 * When compiling the boost libraries:
269 ```bash
270 virtual memory exhausted: Cannot allocate memory
271 ```
272 * Not enough memory available to compile. Increase the amount of RAM or swap space available.
273 <br><br>
274 
275 ### Unrecognized command line option '-m64'
276 * When compiling the boost libraries:
277 ```bash
278 aarch64-linux-gnu-g++: error: unrecognized command line option ‘-m64’
279 ```
280 * Clean the boost library directory before trying to build with a different architecture:
281 ```bash
282 sudo ./b2 clean
283 ```
284 * It should show the following for arm64:
285 ```bash
286 - 32-bit : no
287 - 64-bit : yes
288 - arm : yes
289 ```
290 <br><br>
291 
292 ### Missing libz.so.1
293 * When compiling armNN:
294 ```bash
295 /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)
296 ```
297 
298 * Missing arm64 libraries for libz.so.1, these can be added by adding a second architecture to dpkg and explicitly installing them:
299 ```bash
300 sudo dpkg --add-architecture arm64
301 sudo apt-get install zlib1g:arm64
302 sudo apt-get update
303 sudo ldconfig
304 ```
305 * If apt-get update returns 404 errors for arm64 repos refer to section 5 below.
306 * Alternatively the missing arm64 version of libz.so.1 can be downloaded and installed from a .deb package here:
307  https://launchpad.net/ubuntu/wily/arm64/zlib1g/1:1.2.8.dfsg-2ubuntu4
308 ```bash
309 sudo dpkg -i zlib1g_1.2.8.dfsg-2ubuntu4_arm64.deb
310 ```
311 <br><br>
312 
313 ### Unable to install arm64 packages after adding arm64 architecture
314 * 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:
315 * From stackoverflow:
316 https://askubuntu.com/questions/430705/how-to-use-apt-get-to-download-multi-arch-library/430718
317 * Open /etc/apt/sources.list with your preferred text editor.
318 
319 * Mark all the current (default) repos as \[arch=<current_os_arch>], e.g.
320 ```bash
321 deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ xenial main restricted
322 ```
323 * Then add the following:
324 ```bash
325 deb [arch=arm64] http://ports.ubuntu.com/ xenial main restricted
326 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates main restricted
327 deb [arch=arm64] http://ports.ubuntu.com/ xenial universe
328 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates universe
329 deb [arch=arm64] http://ports.ubuntu.com/ xenial multiverse
330 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates multiverse
331 deb [arch=arm64] http://ports.ubuntu.com/ xenial-backports main restricted universe multiverse
332 ```
333 * Update and install again:
334 ```bash
335 sudo apt-get install zlib1g:arm64
336 sudo apt-get update
337 sudo ldconfig
338 ```
339 <br><br>
340 
341 ### Undefined references to google::protobuf:: functions
342 * Missing or out of date protobuf compilation libraries.
343  Use the command 'protoc --version' to check which version of protobuf is available (version 3.12.0 is required).
344  Follow the instructions above to install protobuf 3.12.0
345 <br><br>
346 
347 ### Errors on strict-aliasing rules when compiling the Compute Library
348 * When compiling the Compute Library there are multiple errors on strict-aliasing rules:
349  ```
350 cc1plus: error: unrecognized command line option ‘-Wno-implicit-fallthrough’ [-Werror]
351  ```
352 * Add Werror=0 to the scons command:
353 ```
354 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j8 internal_only=0 Werror=0
355 ```