Cross-Compiling Arm NN For The Raspberry Pi and TensorFlow
Cross-Compiling Arm NN For The Raspberry Pi and TensorFlow
TensorFlow
Version 1.4
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
Your access to the information in this document is conditional upon your acceptance that you will not use or permit others to use
the information for the purposes of determining whether implementations infringe any third party patents.
THIS DOCUMENT IS PROVIDED “AS IS”. ARM PROVIDES NO REPRESENTATIONS AND NO WARRANTIES, EXPRESS, IMPLIED OR
STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY, NON-
INFRINGEMENT OR FITNESS FOR A PARTICULAR PURPOSE WITH RESPECT TO THE DOCUMENT. For the avoidance of doubt, Arm
makes no representation with respect to, and has undertaken no analysis to identify or understand the scope and content of,
patents, copyrights, trade secrets, or other rights.
TO THE EXTENT NOT PROHIBITED BY LAW, IN NO EVENT WILL ARM BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT
LIMITATION ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES, HOWEVER CAUSED AND
REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF ANY USE OF THIS DOCUMENT, EVEN IF ARM HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
This document consists solely of commercial items. You shall be responsible for ensuring that any use, duplication or disclosure of
this document complies fully with any relevant export laws and regulations to assure that this document or any portion thereof is
not exported, directly or indirectly, in violation of such export laws. Use of the word “partner” in reference to Arm’s customers is not
intended to create or refer to any partnership relationship with any other company. Arm may make changes to this document at any
time and without notice.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 2 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
If any of the provisions contained in these terms conflict with any of the provisions of any click through or signed written agreement
covering this document with Arm, then the click through or signed written agreement prevails over and supersedes the conflicting
provisions of these terms. This document may be translated into other languages for convenience, and you agree that if there is any
conflict between the English version of this document and any translation, the terms of the English version of the Agreement shall
prevail.
The Arm corporate logo and words marked with ® or ™ are registered trademarks or trademarks of Arm Limited (or its subsidiaries)
in the US and/or elsewhere. All rights reserved. Other brands and names mentioned in this document may be the trademarks of
their respective owners. Please follow Arm’s trademark usage guidelines at http://www.arm.com/company/policies/trademarks.
3T 3T
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
LES-PRE-20349
Confidentiality Status
This document is Non-Confidential. The right to use, copy and disclose this document may be subject to license restrictions in
accordance with the terms of the agreement entered into by Arm and the party that Arm delivered this document to.
Product Status
The information in this document is Final, that is for a developed product.
Web Address
www.arm.com
3T 3T
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 3 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
Contents
1 Overview .............................................................................................................................................................................................................5
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 4 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
1 Overview
This guide covers what we must do to cross-compile Arm NN using an x86_64 system to target a Raspberry Pi. Cross-compiling Arm NN allows us to
work around the limited memory of the Raspberry Pi. Read the guide to find out how to build the Compute Library, Boost, Protobuf, TensorFlow,
and Arm NN core libraries that you need for compilation. When we finish, we will be able to compile and run programs that use Arm NN on our
Raspberry Pis.
Arm estimates that you will take 90-120 minutes to complete this guide.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 5 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
• You have a Raspberry Pi board. Arm has tested these instructions on a Raspberry Pi 2 Model B V1.1 that runs Raspian 9, a Rasberry Pi 3
Model B+ that runs Raspian 8, and a Raspberry Pi 4 Model B that runs Raspian 9.
• You compile on a Linux virtual machine. Arm has tested these instructions on an Ubuntu 16.04 virtual machine that runs on MacOS and
Windows 10.
You must install the following software tools on your virtual machine:
Git A version control system software developers use for source code management.
GNU C and C++ compilers for the armhf architecture The Raspberry Pi uses the armhf Arm architecture.
Note: For the instructions in this guide to work, the cross-compiler version on the host machine must match the compiler version on your
Raspberry Pi.
GNU Autoconf A tool for producing configure scripts for building, installing and packaging software.
CMake An open-source and cross-platform family of tools for building, testing, and packaging software.
To install the tools you require, open a command window and enter the following commands:
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 6 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
2. Download the Compute Library, Boost, Protobuf, TensorFlow, and Arm NN git repositories and source bundles. To download the
repositories and bundles, enter the following commands:
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 7 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
cd $BASEDIR/ComputeLibrary
scons extra_cxx_flags="-fPIC" Werror=0 debug=0 asserts=0 neon=1 opencl=0 os=linux arch=armv7a
examples=1
Arm estimates that your virtual machine will take approximately 15-20 minutes to execute this command.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 8 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
cd $BASEDIR/boost_1_64_0/tools/build
./bootstrap.sh
./b2 install --prefix=$BASEDIR/boost.build
export PATH=$BASEDIR/boost.build/bin:$PATH
2. Create a project-config.jam file by copying the user-config.jam file. To copy the user-config.jam file, enter the
following command:
cp $BASEDIR/boost_1_64_0/tools/build/example/user-config.jam
$BASEDIR/boost_1_64_0/project-config.jam
3. Go to the $BASEDIR/boost_1_64_0/ directory and open the project-config.jam file in a text editor. In the GCC
Configuration section, add the following line:
cd $BASEDIR/boost_1_64_0
b2 --build-dir=$BASEDIR/boost_1_64_0/build toolset=gcc-arm link=static cxxflags=-fPIC --with-
filesystem --with-test --with-log --with-program_options install --prefix=$BASEDIR/boost
Arm estimates that your virtual machine will take approximately 15 minutes to execute these commands.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 9 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
6.1. Building the Google Protobuf library for your virtual machine
To build the Google Protobuf library for your virtual machine:
cd $BASEDIR/protobuf
git submodule update --init --recursive
./autogen.sh
./configure --prefix=$BASEDIR/protobuf-host
make
Arm estimates that your virtual machine will take approximately 15 minutes to execute these commands.
make install
make clean
Arm estimates that your virtual machine will take approximately 15 minutes to execute these commands.
make install
cd $BASEDIR/tensorflow
../armnn/scripts/generate_tensorflow_protobuf.sh ../tensorflow-protobuf ../protobuf-host
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 10 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
7 Building Arm NN
To build Arm NN:
cd $BASEDIR/armnn
mkdir build
cd build
2. Place the library files you require in the build directory. To place the library files, enter:
cmake .. -DCMAKE_LINKER=/usr/bin/arm-linux-gnueabihf-ld \
-DCMAKE_C_COMPILER=/usr/bin/arm-linux-gnueabihf-gcc \
-DCMAKE_CXX_COMPILER=/usr/bin/arm-linux-gnueabihf-g++ \
-DCMAKE_C_COMPILER_FLAGS=-fPIC \
-DCMAKE_CXX_FLAGS=-mfpu=neon \
-DARMCOMPUTE_ROOT=$BASEDIR/ComputeLibrary \
-DARMCOMPUTE_BUILD_DIR=$BASEDIR/ComputeLibrary/build \
-DBOOST_ROOT=$BASEDIR/boost \
-DTF_GENERATED_SOURCES=$BASEDIR/tensorflow-protobuf \
-DBUILD_TF_PARSER=1 \
-DPROTOBUF_ROOT=$BASEDIR/protobuf-arm \
-DARMCOMPUTENEON=1
make
Arm estimates that your virtual machine will take approximately 12 minutes to execute these commands.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 11 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
1. Copy the following libraries, binaries, and directories from your virtual machine. To copy these libraries, binaries, and directories enter the
following commands:
cd $BASEDIR
mkdir armnn-dist
mkdir armnn-dist/armnn
mkdir armnn-dist/armnn/lib
cp $BASEDIR/armnn/build/libarmnn.so $BASEDIR/armnn-dist/armnn/lib
cp $BASEDIR/armnn/build/libarmnnTfParser.so $BASEDIR/armnn-dist/armnn/lib
cp $BASEDIR/protobuf-arm/lib/libprotobuf.so.15.0.0 $BASEDIR/armnn-
dist/armnn/lib/libprotobuf.so
cp $BASEDIR/protobuf-arm/lib/libprotobuf.so.15.0.0 $BASEDIR/armnn-
dist/armnn/lib/libprotobuf.so.15
cp -r $BASEDIR/armnn/include $BASEDIR/armnn-dist/armnn/include
cp -r $BASEDIR/boost $BASEDIR/armnn-dist/boost
2. Copy the following dynamic backend related files from your virtual machine. To copy these files, enter the following commands:
mkdir -p $BASEDIR/armnn-dist/src/backends/backendsCommon/test/
cp -r $BASEDIR/armnn/build/src/backends/backendsCommon/test/testSharedObject $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/testSharedObject/
cp -r $BASEDIR/armnn/build/src/backends/backendsCommon/test/testDynamicBackend/
$BASEDIR/armnn-dist/src/backends/backendsCommon/test/testDynamicBackend/
cp -r $BASEDIR/armnn/build/src/backends/backendsCommon/test/backendsTestPath1/ $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath1/
mkdir -p $BASEDIR/armnn-dist/src/backends/backendsCommon/test/backendsTestPath2
cp
$BASEDIR/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so
$BASEDIR/armnn-dist/src/backends/backendsCommon/test/backendsTestPath2/
ln -s Arm_CpuAcc_backend.so $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1
ln -s Arm_CpuAcc_backend.so.1 $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2
ln -s Arm_CpuAcc_backend.so.1.2 $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2.3
cp
$BASEDIR/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_GpuAcc_backend.so
$BASEDIR/armnn-dist/src/backends/backendsCommon/test/backendsTestPath2/
ln -s nothing $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath2/Arm_no_backend.so
mkdir -p $BASEDIR/armnn-dist/src/backends/backendsCommon/test/backendsTestPath3
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 12 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
cp -r $BASEDIR/armnn/build/src/backends/backendsCommon/test/backendsTestPath5/ $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath5
cp -r $BASEDIR/armnn/build/src/backends/backendsCommon/test/backendsTestPath6/ $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath6
mkdir -p $BASEDIR/armnn-dist/src/backends/backendsCommon/test/backendsTestPath7
cp -r $BASEDIR/armnn/build/src/backends/backendsCommon/test/backendsTestPath9/ $BASEDIR/armnn-
dist/src/backends/backendsCommon/test/backendsTestPath9
mkdir -p $BASEDIR/armnn-dist/src/backends/dynamic/reference
cp $BASEDIR/armnn/build/src/backends/dynamic/reference/Arm_CpuRef_backend.so $BASEDIR/armnn-
dist/src/backends/dynamic/reference/
3. Copy the Unit Tests and a sample Arm NN program. To copy this test and program, enter the following commands:
cp $BASEDIR/armnn/build/UnitTests $BASEDIR/armnn-dist
cp $BASEDIR/armnn/samples/SimpleSample.cpp $BASEDIR/armnn-dist
4. Create the archive for your Raspberry Pi. To create the archive, enter the following command:
cd /home/pi
tar -xzf /home/pi/armnn-dist.tar.gz
export LD_LIBRARY_PATH=/home/pi/armnn-dist/armnn/lib
cd /home/pi/armnn-dist
./SimpleSample
345
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 13 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 14 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
export LD_LIBRARY_PATH=/home/pi/armnn-dist/armnn/lib
cd /home/pi/armnn-dist
./UnitTests
If the tests are successful, they output the following to the console:
If some of the tests are unsuccessful, go back through the steps and check that all the commands have been entered correctly.
Note: Arm is aware of an issue resulting in a NeonTimerMeasure unit test error on Raspberry Pi. The implementation by the Raspberry Pi of the
timing libraries Arm NN uses to get its timing data causes this error. You can safely ignore this error.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 15 of 16
ARM062-948681440-2662
Cross-compiling Arm NN for the Raspberry Pi Version 1.4
and TensorFlow
10 Next steps
You are now ready to compile and run programs that use Arm NN on your Raspberry Pi.
Copyright © 2018-2019 Arm Limited (or its affiliates). All rights reserved.
Non-Confidential
Page 16 of 16