Skip to content

Commit b2a0f1c

Browse files
authored
Add instructions for building CUDA-enabled Android TensorFlow (tensorflow#16961)
* Add instructions for building CUDA-enabled Android TensorFlow * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md
1 parent 17103a0 commit b2a0f1c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

tensorflow/contrib/android/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ For documentation on building a self-contained AAR file with cmake, see
8181
[tensorflow/contrib/android/cmake](cmake).
8282

8383

84+
### Makefile
85+
86+
For documentation on building native TF libraries with make, including a CUDA-enabled variant for devices like the Nvidia Shield TV, see [tensorflow/contrib/makefile/README.md](../makefile/README.md)
87+
88+
8489
## AssetManagerFileSystem
8590

8691
This directory also contains a TensorFlow filesystem supporting the Android

tensorflow/contrib/makefile/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,105 @@ adb shell '/data/local/tmp/benchmark \
130130

131131
For more details, see the [benchmark documentation](../../tools/benchmark).
132132

133+
## CUDA support for Tegra devices running Android (Nvidia Shield TV, etc)
134+
135+
With 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.
136+
137+
#### Environment setup:
138+
139+
First, 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.
140+
141+
```bash
142+
git clone https://github.com/tensorflow/tensorflow.git
143+
cd tensorflow
144+
JETPACK=$HOME/JetPack_Android_3.2
145+
TEGRA_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"
146+
```
147+
148+
#### Building all CUDA-enabled native binaries:
149+
This 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)
150+
151+
```bash
152+
NDK_ROOT=$JETPACK/android-ndk-r13b
153+
CC_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
154+
```
155+
(add -T on subsequent builds to skip protobuf downloading/building)
156+
157+
158+
#### Testing the the CUDA-enabled benchmark via adb:
159+
Build binaries first as above, then run:
160+
161+
```bash
162+
adb shell mkdir -p /data/local/tmp/lib64
163+
adb push $TEGRA_LIBS /data/local/tmp/lib64
164+
adb push tensorflow/contrib/makefile/gen/bin/android_arm64-v8a/benchmark /data/local/tmp
165+
wget https://ci.tensorflow.org/view/Nightly/job/nightly-android/lastSuccessfulBuild/artifact/out/tensorflow_demo.apk
166+
unzip tensorflow_demo.apk -d /tmp/tensorflow_demo
167+
adb push /tmp/tensorflow_demo/assets/*.pb /data/local/tmp
168+
adb shell "LD_LIBRARY_PATH=/data/local/tmp/lib64 /data/local/tmp/benchmark --graph=/data/local/tmp/tensorflow_inception_graph.pb"
169+
```
170+
171+
#### Building the CUDA-enabled TensorFlow AAR with Bazel:
172+
Build the native binaries first as above. Then, build the aar and package the native libs by executing the following:
173+
```bash
174+
mkdir -p /tmp/tf/jni/arm64-v8a
175+
cp tensorflow/contrib/makefile/gen/lib/android_tegra/libtensorflow_*.so /tmp/tf/jni/arm64-v8a/
176+
cp $TEGRA_LIBS /tmp/tf/jni/arm64-v8a
177+
bazel build //tensorflow/contrib/android:android_tensorflow_inference_java.aar
178+
cp bazel-bin/tensorflow/contrib/android/android_tensorflow_inference_java.aar /tmp/tf/tensorflow.aar
179+
cd /tmp/tf
180+
chmod +w tensorflow.aar
181+
zip -ur tensorflow.aar $(find jni -name *.so)
182+
```
183+
184+
#### Building the CUDA-enabled TensorFlow Android demo with Bazel:
185+
Build binaries first as above, then edit tensorflow/examples/android/BUILD and replace:
186+
```
187+
srcs = [
188+
":libtensorflow_demo.so",
189+
"//tensorflow/contrib/android:libtensorflow_inference.so",
190+
],
191+
```
192+
with:
193+
```
194+
srcs = glob(["libs/arm64-v8a/*.so"]),
195+
```
196+
197+
Then run:
198+
```bash
199+
# Create dir for native libs
200+
mkdir -p tensorflow/examples/android/libs/arm64-v8a
201+
202+
# Copy JetPack libs
203+
cp $TEGRA_LIBS tensorflow/examples/android/libs/arm64-v8a
204+
205+
# Copy native TensorFlow libraries
206+
cp tensorflow/contrib/makefile/gen/lib/android_arm64-v8a/libtensorflow_*.so tensorflow/examples/android/libs/arm64-v8a/
207+
208+
# Build APK
209+
bazel build -c opt --fat_apk_cpu=arm64-v8a tensorflow/android:tensorflow_demo
210+
211+
# Install
212+
adb install -r -f bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
213+
```
214+
215+
#### Building the CUDA-enabled Android demo with gradle/Android Studio:
216+
217+
Add tensorflow/examples/android as an Android project in Android Studio as normal.
218+
219+
Edit build.gradle and:
220+
* set nativeBuildSystem = 'makefile'
221+
* set cpuType = 'arm64-v8a'
222+
* in "buildNativeMake", replace cpuType with 'tegra' (optional speedups like -T and ccache also work)
223+
* set the environment "NDK_ROOT" var to $JETPACK/android-ndk-r13b
224+
225+
Click "build apk" to build.
226+
227+
Install:
228+
```bash
229+
adb install -r -f tensorflow/examples/android/gradleBuild/outputs/apk/debug/android-debug.apk
230+
```
231+
133232
## iOS
134233

135234
_Note: To use this library in an iOS application, see related instructions in

0 commit comments

Comments
 (0)