160796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke### TensorFlow Makefile
260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
360796d7c0d401e5e7b7a139f165e78ce778583beMartin WickeThe recommended way to build TensorFlow from source is using the Bazel
4a93234a60244ffe843312c3f49ee495d1524dd48Andrew Selleopen-source build system. Sometimes this isn't possible. For example,
5a93234a60244ffe843312c3f49ee495d1524dd48Andrew Selleif you are building for iOS, you currently need to use the Makefile.
660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
7e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower - The build system may not have the RAM or processing power to support Bazel.
8e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower - Bazel or its dependencies may not be available.
960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke - You may want to cross-compile for an unsupported target system.
1060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
1160796d7c0d401e5e7b7a139f165e78ce778583beMartin WickeThis experimental project supplies a Makefile automatically derived from the
12e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerdependencies listed in the Bazel project that can be used with GNU's make tool.
13e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerWith it, you can compile the core C++ runtime into a static library.
14e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
15e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThis static library will not contain:
16e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
17e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower - Python or other language bindings
18e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower - GPU support
19191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
2021716d8f6e175cd6e8cd97a84e48497574268b0cMartin WickeYou can target:
2121716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicke- iOS
2221716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicke- OS X (macOS)
2321716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicke- Android
2421716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicke- Raspberry-PI
25191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
2621716d8f6e175cd6e8cd97a84e48497574268b0cMartin WickeYou will compile tensorflow and protobuf libraries that you can link into other
2721716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wickeapplications.  You will also compile the [benchmark](../../tools/benchmark/)
2821716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wickeapplication that will let you check your application.
29191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
30e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower## Before you start (all platforms)
31e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
32e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerFirst, clone this TensorFlow repository.
33e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
34e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerYou will need to download all dependencies as well.  We have provided a script
3593a975e114ee1c35f01ed3bdd47170e6f7129014Vijay Vasudevanthat does so, to be run (as with all commands) **at the root of the repository**:
36e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
3710ff074b89cd948db16f669cea019d0e9d7de91ePete Warden```bash
38e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowertensorflow/contrib/makefile/download_dependencies.sh
39e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
4060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
41e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerYou should only need to do this step once.  It downloads the required libraries
42e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerlike Eigen in the `tensorflow/contrib/makefile/downloads/` folder.
4360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
44e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerYou should download the example graph from [https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip](https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip).
4560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
46e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower## Building on Linux
47e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
48e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower_Note: This has only been tested on Ubuntu._
49e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
5060360feb1a5c22091a835a3c4d11beeed46ad206Pete WardenAs a first step, you need to make sure the required packages are installed:
51e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
5260360feb1a5c22091a835a3c4d11beeed46ad206Pete Wardensudo apt-get install autoconf automake libtool curl make g++ unzip zlib1g-dev \
5360360feb1a5c22091a835a3c4d11beeed46ad206Pete Wardengit python
54e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
55e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
5660360feb1a5c22091a835a3c4d11beeed46ad206Pete WardenYou should then be able to run the `build_all_linux.sh` script to compile:
5760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
5860360feb1a5c22091a835a3c4d11beeed46ad206Pete Wardentensorflow/contrib/makefile/build_all_linux.sh
5960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
6060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
61191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlowerThis should compile a static library in
62191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower`tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a`,
63191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlowerand create an example executable at `tensorflow/contrib/makefile/gen/bin/benchmark`.
64e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
65e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerGet the graph file, if you have not already:
6660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
6760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
68e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowermkdir -p ~/graphs
69e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowercurl -o ~/graphs/inception.zip \
70e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip \
71e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower && unzip ~/graphs/inception.zip -d ~/graphs/inception
7260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
7360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
74e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerTo run the executable, use:
7560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
76e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
77e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowertensorflow/contrib/makefile/gen/bin/benchmark \
78ccbc8991db3943ef984405881a1c917c530f902fA. Unique TensorFlower --graph=$HOME/graphs/inception/tensorflow_inception_graph.pb
79e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
8060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
8160796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke## Android
8260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
83e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerFirst, you will need to download and unzip the
846cd8b28da1cd42f9bacbef9d55f64c7f5162bb9cAndrew Harp[Native Development Kit (NDK)](https://developer.android.com/ndk/). You will not
85e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerneed to install the standalone toolchain, however.
86e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
87e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerAssign your NDK location to $NDK_ROOT:
8860796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
8960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
90e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerexport NDK_ROOT=/absolute/path/to/NDK/android-ndk-rxxx/
91e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
92e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
93e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerDownload the graph if you haven't already:
94e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
95e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
96e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowermkdir -p ~/graphs
97e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowercurl -o ~/graphs/inception.zip \
98e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip \
99e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower && unzip ~/graphs/inception.zip -d ~/graphs/inception
100e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
101e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
102e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThen, execute the following:
103e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
104e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
105e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowertensorflow/contrib/makefile/download_dependencies.sh
106e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowertensorflow/contrib/makefile/compile_android_protobuf.sh -c
107b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowerexport HOST_NSYNC_LIB=`tensorflow/contrib/makefile/compile_nsync.sh`
108b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowerexport TARGET_NSYNC_LIB=`CC_PREFIX="${CC_PREFIX}" NDK_ROOT="${NDK_ROOT}" \
109b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlower	tensorflow/contrib/makefile/compile_nsync.sh -t android -a armeabi-v7a`
110e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowermake -f tensorflow/contrib/makefile/Makefile TARGET=ANDROID
111e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
112e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
113e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerAt this point, you will have compiled libraries in `gen/lib/*` and the
114e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower[benchmark app](../../tools/benchmark) compiled for Android.
115e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
116e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerRun the benchmark by pushing both the benchmark and the graph file to your
117e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerattached Android device:
118e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
119e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
120e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFloweradb push ~/graphs/inception/tensorflow_inception_graph.pb /data/local/tmp/
121e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFloweradb push tensorflow/contrib/makefile/gen/bin/benchmark /data/local/tmp/
122e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFloweradb shell '/data/local/tmp/benchmark \
123a771598ad83ca33eb42594d7e804859371ba4ca9Benoit Steiner --graph=/data/local/tmp/tensorflow_inception_graph.pb \
124e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower --input_layer="input:0" \
125e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower --input_layer_shape="1,224,224,3" \
126e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower --input_layer_type="float" \
127e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower --output_layer="output:0"
128e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower'
12960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
13060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
131e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerFor more details, see the [benchmark documentation](../../tools/benchmark).
13260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
133b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp## CUDA support for Tegra devices running Android (Nvidia Shield TV, etc)
134b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
135b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpWith the release of TF 1.6 and JetPack for Android 3.2 (currently pending), you can now build a version of TensorFlow for compatible devices according to the following instructions which will receive the full benefits of GPU acceleration.
136b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
137b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp#### Environment setup:
138b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
139b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpFirst, download and install JetPack for Android version 3.2 or greater from [Nvidia](https://developers.nvidia.com). Note that as of the TF 1.6 release the JetPack for Android 3.2 release is still pending, and regular JetPack for L4T will not work.
140b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
141b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```bash
142b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpgit clone https://github.com/tensorflow/tensorflow.git
143b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcd tensorflow
144b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpJETPACK=$HOME/JetPack_Android_3.2
145b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpTEGRA_LIBS="$JETPACK/cuDNN/aarch64/cuda/lib64/libcudnn.so  $JETPACK/cuda-9.0/extras/CUPTI/lib64/libcupti.so $JETPACK/cuda/targets/aarch64-linux-androideabi/lib64/libcufft.so"
146b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
147b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
148b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp#### Building all CUDA-enabled native binaries:
149b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpThis will build CUDA-enabled versions of libtensorflow_inference.so and the benchmark binary. (libtensorflow_demo.so will also be built incidentally, but it does not support CUDA)
150b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
151b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```bash
152b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpNDK_ROOT=$JETPACK/android-ndk-r13b
153b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpCC_PREFIX=ccache tensorflow/contrib/makefile/build_all_android.sh -s tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in -t "libtensorflow_inference.so libtensorflow_demo.so all" -a tegra
154b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
155b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp(add -T on subsequent builds to skip protobuf downloading/building)
156b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
157b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
15849c20c5814dd80f81ced493d362d374be9ab0b3eManHyuk#### Testing the CUDA-enabled benchmark via adb:
159b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpBuild binaries first as above, then run:
160b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
161b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```bash
162b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb shell mkdir -p /data/local/tmp/lib64
163b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb push $TEGRA_LIBS /data/local/tmp/lib64
164b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb push tensorflow/contrib/makefile/gen/bin/android_arm64-v8a/benchmark /data/local/tmp
165b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpwget  https://ci.tensorflow.org/view/Nightly/job/nightly-android/lastSuccessfulBuild/artifact/out/tensorflow_demo.apk
166b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpunzip tensorflow_demo.apk -d /tmp/tensorflow_demo
167b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb push /tmp/tensorflow_demo/assets/*.pb /data/local/tmp
168b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb shell "LD_LIBRARY_PATH=/data/local/tmp/lib64 /data/local/tmp/benchmark --graph=/data/local/tmp/tensorflow_inception_graph.pb"
169b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
170b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
171b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp#### Building the CUDA-enabled TensorFlow AAR with Bazel:
172b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpBuild the native binaries first as above. Then, build the aar and package the native libs by executing the following:
173b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```bash
174b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpmkdir -p /tmp/tf/jni/arm64-v8a
175b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcp tensorflow/contrib/makefile/gen/lib/android_tegra/libtensorflow_*.so /tmp/tf/jni/arm64-v8a/
176b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcp $TEGRA_LIBS /tmp/tf/jni/arm64-v8a
177b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpbazel build //tensorflow/contrib/android:android_tensorflow_inference_java.aar
178b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcp bazel-bin/tensorflow/contrib/android/android_tensorflow_inference_java.aar /tmp/tf/tensorflow.aar
179b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcd /tmp/tf
180b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpchmod +w tensorflow.aar
181b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpzip -ur tensorflow.aar $(find jni -name *.so)
182b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
183b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
184b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp#### Building the CUDA-enabled TensorFlow Android demo with Bazel:
185b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpBuild binaries first as above, then edit tensorflow/examples/android/BUILD and replace: 
186b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
187b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp    srcs = [
188b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp       ":libtensorflow_demo.so",
189b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp       "//tensorflow/contrib/android:libtensorflow_inference.so",
190b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp    ],
191b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
192b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpwith:
193b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
194b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpsrcs = glob(["libs/arm64-v8a/*.so"]),
195b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
196b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
197b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpThen run:
198b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```bash
199b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp# Create dir for native libs
200b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpmkdir -p tensorflow/examples/android/libs/arm64-v8a
201b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
202b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp# Copy JetPack libs
203b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcp $TEGRA_LIBS  tensorflow/examples/android/libs/arm64-v8a
204b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
205b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp# Copy native TensorFlow libraries
206b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpcp tensorflow/contrib/makefile/gen/lib/android_arm64-v8a/libtensorflow_*.so tensorflow/examples/android/libs/arm64-v8a/
207b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
208b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp# Build APK
209b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpbazel build -c opt --fat_apk_cpu=arm64-v8a tensorflow/android:tensorflow_demo
210b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
211b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp# Install
212b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb install -r -f bazel-bin/tensorflow/examples/android/tensorflow_demo.apk 
213b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
214b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
215b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp#### Building the CUDA-enabled Android demo with gradle/Android Studio:
216b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
217b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpAdd tensorflow/examples/android as an Android project in Android Studio as normal.
218b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
219b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpEdit build.gradle and:
220b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp* set nativeBuildSystem = 'makefile'
221b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp* set cpuType = 'arm64-v8a'
222b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp* in "buildNativeMake", replace cpuType with 'tegra' (optional speedups like -T and ccache also work) 
223b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp* set the environment "NDK_ROOT" var to $JETPACK/android-ndk-r13b
224b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
225b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpClick "build apk" to build.
226b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
227b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew HarpInstall:
228b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```bash
229b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harpadb install -r -f tensorflow/examples/android/gradleBuild/outputs/apk/debug/android-debug.apk
230b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp```
231b2a0f1c45b2283910548ebd88ee5aaf4b6fc6077Andrew Harp
23260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke## iOS
23360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
234e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower_Note: To use this library in an iOS application, see related instructions in
235a0ffaf3caa0234653035a692858606c7bdacd63bFrank Chenthe [iOS examples](../../examples/ios/) directory._
236e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
237e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerInstall XCode 7.3 or more recent. If you have not already, you will need to
238e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerinstall the command-line tools using `xcode-select`:
23960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
24060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
241e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerxcode-select --install
24260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
24360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
244e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerIf this is a new install, you will need to run XCode once to agree to the
245e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerlicense before continuing.
24660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
24793a975e114ee1c35f01ed3bdd47170e6f7129014Vijay Vasudevan(You will also need to have [Homebrew](http://brew.sh/) installed.)
24893a975e114ee1c35f01ed3bdd47170e6f7129014Vijay Vasudevan
2491cb96893a64f59b7265f9def9968f7bed1e57662Andrew HarpThen install [automake](https://en.wikipedia.org/wiki/Automake)/[libtool](https://en.wikipedia.org/wiki/GNU_Libtool):
2501283b84a49a9f5e14aca833cf981b61848aaf916Jonathan Hseu
2511283b84a49a9f5e14aca833cf981b61848aaf916Jonathan Hseu```bash
2521283b84a49a9f5e14aca833cf981b61848aaf916Jonathan Hseubrew install automake
2531cb96893a64f59b7265f9def9968f7bed1e57662Andrew Harpbrew install libtool
2541283b84a49a9f5e14aca833cf981b61848aaf916Jonathan Hseu```
2551283b84a49a9f5e14aca833cf981b61848aaf916Jonathan Hseu
256e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerAlso, download the graph if you haven't already:
25760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
258e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
259e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowermkdir -p ~/graphs
260e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowercurl -o ~/graphs/inception.zip \
261e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip \
262e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower && unzip ~/graphs/inception.zip -d ~/graphs/inception
263e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
264e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
265e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower### Building all at once
266e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
26721716d8f6e175cd6e8cd97a84e48497574268b0cMartin WickeIf you just want to get the libraries compiled in a hurry, you can run this
26821716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wickefrom the root of your TensorFlow source folder:
26960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
27060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
27121716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicketensorflow/contrib/makefile/build_all_ios.sh
27260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
27360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
27421716d8f6e175cd6e8cd97a84e48497574268b0cMartin WickeThis process will take around twenty minutes on a modern MacBook Pro.
275e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
276b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengWhen it completes, you will have a unified library for all architectures
277b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng(i386sim, x86_64sim, armv7, armv7s and arm64)  and the benchmark program.
278b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengAlthough successfully compiling the benchmark program is a
279e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowersign of success, the program is not a complete iOS app.
280e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
281b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengIf you would only like to build only one architecture to save time:
282b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng(iOS 11+ only supports 64bit so you can get away with arm64)
283b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng
284b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng```bash
285b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fengtensorflow/contrib/makefile/build_all_ios.sh -a arm64
286b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng```
287b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng
288b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengAfter the first build if you would like to just build the tensorflow
289b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fenglibrary you can pass the -T flag to avoid a clean & rebuild. This should
290b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fengtake you just a few seconds to generate the library if you modified one file.
291b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng
292b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng```bash
293b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fengtensorflow/contrib/makefile/build_all_ios.sh -a arm64 -T
294b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng```
295b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng
296e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerTo see TensorFlow running on iOS, the example Xcode project in
297a0ffaf3caa0234653035a692858606c7bdacd63bFrank Chen[tensorflow/examples/ios](../../examples/ios/) shows how to use the static
298e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerlibrary in a simple app.
299e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
300e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower### Building by hand
301e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
302e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThis section covers each step of building.  For all the code in one place, see
303191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower[build_all_ios.sh](build_all_ios.sh).
304e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
305e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerIf you have not already, you will need to download dependencies:
30660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
30760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
30860796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicketensorflow/contrib/makefile/download_dependencies.sh
30960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
31060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
311b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengNext, you will need to compile protobufs for iOS (optionally takes the -a $ARCH flag):
31260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
31360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
314b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fengtensorflow/contrib/makefile/compile_ios_protobuf.sh
31560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
31660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
317b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengThen, you will need to compile the nsync library for iOS (optionally takes -a $ARCH flag):
318b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlower
319d57572e996dce24abf4d9cf6ea04e7104b3d743bMartin Wicke```bash
320d57572e996dce24abf4d9cf6ea04e7104b3d743bMartin Wickeexport HOST_NSYNC_LIB=`tensorflow/contrib/makefile/compile_nsync.sh`
321b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowerexport TARGET_NSYNC_LIB=`tensorflow/contrib/makefile/compile_nsync.sh -t ios`
322b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlower```
323e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThen, you can run the makefile specifying iOS as the target, along with the
324e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerarchitecture you want to build for:
32560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
32660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
327e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowermake -f tensorflow/contrib/makefile/Makefile \
328e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower TARGET=IOS \
329e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower IOS_ARCH=ARM64
33060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
33160796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
332e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThis creates a library in
333e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower`tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a` that you can link any
334191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlowerxcode project against.
335e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
336e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerTo see TensorFlow running on iOS, the example Xcode project in
337a0ffaf3caa0234653035a692858606c7bdacd63bFrank Chen[tensorflow/examples/ios](../../examples/ios/) shows how to use the static
338e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerlibrary in a simple app.
339e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
340e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower#### Universal binaries
341e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
342e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerIn some situations, you will need a universal library.  In that case, you will
343b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowerstill need to run `compile_ios_protobuf.sh` and `compile_nsync.sh`, but this
344b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowertime follow it with:
34560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
34660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
347e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowercompile_ios_tensorflow.sh
34860796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
34960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
350b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng`compile_ios_tensorflow.sh` takes the -a flag to build only for one architecture.
351b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei FengIn case you run into issues with unresolved symbols with nsync you can also pass
352b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng-h ${HOST_NSYNC_LIB} and -n {TARGET_NSYNC_LIB} so it would look like:
353b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng
354b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng```bash
355b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fengtensorflow/contrib/makefile/compile_ios_tensorflow.sh -f "-O3" -h tensorflow/contrib/makefile/downloads/nsync/builds/default.macos.c++11/nsync.a -n tensorflow/contrib/makefile/downloads/nsync/builds/lipo.ios.c++11/nsync.a -a arm64
356b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng```
357b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Feng
358e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerIn XCode, you will need to use -force_load in the linker flags
35960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wickesection of the build settings to pull in the global constructors that are used
360191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlowerto register ops and kernels.
36160796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
36210ff074b89cd948db16f669cea019d0e9d7de91ePete Warden#### Optimization
363191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
364c0bda7fe2817107440e6c2736818c84f5529d882powderluvThe `build_all_ios.sh` script can take optional command-line arguments to
365c0bda7fe2817107440e6c2736818c84f5529d882powderluvselectively register only for the operators used in your graph.
366c0bda7fe2817107440e6c2736818c84f5529d882powderluv
367c0bda7fe2817107440e6c2736818c84f5529d882powderluv```bash
368c0bda7fe2817107440e6c2736818c84f5529d882powderluvtensorflow/contrib/makefile/build_all_ios.sh -a arm64 -g $HOME/graphs/inception/tensorflow_inception_graph.pb
369c0bda7fe2817107440e6c2736818c84f5529d882powderluv```
3707a5fb00a976ad1b4e3d13be6af5b9e3558499b2bbrett kooncePlease note this is an aggressive optimization of the operators and the resulting library may not work with other graphs but will reduce the size of the final library.
371c0bda7fe2817107440e6c2736818c84f5529d882powderluv
37210ff074b89cd948db16f669cea019d0e9d7de91ePete WardenThe `compile_ios_tensorflow.sh` script can take optional command-line arguments.
37310ff074b89cd948db16f669cea019d0e9d7de91ePete WardenThe first argument will be passed as a C++ optimization flag and defaults to
37410ff074b89cd948db16f669cea019d0e9d7de91ePete Wardendebug mode. If you are concerned about performance or are working on a release
37510ff074b89cd948db16f669cea019d0e9d7de91ePete Wardenbuild, you would likely want a higher optimization setting, like so:
376191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
37710ff074b89cd948db16f669cea019d0e9d7de91ePete Warden```bash
378b1d8c59e9b014b527fb2fbef9ce9afc14dbc4938Yifei Fengcompile_ios_tensorflow.sh -f "-Os"
37910ff074b89cd948db16f669cea019d0e9d7de91ePete Warden```
38010ff074b89cd948db16f669cea019d0e9d7de91ePete Warden
38110ff074b89cd948db16f669cea019d0e9d7de91ePete WardenFor other variations of valid optimization flags, see [clang optimization levels](http://stackoverflow.com/questions/15548023/clang-optimization-levels).
38210ff074b89cd948db16f669cea019d0e9d7de91ePete Warden
38360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke## Raspberry Pi
38460796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
3858b3d8c8b5e96036ec78e01292c56ce099c5a62abBenoit SteinerBuilding on the Raspberry Pi is similar to a normal Linux system. First
386b0bdff4827f867a67f572ed99d85f9a847788326A. Unique TensorFlowerdownload the dependencies, install the required packages and build protobuf:
38760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
38860796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
3898b3d8c8b5e96036ec78e01292c56ce099c5a62abBenoit Steinertensorflow/contrib/makefile/download_dependencies.sh
39068f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harpsudo apt-get install -y autoconf automake libtool gcc-4.8 g++-4.8
3915a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevancd tensorflow/contrib/makefile/downloads/protobuf/
3928b3d8c8b5e96036ec78e01292c56ce099c5a62abBenoit Steiner./autogen.sh
3935a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevan./configure
3945a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevanmake
3955a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevansudo make install
396b0bdff4827f867a67f572ed99d85f9a847788326A. Unique TensorFlowersudo ldconfig  # refresh shared library cache
3975a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevancd ../../../../..
398b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowerexport HOST_NSYNC_LIB=`tensorflow/contrib/makefile/compile_nsync.sh`
399b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlowerexport TARGET_NSYNC_LIB="$HOST_NSYNC_LIB"
40060796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
40160796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
4025a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay VasudevanOnce that's done, you can use make to build the library and example:
40360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
40460796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
40568f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harpmake -f tensorflow/contrib/makefile/Makefile HOST_OS=PI TARGET=PI OPTFLAGS="-Os" CXX=g++-4.8
40660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
40760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
4085a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay VasudevanIf you're only interested in building for Raspberry Pi's 2 and 3, you can supply
4095a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevansome extra optimization flags to give you code that will run faster:
4105a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevan
4115a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevan```bash
4125a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevanmake -f tensorflow/contrib/makefile/Makefile HOST_OS=PI TARGET=PI \
41368f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harp OPTFLAGS="-Os -mfpu=neon-vfpv4 -funsafe-math-optimizations -ftree-vectorize" CXX=g++-4.8
4145a65d43a9e456aac08b32a1d38cbe123d73fcda8Vijay Vasudevan```
41560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
41668f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew HarpOne thing to be careful of is that the gcc version 4.9 currently installed on
41768f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew HarpJessie by default will hit an error mentioning `__atomic_compare_exchange`. This
41868f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harpis why the examples above specify `CXX=g++-4.8` explicitly, and why we install
41968f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harpit using apt-get. If you have partially built using the default gcc 4.9, hit the
42068f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harperror and switch to 4.8, you need to do a
42168f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harp`make -f tensorflow/contrib/makefile/Makefile clean` before you build. If you
42268f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harpdon't, the build will appear to succeed but you'll encounter [malloc(): memory corruption errors](https://github.com/tensorflow/tensorflow/issues/3442)
42368f1a7d05ce031192321a27daaf19529b4cb0a9bAndrew Harpwhen you try to run any programs using the library.
42421716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicke
4258b3d8c8b5e96036ec78e01292c56ce099c5a62abBenoit SteinerFor more examples, look at the tensorflow/contrib/pi_examples folder in the
4268b3d8c8b5e96036ec78e01292c56ce099c5a62abBenoit Steinersource tree, which contains code samples aimed at the Raspberry Pi.
42721716d8f6e175cd6e8cd97a84e48497574268b0cMartin Wicke
428e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower# Other notes
429e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
430e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower## Supported Systems
431e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
432e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThe Make script has been tested on Ubuntu and OS X. If you look in the Makefile
433e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFloweritself, you'll see it's broken up into host and target sections. If you are
434e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowercross-compiling, you should look at customizing the target settings to match
435e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerwhat you need for your desired system.
436e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
437ee112cff56081fb9d0b74c987a8935acc360b05cBenoit Steiner## Dependency Management
43860796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
43960796d7c0d401e5e7b7a139f165e78ce778583beMartin WickeThe Makefile loads in a list of dependencies stored in text files. These files
440191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlowerare generated from the main Bazel build by running
44160796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke`tensorflow/contrib/makefile/gen_file_lists.sh`. You'll need to re-run this i
44260796d7c0d401e5e7b7a139f165e78ce778583beMartin Wickeyou make changes to the files that are included in the build.
44360796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
44460796d7c0d401e5e7b7a139f165e78ce778583beMartin WickeHeader dependencies are not automatically tracked by the Makefile, so if you
44560796d7c0d401e5e7b7a139f165e78ce778583beMartin Wickemake header changes you will need to run this command to recompile cleanly:
44660796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke
44760796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```bash
44860796d7c0d401e5e7b7a139f165e78ce778583beMartin Wickemake -f tensorflow/contrib/makefile/Makefile clean
44960796d7c0d401e5e7b7a139f165e78ce778583beMartin Wicke```
450e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
451e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower### Cleaning up
452e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
453e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerIn some situations, you may want to completely clean up. The dependencies,
454e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerintermediate stages, and generated files are stored in:
455e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
456e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```bash
457e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowertensorflow/contrib/makefile/downloads
458e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowertensorflow/contrib/makefile/gen
459e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower```
460e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower
461e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlowerThose directories can safely be removed, but you will have to start over with
462e231a8b382195bf819bfac71a0233602a7101760A. Unique TensorFlower`download_dependencies.sh` once you delete them.
4636a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden
4646a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden### Fixing Makefile Issues
4656a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden
4666a83b0787f2f5037eed2f2f47cc76b94bc740aebPete WardenBecause the main development of TensorFlow is done using Bazel, changes to the
4676a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardencodebase can sometimes break the makefile build process. If you find that tests
4686a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardenrelying on this makefile are failing with a change you're involved in, here are
4696a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardensome trouble-shooting steps:
4706a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden
471191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower - Try to reproduce the issue on your platform. If you're on Linux, running
4726a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden `make -f tensorflow/contrib/makefile/Makefile` should be enough to recreate
4736a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  most issues. For other platforms, see the sections earlier in this document.
474191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
4756a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden - The most common cause of breakages are files that have been added to the
4766a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  Bazel build scripts, but that the makefile isn't aware of. Typical symptoms
4776a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  of this include linker errors mentioning missing symbols or protobuf headers
4786a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  that aren't found. To address these problems, take a look at the *.txt files
4796a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  in `tensorflow/contrib/makefile`. If you have a new operator, you may need to
4806a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  add it to `tf_op_files.txt`, or for a new proto to `tf_proto_files.txt`.
4816a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden
4826a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden - There's also a wildcard system in `Makefile` that defines what core C++ files
4836a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  are included in the library. This is designed to match the equivalent rule in
4846a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  `tensorflow/core/BUILD`, so if you change the wildcards there to include new
4856a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  files you'll need to also update `CORE_CC_ALL_SRCS` and `CORE_CC_EXCLUDE_SRCS`
4866a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  in the makefile.
487191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
4886a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden - Some of the supported platforms use clang instead of gcc as their compiler,
4896a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  so if you're hitting compile errors you may need to tweak your code to be more
4906a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Warden  friendly to different compilers by avoiding gcc extensions or idioms.
491191825e63f341a4e7777b85254f616e541000d5cA. Unique TensorFlower
4926a83b0787f2f5037eed2f2f47cc76b94bc740aebPete WardenThese are the most common reasons for makefile breakages, but it's also
4936a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardenpossible you may hit something unusual, like a platform incompatibility. For
4946a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardenthose, you'll need to see if you can reproduce the issue on that particular
4956a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardenplatform and debug it there. You can also reach out to the broader TensorFlow
4966a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardenteam by [filing a Github issue](https://github.com/tensorflow/tensorflow/issues)
4976a83b0787f2f5037eed2f2f47cc76b94bc740aebPete Wardento ask for help.
498