Skip to content

Commit

Permalink
Adds new podspecs for the TensorFlow Lite iOS libraries.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 239748653
  • Loading branch information
tensorflower-gardener committed Mar 22, 2019
1 parent da1259d commit 76e879d
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 249 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ Pods
Podfile.lock
*.pbxproj
*.xcworkspacedata
/tensorflow/lite/tools/make/downloads/**
/tensorflow/lite/gen/**
/*.podspec
/tensorflow/lite/experimental/objc/BUILD
/tensorflow/lite/experimental/swift/BUILD
/tensorflow/lite/examples/ios/simple/data/*.txt
/tensorflow/lite/examples/ios/simple/data/*.tflite
/tensorflow/lite/gen/**
/tensorflow/lite/tools/make/downloads/**
xcuserdata/**
/api_init_files_list.txt
/estimator_api_init_files_list.txt
Expand Down
44 changes: 24 additions & 20 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@
'lib64/', 'lib/powerpc64le-linux-gnu/', 'lib/x86_64-linux-gnu/', ''
]

# List of files to be configured for using Bazel on Apple platforms.
# List of files to configure when building Bazel on Apple platforms.
APPLE_BAZEL_FILES = [
'tensorflow/lite/experimental/objc/BUILD',
'tensorflow/lite/experimental/swift/BUILD'
]

# List of files to move when building for iOS.
IOS_FILES = [
'tensorflow/lite/experimental/objc/TensorFlowLiteObjC.podspec',
'tensorflow/lite/experimental/swift/TensorFlowLiteSwift.podspec',
]

if platform.machine() == 'ppc64le':
_DEFAULT_TENSORRT_PATH_LINUX = '/usr/lib/powerpc64le-linux-gnu/'
else:
Expand Down Expand Up @@ -1585,24 +1591,24 @@ def config_info_line(name, help_text):
print('\t--config=%-12s\t# %s' % (name, help_text))


def configure_apple_bazel_rules():
"""Configures Bazel rules for building on Apple platforms.
def configure_ios():
"""Configures TensorFlow for iOS builds.
Enables analyzing and building Apple Bazel rules on Apple platforms. This
function will only be executed if `is_macos()` is true.
This function will only be executed if `is_macos()` is true.
"""
if not is_macos():
return
for filepath in APPLE_BAZEL_FILES:
print(
'Configuring %s file to analyze and build Bazel rules on Apple platforms.'
% filepath)
existing_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath + '.apple')
renamed_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath)
os.rename(existing_filepath, renamed_filepath)
if _TF_CURRENT_BAZEL_VERSION is None or _TF_CURRENT_BAZEL_VERSION < 23000:
print(
'Building Bazel rules on Apple platforms requires Bazel 0.23 or later.')
for filepath in APPLE_BAZEL_FILES:
existing_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath + '.apple')
renamed_filepath = os.path.join(_TF_WORKSPACE_ROOT, filepath)
symlink_force(existing_filepath, renamed_filepath)
for filepath in IOS_FILES:
filename = os.path.basename(filepath)
new_filepath = os.path.join(_TF_WORKSPACE_ROOT, filename)
symlink_force(filepath, new_filepath)


def main():
Expand Down Expand Up @@ -1648,7 +1654,7 @@ def main():
if is_macos():
environ_cp['TF_NEED_TENSORRT'] = '0'
else:
environ_cp['TF_CONFIGURE_APPLE_BAZEL_RULES'] = '0'
environ_cp['TF_CONFIGURE_IOS'] = '0'

# The numpy package on ppc64le uses OpenBLAS which has multi-threading
# issues that lead to incorrect answers. Set OMP_NUM_THREADS=1 at
Expand Down Expand Up @@ -1753,13 +1759,11 @@ def main():

system_specific_config(os.environ)

if get_var(
environ_cp, 'TF_CONFIGURE_APPLE_BAZEL_RULES',
'Configure Bazel rules for Apple platforms', False,
('Would you like to configure Bazel rules for building on Apple platforms?'
), 'Configuring Bazel rules for Apple platforms.',
'Not configuring Bazel rules for Apple platforms.'):
configure_apple_bazel_rules()
if get_var(environ_cp, 'TF_CONFIGURE_IOS', 'Configure TensorFlow for iOS',
False, ('Would you like to configure TensorFlow for iOS builds?'),
'Configuring TensorFlow for iOS builds.',
'Not configuring TensorFlow for iOS builds.'):
configure_ios()

print('Preconfigured Bazel build configs. You can use any of the below by '
'adding "--config=<>" to your build command. See .bazelrc for more '
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/lite/experimental/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ cc_library(
srcs = ["c_api.cc"],
hdrs = ["c_api.h"],
copts = tflite_copts(),
tags = ["swift_module=TensorFlowLiteCAPI"],
tags = ["swift_module=TensorFlowLiteC"],
visibility = [
":experimental",
],
Expand Down
25 changes: 25 additions & 0 deletions tensorflow/lite/experimental/c/TensorFlowLiteC.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Bazel rules for building the TensorFlowLiteC iOS static framework.
#
# Add the build rules below to `//tensorflow/lite/experimental/c/BUILD`.
# Build the framework:
# bazel build tensorflow/lite/experimental/c:TensorFlowLiteC_framework -c opt --ios_multi_cpus=x86_64,armv7,arm64
# Unzip the generated framework:
# unzip bazel-bin/tensorflow/lite/experimental/c/TensorFlowLiteC_framework.zip -d /Users/path/to/TensorFlowLiteC.framework

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_static_framework")
load("@build_bazel_rules_apple//apple:versioning.bzl", "apple_bundle_version")

apple_bundle_version(
name = "TensorFlowLiteC_version",
build_version = "0.1.0",
short_version_string = "0.1",
)

ios_static_framework(
name = "TensorFlowLiteC_framework",
hdrs = ["c_api.h"],
bundle_name = "TensorFlowLiteC",
minimum_os_version = "9.0",
version = ":TensorFlowLiteC_version",
deps = [":c_api"],
)
24 changes: 24 additions & 0 deletions tensorflow/lite/experimental/c/TensorFlowLiteC.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Run `pod lib lint TensorFlowLiteC.podspec` to ensure this is a valid spec.

Pod::Spec.new do |s|
s.name = 'TensorFlowLiteC'
s.version = '0.1.0'
s.authors = 'Google Inc.'
s.license = { :type => 'Apache' }
s.homepage = 'https://github.com/tensorflow/tensorflow'
s.source = { :http => 'https://dl.google.com/dl/tensorflow/lite/frameworks/TensorFlowLiteC/0.1.0/TensorFlowLiteC-0.1.0.tar.gz' }
s.summary = 'TensorFlow Lite'
s.description = <<-DESC
TensorFlow Lite is TensorFlow's lightweight solution for mobile developers. It
enables low-latency inference of on-device machine learning models with a
small binary size and fast performance supporting hardware acceleration.
DESC

s.ios.deployment_target = '9.0'

s.module_name = 'TensorFlowLiteC'
s.library = 'c++'
s.static_framework = true
s.vendored_frameworks = 'Frameworks/TensorFlowLiteC.framework'
end
1 change: 1 addition & 0 deletions tensorflow/lite/experimental/objc/BUILD.apple
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ objc_library(
srcs = SOURCES,
hdrs = API_HEADERS,
copts = RELEASE_COPTS,
module_map = "apis/module.modulemap",
tags = DEFAULT_TAGS,
deps = [
"//tensorflow/lite/experimental/c:c_api",
Expand Down
47 changes: 39 additions & 8 deletions tensorflow/lite/experimental/objc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ supporting hardware acceleration.

## Getting Started

To build the Objective-C TensorFlow Lite library on Apple platforms,
[install from source](https://www.tensorflow.org/install/source#setup_for_linux_and_macos)
or [clone the GitHub repo](https://github.com/tensorflow/tensorflow).
Then, configure TensorFlow by navigating to the root directory and executing the
`configure.py` script:

```shell
python configure.py
```

Follow the prompts and when asked to configure TensorFlow for iOS builds,
enter `y`.

### Bazel

In your `BUILD` file, add the `TensorFlowLite` dependency:
Expand All @@ -19,17 +32,12 @@ objc_library(
)
```

If you would like to build the Objective-C TensorFlow Lite library using Bazel on Apple
platforms, clone or download the [TensorFlow GitHub repo](https://github.com/tensorflow/tensorflow),
then navigate to the root `tensorflow` directory and execute the `configure.py` script:
In your Objective-C files, import the umbrella header:

```shell
python configure.py
```objectivec
#import "TFLTensorFlowLite.h"
```

Follow the prompts and when asked to configure the Bazel rules for Apple
platforms, enter `y`.

Build the `TensorFlowLite` Objective-C library target:

```shell
Expand All @@ -52,3 +60,26 @@ script from the root `tensorflow` directory:
```shell
generate_xcodeproj.sh --genconfig tensorflow/lite/experimental/objc/TensorFlowLite.tulsiproj:TensorFlowLite --outputfolder ~/path/to/generated/TensorFlowLite.xcodeproj
```

### CocoaPods

Add the following to your `Podfile`:

```ruby
pod 'TensorFlowLiteObjC'
```

Then, run `pod install`.

In your Objective-C files, import the umbrella header:

```objectivec
#import "TFLTensorFlowLite.h"
```

Or, the module if `CLANG_ENABLE_MODULES = YES` and `use_frameworks!` is
specified in your `Podfile`:

```objectivec
@import TFLTensorFlowLite;
```
39 changes: 39 additions & 0 deletions tensorflow/lite/experimental/objc/TensorFlowLiteObjC.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Run `pod lib lint TensorFlowLiteObjC.podspec` to ensure this is a valid spec.

Pod::Spec.new do |s|
s.name = 'TensorFlowLiteObjC'
s.version = '0.1.0'
s.authors = 'Google Inc.'
s.license = { :type => 'Apache' }
s.homepage = 'https://github.com/tensorflow/tensorflow'
s.source = { :git => 'https://github.com/tensorflow/tensorflow.git', :tag => 'v2.0.0-alpha0' }
s.summary = 'TensorFlow Lite for Objective-C'
s.description = <<-DESC
TensorFlow Lite is TensorFlow's lightweight solution for Objective-C
developers. It enables low-latency inference of on-device machine learning
models with a small binary size and fast performance supporting hardware
acceleration.
DESC

s.ios.deployment_target = '9.0'

s.module_name = 'TFLTensorFlowLite'
s.static_framework = true
s.prefix_header_file = false

base_dir = 'tensorflow/lite/experimental/objc/'
s.public_header_files = base_dir + 'apis/*.h'
s.source_files = base_dir + '{apis,sources}/*.{h,m,mm}'
s.module_map = base_dir + 'apis/framework.modulemap'
s.dependency 'TensorFlowLiteC', "#{s.version}"
s.pod_target_xcconfig = {
'HEADER_SEARCH_PATHS' =>
'"${PODS_TARGET_SRCROOT}" ' +
'"${PODS_TARGET_SRCROOT}/"' + base_dir + '"apis"',
}

s.test_spec 'Tests' do |ts|
ts.source_files = base_dir + 'tests/*.m'
end
end
18 changes: 18 additions & 0 deletions tensorflow/lite/experimental/objc/apis/TFLTensorFlowLite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2019 Google Inc. 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.

#import "TFLInterpreter.h"
#import "TFLInterpreterOptions.h"
#import "TFLQuantizationParameters.h"
#import "TFLTensor.h"
10 changes: 10 additions & 0 deletions tensorflow/lite/experimental/objc/apis/framework.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
framework module TFLTensorFlowLite {
umbrella header "TFLTensorFlowLite.h"

header "TFLInterpreter.h"
header "TFLInterpreterOptions.h"
header "TFLQuantizationParameters.h"
header "TFLTensor.h"

export *
}
10 changes: 10 additions & 0 deletions tensorflow/lite/experimental/objc/apis/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TFLTensorFlowLite {
umbrella header "TFLTensorFlowLite.h"

header "TFLInterpreter.h"
header "TFLInterpreterOptions.h"
header "TFLQuantizationParameters.h"
header "TFLTensor.h"

export *
}
2 changes: 0 additions & 2 deletions tensorflow/lite/experimental/swift/BUILD.apple
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package(default_visibility = ["//visibility:private"])

licenses(["notice"]) # Apache 2.0

exports_files(["LICENSE"])

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application", "ios_unit_test")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")

Expand Down
Loading

0 comments on commit 76e879d

Please sign in to comment.