From 93851963bd3b8e48eda8b6f1e62b6bcf4e8d5d10 Mon Sep 17 00:00:00 2001 From: Yu-Cheng Ling Date: Mon, 22 Oct 2018 14:47:38 -0700 Subject: [PATCH] Build TensorFlow Lite Flex iOS with TF Makefile PiperOrigin-RevId: 218237600 --- tensorflow/contrib/makefile/Makefile | 61 ++++++++++++++++++- tensorflow/contrib/makefile/build_all_ios.sh | 2 +- .../build_all_ios_with_tflite_flex.sh | 40 ++++++++++++ .../contrib/makefile/download_dependencies.sh | 8 +++ .../makefile/proto_text_pb_cc_files.txt | 1 + .../makefile/proto_text_pb_h_files.txt | 1 + .../contrib/makefile/tf_proto_files.txt | 1 + 7 files changed, 111 insertions(+), 3 deletions(-) create mode 100755 tensorflow/contrib/makefile/build_all_ios_with_tflite_flex.sh diff --git a/tensorflow/contrib/makefile/Makefile b/tensorflow/contrib/makefile/Makefile index 36125c198e0..2dae7993d62 100644 --- a/tensorflow/contrib/makefile/Makefile +++ b/tensorflow/contrib/makefile/Makefile @@ -208,6 +208,16 @@ endif # override local versions in the source tree. INCLUDES += -I/usr/local/include +# If `$(WITH_TFLITE_FLEX)` is `true`, this Makefile will build a library +# for TensorFlow Lite Flex runtime. +# Farmhash and Flatbuffer is required for TensorFlow Lite Flex runtime. +ifeq ($(WITH_TFLITE_FLEX), true) + HOST_INCLUDES += -I$(MAKEFILE_DIR)/downloads/farmhash/src + HOST_INCLUDES += -I$(MAKEFILE_DIR)/downloads/flatbuffers/include + INCLUDES += -I$(MAKEFILE_DIR)/downloads/farmhash/src + INCLUDES += -I$(MAKEFILE_DIR)/downloads/flatbuffers/include +endif + LIBS := \ $(TARGET_NSYNC_LIB) \ -lstdc++ \ @@ -283,7 +293,7 @@ ifeq ($(TARGET),ANDROID) else ANDROID_HOST_OS_ARCH := $(ANDROID_HOST_OS_ARCH)-$(HOST_ARCH) endif - + ifndef ANDROID_ARCH ANDROID_ARCH := armeabi-v7a endif @@ -330,7 +340,7 @@ ifeq ($(TARGET),ANDROID) BIN_PREFIX := x86_64-linux-android MARCH_OPTION := endif - + ifndef NDK_ROOT $(error "NDK_ROOT is not defined.") endif @@ -717,6 +727,53 @@ tensorflow/core/util/reporter.cc \ tensorflow/tools/benchmark/benchmark_model.cc \ tensorflow/tools/benchmark/benchmark_model_main.cc +# If `$(WITH_TFLITE_FLEX)` is `true`, this Makefile will build a library +# for TensorFlow Lite Flex runtime. +# Adding the following dependencies> +# * TensorFlow Eager Runtime. +# * TensorFlow Lite Runtime. +# * TensorFlow Lite Flex Delegate. +ifeq ($(WITH_TFLITE_FLEX), true) + CORE_CC_SRCS += $(wildcard tensorflow/core/common_runtime/eager/*.cc) + TF_LITE_CORE_CC_ALL_SRCS := \ + $(wildcard tensorflow/contrib/lite/*.cc) \ + $(wildcard tensorflow/contrib/lite/*.c) \ + $(wildcard tensorflow/contrib/lite/c/*.c) \ + $(wildcard tensorflow/contrib/lite/core/api/*.cc) + + TF_LITE_CORE_CC_ALL_SRCS += \ + $(wildcard tensorflow/contrib/lite/kernels/*.cc) \ + $(wildcard tensorflow/contrib/lite/kernels/internal/*.cc) \ + $(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.cc) \ + $(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.cc) \ + $(PROFILER_SRCS) \ + $(wildcard tensorflow/contrib/lite/kernels/*.c) \ + $(wildcard tensorflow/contrib/lite/kernels/internal/*.c) \ + $(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.c) \ + $(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.c) \ + $(wildcard tensorflow/contrib/lite/delegates/flex/*.cc) + + # Hack. This shouldn't be here? + TF_LITE_CORE_CC_ALL_SRCS += \ + $(wildcard tensorflow/contrib/makefile/downloads/farmhash/src/farmhash.cc) \ + + # Remove any duplicates. + TF_LITE_CORE_CC_ALL_SRCS := $(sort $(TF_LITE_CORE_CC_ALL_SRCS)) + TF_LITE_CORE_CC_EXCLUDE_SRCS := \ + $(wildcard tensorflow/contrib/lite/*test.cc) \ + $(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) \ + $(wildcard tensorflow/contrib/lite/delegates/flex/test_util.cc) \ + $(wildcard tensorflow/contrib/lite/nnapi_delegate.cc) \ + $(wildcard tensorflow/contrib/lite/mmap_allocation_disabled.cc) + + # Filter out all the excluded files. + TF_LITE_CC_SRCS := $(filter-out $(TF_LITE_CORE_CC_EXCLUDE_SRCS), $(TF_LITE_CORE_CC_ALL_SRCS)) + TF_CC_SRCS += $(TF_LITE_CC_SRCS) +endif + ifdef HEXAGON_LIBS TF_CC_SRCS += \ tensorflow/cc/framework/scope.cc \ diff --git a/tensorflow/contrib/makefile/build_all_ios.sh b/tensorflow/contrib/makefile/build_all_ios.sh index 1d4677ef4bd..8bfaf517fdf 100755 --- a/tensorflow/contrib/makefile/build_all_ios.sh +++ b/tensorflow/contrib/makefile/build_all_ios.sh @@ -138,7 +138,7 @@ if [[ ! -z "${BUILD_ARCH}" ]]; then fi # build the ios tensorflow libraries. -echo "Building TensorFlow with flags: ${TF_SCRIPT_FLAGS} -f ${TF_CC_FLAGS}" +echo "Building TensorFlow with command: ${TF_SCRIPT_FLAGS} -f ${TF_CC_FLAGS}" tensorflow/contrib/makefile/compile_ios_tensorflow.sh ${TF_SCRIPT_FLAGS} -f "${TF_CC_FLAGS}" # Creates a static universal library in diff --git a/tensorflow/contrib/makefile/build_all_ios_with_tflite_flex.sh b/tensorflow/contrib/makefile/build_all_ios_with_tflite_flex.sh new file mode 100755 index 00000000000..8d34911f154 --- /dev/null +++ b/tensorflow/contrib/makefile/build_all_ios_with_tflite_flex.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + +# This shell script is used to build TensorFlow Lite Flex runtime for iOS. +# It compiles TensorFlow Lite and TensorFlow codebases together, and enable a +# route to use TensorFlow kernels in TensorFlow Lite. +# +# After the script is executed, the multi-architecture static libraries will be +# created under: `tensorflow/contrib/makefile/gen/lib/`. + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TOP_SRCDIR="${SCRIPT_DIR}/../../../" +cd ${TOP_SRCDIR} + +# Exporting `WITH_TFLITE_FLEX`. The flag will be propagated all the way +# down to Makefile. +export WITH_TFLITE_FLEX="true" +# Execute `build_all_ios.sh` and propagate all parameters. +tensorflow/contrib/makefile/build_all_ios.sh $* + +# Copy all the libraries required for TFLite Flex runtime together. +cd "${TOP_SRCDIR}/tensorflow/contrib/makefile" +cp 'downloads/nsync/builds/lipo.ios.c++11/nsync.a' 'gen/lib/' +cp 'gen/protobuf_ios/lib/libprotobuf.a' 'gen/lib/' +cp 'gen/lib/libtensorflow-core.a' 'gen/lib/libtensorflow-lite.a' diff --git a/tensorflow/contrib/makefile/download_dependencies.sh b/tensorflow/contrib/makefile/download_dependencies.sh index dc9b17a6278..0a07588f07f 100755 --- a/tensorflow/contrib/makefile/download_dependencies.sh +++ b/tensorflow/contrib/makefile/download_dependencies.sh @@ -43,6 +43,10 @@ DOUBLE_CONVERSION_URL="$(grep -o "https.*google/double-conversion.*\.zip" "${BZL ABSL_URL="$(grep -o 'https://github.com/abseil/abseil-cpp/.*tar.gz' "${BZL_FILE_PATH}" | head -n1)" CUB_URL="$(grep -o 'https.*cub/archive.*zip' "${BZL_FILE_PATH}" | grep -v mirror.bazel | head -n1)" +# Required for TensorFlow Lite Flex runtime. +FARMHASH_URL="https://mirror.bazel.build/github.com/google/farmhash/archive/816a4ae622e964763ca0862d9dbd19324a1eaf45.tar.gz" +FLATBUFFERS_URL="https://github.com/google/flatbuffers/archive/1f5eae5d6a135ff6811724f6c57f911d1f46bb15.tar.gz" + # TODO(petewarden): Some new code in Eigen triggers a clang bug with iOS arm64, # so work around it by patching the source. replace_by_sed() { @@ -98,6 +102,10 @@ download_and_extract "${DOUBLE_CONVERSION_URL}" "${DOWNLOADS_DIR}/double_convers download_and_extract "${ABSL_URL}" "${DOWNLOADS_DIR}/absl" download_and_extract "${CUB_URL}" "${DOWNLOADS_DIR}/cub/external/cub_archive" +# Required for TensorFlow Lite Flex runtime. +download_and_extract "${FARMHASH_URL}" "${DOWNLOADS_DIR}/farmhash" +download_and_extract "${FLATBUFFERS_URL}" "${DOWNLOADS_DIR}/flatbuffers" + replace_by_sed 's#static uint32x4_t p4ui_CONJ_XOR = vld1q_u32( conj_XOR_DATA );#static uint32x4_t p4ui_CONJ_XOR; // = vld1q_u32( conj_XOR_DATA ); - Removed by script#' \ "${DOWNLOADS_DIR}/eigen/Eigen/src/Core/arch/NEON/Complex.h" replace_by_sed 's#static uint32x2_t p2ui_CONJ_XOR = vld1_u32( conj_XOR_DATA );#static uint32x2_t p2ui_CONJ_XOR;// = vld1_u32( conj_XOR_DATA ); - Removed by scripts#' \ diff --git a/tensorflow/contrib/makefile/proto_text_pb_cc_files.txt b/tensorflow/contrib/makefile/proto_text_pb_cc_files.txt index 0d8df93d111..87c73ec1ca6 100644 --- a/tensorflow/contrib/makefile/proto_text_pb_cc_files.txt +++ b/tensorflow/contrib/makefile/proto_text_pb_cc_files.txt @@ -27,6 +27,7 @@ tensorflow/core/grappler/costs/op_performance_data.pb.cc tensorflow/core/lib/core/error_codes.pb.cc tensorflow/core/protobuf/cluster.pb.cc tensorflow/core/protobuf/config.pb.cc +tensorflow/core/protobuf/eager_service.pb.cc tensorflow/core/protobuf/debug.pb.cc tensorflow/core/protobuf/device_properties.pb.cc tensorflow/core/protobuf/meta_graph.pb.cc diff --git a/tensorflow/contrib/makefile/proto_text_pb_h_files.txt b/tensorflow/contrib/makefile/proto_text_pb_h_files.txt index d982df93193..4120ea52ec5 100644 --- a/tensorflow/contrib/makefile/proto_text_pb_h_files.txt +++ b/tensorflow/contrib/makefile/proto_text_pb_h_files.txt @@ -29,6 +29,7 @@ tensorflow/core/protobuf/cluster.pb.h tensorflow/core/protobuf/config.pb.h tensorflow/core/protobuf/debug.pb.h tensorflow/core/protobuf/device_properties.pb.h +tensorflow/core/protobuf/eager_service.pb.h tensorflow/core/protobuf/meta_graph.pb.h tensorflow/core/protobuf/named_tensor.pb.h tensorflow/core/protobuf/queue_runner.pb.h diff --git a/tensorflow/contrib/makefile/tf_proto_files.txt b/tensorflow/contrib/makefile/tf_proto_files.txt index 8bec3e3e01f..2712e906d71 100644 --- a/tensorflow/contrib/makefile/tf_proto_files.txt +++ b/tensorflow/contrib/makefile/tf_proto_files.txt @@ -34,6 +34,7 @@ tensorflow/core/lib/core/error_codes.proto tensorflow/core/protobuf/cluster.proto tensorflow/core/protobuf/config.proto tensorflow/core/protobuf/debug.proto +tensorflow/core/protobuf/eager_service.proto tensorflow/core/protobuf/device_properties.proto tensorflow/core/protobuf/meta_graph.proto tensorflow/core/protobuf/named_tensor.proto