|
| 1 | +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""TensorFlow Lite is for mobile and embedded devices. |
| 16 | +
|
| 17 | +TensorFlow Lite is the official solution for running machine learning models on |
| 18 | +mobile and embedded devices. It enables on-device machine learning inference |
| 19 | +with low latency and a small binary size on Android, iOS, and other operating |
| 20 | +systems. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import absolute_import |
| 24 | +from __future__ import division |
| 25 | +from __future__ import print_function |
| 26 | + |
| 27 | +import multiprocessing |
| 28 | +import os |
| 29 | +import subprocess |
| 30 | + |
| 31 | +from distutils.command.build_ext import build_ext |
| 32 | +import numpy |
| 33 | + |
| 34 | +from setuptools import Extension |
| 35 | +from setuptools import find_packages |
| 36 | +from setuptools import setup |
| 37 | +from setuptools.command.build_py import build_py |
| 38 | +PACKAGE_NAME = 'tflite-runtime' |
| 39 | +PACKAGE_VERSION = os.environ['TENSORFLOW_VERSION'] |
| 40 | +DOCLINES = __doc__.split('\n') |
| 41 | +PACKAGE = 'tflite_runtime.lite.python' |
| 42 | +TENSORFLOW_DIR = os.environ['TENSORFLOW_SRC_ROOT'] |
| 43 | + |
| 44 | +# Setup cross compiling |
| 45 | +TARGET = ( |
| 46 | + os.environ['TENSORFLOW_TARGET'] if 'TENSORFLOW_TARGET' in os.environ |
| 47 | + else None) |
| 48 | +if TARGET == 'rpi': |
| 49 | + os.environ['CXX'] = 'arm-linux-gnueabihf-g++' |
| 50 | + os.environ['CC'] = 'arm-linux-gnueabihf-g++' |
| 51 | +MAKE_CROSS_OPTIONS = ['TARGET=%s' % TARGET] if TARGET else [] |
| 52 | + |
| 53 | +RELATIVE_MAKE_DIR = os.path.join('tensorflow', 'lite', 'tools', 'make') |
| 54 | +MAKE_DIR = os.path.join(TENSORFLOW_DIR, RELATIVE_MAKE_DIR) |
| 55 | +DOWNLOADS_DIR = os.path.join(MAKE_DIR, 'downloads') |
| 56 | +RELATIVE_MAKEFILE_PATH = os.path.join(RELATIVE_MAKE_DIR, 'Makefile') |
| 57 | +DOWNLOAD_SCRIPT_PATH = os.path.join(MAKE_DIR, 'download_dependencies.sh') |
| 58 | + |
| 59 | + |
| 60 | +def make_args(target='', quiet=True): |
| 61 | + """Construct make command line.""" |
| 62 | + args = (['make', 'SHELL=/bin/bash', '-C', TENSORFLOW_DIR] |
| 63 | + + MAKE_CROSS_OPTIONS + |
| 64 | + ['-f', RELATIVE_MAKEFILE_PATH, '-j', |
| 65 | + str(multiprocessing.cpu_count())]) |
| 66 | + if quiet: |
| 67 | + args.append('--quiet') |
| 68 | + if target: |
| 69 | + args.append(target) |
| 70 | + return args |
| 71 | + |
| 72 | + |
| 73 | +def make_output(target): |
| 74 | + """Invoke make on the target and return output.""" |
| 75 | + return subprocess.check_output(make_args(target)).decode('utf-8').strip() |
| 76 | + |
| 77 | + |
| 78 | +def make(): |
| 79 | + """Invoke make to build tflite C++ sources. |
| 80 | +
|
| 81 | + Build dependencies: |
| 82 | + apt-get install swig libjpeg-dev zlib1g-dev python3-dev python3-nump |
| 83 | + """ |
| 84 | + subprocess.check_call(make_args(quiet=False)) |
| 85 | + |
| 86 | + |
| 87 | +def download_dependencies(): |
| 88 | + """Download build dependencies if haven't done yet.""" |
| 89 | + if not os.path.isdir(DOWNLOADS_DIR) or not os.listdir(DOWNLOADS_DIR): |
| 90 | + subprocess.check_call(DOWNLOAD_SCRIPT_PATH) |
| 91 | + |
| 92 | + |
| 93 | +class CustomBuildExt(build_ext, object): |
| 94 | + |
| 95 | + def run(self): |
| 96 | + download_dependencies() |
| 97 | + make() |
| 98 | + |
| 99 | + return super(CustomBuildExt, self).run() |
| 100 | + |
| 101 | + |
| 102 | +class CustomBuildPy(build_py, object): |
| 103 | + |
| 104 | + def run(self): |
| 105 | + self.run_command('build_ext') |
| 106 | + return super(CustomBuildPy, self).run() |
| 107 | + |
| 108 | + |
| 109 | +LIB_TFLITE = 'tensorflow-lite' |
| 110 | +LIB_TFLITE_DIR = make_output('libdir') |
| 111 | + |
| 112 | +ext = Extension( |
| 113 | + name='%s._interpreter_wrapper' % PACKAGE, |
| 114 | + language='c++', |
| 115 | + sources=['interpreter_wrapper/interpreter_wrapper.i', |
| 116 | + 'interpreter_wrapper/interpreter_wrapper.cc'], |
| 117 | + swig_opts=['-c++', |
| 118 | + '-I%s' % TENSORFLOW_DIR, |
| 119 | + '-module', 'interpreter_wrapper', |
| 120 | + '-outdir', '.'], |
| 121 | + extra_compile_args=['-std=c++11'], |
| 122 | + include_dirs=[TENSORFLOW_DIR, |
| 123 | + os.path.join(TENSORFLOW_DIR, 'tensorflow', 'lite', 'tools', |
| 124 | + 'pip_package'), |
| 125 | + numpy.get_include(), |
| 126 | + os.path.join(DOWNLOADS_DIR, 'flatbuffers', 'include'), |
| 127 | + os.path.join(DOWNLOADS_DIR, 'absl')], |
| 128 | + libraries=[LIB_TFLITE], |
| 129 | + library_dirs=[LIB_TFLITE_DIR]) |
| 130 | + |
| 131 | + |
| 132 | +setup( |
| 133 | + name=PACKAGE_NAME, |
| 134 | + version=PACKAGE_VERSION, |
| 135 | + description=DOCLINES[0], |
| 136 | + long_description='\n'.join(DOCLINES[2:]), |
| 137 | + url='https://www.tensorflow.org/lite/', |
| 138 | + author='Google Inc.', |
| 139 | + |
| 140 | + license='Apache 2.0', |
| 141 | + include_package_data=True, |
| 142 | + keywords='tflite tensorflow tensor machine learning', |
| 143 | + packages=find_packages(exclude=[]), |
| 144 | + ext_modules=[ext], |
| 145 | + package_dir={PACKAGE: '.'}, |
| 146 | + cmdclass={ |
| 147 | + 'build_ext': CustomBuildExt, |
| 148 | + 'build_py': CustomBuildPy, |
| 149 | + } |
| 150 | +) |
0 commit comments