Add g3doc for TFLite CMake build

PiperOrigin-RevId: 345141508
Change-Id: I9542ce8624f6522ae3227eeba165502eafefcdb1
This commit is contained in:
Terry Heo 2020-12-01 18:23:41 -08:00 committed by TensorFlower Gardener
parent 2527840a36
commit cced38dbdb
3 changed files with 142 additions and 90 deletions

View File

@ -179,6 +179,9 @@ upper_tabs:
path: /lite/guide/build_arm64
- title: "Build for Raspberry Pi"
path: /lite/guide/build_rpi
- title: "Build with CMake"
path: /lite/guide/build_cmake
status: experimental
- title: "Reduce binary size"
path: /lite/guide/reduce_binary_size
status: experimental

View File

@ -0,0 +1,138 @@
# Build TensorFlow Lite with CMake
This page describes how to build and use the TensorFlow Lite library with
[CMake](https://cmake.org/) tool.
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/)
and Windows 10.
**Note:** This feature is currently experimental and available since version 2.4
and may change.
### Step 1. Install CMake tool
It requires CMake 3.16 or higher. On Ubuntu, you can simply run the following
command.
```sh
sudo apt-get install cmake
```
Or you can follow
[the official cmake installation guide](https://cmake.org/install/)
### Step 2. Clone TensorFlow repository
```sh
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
```
**Note:** If you're using the TensorFlow Docker image, the repo is already
provided in `/tensorflow_src/`.
### Step 3. Create CMake build directory
```sh
mkdir tflite_build
cd tflite_build
```
### Step 4. Run CMake tool with configurations
#### Release build
It generates an optimized release binary by default. If you want to build for
your workstation, simply run the following command.
```sh
cmake ../tensorflow_src/tensorflow/lite
```
#### Debug build
If you need to produce a debug build which has symbol information, you need to
provide `-DCMAKE_BUILD_TYPE=Debug` option.
```sh
cmake ../tensorflow_src/tensorflow/lite -DCMAKE_BUILD_TYPE=Debug
```
#### Cross-compilation for Android
You can use CMake to build Android binaries. You need to install
[Android NDK](https://developer.android.com/ndk) and provide the NDK path with
`-DDCMAKE_TOOLCHAIN_FILE` flag. You also need to set target ABI with
`-DANDROID_ABI` flag.
```sh
cmake -DCMAKE_TOOLCHAIN_FILE=<NDK path>/build/cmake/android.toolchain.cmake \
-DANDROID_ABI=arm64-v8a ../tensorflow_src/tensorflow/lite
```
#### OpenCL GPU delegate
If your target machine has OpenCL support, you can use
[GPU delegate](https://www.tensorflow.org/lite/performance/gpu) which can
leverage your GPU power.
To configure OpenCL GPU delegate support:
```sh
cmake ../tensorflow_src/tensorflow/lite -DTFLITE_ENABLE_GPU=ON
```
**Note:** It's experimental and available only on master(r2.5) branch. There
could be compatbility issues. It's only verified with Android devices and NVidia
CUDA OpenCL 1.2.
### Step 5. Build TensorFlow Lite
In the tflite_build directory,
```sh
cmake --build . -j
```
**Note:** This generates a static library `libtensorflow-lite.a` in the current
directory but the library isn't self-contained since all the transitive
dependencies are not included. To use the library properly, you need to create a
CMake project. Please refer the
["Create a CMake project which uses TensorFlow Lite"](#create_a_cmake_project_which_uses_tensorflow_lite)
section.
### Step 6. Build TensorFlow Lite Benchmark Tool
In the tflite_build directory,
```sh
cmake --build . -j -t benchmark_model
```
## Create a CMake project which uses TensorFlow Lite
Here is the CMakeLists.txt of
[TFLite minimal example](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/minimal).
You need to have add_subdirectory() for TensorFlow Lite directory and link
`tensorflow-lite` with target_link_libraries().
```
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)
set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
"Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
get_filename_component(TENSORFLOW_SOURCE_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../../../" ABSOLUTE)
endif()
add_subdirectory(
"${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
add_executable(minimal minimal.cc)
target_link_libraries(minimal tensorflow-lite ${CMAKE_DL_LIBS}
```

View File

@ -1,92 +1,3 @@
# Build TensorFlow Lite with CMake
This page describes how to build the TensorFlow Lite static library with CMake
tool.
The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64)
, TensorFlow devel docker image and Windows 10.
[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/).
**Note:** This is an experimental that is subject to change.
**Note:** The following are not currently supported: iOS, Tests and
Host Tools (i.e analysis tools etc.)
#### Step 1. Install CMake tool
It requires CMake 3.16 or higher. On Ubuntu, you can simply run the following
command.
```sh
sudo apt-get install cmake
```
Or you can follow
[the official cmake installation guide](https://cmake.org/install/)
#### Step 2. Clone TensorFlow repository
```sh
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
```
**Note:** If you're using the TensorFlow Docker image, the repo is already
provided in `/tensorflow_src/`.
#### Step 3. Create CMake build directory and run CMake tool
```sh
mkdir tflite_build
cd tflite_build
cmake ../tensorflow_src/tensorflow/lite
```
It generates release binary by default. If you need to produce debug builds, you
need to provide '-DCMAKE_BUILD_TYPE=Debug' option.
```sh
cmake ../tensorflow_src/tensorflow/lite -DCMAKE_BUILD_TYPE=Debug
```
If you want to configure Android build with GPU delegate support,
```sh
mkdir tflite_build
cd tflite_build
cmake -DCMAKE_TOOLCHAIN_FILE=<NDK path>/build/cmake/android.toolchain.cmake \
-DANDROID_ABI=arm64-v8a -DTFLITE_ENABLE_GPU=ON ../tensorflow_src/tensorflow/lite
```
#### Step 4. Build TensorFlow Lite
In the tflite_build directory,
```sh
cmake --build . -j
```
Or
```sh
make -j
```
**Note:** This should compile a static library `libtensorflow-lite.a` in the
current directory.
#### Step 5. Build TensorFlow Lite Benchmark Tool
In the tflite_build directory,
```sh
cmake --build . -j -t benchmark_model
```
Or
```sh
make benchmark_model -j
```
Please refer [../../g3doc/guide/build_cmake.md](../../g3doc/guide/build_cmake.md)