• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Cross compilation TensorFlow Lite with CMake
2
3This page describes how to build the TensorFlow Lite library for various ARM
4devices.
5
6The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64)
7, TensorFlow devel docker image
8[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/).
9
10**Note:** This feature is currently experimental and available since version 2.4
11and may change.
12
13### Prerequisites
14
15You need CMake installed and downloaded TensorFlow source code. Please check
16[Build TensorFlow Lite with CMake](https://www.tensorflow.org/lite/guide/build_cmake)
17page for the details.
18
19### Check your target environment
20
21The following examples are tested under Raspberry Pi OS, Ubuntu Server 20.04 LTS
22and Mendel Linux 4.0. Depending on your target glibc version and CPU
23capabilities, you may need to use different version of toolchain and build
24parameters.
25
26#### Checking glibc version
27
28```sh
29ldd --version
30```
31
32<pre  class="tfo-notebook-code-cell-output">
33ldd (Debian GLIBC 2.28-10) 2.28
34Copyright (C) 2018 Free Software Foundation, Inc.
35This is free software; see the source for copying conditions.  There is NO
36warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
37Written by Roland McGrath and Ulrich Drepper.
38</pre>
39
40#### Checking ABI compatibility
41
42If your target is ARM 32-bit, there are two ABI available depending on VFP
43availity. [armhf](https://wiki.debian.org/ArmHardFloatPort) and
44[armel](https://wiki.debian.org/ArmEabiPort). This document shows an armhf
45example, you need to use different toolchain for armel targets.
46
47#### Checking CPU capability
48
49For ARMv7, you should know target's supported VFP version and NEON availability.
50
51```sh
52cat /proc/cpuinfo
53```
54
55<pre  class="tfo-notebook-code-cell-output">
56processor   : 0
57model name  : ARMv7 Processor rev 3 (v7l)
58BogoMIPS    : 108.00
59Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
60CPU implementer : 0x41
61CPU architecture: 7
62CPU variant : 0x0
63CPU part    : 0xd08
64CPU revision    : 3
65</pre>
66
67## Build for AArch64 (ARM64)
68
69This instruction shows how to build AArch64 binary which is compatible with
70[Coral Mendel Linux 4.0](https://coral.ai/), Raspberry Pi (with
71[Ubuntu Server 20.04.01 LTS 64-bit](https://ubuntu.com/download/raspberry-pi)
72installed).
73
74#### Download toolchain
75
76These commands install gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu toolchain
77under ${HOME}/toolchains.
78
79```sh
80curl -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
81mkdir -p ${HOME}/toolchains
82tar xvf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz -C ${HOME}/toolchains
83```
84
85**Note:** Binaries built with GCC 8.3 require glibc 2.28 or higher. If your
86target has lower glibc version, you need to use older GCC toolchain.
87
88#### Run CMake
89
90```sh
91ARMCC_PREFIX=${HOME}/toolchains/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu-
92ARMCC_FLAGS="-funsafe-math-optimizations"
93cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
94  -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
95  -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
96  -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
97  -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
98  -DCMAKE_SYSTEM_NAME=Linux \
99  -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
100  ../tensorflow/lite/
101```
102
103**Note:** You can enable GPU delegate with "-DTFLITE_ENABLE_GPU=ON" if your
104target device supports OpenCL 1.2 or higher.
105
106## Build for ARMv7 NEON enabled
107
108This instruction shows how to build ARMv7 with VFPv4 and NEON enabled binary
109which is compatible with Raspberry Pi 3 and 4.
110
111#### Download toolchain
112
113These commands install gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf toolchain
114under ${HOME}/toolchains.
115
116```sh
117curl -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-arm-linux-gnueabihf.tar.xz
118mkdir -p ${HOME}/toolchains
119tar xvf gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz -C ${HOME}/toolchains
120```
121
122**Note:** Binaries built with GCC 8.3 require glibc 2.28 or higher. If your
123target has lower glibc version, you need to use older GCC toolchain.
124
125#### Run CMake
126
127```sh
128ARMCC_FLAGS="-march=armv7-a -mfpu=neon-vfpv4 -funsafe-math-optimizations"
129ARMCC_PREFIX=${HOME}/toolchains/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
130cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
131  -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
132  -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
133  -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
134  -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
135  -DCMAKE_SYSTEM_NAME=Linux \
136  -DCMAKE_SYSTEM_PROCESSOR=armv7 \
137  ../tensorflow/lite/
138```
139
140**Note:** Since ARMv7 architecture is diverse, you may need to update
141ARMCC_FLAGS for your target device profiles.
142
143## Build for Raspberry Pi Zero (ARMv6)
144
145This instruction shows how to build ARMv6 binary which is compatible with
146Raspberry Pi Zero.
147
148#### Download toolchain
149
150These commands install arm-rpi-linux-gnueabihf toolchain under
151${HOME}/toolchains.
152
153```sh
154curl -L https://github.com/rvagg/rpi-newer-crosstools/archive/eb68350c5c8ec1663b7fe52c742ac4271e3217c5.tar.gz -o rpi-toolchain.tar.gz
155tar xzf rpi-toolchain.tar.gz -C ${HOME}/toolchains
156mv ${HOME}/toolchains/rpi-newer-crosstools-eb68350c5c8ec1663b7fe52c742ac4271e3217c5 ${HOME}/toolchains/arm-rpi-linux-gnueabihf
157```
158
159#### Run CMake
160
161```sh
162ARMCC_PREFIX=${HOME}/toolchains/arm-rpi-linux-gnueabihf/x64-gcc-6.5.0/arm-rpi-linux-gnueabihf/bin/arm-rpi-linux-gnueabihf-
163ARMCC_FLAGS="-march=armv6 -mfpu=vfp -funsafe-math-optimizations"
164cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
165  -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
166  -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
167  -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
168  -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
169  -DCMAKE_SYSTEM_NAME=Linux \
170  -DCMAKE_SYSTEM_PROCESSOR=armv6 \
171  -DTFLITE_ENABLE_XNNPACK=OFF \
172  ../tensorflow/lite/
173```
174
175**Note:** XNNPACK is disabled since there is no NEON support.
176