1# Build TensorFlow Lite for ARM64 boards 2 3This page describes how to build the TensorFlow Lite static and shared libraries 4for ARM64-based computers. If you just want to start using TensorFlow Lite to 5execute your models, the fastest option is to install the TensorFlow Lite 6runtime package as shown in the [Python quickstart](python.md). 7 8Note: This page shows how to compile only the C++ static and shared libraries 9for TensorFlow Lite. Alternative install options include: 10[install just the Python interpreter API](python.md) (for inferencing only); 11[install the full TensorFlow package from pip](https://www.tensorflow.org/install/pip); 12or 13[build the full TensorFlow package](https://www.tensorflow.org/install/source). 14 15**Note:** Cross-compile ARM with CMake is available. Please check 16[this](https://www.tensorflow.org/lite/guide/build_cmake_arm). 17 18## Cross-compile for ARM64 with Make 19 20To ensure the proper build environment, we recommend using one of our TensorFlow 21Docker images such as 22[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/). 23 24To get started, install the toolchain and libs: 25 26```bash 27sudo apt-get update 28sudo apt-get install crossbuild-essential-arm64 29``` 30 31If you are using Docker, you may not use `sudo`. 32 33Now git-clone the TensorFlow repository 34(https://github.com/tensorflow/tensorflow)—if you're using the TensorFlow Docker 35image, the repo is already provided in `/tensorflow_src/`—and then run this 36script at the root of the TensorFlow repository to download all the build 37dependencies: 38 39```bash 40./tensorflow/lite/tools/make/download_dependencies.sh 41``` 42 43Note that you only need to do this once. 44 45Then compile: 46 47```bash 48./tensorflow/lite/tools/make/build_aarch64_lib.sh 49``` 50 51This should compile a static library in: 52`tensorflow/lite/tools/make/gen/linux_aarch64/lib/libtensorflow-lite.a`. 53 54## Compile natively on ARM64 55 56These steps were tested on HardKernel Odroid C2, gcc version 5.4.0. 57 58Log in to your board and install the toolchain: 59 60```bash 61sudo apt-get install build-essential 62``` 63 64Now git-clone the TensorFlow repository 65(https://github.com/tensorflow/tensorflow) and run this at the root of the 66repository: 67 68```bash 69./tensorflow/lite/tools/make/download_dependencies.sh 70``` 71 72Note that you only need to do this once. 73 74Then compile: 75 76```bash 77./tensorflow/lite/tools/make/build_aarch64_lib.sh 78``` 79 80This should compile a static library in: 81`tensorflow/lite/tools/make/gen/linux_aarch64/lib/libtensorflow-lite.a`. 82 83## Cross-compile for ARM64 with Bazel 84 85You can use 86[ARM GCC toolchains](https://github.com/tensorflow/tensorflow/tree/master/third_party/toolchains/embedded/arm-linux) 87with Bazel to build an ARM64 shared library. 88 89Note: The generated shared library requires glibc 2.28 or higher to run. 90 91The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64) 92and TensorFlow devel docker image 93[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/). 94 95To cross compile TensorFlow Lite with Bazel, follow the steps: 96 97#### Step 1. Install Bazel 98 99Bazel is the primary build system for TensorFlow. Install the latest version of 100the [Bazel build system](https://bazel.build/versions/master/docs/install.html). 101 102**Note:** If you're using the TensorFlow Docker image, Bazel is already 103available. 104 105#### Step 2. Clone TensorFlow repository 106 107```sh 108git clone https://github.com/tensorflow/tensorflow.git tensorflow_src 109``` 110 111**Note:** If you're using the TensorFlow Docker image, the repo is already 112provided in `/tensorflow_src/`. 113 114#### Step 3. Build ARM64 binary 115 116##### C library 117 118```bash 119bazel build --config=elinux_aarch64 -c opt //tensorflow/lite/c:libtensorflowlite_c.so 120``` 121 122Check 123[TensorFlow Lite C API](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/c) 124page for the detail. 125 126##### C++ library 127 128```bash 129bazel build --config=elinux_aarch64 -c opt //tensorflow/lite:libtensorflowlite.so 130``` 131 132You can find a shared library in: 133`bazel-bin/tensorflow/lite/libtensorflowlite.so`. 134 135Currently, there is no straightforward way to extract all header files needed, 136so you must include all header files in tensorflow/lite/ from the TensorFlow 137repository. Additionally, you will need header files from FlatBuffers and 138Abseil. 139 140##### Etc 141 142You can also build other Bazel targets with the toolchain. Here are some useful 143targets. 144 145* //tensorflow/lite/tools/benchmark:benchmark_model 146* //tensorflow/lite/examples/label_image:label_image 147