tensorflow icon indicating copy to clipboard operation
tensorflow copied to clipboard

Fail to compile binary for Android

Open PKUEcho opened this issue 9 years ago • 18 comments

Is there simple way to compile a native binary for inference on Android? Now I can successfully build the benchmark and run it on my Android device. But it does not contain any actions about ops. I want to do something similar to the benchmark_model_test but it fails to build. I think it now lacks a android version of //tensorflow/cc APIs. How can I tackle it?

PKUEcho avatar May 04 '17 14:05 PKUEcho

Have you read this? https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/android

flawless2011 avatar May 04 '17 14:05 flawless2011

Thanks @flawless2011 I've read that. I want to use the apis under //tensorflow/cc/..., but it seems that the lib you provide doesn't refer to these apis? Code in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/benchmark/benchmark_model_test.cc uses some ops apis instead. For most TF apis, we already have //tensorflow/core:android_tensorflow_lib, but in my case I want to compile more (under //tensorflow/cc) but failed.

PKUEcho avatar May 04 '17 15:05 PKUEcho

@flawless2011 Not sure if I've made my question clear. I hope you can have comments or help refer to someone who should be familiar with this. Really appreciate that.

PKUEcho avatar May 04 '17 15:05 PKUEcho

Sorry I'm not getting this. What do you want to do? Compile directly against the C++ library on android?

/CC: @asimshankar

drpngx avatar May 04 '17 15:05 drpngx

@drpngx Basically, I want to build a native binary on Android, which simply creates a graph with some ops and execute it with some random input. The benchmark is a good example which can run on Android, but it just reads the graph from disk without constructing its own graph. From what I've found via the benchmark_model_test and label_image examples, I need to include apis under //tensorflow//cc to use ::tensorflow::ops::XXX (such as ResizeBilinear). So my question is how to compile the //tensorflow/cc for Android? I didn't find any target inside //tensorflow/cc.BUILD, and failed to build it by myself. I'm kind of new on this so maybe it's a very stupid question :)

PKUEcho avatar May 04 '17 16:05 PKUEcho

@PKUEcho : So you want to compile essentially an arbitrary binary that uses the C++ API for graph construction for Android?

This seems more like a bazel question :), but the good news is that that the only difference between compiling for Android or for your host machine should be flags to bazel.

I suppose you could do one of the following:

  • Modify benchmark.cc with the code you want, and add //tensorflow/cc:cc_ops to the deps section in the BUILD file. And build the binary as per the instructions you pointed to. Those instructions show how to build any bazel target for Android.

  • Compile the shared library for Android using something like:

bazel build -c opt \
  --crosstool_top=//external:android/crosstool \
  --cpu=armeabi-v7a \
  --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
  tensorflow:libtensorflow_cc.so

This would be a shared library that you can link against in any native code you build for Android.

Hope that helps.

asimshankar avatar May 04 '17 18:05 asimshankar

@asimshankar thanks for your reply! I'm missing some details here. So, basically you provide two solutions here, right?

  • Inside the deps, I think I need to insert //tensorflow/cc:cc_ops after //tensorflow:android rather than //conditions:default? Because I think that's the true deps when I build it for Android.After that, I tried use the following command to build it but failed. Did I miss anything from your answer? $bazel build -c opt \ --crosstool_top=//external:android/crosstool \ --cpu=armeabi-v7a \ --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \ tensorflow/tools/benchmark:benchmark_model

  • So the second solution is to add a target inside //tensorflow/cc/BUILD, and build it myself?

PKUEcho avatar May 05 '17 01:05 PKUEcho

@asimshankar I tried the second way you provided as following. bazel build -c opt \ --crosstool_top=//external:android/crosstool \ --cpu=armeabi-v7a \ --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \ tensorflow:libtensorflow_cc.so

But there are many errors such as external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/atomic:655:7: note: C++11 'constexpr' only available with -std=c++11 or -std=gnu++11 external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/atomic:668:14: error: expected ';' at end of member declaration atomic() noexcept = default; ^ external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/atomic:668:16: error: 'noexcept' does not name a type atomic() noexcept = default; ^ external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/atomic:669:15: error: expected ';' at end of member declaration ~atomic() noexcept = default; ^ external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/atomic:669:17: error: 'noexcept' does not name a type ~atomic() noexcept = default; ^ external/androidndk/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include/atomic:674:7: error: 'constexpr' does not name a type constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }

I think there's something wrong about the compilers. How do you think?

PKUEcho avatar May 05 '17 02:05 PKUEcho

@PKUEcho : What errors were you getting when you tried to build benchmark_model?

(Based on updates in #2412 - I'd avoid that second suggestion altogether for now, though the errors you're seeing are different, but it seems using libtensorflow_cc.so isn't convenient yet)

That said, CCing @andrewharp in case he has ideas, but marking this as "community support" since building native binaries using the C++ API for Android isn't something that we are currently actively working on or supporting.

asimshankar avatar May 05 '17 20:05 asimshankar

Right, unifying the android_* libs with the other cc_library TF targets isn't really something on our radar.

However, if you take a look at https://github.com/tensorflow/tensorflow/pull/9563, they manage to build cc_ops into an Android binary, so that might be a possible route to investigate.

andrewharp avatar May 05 '17 21:05 andrewharp

@asimshankar @andrewharp Thanks for your help. After reading the #9563, I found that I missed the flags --cxxopt="-std=c++11" --cxxopt="-DTENSORFLOW_DISABLE_META" which makes my previous compilation fail. Though I'm not sure why in other cases such as benchmark_mode I don't need these flags? Also, I found that some ops are not supported in mobile version currently. How can I check which ops are supported and which are not? This question might be out of the scope of this thread so you may close this.

PKUEcho avatar May 06 '17 06:05 PKUEcho

There are kernels that definitely work, and kernels that may work.

The kernels that may work are here.

The ones that definitely work are core and extended.

Closing for now.

drpngx avatar May 06 '17 16:05 drpngx

@PKUEcho some ops use C++11 features, but the default cross-toolchain in NDK (gcc-4.9) uses C++98. That's why you need --cxxopt="-std=c++11"

freedomtan avatar May 08 '17 07:05 freedomtan

@freedomtan Thanks for your reply, that makes sense :)

PKUEcho avatar May 08 '17 07:05 PKUEcho

Hi, I'm trying to build the standard TensorFlow C API (not the JNI API) on Android so I can build a standalone binary in Rust on Android, what would be the recommended way? I saw the contrib/android builds the JNI lib, should I simply do a cross-compile bazel build of //tensorflow:libtensorflow.so ?

maelp avatar Aug 01 '17 17:08 maelp

@maelp You'll need a target that depends on //tensorflow/core:android_tensorflow_lib + //tensorflow/c:c_api (such as //tensorflow/java/src/main/native:native -- you don't have to use the JNI interfaces). See //tensorflow/contrib/android/README.md for the set of build flags to use.

(for further assistance on this issue please refer to StackOverflow, as integrating with Rust is outside the scope of TF github support)

andrewharp avatar Aug 01 '17 18:08 andrewharp

Hi @andrewharp thanks for your answer, my question is not so much about Rust integration as it is about getting the C or C++ library cross-compiled on Android

From your comment I assume that I need to add a build target with both android_tensorflow_lib and c_api to build those? There is not yet an existing cross-compile build target which compiles the C .so for Android without the rest of the JNI bindings?

maelp avatar Aug 07 '17 10:08 maelp

Correct: the c_api target depends on android_tensorflow_lib_lite, which is runtime-only (no ops), so you'll need a target that adds them in. My suggestion would be to take //tensorflow/contrib/android:libtensorflow_inference.so as a starting point and modify it to fit your needs (remove the jni files, change the whitelisted methods so they don't get stripped out, etc).

andrewharp avatar Aug 08 '17 05:08 andrewharp