-
Notifications
You must be signed in to change notification settings - Fork 1.9k
tflite-runtime recipe #2554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
tflite-runtime recipe #2554
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5d36c40
add pybind11
RobertFlatt f2485f4
add tflite-runtime
RobertFlatt e76e12c
Update __init__.py
RobertFlatt 9a646c4
Update __init__.py
RobertFlatt ff639b9
Update __init__.py
RobertFlatt 2570890
Update __init__.py
RobertFlatt 3236e4a
Update __init__.py
RobertFlatt 08f7e54
remove apt pybind11 redundant dependency
RobertFlatt d88dace
remove redundant bdist_wheel reference
RobertFlatt 774b125
typo
RobertFlatt 6fc71c3
move 'v' in version
RobertFlatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pythonforandroid.recipe import Recipe | ||
from os.path import join | ||
|
||
|
||
class Pybind11Recipe(Recipe): | ||
|
||
version = 'v2.9.0' | ||
url = 'https://github.com/pybind/pybind11/archive/refs/tags/{version}.zip' | ||
|
||
def get_include_dir(self, arch): | ||
return join(self.get_build_dir(arch.arch), 'include') | ||
|
||
|
||
recipe = Pybind11Recipe() |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- tflite-runtime/tensorflow/lite/CMakeLists.txt 2022-01-27 17:29:49.460000000 -1000 | ||
+++ CMakeLists.txt 2022-02-21 15:03:09.568367300 -1000 | ||
@@ -220,6 +220,9 @@ | ||
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "iOS") | ||
list(FILTER TFLITE_SRCS EXCLUDE REGEX ".*minimal_logging_ios\\.cc$") | ||
endif() | ||
+if("${CMAKE_SYSTEM_NAME}" STREQUAL "Android") | ||
+ list(FILTER TFLITE_SRCS EXCLUDE REGEX ".*minimal_logging_default\\.cc$") | ||
+endif() | ||
populate_tflite_source_vars("core" TFLITE_CORE_SRCS) | ||
populate_tflite_source_vars("core/api" TFLITE_CORE_API_SRCS) | ||
populate_tflite_source_vars("c" TFLITE_C_SRCS) | ||
@@ -505,6 +508,7 @@ | ||
ruy | ||
${CMAKE_DL_LIBS} | ||
${TFLITE_TARGET_DEPENDENCIES} | ||
+ ${ANDROID_LOG_LIB} | ||
) | ||
|
||
if (NOT BUILD_SHARED_LIBS) | ||
@@ -550,6 +554,7 @@ | ||
tensorflow-lite | ||
${CMAKE_DL_LIBS} | ||
) | ||
+ | ||
target_compile_options(_pywrap_tensorflow_interpreter_wrapper | ||
PUBLIC ${TFLITE_TARGET_PUBLIC_OPTIONS} | ||
PRIVATE ${TFLITE_TARGET_PRIVATE_OPTIONS} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from pythonforandroid.recipe import PythonRecipe, current_directory,\ | ||
shprint, info_main, warning | ||
from pythonforandroid.logger import error | ||
from os.path import join | ||
import sh | ||
|
||
|
||
class TFLiteRuntimeRecipe(PythonRecipe): | ||
|
||
############################################################### | ||
# | ||
# tflite-runtime README: | ||
# https://github.com/Android-for-Python/c4k_tflite_example/blob/main/README.md | ||
# | ||
# Recipe build references: | ||
# https://developer.android.com/ndk/guides/cmake | ||
# https://developer.android.com/ndk/guides/cpu-arm-neon#cmake | ||
# https://www.tensorflow.org/lite/guide/build_cmake | ||
# https://www.tensorflow.org/lite/guide/build_cmake_arm | ||
# | ||
# Tested using cmake 3.16.3 probably requires cmake >= 3.13 | ||
# | ||
# THIS RECIPE DOES NOT BUILD x86_64, USE X86 FOR AN EMULATOR | ||
# | ||
############################################################### | ||
|
||
version = '2.8.0' | ||
url = 'https://github.com/tensorflow/tensorflow/archive/refs/tags/v{version}.zip' | ||
depends = ['pybind11', 'numpy'] | ||
patches = ['CMakeLists.patch', 'build_with_cmake.patch'] | ||
site_packages_name = 'tflite-runtime' | ||
call_hostpython_via_targetpython = False | ||
|
||
def build_arch(self, arch): | ||
if arch.arch == 'x86_64': | ||
warning("******** tflite-runtime x86_64 will not be built *******") | ||
warning("Expect one of these app run time error messages:") | ||
warning("ModuleNotFoundError: No module named 'tensorflow'") | ||
warning("ModuleNotFoundError: No module named 'tflite_runtime'") | ||
warning("Use x86 not x86_64") | ||
return | ||
|
||
env = self.get_recipe_env(arch) | ||
|
||
# Directories | ||
root_dir = self.get_build_dir(arch.arch) | ||
script_dir = join(root_dir, | ||
'tensorflow', 'lite', 'tools', 'pip_package') | ||
build_dir = join(script_dir, 'gen', 'tflite_pip', 'python3') | ||
|
||
# Includes | ||
python_include_dir = self.ctx.python_recipe.include_root(arch.arch) | ||
pybind11_recipe = self.get_recipe('pybind11', self.ctx) | ||
pybind11_include_dir = pybind11_recipe.get_include_dir(arch) | ||
numpy_include_dir = join(self.ctx.get_site_packages_dir(arch), | ||
'numpy', 'core', 'include') | ||
includes = ' -I' + python_include_dir +\ | ||
' -I' + numpy_include_dir +\ | ||
' -I' + pybind11_include_dir | ||
|
||
# Scripts | ||
build_script = join(script_dir, 'build_pip_package_with_cmake.sh') | ||
toolchain = join(self.ctx.ndk_dir, | ||
'build', 'cmake', 'android.toolchain.cmake') | ||
|
||
# Build | ||
######## | ||
with current_directory(root_dir): | ||
env.update({ | ||
'TENSORFLOW_TARGET': 'android', | ||
'CMAKE_TOOLCHAIN_FILE': toolchain, | ||
'ANDROID_PLATFORM': str(self.ctx.ndk_api), | ||
'ANDROID_ABI': arch.arch, | ||
'WRAPPER_INCLUDES': includes, | ||
'CMAKE_SHARED_LINKER_FLAGS': env['LDFLAGS'], | ||
}) | ||
|
||
try: | ||
info_main('tflite-runtime is building...') | ||
info_main('Expect this to take at least 5 minutes...') | ||
cmd = sh.Command(build_script) | ||
cmd(_env=env) | ||
except sh.ErrorReturnCode as e: | ||
error(str(e.stderr)) | ||
exit(1) | ||
|
||
# Install | ||
########## | ||
info_main('Installing tflite-runtime into site-packages') | ||
with current_directory(build_dir): | ||
hostpython = sh.Command(self.hostpython_location) | ||
install_dir = self.ctx.get_python_install_dir(arch.arch) | ||
env['PACKAGE_VERSION'] = self.version | ||
shprint(hostpython, 'setup.py', 'install', '-O2', | ||
'--root={}'.format(install_dir), | ||
'--install-lib=.', | ||
_env=env) | ||
|
||
|
||
recipe = TFLiteRuntimeRecipe() |
48 changes: 48 additions & 0 deletions
48
pythonforandroid/recipes/tflite-runtime/build_with_cmake.patch
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- tflite-runtime/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh 2022-01-22 08:57:16.000000000 -1000 | ||
+++ build_pip_package_with_cmake.sh 2022-03-02 18:19:05.185550500 -1000 | ||
@@ -28,7 +28,7 @@ | ||
export TENSORFLOW_TARGET="armhf" | ||
fi | ||
PYTHON_INCLUDE=$(${PYTHON} -c "from sysconfig import get_paths as gp; print(gp()['include'])") | ||
-PYBIND11_INCLUDE=$(${PYTHON} -c "import pybind11; print (pybind11.get_include())") | ||
+# PYBIND11_INCLUDE=$(${PYTHON} -c "import pybind11; print (pybind11.get_include())") | ||
export CROSSTOOL_PYTHON_INCLUDE_PATH=${PYTHON_INCLUDE} | ||
|
||
# Fix container image for cross build. | ||
@@ -58,7 +58,7 @@ | ||
"${TENSORFLOW_LITE_DIR}/python/metrics/metrics_portable.py" \ | ||
"${BUILD_DIR}/tflite_runtime" | ||
echo "__version__ = '${PACKAGE_VERSION}'" >> "${BUILD_DIR}/tflite_runtime/__init__.py" | ||
-echo "__git_version__ = '$(git -C "${TENSORFLOW_DIR}" describe)'" >> "${BUILD_DIR}/tflite_runtime/__init__.py" | ||
+echo "__git_version__ = '${PACKAGE_VERSION}'" >> "${BUILD_DIR}/tflite_runtime/__init__.py" | ||
|
||
# Build python interpreter_wrapper. | ||
mkdir -p "${BUILD_DIR}/cmake_build" | ||
@@ -111,6 +111,18 @@ | ||
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \ | ||
"${TENSORFLOW_LITE_DIR}" | ||
;; | ||
+ android) | ||
+ BUILD_FLAGS=${BUILD_FLAGS:-"${WRAPPER_INCLUDES}"} | ||
+ cmake \ | ||
+ -DCMAKE_SYSTEM_NAME=Android \ | ||
+ -DANDROID_ARM_NEON=ON \ | ||
+ -DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \ | ||
+ -DCMAKE_SHARED_LINKER_FLAGS="${CMAKE_SHARED_LINKER_FLAGS}" \ | ||
+ -DCMAKE_TOOLCHAIN_FILE="${CMAKE_TOOLCHAIN_FILE}" \ | ||
+ -DANDROID_PLATFORM="${ANDROID_PLATFORM}" \ | ||
+ -DANDROID_ABI="${ANDROID_ABI}" \ | ||
+ "${TENSORFLOW_LITE_DIR}" | ||
+ ;; | ||
*) | ||
BUILD_FLAGS=${BUILD_FLAGS:-"-I${PYTHON_INCLUDE} -I${PYBIND11_INCLUDE}"} | ||
cmake \ | ||
@@ -162,7 +174,7 @@ | ||
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \ | ||
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME} | ||
else | ||
- ${PYTHON} setup.py bdist bdist_wheel | ||
+ ${PYTHON} setup.py bdist | ||
fi | ||
;; | ||
esac |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.