Thursday, June 27, 2019

Building Full Static libtensorflow-core.a for Linux(x86) and Android(Arm) and Running Sample App.

Building Full Static libtensorflow-core.a for Linux(x86) and Android(Arm) and Running Sample App.

LINUX
>Prerequisite

sudo apt-get install autoconf automake libtool curl make g++ unzip zlib1g-dev git python

Compilation
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout -b r1.14
git pull
#Changes for Full Build
1.tensorflow/contrib/makefile/Makefile
 comment line
 ANDROID_TYPES := -D__ANDROID_TYPES_SLIM__
2.Remove
 -DIS_SLIM_BUILD
3.All two lines after (line 645), or search libtensorflow-core.a
+$(wildcard tensorflow/core/kernel/*.cc) \
+$(wildcard tensorflow/core/math/*.cc) \
4.In file tensorflow/core/platform/platform.h
add #define IS_MOBILE_PLATFORM at TOP.

tensorflow/contrib/makefile/build_all_linux.sh

#If there is some error in buid, comments few lines so that it do not download packages again and again  on rebuild.
+#rm -rf tensorflow/contrib/makefile/downloads
+#tensorflow/contrib/makefile/download_dependencies.sh

and rerun tensorflow/contrib/makefile/build_all_linux.sh

* if you get cc1plus oom killed ( unexpected error) reduce job count .

ANDROID
Use ndk14b, as ndk15 and later have unified header

https://android.googlesource.com/platform/ndk/+/ndk-release-r16/docs/UnifiedHeaders.md

Android build is tightly coupled with NDK version , to choose right version
see below file in tensorflow source , that will ensure your compilation
goes through.

In tensorflow/tools/ci_build/Dockerfile.android check for Variable  ANDROID_NDK_FILENAME
For tensorflow 1.14 it is android-ndk-r17c-linux-x86_64.zip
wget https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip

Compilation
git clone https://github.com/tensorflow/tensorflow.git
         (git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git)
 cd tensorflow
git checkout -b r1.14
git pull 
export NDK_ROOT=/opt/google/android-ndk-r14b 
tensorflow/contrib/makefile/download_dependencies.sh
          tensorflow/contrib/makefile/compile_android_protobuf.sh -c -a arm64-v8a
export HOST_NSYNC_LIB=`tensorflow/contrib/makefile/compile_nsync.sh`
export TARGET_NSYNC_LIB=`CC_PREFIX="${CC_PREFIX}" NDK_ROOT="${NDK_ROOT}" \
tensorflow/contrib/makefile/compile_nsync.sh -t android -a arm64-v8a`
make -f   tensorflow/contrib/makefile/Makefile TARGET=ANDROID ANDROID_ARCH=arm64-v8a ANDROID_TYPES="-D__ANDROID_TYPES_FULL__" 


Application Compilation on Android ( use 16b)
-----------------------------------
export NDK_ROOT=/opt/google/android-ndk-r17c
cmake -DCMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a  -DTARGET_PLATFORM=android -DTARGET_ARCHITECTURE=arm64 -DNDK_PATH=${NDK_ROOT} ..
make


Fixing Missing Kernel Errors on Mobile

The mobile version of TensorFlow is focused on inference, and so by default the list of supported ops (defined in tensorflow/core/kernels/BUILD:android_extended_ops for Bazel and tensorflow/contrib/makefile/tf_op_files.txt for make builds) doesn't include a lot that are training related. This can cause No OpKernel was registered to support Op errors when a GraphDef is loaded, even if the op isn't going to be executed.
If you see this error and it's an op that you do actually want to run on mobile, then you'll need to make local modifications to the build files to include the right .cc file that defines it



No comments:

Post a Comment

Featured Post

XDP - Getting Started with XDP (Linux)