Created
November 21, 2017 10:10
-
-
Save shaurya0/eac838b390df3461b972e966b015a3a2 to your computer and use it in GitHub Desktop.
Compiling tensorflow lite with Android NDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/WORKSPACE b/WORKSPACE | |
old mode 100644 | |
new mode 100755 | |
index b40913801b..f3ac32a956 | |
--- a/WORKSPACE | |
+++ b/WORKSPACE | |
@@ -17,25 +17,25 @@ closure_repositories() | |
load("//tensorflow:workspace.bzl", "tf_workspace") | |
# Uncomment and update the paths in these entries to build the Android demo. | |
-#android_sdk_repository( | |
-# name = "androidsdk", | |
-# api_level = 23, | |
-# # Ensure that you have the build_tools_version below installed in the | |
-# # SDK manager as it updates periodically. | |
-# build_tools_version = "26.0.1", | |
-# # Replace with path to Android SDK on your system | |
-# path = "<PATH_TO_SDK>", | |
-#) | |
+android_sdk_repository( | |
+ name = "androidsdk", | |
+ api_level = 23, | |
+ # Ensure that you have the build_tools_version below installed in the | |
+ # SDK manager as it updates periodically. | |
+ build_tools_version = "26.0.1", | |
+ # Replace with path to Android SDK on your system | |
+ path = "/home/shaurya/AndroidSDK/", | |
+) | |
# | |
-#android_ndk_repository( | |
-# name="androidndk", | |
-# path="<PATH_TO_NDK>", | |
-# # This needs to be 14 or higher to compile TensorFlow. | |
-# # Please specify API level to >= 21 to build for 64-bit | |
-# # archtectures or the Android NDK will automatically select biggest | |
-# # API level that it supports without notice. | |
-# # Note that the NDK version is not the API level. | |
-# api_level=14) | |
+android_ndk_repository( | |
+ name="androidndk", | |
+ path="/home/shaurya/android-ndk-r14b", | |
+ # This needs to be 14 or higher to compile TensorFlow. | |
+ # Please specify API level to >= 21 to build for 64-bit | |
+ # archtectures or the Android NDK will automatically select biggest | |
+ # API level that it supports without notice. | |
+ # Note that the NDK version is not the API level. | |
+ api_level=21) | |
# Please add all new TensorFlow dependencies in workspace.bzl. | |
tf_workspace() | |
diff --git a/tensorflow/contrib/lite/Makefile b/tensorflow/contrib/lite/Makefile | |
old mode 100644 | |
new mode 100755 | |
index 78402727ab..531ff2499d | |
--- a/tensorflow/contrib/lite/Makefile | |
+++ b/tensorflow/contrib/lite/Makefile | |
@@ -4,6 +4,7 @@ ifeq ($(origin MAKEFILE_DIR), undefined) | |
MAKEFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) | |
endif | |
+ | |
# Try to figure out the host system | |
HOST_OS := | |
ifeq ($(OS),Windows_NT) | |
@@ -18,7 +19,9 @@ else | |
endif | |
endif | |
-ARCH := $(shell if [[ $(shell uname -m) =~ i[345678]86 ]]; then echo x86_32; else echo $(shell uname -m); fi) | |
+# ARCH := $(shell if [[ $(shell uname -m) =~ i[345678]86 ]]; then echo x86_32; else echo $(shell uname -m); fi) | |
+ARCH := $(shell uname -p) | |
+HOST_ARCH := $(ARCH) | |
# Where compiled objects are stored. | |
OBJDIR := $(MAKEFILE_DIR)/gen/obj/ | |
@@ -26,6 +29,8 @@ BINDIR := $(MAKEFILE_DIR)/gen/bin/ | |
LIBDIR := $(MAKEFILE_DIR)/gen/lib/ | |
GENDIR := $(MAKEFILE_DIR)/gen/obj/ | |
+ | |
+$(warning CC_PREFIX is $(CC_PREFIX)) | |
# Settings for the host compiler. | |
CXX := $(CC_PREFIX) gcc | |
CXXFLAGS := --std=c++11 -O3 -DNDEBUG | |
@@ -53,13 +58,14 @@ LIBS := \ | |
-lstdc++ \ | |
-lpthread \ | |
-lm \ | |
--lz | |
+-lz \ | |
# If we're on Linux, also link in the dl library. | |
-ifeq ($(OS),LINUX) | |
+ifeq ($(HOST_OS),LINUX) | |
LIBS += -ldl -lpthread | |
endif | |
+include $(MAKEFILE_DIR)/android_makefile.inc | |
include $(MAKEFILE_DIR)/ios_makefile.inc | |
# This library is the main target for this makefile. It will contain a minimal | |
@@ -75,6 +81,7 @@ tensorflow/contrib/lite/tools/benchmark_model.cc | |
BENCHMARK_OBJS := $(addprefix $(OBJDIR), \ | |
$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(BENCHMARK_SRCS)))) | |
+ | |
# What sources we want to compile, must be kept in sync with the main Bazel | |
# build files. | |
@@ -98,7 +105,9 @@ $(wildcard tensorflow/contrib/lite/*/*test.cc) \ | |
$(wildcard tensorflow/contrib/lite/*/*/*test.cc) \ | |
$(wildcard tensorflow/contrib/lite/*/*/*/*test.cc) \ | |
$(wildcard tensorflow/contrib/lite/kernels/test_util.cc) \ | |
-$(BENCHMARK_SRCS) | |
+$(BENCHMARK_SRCS) \ | |
+$(TESTAPP_SRCS) | |
+ | |
# Filter out all the excluded files. | |
TF_LITE_CC_SRCS := $(filter-out $(CORE_CC_EXCLUDE_SRCS), $(CORE_CC_ALL_SRCS)) | |
# File names of the intermediate files target compilation generates. | |
diff --git a/tensorflow/contrib/lite/android_makefile.inc b/tensorflow/contrib/lite/android_makefile.inc | |
index e69de29bb2..98e3347b8b 100755 | |
--- a/tensorflow/contrib/lite/android_makefile.inc | |
+++ b/tensorflow/contrib/lite/android_makefile.inc | |
@@ -0,0 +1,124 @@ | |
+ifeq ($(TARGET),ANDROID) | |
+# Override NDK_ROOT on the command line with your own NDK location, e.g. | |
+# make -f tensorflow/contrib/makefile/Makefile TARGET=ANDROID \ | |
+# NDK_ROOT=/path/to/your/ndk | |
+# You need to have an Android version of the protobuf libraries compiled to link | |
+# in. The compile_android_protobuf.sh script may help. | |
+ | |
+ ANDROID_HOST_OS_ARCH := | |
+ ifeq ($(HOST_OS),LINUX) | |
+ ANDROID_HOST_OS_ARCH=linux | |
+ endif | |
+ ifeq ($(HOST_OS),OSX) | |
+ ANDROID_HOST_OS_ARCH=darwin | |
+ endif | |
+ ifeq ($(HOST_OS),WINDOWS) | |
+ $(error "windows is not supported.") | |
+ endif | |
+ | |
+ ifeq ($(HOST_ARCH),x86_32) | |
+ ANDROID_HOST_OS_ARCH := $(ANDROID_HOST_OS_ARCH)-x86 | |
+ else | |
+ ANDROID_HOST_OS_ARCH := $(ANDROID_HOST_OS_ARCH)-$(HOST_ARCH) | |
+ endif | |
+ | |
+ ifndef ANDROID_ARCH | |
+ ANDROID_ARCH := armeabi-v7a | |
+ endif | |
+ | |
+ ifeq ($(ANDROID_ARCH),arm64-v8a) | |
+ TOOLCHAIN := aarch64-linux-android-4.9 | |
+ SYSROOT_ARCH := arm64 | |
+ BIN_PREFIX := aarch64-linux-android | |
+ MARCH_OPTION := | |
+ endif | |
+ ifeq ($(ANDROID_ARCH),armeabi) | |
+ TOOLCHAIN := arm-linux-androideabi-4.9 | |
+ SYSROOT_ARCH := arm | |
+ BIN_PREFIX := arm-linux-androideabi | |
+ MARCH_OPTION := | |
+ endif | |
+ ifeq ($(ANDROID_ARCH),armeabi-v7a) | |
+ TOOLCHAIN := arm-linux-androideabi-4.9 | |
+ SYSROOT_ARCH := arm | |
+ BIN_PREFIX := arm-linux-androideabi | |
+ MARCH_OPTION := -march=armv7-a -mfloat-abi=softfp -mfpu=neon | |
+ endif | |
+ ifeq ($(ANDROID_ARCH),mips) | |
+ TOOLCHAIN := mipsel-linux-android-4.9 | |
+ SYSROOT_ARCH := mips | |
+ BIN_PREFIX := mipsel-linux-android | |
+ MARCH_OPTION := | |
+ endif | |
+ ifeq ($(ANDROID_ARCH),mips64) | |
+ TOOLCHAIN := mips64el-linux-android-4.9 | |
+ SYSROOT_ARCH := mips64 | |
+ BIN_PREFIX := mips64el-linux-android | |
+ MARCH_OPTION := | |
+ endif | |
+ ifeq ($(ANDROID_ARCH),x86) | |
+ TOOLCHAIN := x86-4.9 | |
+ SYSROOT_ARCH := x86 | |
+ BIN_PREFIX := i686-linux-android | |
+ MARCH_OPTION := | |
+ endif | |
+ ifeq ($(ANDROID_ARCH),x86_64) | |
+ TOOLCHAIN := x86_64-4.9 | |
+ SYSROOT_ARCH := x86_64 | |
+ BIN_PREFIX := x86-64-linux-android | |
+ MARCH_OPTION := | |
+ endif | |
+ | |
+ ifndef NDK_ROOT | |
+ $(error "NDK_ROOT is not defined.") | |
+ endif | |
+ | |
+ CXX := $(CC_PREFIX) $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-g++ | |
+ CC := $(CC_PREFIX) $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-gcc | |
+ CXXFLAGS +=\ | |
+--sysroot=$(NDK_ROOT)/platforms/android-21/arch-$(SYSROOT_ARCH) \ | |
+-Wno-narrowing \ | |
+-fomit-frame-pointer \ | |
+$(MARCH_OPTION) \ | |
+-fPIE \ | |
+-fPIC | |
+ INCLUDES += \ | |
+-I$(NDK_ROOT)/sources/android/support/include \ | |
+-I$(NDK_ROOT)/sources/android/cpufeatures/ \ | |
+-I$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/include \ | |
+-I$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/libs/$(ANDROID_ARCH)/include \ | |
+-I$(NDK_ROOT)/platforms/android-21/arch-arm/usr/include \ | |
+-I$(NDK_ROOT)/platforms/android-21/arch-arm/usr/include/machine \ | |
+-I. \ | |
+ | |
+ LIBS := \ | |
+-lgnustl_static \ | |
+-llog \ | |
+-lz \ | |
+-lm \ | |
+-ldl \ | |
+-latomic \ | |
+-lcpufeatures | |
+ | |
+ LD := $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/$(BIN_PREFIX)/bin/ld | |
+ | |
+ LDFLAGS := \ | |
+$(MARCH_OPTION) \ | |
+-L$(MAKEFILE_DIR)/gen/protobuf_android/$(ANDROID_ARCH)/lib \ | |
+-L$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/libs/$(ANDROID_ARCH) \ | |
+-L$(NDK_ROOT)/sources/android/cpufeatures/obj/local/$(ANDROID_ARCH) \ | |
+-fPIE \ | |
+-pie \ | |
+-v | |
+ | |
+ AR := $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-ar | |
+ ARFLAGS := r | |
+ LIBFLAGS += -Wl,--allow-multiple-definition -Wl,--whole-archive | |
+ | |
+ | |
+ OBJDIR := $(OBJDIR)android_$(ANDROID_ARCH)/ | |
+ LIBDIR := $(LIBDIR)android_$(ANDROID_ARCH)/ | |
+ BINDIR := $(BINDIR)android_$(ANDROID_ARCH)/ | |
+ DEPDIR := $(DEPDIR)android_$(ANDROID_ARCH)/ | |
+ | |
+endif # ANDROID | |
\ No newline at end of file | |
diff --git a/tensorflow/contrib/lite/download_dependencies.sh b/tensorflow/contrib/lite/download_dependencies.sh | |
index 41480c2007..5d77f4c049 100755 | |
--- a/tensorflow/contrib/lite/download_dependencies.sh | |
+++ b/tensorflow/contrib/lite/download_dependencies.sh | |
@@ -85,7 +85,7 @@ replace_by_sed 's#static uint32x2_t p2ui_CONJ_XOR = vld1_u32( conj_XOR_DATA );#s | |
replace_by_sed 's#static uint64x2_t p2ul_CONJ_XOR = vld1q_u64( p2ul_conj_XOR_DATA );#static uint64x2_t p2ul_CONJ_XOR;// = vld1q_u64( p2ul_conj_XOR_DATA ); - Removed by script#' \ | |
"${DOWNLOADS_DIR}/eigen/Eigen/src/Core/arch/NEON/Complex.h" | |
-cp ${DOWNLOADS_DIR}/models/models/* tensorflow/contrib/lite/examples/ios/simple/data/ | |
-cp ${DOWNLOADS_DIR}/quantized_models/* tensorflow/contrib/lite/examples/ios/camera/data/ | |
+# cp ${DOWNLOADS_DIR}/models/* tensorflow/contrib/lite/examples/ios/simple/data/ | |
+# cp ${DOWNLOADS_DIR}/quantized_models/* tensorflow/contrib/lite/examples/ios/camera/data/ | |
echo "download_dependencies.sh completed successfully." >&2 | |
diff --git a/tensorflow/contrib/lite/kernels/internal/optimized/cpu_check.h b/tensorflow/contrib/lite/kernels/internal/optimized/cpu_check.h | |
old mode 100644 | |
new mode 100755 | |
index dea46cc120..e80be5ae76 | |
--- a/tensorflow/contrib/lite/kernels/internal/optimized/cpu_check.h | |
+++ b/tensorflow/contrib/lite/kernels/internal/optimized/cpu_check.h | |
@@ -18,7 +18,7 @@ limitations under the License. | |
namespace tflite { | |
#ifdef __ANDROID__ | |
-#include "ndk/sources/android/cpufeatures/cpu-features.h" | |
+#include "cpu-features.h" | |
// Runtime check for Neon support on Android. | |
inline bool TestCPUFeatureNeon() { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment