1 # How to Cross-Compile ArmNN on x86_64 for arm64
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 Caffe for x86_64](#build-caffe-for-x86-64)
7 - [Build Boost library for arm64](#build-boost-library-for-arm64)
8 - [Build Compute Library](#build-compute-library)
9 - [Build Tensorflow](#build-tensorflow)
10 - [Build Flatbuffer](#build-flatbuffer)
11 - [Build Onnx](#build-onnx)
12 - [Build TfLite](#build-tflite)
13 - [Build ArmNN](#build-armnn)
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-)
20 These are the step by step instructions on Cross-Compiling ArmNN under an x86_64 system to target an Arm64 system. This build flow has been tested with Ubuntu 16.04.
21 The instructions show how to build the ArmNN core library and the Boost, Protobuf, Caffe, Tensorflow, Tflite, Flatbuffer and Compute Libraries necessary for compilation.
23 ## Cross-compiling ToolChain
24 * Install the standard cross-compilation libraries for arm64:
26 sudo apt install crossbuild-essential-arm64
29 ## Build and install Google's Protobuf library
31 We support protobuf version 3.12.0
32 * Get protobuf from here: https://github.com/protocolbuffers/protobuf :
34 git clone -b v3.12.0 https://github.com/google/protobuf.git protobuf
36 git submodule update --init --recursive
39 * Build a native (x86_64) version of the protobuf libraries and compiler (protoc):
40 (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: sudo apt install curl autoconf libtool build-essential g++)
44 ../configure --prefix=$HOME/armnn-devenv/google/x86_64_pb_install
48 * Build the arm64 version of the protobuf libraries:
52 export CC=aarch64-linux-gnu-gcc \
53 export CXX=aarch64-linux-gnu-g++ \
54 ../configure --host=aarch64-linux \
55 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
56 --with-protoc=$HOME/armnn-devenv/google/x86_64_pb_install/bin/protoc
61 ## Build Caffe for x86_64
62 * Ubuntu 16.04 installation. These steps are taken from the full Caffe installation documentation at: http://caffe.berkeleyvision.org/install_apt.html
63 * Install dependencies:
65 sudo apt-get install libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev
66 sudo apt-get install --no-install-recommends libboost-all-dev
67 sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
68 sudo apt-get install libopenblas-dev
69 sudo apt-get install libatlas-base-dev
71 * Download Caffe from: https://github.com/BVLC/caffe. We have tested using tag 1.0
73 git clone https://github.com/BVLC/caffe.git
75 git checkout eeebdab16155d34ff8f5f42137da7df4d1c7eab0
76 cp Makefile.config.example Makefile.config
78 * Adjust Makefile.config as necessary for your environment, for example:
83 #Add hdf5 and protobuf include and library directories (Replace $HOME with explicit /home/username dir):
84 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/ $HOME/armnn-devenv/google/x86_64_pb_install/include/
85 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial/ $HOME/armnn-devenv/google/x86_64_pb_install/lib/
89 export PATH=$HOME/armnn-devenv/google/x86_64_pb_install/bin/:$PATH
90 export LD_LIBRARY_PATH=$HOME/armnn-devenv/google/x86_64_pb_install/lib/:$LD_LIBRARY_PATH
92 * Compilation with Make:
97 # These should all run without errors
100 * caffe.pb.h and caffe.pb.cc will be needed when building ArmNN's Caffe Parser
102 ## Build Boost library for arm64
103 * Build Boost library for arm64
104 Download Boost version 1.64 from http://www.boost.org/doc/libs/1_64_0/more/getting_started/unix-variants.html
105 Using any version of Boost greater than 1.64 will fail to build ArmNN, due to different dependency issues.
107 tar -zxvf boost_1_64_0.tar.gz
109 echo "using gcc : arm : aarch64-linux-gnu-g++ ;" > user_config.jam
110 ./bootstrap.sh --prefix=$HOME/armnn-devenv/boost_arm64_install
111 ./b2 install toolset=gcc-arm link=static cxxflags=-fPIC --with-test --with-log --with-program_options -j32 --user-config=user_config.jam
114 ## Build Compute Library
115 * Building the Arm Compute Library:
117 git clone https://github.com/ARM-software/ComputeLibrary.git
119 git checkout <branch_name>
121 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j8 internal_only=0
124 For example, if you want to checkout release branch of 20.02:
126 git checkout branches/arm_compute_20_02
131 * Building Tensorflow version 2.3.1:
133 git clone https://github.com/tensorflow/tensorflow.git
135 git checkout fcc4b966f1265f466e82617020af93670141b009
136 ../armnn/scripts/generate_tensorflow_protobuf.sh ../tensorflow-protobuf ../google/x86_64_pb_install
140 * Building Flatbuffer version 1.12.0
142 wget -O flatbuffers-1.12.0.tar.gz https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz
143 tar xf flatbuffers-1.12.0.tar.gz
144 cd flatbuffers-1.12.0
148 cmake .. -DFLATBUFFERS_BUILD_FLATC=1 \
149 -DCMAKE_INSTALL_PREFIX:PATH=$<DIRECTORY_PATH> \
150 -DFLATBUFFERS_BUILD_TESTS=0
154 * Build arm64 version of flatbuffer
158 # Add -fPIC to allow us to use the libraries in shared objects.
159 CXXFLAGS="-fPIC" cmake .. -DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc \
160 -DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++ \
161 -DFLATBUFFERS_BUILD_FLATC=1 \
162 -DCMAKE_INSTALL_PREFIX:PATH=$<DIRECTORY_PATH> \
163 -DFLATBUFFERS_BUILD_TESTS=0
170 git clone https://github.com/onnx/onnx.git
172 git fetch https://github.com/onnx/onnx.git 553df22c67bee5f0fe6599cff60f1afc6748c635 && git checkout FETCH_HEAD
173 export LD_LIBRARY_PATH=$<DIRECTORY_PATH>/protobuf-host/lib:$LD_LIBRARY_PATH
174 ../google/x86_64_pb_install/bin/protoc onnx/onnx.proto --proto_path=. --proto_path=../google/x86_64_pb_install/include --cpp_out ../onnx
182 cp ../tensorflow/tensorflow/lite/schema/schema.fbs .
183 ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs
187 * Compile ArmNN for arm64:
189 git clone https://github.com/ARM-software/armnn.git
191 git checkout <branch_name>
197 For example, if you want to checkout release branch of 20.02:
199 git checkout branches/armnn_20_02
203 * Use CMake to configure your build environment, update the following script and run it from the armnn/build directory to set up the armNN build:
206 export CXX=aarch64-linux-gnu-g++ \
207 export CC=aarch64-linux-gnu-gcc \
209 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary \
210 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build/ \
211 -DBOOST_ROOT=$HOME/armnn-devenv/boost_arm64_install/ \
212 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
213 -DCAFFE_GENERATED_SOURCES=$HOME/armnn-devenv/caffe/build/src \
214 -DBUILD_CAFFE_PARSER=1 \
215 -DONNX_GENERATED_SOURCES=$HOME/onnx \
216 -DBUILD_ONNX_PARSER=1 \
217 -DTF_GENERATED_SOURCES=$HOME/tensorflow-protobuf \
218 -DBUILD_TF_PARSER=1 \
219 -DBUILD_TF_LITE_PARSER=1 \
220 -DTF_LITE_GENERATED_PATH=$HOME/tflite \
221 -DFLATBUFFERS_ROOT=$HOME/flatbuffers-arm64 \
222 -DFLATC_DIR=$HOME/flatbuffers-1.12.0/build \
223 -DPROTOBUF_ROOT=$HOME/google/x86_64_pb_install \
224 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/x86_64_pb_install/ \
225 -DPROTOBUF_LIBRARY_DEBUG=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0 \
226 -DPROTOBUF_LIBRARY_RELEASE=$HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so.23.0.0
229 * 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:
231 -DSAMPLE_DYNAMIC_BACKEND=1 \
232 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
239 ## Build Standalone Sample Dynamic Backend
240 * The sample dynamic backend is located in armnn/src/dynamic/sample
246 * 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 armNN build:
249 export CXX=aarch64-linux-gnu-g++ \
250 export CC=aarch64-linux-gnu-gcc \
252 -DCMAKE_CXX_FLAGS=--std=c++14 \
253 -DBOOST_ROOT=$HOME/armnn-devenv/boost_arm64_install/ \
254 -DBoost_SYSTEM_LIBRARY=$HOME/armnn-devenv/boost_arm64_install/lib/libboost_system.a \
255 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so
264 * Copy the build folder to an arm64 linux machine
265 * Copy the libprotobuf.so.15.0.1 library file to the build folder
266 * 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 ArmNN
267 * cd to the build folder on your arm64 machine and set your LD_LIBRARY_PATH to its current location:
271 export LD_LIBRARY_PATH=<current_working_directory>
274 * Create a symbolic link to libprotobuf.so.23.0.0:
277 ln -s libprotobuf.so.23.0.0 ./libprotobuf.so.23
284 Running 567 test cases...
286 *** No errors detected
289 ## Troubleshooting and Errors:
290 ### Error adding symbols: File in wrong format
291 * When building armNN:
293 /usr/local/lib/libboost_log.a: error adding symbols: File in wrong format
294 collect2: error: ld returned 1 exit status
295 CMakeFiles/armnn.dir/build.make:4028: recipe for target 'libarmnn.so' failed
296 make[2]: *** [libarmnn.so] Error 1
297 CMakeFiles/Makefile2:105: recipe for target 'CMakeFiles/armnn.dir/all' failed
298 make[1]: *** [CMakeFiles/armnn.dir/all] Error 2
299 Makefile:127: recipe for target 'all' failed
300 make: *** [all] Error 2
302 * Boost libraries are not compiled for the correct architecture, try recompiling for arm64
305 ### Virtual memory exhausted
306 * When compiling the boost libraries:
308 virtual memory exhausted: Cannot allocate memory
310 * Not enough memory available to compile. Increase the amount of RAM or swap space available.
313 ### Unrecognized command line option '-m64'
314 * When compiling the boost libraries:
316 aarch64-linux-gnu-g++: error: unrecognized command line option ‘-m64’
318 * Clean the boost library directory before trying to build with a different architecture:
322 * It should show the following for arm64:
330 ### Missing libz.so.1
331 * When compiling armNN:
333 /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)
336 * Missing arm64 libraries for libz.so.1, these can be added by adding a second architecture to dpkg and explicitly installing them:
338 sudo dpkg --add-architecture arm64
339 sudo apt-get install zlib1g:arm64
343 * If apt-get update returns 404 errors for arm64 repos refer to section 5 below.
344 * Alternatively the missing arm64 version of libz.so.1 can be downloaded and installed from a .deb package here:
345 https://launchpad.net/ubuntu/wily/arm64/zlib1g/1:1.2.8.dfsg-2ubuntu4
347 sudo dpkg -i zlib1g_1.2.8.dfsg-2ubuntu4_arm64.deb
351 ### Unable to install arm64 packages after adding arm64 architecture
352 * 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:
353 * From stackoverflow:
354 https://askubuntu.com/questions/430705/how-to-use-apt-get-to-download-multi-arch-library/430718
355 * Open /etc/apt/sources.list with your preferred text editor.
357 * Mark all the current (default) repos as \[arch=<current_os_arch>], e.g.
359 deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ xenial main restricted
361 * Then add the following:
363 deb [arch=arm64] http://ports.ubuntu.com/ xenial main restricted
364 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates main restricted
365 deb [arch=arm64] http://ports.ubuntu.com/ xenial universe
366 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates universe
367 deb [arch=arm64] http://ports.ubuntu.com/ xenial multiverse
368 deb [arch=arm64] http://ports.ubuntu.com/ xenial-updates multiverse
369 deb [arch=arm64] http://ports.ubuntu.com/ xenial-backports main restricted universe multiverse
371 * Update and install again:
373 sudo apt-get install zlib1g:arm64
379 ### Undefined references to google::protobuf:: functions
380 * When compiling armNN there are multiple errors of the following type:
382 libarmnnCaffeParser.so: undefined reference to `google::protobuf:*
384 * Missing or out of date protobuf compilation libraries.
385 Use the command 'protoc --version' to check which version of protobuf is available (version 3.12.0 is required).
386 Follow the instructions above to install protobuf 3.12.0
387 Note this will require you to recompile Caffe for x86_64
390 ### Errors on strict-aliasing rules when compiling the Compute Library
391 * When compiling the Compute Library there are multiple errors on strict-aliasing rules:
393 cc1plus: error: unrecognized command line option ‘-Wno-implicit-fallthrough’ [-Werror]
395 * Add Werror=0 to the scons command:
397 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" -j8 internal_only=0 Werror=0