From 71c3e5eaaf71edbb4208c4d46f9bbbfddeb72c84 Mon Sep 17 00:00:00 2001 From: Terry Heo Date: Mon, 1 Feb 2021 18:52:43 -0800 Subject: [PATCH] Update CMake cross compilation guide PiperOrigin-RevId: 355074098 Change-Id: Ie6b23db2a0becd83303b6712cdf8228c1c9ddbaf --- tensorflow/lite/g3doc/_book.yaml | 4 + tensorflow/lite/g3doc/guide/build_arm64.md | 3 + .../lite/g3doc/guide/build_cmake_arm.md | 175 ++++++++++++++++++ tensorflow/lite/g3doc/guide/build_rpi.md | 3 + 4 files changed, 185 insertions(+) create mode 100644 tensorflow/lite/g3doc/guide/build_cmake_arm.md diff --git a/tensorflow/lite/g3doc/_book.yaml b/tensorflow/lite/g3doc/_book.yaml index a0f40ee6767..815e9bbfe39 100644 --- a/tensorflow/lite/g3doc/_book.yaml +++ b/tensorflow/lite/g3doc/_book.yaml @@ -184,6 +184,10 @@ upper_tabs: - title: "Build with CMake" path: /lite/guide/build_cmake status: experimental + section: + - title: "Cross compilation for ARM" + path: /lite/guide/build_cmake_arm + - title: "Reduce binary size" path: /lite/guide/reduce_binary_size status: experimental diff --git a/tensorflow/lite/g3doc/guide/build_arm64.md b/tensorflow/lite/g3doc/guide/build_arm64.md index 9a7bf12c1b8..9d5fbb38c43 100644 --- a/tensorflow/lite/g3doc/guide/build_arm64.md +++ b/tensorflow/lite/g3doc/guide/build_arm64.md @@ -12,6 +12,9 @@ for TensorFlow Lite. Alternative install options include: or [build the full TensorFlow package](https://www.tensorflow.org/install/source). +**Note:** Cross-compile ARM with CMake is available. Please check +[this](https://www.tensorflow.org/lite/guide/build_cmake_arm). + ## Cross-compile for ARM64 with Make To ensure the proper build environment, we recommend using one of our TensorFlow diff --git a/tensorflow/lite/g3doc/guide/build_cmake_arm.md b/tensorflow/lite/g3doc/guide/build_cmake_arm.md new file mode 100644 index 00000000000..c9d58055807 --- /dev/null +++ b/tensorflow/lite/g3doc/guide/build_cmake_arm.md @@ -0,0 +1,175 @@ +# Cross compilation TensorFlow Lite with CMake + +This page describes how to build the TensorFlow Lite library for various ARM +devices. + +The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64) +, TensorFlow devel docker image +[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/). + +**Note:** This feature is currently experimental and available since version 2.4 +and may change. + +### Prerequisites + +You need CMake installed and downloaded TensorFlow source code. Please check +[Build TensorFlow Lite with CMake](https://www.tensorflow.org/lite/guide/build_cmake) +page for the details. + +### Check your target environment + +The following examples are tested under Raspberry Pi OS, Ubuntu Server 20.04 LTS +and Mendel Linux 4.0. Depending on your target glibc version and CPU +capabilities, you may need to use different version of toolchain and build +parameters. + +#### Checking glibc version + +```sh +ldd --version +``` + +
+ldd (Debian GLIBC 2.28-10) 2.28
+Copyright (C) 2018 Free Software Foundation, Inc.
+This is free software; see the source for copying conditions.  There is NO
+warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+Written by Roland McGrath and Ulrich Drepper.
+
+ +#### Checking ABI compatibility + +If your target is ARM 32-bit, there are two ABI available depending on VFP +availity. [armhf](https://wiki.debian.org/ArmHardFloatPort) and +[armel](https://wiki.debian.org/ArmEabiPort). This document shows an armhf +example, you need to use different toolchain for armel targets. + +#### Checking CPU capability + +For ARMv7, you should know target's supported VFP version and NEON availability. + +```sh +cat /proc/cpuinfo +``` + +
+processor   : 0
+model name  : ARMv7 Processor rev 3 (v7l)
+BogoMIPS    : 108.00
+Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
+CPU implementer : 0x41
+CPU architecture: 7
+CPU variant : 0x0
+CPU part    : 0xd08
+CPU revision    : 3
+
+ +## Build for AArch64 (ARM64) + +This instruction shows how to build AArch64 binary which is compatibile with +[Coral Mendel Linux 4.0](https://coral.ai/), Raspberry Pi (with +[Ubuntu Server 20.04.01 LTS 64-bit](https://ubuntu.com/download/raspberry-pi) +installed). + +#### Download toolchain + +These commands install gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu toolchain +under ${HOME}/toolchains. + +```sh +curl -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 +mkdir -p ${HOME}/toolchains +tar xvf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz -C ${HOME}/toolchains +``` + +**Note:** Binaries built with GCC 8.3 require glibc 2.28 or higher. If your +target has lower glibc version, you need to use older GCC toolchain. + +#### Run CMake + +```sh +ARMCC_PREFIX=${HOME}/toolchains/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu- +ARMCC_FLAGS="-funsafe-math-optimizations" +cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \ + -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \ + -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \ + -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + -DCMAKE_SYSTEM_NAME=Linux \ + -DCMAKE_SYSTEM_PROCESSOR=aarch64 \ + ../tensorflow/lite/ +``` + +**Note:** You can enable GPU delegate with "-DTFLITE_ENABLE_GPU=ON" if your +target device supports OpenCL 1.2 or higher. + +## Build for ARMv7 NEON enabled + +This instruction shows how to build ARMv7 with VFPv4 and NEON enabled binary +which is compatibile with Raspberry Pi 3 and 4. + +#### Download toolchain + +These commands install gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf toolchain +under ${HOME}/toolchains. + +```sh +curl -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 +mkdir -p ${HOME}/toolchains +tar xvf gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz -C ${HOME}/toolchains +``` + +**Note:** Binaries built with GCC 8.3 require glibc 2.28 or higher. If your +target has lower glibc version, you need to use older GCC toolchain. + +#### Run CMake + +```sh +ARMCC_FLAGS="-march=armv7-a -mfpu=neon-vfpv4 -funsafe-math-optimizations" +ARMCC_PREFIX=${HOME}/toolchains/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf- +cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \ + -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \ + -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \ + -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + -DCMAKE_SYSTEM_NAME=Linux \ + -DCMAKE_SYSTEM_PROCESSOR=armv7 \ + ../tensorflow/lite/ +``` + +**Note:** Since ARMv7 architecture is diverse, you may need to update +ARMCC_FLAGS for your target device profiles. + +## Build for Raspberry Pi Zero (ARMv6) + +This instruction shows how to build ARMv6 binary which is compatibile with +Raspberry Pi Zero. + +#### Download toolchain + +These commands install arm-rpi-linux-gnueabihf toolchain under +${HOME}/toolchains. + +```sh +curl -L https://github.com/rvagg/rpi-newer-crosstools/archive/eb68350c5c8ec1663b7fe52c742ac4271e3217c5.tar.gz -o rpi-toolchain.tar.gz +tar xzf rpi-toolchain.tar.gz -C ${HOME}/toolchains +mv ${HOME}/toolchains/rpi-newer-crosstools-eb68350c5c8ec1663b7fe52c742ac4271e3217c5 ${HOME}/toolchains/arm-rpi-linux-gnueabihf +``` + +#### Run CMake + +```sh +ARMCC_PREFIX=${HOME}/toolchains/arm-rpi-linux-gnueabihf/x64-gcc-6.5.0/arm-rpi-linux-gnueabihf/bin/arm-rpi-linux-gnueabihf- +ARMCC_FLAGS="-march=armv6 -mfpu=vfp -funsafe-math-optimizations" +cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \ + -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \ + -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \ + -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \ + -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ + -DCMAKE_SYSTEM_NAME=Linux \ + -DCMAKE_SYSTEM_PROCESSOR=armv6 \ + -DTFLITE_ENABLE_XNNPACK=OFF \ + ../tensorflow/lite/ +``` + +**Note:** XNNPACK is disabled since there is no NEON support. diff --git a/tensorflow/lite/g3doc/guide/build_rpi.md b/tensorflow/lite/g3doc/guide/build_rpi.md index 408a0f11856..c837fb37abe 100644 --- a/tensorflow/lite/g3doc/guide/build_rpi.md +++ b/tensorflow/lite/g3doc/guide/build_rpi.md @@ -15,6 +15,9 @@ or **Note:** This page only covers 32-bit builds. If you're looking for 64-bit builds, check [Build for ARM64](build_arm64.md) page. +**Note:** Cross-compile ARM with CMake is available. Please check +[this](https://www.tensorflow.org/lite/guide/build_cmake_arm). + ## Cross-compile for Raspberry Pi with Make The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64)