chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,67 @@
# Copyright 2022 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.
ARG IMAGE
FROM ${IMAGE}
ARG PYTHON_VERSION
ARG NUMPY_VERSION
COPY update_sources.sh /
RUN /update_sources.sh
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC \
apt-get install -y \
build-essential \
software-properties-common \
zlib1g-dev \
curl \
wget \
unzip \
git && \
apt-get clean
# Install Bazel.
RUN wget https://github.com/bazelbuild/bazelisk/releases/download/v1.15.0/bazelisk-linux-amd64 \
-O /usr/local/bin/bazel && chmod +x /usr/local/bin/bazel
# Install Python packages.
RUN dpkg --add-architecture armhf
RUN dpkg --add-architecture arm64
RUN yes | add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update && \
apt-get install -y \
python$PYTHON_VERSION \
python$PYTHON_VERSION-dev \
python$PYTHON_VERSION-venv \
libpython$PYTHON_VERSION-dev \
libpython$PYTHON_VERSION-dev:armhf \
libpython$PYTHON_VERSION-dev:arm64
RUN ln -sf /usr/bin/python$PYTHON_VERSION /usr/bin/python3
RUN curl -OL https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN rm get-pip.py
RUN pip3 install --upgrade pip
RUN pip3 install numpy~=$NUMPY_VERSION setuptools pybind11
RUN ln -sf /usr/include/python$PYTHON_VERSION /usr/include/python3
RUN ln -sf /usr/local/lib/python$PYTHON_VERSION/dist-packages/numpy/core/include/numpy /usr/include/python3/numpy
RUN curl -OL https://github.com/Kitware/CMake/releases/download/v3.16.8/cmake-3.16.8-Linux-x86_64.sh
RUN mkdir /opt/cmake
RUN sh cmake-3.16.8-Linux-x86_64.sh --prefix=/opt/cmake --skip-license
RUN ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
ENV CI_BUILD_PYTHON=python$PYTHON_VERSION
ENV CROSSTOOL_PYTHON_INCLUDE_PATH=/usr/include/python$PYTHON_VERSION
COPY with_the_same_user /
@@ -0,0 +1 @@
recursive-include * *.py
@@ -0,0 +1,76 @@
# Copyright 2022 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.
# Values: debian:<version>, ubuntu:<version>
BASE_IMAGE ?= ubuntu:20.04
PYTHON_VERSION ?= 3.11
NUMPY_VERSION ?= 1.23.2
# Values: rpi, aarch64, native
TENSORFLOW_TARGET ?= native
WHEEL_PROJECT_NAME ?= tflite_runtime
# Values: according to https://www.python.org/dev/peps/pep-0440/
VERSION_SUFFIX ?=
MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
TENSORFLOW_DIR := $(MAKEFILE_DIR)/../../../..
TAG_IMAGE := "tflite-runtime-builder-$(subst :,-,$(BASE_IMAGE))"
DOCKER_PARAMS := --pid=host \
--env "CI_BUILD_USER=$(shell id -u -n)" \
--env "CI_BUILD_UID=$(shell id -u)" \
--env "CI_BUILD_GROUP=$(shell id -g -n)" \
--env "CI_BUILD_GID=$(shell id -g)" \
--env "CI_BUILD_HOME=$(TENSORFLOW_DIR)/bazel-ci_build-cache" \
--env "WHEEL_PROJECT_NAME=$(WHEEL_PROJECT_NAME)" \
--env "VERSION_SUFFIX=$(VERSION_SUFFIX)" \
--volume $(TENSORFLOW_DIR):/tensorflow \
--workdir /tensorflow
ifneq ($(WHEEL_PLATFORM_NAME),)
DOCKER_PARAMS += --env WHEEL_PLATFORM_NAME=$(WHEEL_PLATFORM_NAME)
endif
.PHONY: help \
docker-image \
docker-shell \
docker-build \
clean
help:
@echo "make docker-image -- build docker image"
@echo "make docker-shell -- run shell inside the docker image"
@echo "make docker-build -- build wheel and deb inside the docker image"
@echo "make clean -- remove wheel and deb files"
docker-image:
docker build -t $(TAG_IMAGE) \
--build-arg IMAGE=$(BASE_IMAGE) \
--build-arg PYTHON_VERSION=$(PYTHON_VERSION) \
--build-arg NUMPY_VERSION=$(NUMPY_VERSION) \
-f $(MAKEFILE_DIR)/Dockerfile.py3 $(MAKEFILE_DIR)/.
docker-shell: docker-image
mkdir -p $(TENSORFLOW_DIR)/bazel-ci_build-cache
docker run --rm --interactive --tty \
$(DOCKER_PARAMS) \
$(TAG_IMAGE) /with_the_same_user /bin/bash
docker-build: docker-image
mkdir -p $(TENSORFLOW_DIR)/bazel-ci_build-cache
docker run \
--rm --interactive $(shell tty -s && echo --tty) \
$(DOCKER_PARAMS) \
$(TAG_IMAGE) \
/with_the_same_user /bin/bash -C /tensorflow/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh $(TENSORFLOW_TARGET)
+153
View File
@@ -0,0 +1,153 @@
# Building TensorFlow Lite Standalone Pip
Many users would like to deploy TensorFlow lite interpreter and use it from
Python without requiring the rest of TensorFlow.
## Steps
To build a binary wheel run this script:
```sh
sudo apt install swig libjpeg-dev zlib1g-dev python3-dev python3-numpy
pip install numpy pybind11
sh tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh
```
That will print out some output and a .whl file. You can then install that
```sh
pip install --upgrade <wheel>
```
You can also build a wheel inside docker container using make tool. For example
the following command will cross-compile tflite-runtime package for python2.7
and python3.7 (from Debian Buster) on Raspberry Pi:
```sh
make BASE_IMAGE=debian:buster PYTHON_VERSION=2.7 TENSORFLOW_TARGET=rpi docker-build
make BASE_IMAGE=debian:buster PYTHON_VERSION=3.7 TENSORFLOW_TARGET=rpi docker-build
```
Another option is to cross-compile for python3.5 (from Debian Stretch) on ARM64
board:
```sh
make BASE_IMAGE=debian:stretch PYTHON_VERSION=3.5 TENSORFLOW_TARGET=aarch64 docker-build
```
To build for python3.6 (from Ubuntu 18.04) on x86_64 (native to the docker
image) run:
```sh
make BASE_IMAGE=ubuntu:18.04 PYTHON_VERSION=3.6 TENSORFLOW_TARGET=native docker-build
```
In addition to the wheel there is a way to build Debian package by adding
`BUILD_DEB=y` to the make command (only for python3):
```sh
make BASE_IMAGE=debian:buster PYTHON_VERSION=3.6 TENSORFLOW_TARGET=rpi BUILD_DEB=y docker-build
```
## Alternative build with Bazel (experimental)
There is another build steps to build a binary wheel which uses Bazel instead of
Makefile. You don't need to install additional dependencies.
This approach can leverage TF's `ci_build.sh` for ARM cross builds.
### Normal build for your workstation
```sh
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh
```
### Optimized build for your workstation
The output may have a compatibility issue with other machines but it gives the
best performance for your workstation.
```sh
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh native
```
### Cross build for armhf Python 3.5
```sh
tensorflow/tools/ci_build/ci_build.sh PI-PYTHON3 \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh armhf
```
### Cross build for armhf Python 3.7
```sh
tensorflow/tools/ci_build/ci_build.sh PI-PYTHON37 \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh armhf
```
### Cross build for aarch64 Python 3.5
```sh
tensorflow/tools/ci_build/ci_build.sh PI-PYTHON3 \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh aarch64
```
### Cross build for aarch64 Python 3.8
```sh
tensorflow/tools/ci_build/ci_build.sh PI-PYTHON38 \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh aarch64
```
### Cross build for aarch64 Python 3.9
```sh
tensorflow/tools/ci_build/ci_build.sh PI-PYTHON39 \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh aarch64
```
### Native build for Windows
```sh
bash tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh windows
```
## Enable TF OP support (Flex delegate)
If you want to use TF ops with Python API, you need to enable flex support.
You can build TFLite interpreter with flex ops support by providing
`--define=tflite_pip_with_flex=true` to Bazel.
Here are some examples.
### Normal build with Flex for your workstation
```sh
CUSTOM_BAZEL_FLAGS=--define=tflite_pip_with_flex=true \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh
```
### Cross build with Flex for armhf Python 3.7
```sh
CI_DOCKER_EXTRA_PARAMS="-e CUSTOM_BAZEL_FLAGS=--define=tflite_pip_with_flex=true" \
tensorflow/tools/ci_build/ci_build.sh PI-PYTHON37 \
tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh armhf
```
## Usage
Note, unlike tensorflow this will be installed to a `tflite_runtime` namespace.
You can then use the Tensorflow Lite interpreter as.
```python
from tflite_runtime.interpreter import Interpreter
interpreter = Interpreter(model_path="foo.tflite")
```
This currently works to build on Linux machines including Raspberry Pi. In
the future, cross compilation to smaller SOCs like Raspberry Pi from
bigger host will be supported.
## Caveats
* You cannot use TensorFlow Select ops, only TensorFlow Lite builtins.
* Currently custom ops and delegates cannot be registered.
@@ -0,0 +1,184 @@
#!/usr/bin/env bash
# Copyright 2020 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.
# ==============================================================================
set -ex
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PYTHON="${CI_BUILD_PYTHON:-python3}"
VERSION_SUFFIX=${VERSION_SUFFIX:-}
export TENSORFLOW_DIR="${SCRIPT_DIR}/../../../.."
TENSORFLOW_LITE_DIR="${TENSORFLOW_DIR}/tensorflow/lite"
TENSORFLOW_VERSION=$(grep "TF_VERSION = " "${TENSORFLOW_DIR}/tensorflow/tf_version.bzl" | cut -d= -f2 | sed 's/[ "-]//g')
export PACKAGE_VERSION="${TENSORFLOW_VERSION}${VERSION_SUFFIX}"
export PROJECT_NAME=${WHEEL_PROJECT_NAME:-tflite_runtime}
BUILD_DIR="${SCRIPT_DIR}/gen/tflite_pip/${PYTHON}"
TENSORFLOW_TARGET=${TENSORFLOW_TARGET:-$1}
if [ "${TENSORFLOW_TARGET}" = "rpi" ]; then
export TENSORFLOW_TARGET="armhf"
fi
export CROSSTOOL_PYTHON_INCLUDE_PATH=$(${PYTHON} -c "from sysconfig import get_paths as gp; print(gp()['include'])")
# Fix container image for cross build.
if [ ! -z "${CI_BUILD_HOME}" ] && [ `pwd` = "/workspace" ]; then
# Fix for curl build problem in 32-bit, see https://stackoverflow.com/questions/35181744/size-of-array-curl-rule-01-is-negative
if [ "${TENSORFLOW_TARGET}" = "armhf" ] && [ -f /usr/include/curl/curlbuild.h ]; then
sudo sed -i 's/define CURL_SIZEOF_LONG 8/define CURL_SIZEOF_LONG 4/g' /usr/include/curl/curlbuild.h
sudo sed -i 's/define CURL_SIZEOF_CURL_OFF_T 8/define CURL_SIZEOF_CURL_OFF_T 4/g' /usr/include/curl/curlbuild.h
fi
# The system-installed OpenSSL headers get pulled in by the latest BoringSSL
# release on this configuration, so move them before we build:
if [ -d /usr/include/openssl ]; then
sudo mv /usr/include/openssl /usr/include/openssl.original
fi
fi
# Build source tree.
rm -rf "${BUILD_DIR}" && mkdir -p "${BUILD_DIR}/tflite_runtime"
cp -r "${TENSORFLOW_LITE_DIR}/tools/pip_package/debian" \
"${TENSORFLOW_LITE_DIR}/tools/pip_package/MANIFEST.in" \
"${TENSORFLOW_LITE_DIR}/python/interpreter_wrapper" \
"${BUILD_DIR}"
cp "${TENSORFLOW_LITE_DIR}/tools/pip_package/setup_with_binary.py" "${BUILD_DIR}/setup.py"
cp "${TENSORFLOW_LITE_DIR}/python/interpreter.py" \
"${TENSORFLOW_LITE_DIR}/python/metrics/metrics_interface.py" \
"${TENSORFLOW_LITE_DIR}/python/metrics/metrics_portable.py" \
"${BUILD_DIR}/tflite_runtime"
${PYTHON} -c "import sys, json; print('__version__ = ' + json.dumps(sys.argv[1]))" "${PACKAGE_VERSION}" >> "${BUILD_DIR}/tflite_runtime/__init__.py"
${PYTHON} -c "import sys, json; print('__git_version__ = ' + json.dumps(sys.argv[1]))" "$(git -C "${TENSORFLOW_DIR}" describe)" >> "${BUILD_DIR}/tflite_runtime/__init__.py"
# Build python interpreter_wrapper.
cd "${BUILD_DIR}"
case "${TENSORFLOW_TARGET}" in
armhf)
BAZEL_FLAGS="--config=elinux_armhf
--copt=-march=armv7-a --copt=-mfpu=neon-vfpv4
--copt=-O3 --copt=-fno-tree-pre --copt=-fpermissive
--define tensorflow_mkldnn_contraction_kernel=0
--define=raspberry_pi_with_neon=true"
;;
rpi0)
BAZEL_FLAGS="--config=elinux_armhf
--copt=-march=armv6 -mfpu=vfp -mfloat-abi=hard
--copt=-O3 --copt=-fno-tree-pre --copt=-fpermissive
--define tensorflow_mkldnn_contraction_kernel=0
--define=raspberry_pi_with_neon=true"
;;
aarch64)
BAZEL_FLAGS="--config=elinux_aarch64
--define tensorflow_mkldnn_contraction_kernel=0
--copt=-O3"
;;
native)
BAZEL_FLAGS="--copt=-O3 --copt=-march=native"
;;
*)
BAZEL_FLAGS="--copt=-O3"
;;
esac
# We need to pass down the environment variable with a possible alternate Python
# include path for Python 3.x builds to work.
export CROSSTOOL_PYTHON_INCLUDE_PATH
case "${TENSORFLOW_TARGET}" in
windows)
LIBRARY_EXTENSION=".pyd"
;;
*)
LIBRARY_EXTENSION=".so"
;;
esac
bazel ${BAZEL_STARTUP_OPTIONS} build -c opt -s --config=monolithic --config=nogcp --config=nonccl \
${BAZEL_FLAGS} ${CUSTOM_BAZEL_FLAGS} //tensorflow/lite/python/interpreter_wrapper:_pywrap_tensorflow_interpreter_wrapper
cp "${TENSORFLOW_DIR}/bazel-bin/tensorflow/lite/python/interpreter_wrapper/_pywrap_tensorflow_interpreter_wrapper${LIBRARY_EXTENSION}" \
"${BUILD_DIR}/tflite_runtime"
# Bazel generates the wrapper library with r-x permissions for user.
# At least on Windows, we need write permissions to delete the file.
# Without this, setuptools fails to clean the build directory.
chmod u+w "${BUILD_DIR}/tflite_runtime/_pywrap_tensorflow_interpreter_wrapper${LIBRARY_EXTENSION}"
# Build python wheel.
cd "${BUILD_DIR}"
case "${TENSORFLOW_TARGET}" in
armhf)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-armv7l}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
rpi0)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-armv6l}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
aarch64)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-aarch64}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
*)
if [[ -n "${WHEEL_PLATFORM_NAME}" ]]; then
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
else
${PYTHON} setup.py bdist bdist_wheel
fi
;;
esac
echo "Output can be found here:"
find "${BUILD_DIR}"
# Build debian package.
if [[ "${BUILD_DEB}" != "y" ]]; then
exit 0
fi
PYTHON_VERSION=$(${PYTHON} -c "import sys;print(sys.version_info.major)")
if [[ ${PYTHON_VERSION} != 3 ]]; then
echo "Debian package can only be generated for python3." >&2
exit 1
fi
DEB_VERSION=$(dpkg-parsechangelog --show-field Version | cut -d- -f1)
if [[ "${DEB_VERSION}" != "${PACKAGE_VERSION}" ]]; then
cat << EOF > "${BUILD_DIR}/debian/changelog"
tflite-runtime (${PACKAGE_VERSION}-1) unstable; urgency=low
* Bump version to ${PACKAGE_VERSION}.
-- TensorFlow team <packages@tensorflow.org> $(date -R)
$(<"${BUILD_DIR}/debian/changelog")
EOF
fi
case "${TENSORFLOW_TARGET}" in
armhf)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armhf
;;
rpi0)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armel
;;
aarch64)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a arm64
;;
*)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d
;;
esac
cat "${BUILD_DIR}/debian/changelog"
@@ -0,0 +1,258 @@
#!/usr/bin/env bash
# Copyright 2021 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.
# ==============================================================================
set -ex
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PYTHON="${CI_BUILD_PYTHON:-python3}"
VERSION_SUFFIX=${VERSION_SUFFIX:-}
export TENSORFLOW_DIR="${SCRIPT_DIR}/../../../.."
TENSORFLOW_LITE_DIR="${TENSORFLOW_DIR}/tensorflow/lite"
TENSORFLOW_VERSION=$(grep "TF_VERSION = " "${TENSORFLOW_DIR}/tensorflow/tf_version.bzl" | cut -d= -f2 | sed 's/[ "-]//g')
IFS='.' read -r -a array <<< "$TENSORFLOW_VERSION"
TF_MAJOR=${array[0]}
TF_MINOR=${array[1]}
TF_PATCH=${array[2]}
TF_CXX_FLAGS="-DTF_MAJOR_VERSION=${TF_MAJOR} -DTF_MINOR_VERSION=${TF_MINOR} -DTF_PATCH_VERSION=${TF_PATCH} -DTF_VERSION_SUFFIX=''"
export PACKAGE_VERSION="${TENSORFLOW_VERSION}${VERSION_SUFFIX}"
export PROJECT_NAME=${WHEEL_PROJECT_NAME:-tflite_runtime}
BUILD_DIR="${SCRIPT_DIR}/gen/tflite_pip/${PYTHON}"
TENSORFLOW_TARGET=${TENSORFLOW_TARGET:-$1}
BUILD_NUM_JOBS="${BUILD_NUM_JOBS:-4}"
if [ "${TENSORFLOW_TARGET}" = "rpi" ]; then
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())")
NUMPY_INCLUDE=$(${PYTHON} -c "import numpy; print (numpy.get_include())")
export CROSSTOOL_PYTHON_INCLUDE_PATH=${PYTHON_INCLUDE}
# Fix container image for cross build.
if [ ! -z "${CI_BUILD_HOME}" ] && [ `pwd` = "/workspace" ]; then
# Fix for curl build problem in 32-bit, see https://stackoverflow.com/questions/35181744/size-of-array-curl-rule-01-is-negative
if [ "${TENSORFLOW_TARGET}" = "armhf" ] && [ -f /usr/include/curl/curlbuild.h ]; then
sudo sed -i 's/define CURL_SIZEOF_LONG 8/define CURL_SIZEOF_LONG 4/g' /usr/include/curl/curlbuild.h
sudo sed -i 's/define CURL_SIZEOF_CURL_OFF_T 8/define CURL_SIZEOF_CURL_OFF_T 4/g' /usr/include/curl/curlbuild.h
fi
# The system-installed OpenSSL headers get pulled in by the latest BoringSSL
# release on this configuration, so move them before we build:
if [ -d /usr/include/openssl ]; then
sudo mv /usr/include/openssl /usr/include/openssl.original
fi
fi
# Build source tree.
rm -rf "${BUILD_DIR}" && mkdir -p "${BUILD_DIR}/tflite_runtime"
cp -r "${TENSORFLOW_LITE_DIR}/tools/pip_package/debian" \
"${TENSORFLOW_LITE_DIR}/tools/pip_package/MANIFEST.in" \
"${TENSORFLOW_LITE_DIR}/python/interpreter_wrapper" \
"${BUILD_DIR}"
cp "${TENSORFLOW_LITE_DIR}/tools/pip_package/setup_with_binary.py" "${BUILD_DIR}/setup.py"
cp "${TENSORFLOW_LITE_DIR}/python/interpreter.py" \
"${TENSORFLOW_LITE_DIR}/python/metrics/metrics_interface.py" \
"${TENSORFLOW_LITE_DIR}/python/metrics/metrics_portable.py" \
"${BUILD_DIR}/tflite_runtime"
${PYTHON} -c "import sys, json; print('__version__ = ' + json.dumps(sys.argv[1]))" "${PACKAGE_VERSION}" >> "${BUILD_DIR}/tflite_runtime/__init__.py"
${PYTHON} -c "import sys, json; print('__git_version__ = ' + json.dumps(sys.argv[1]))" "$(git -C "${TENSORFLOW_DIR}" describe)" >> "${BUILD_DIR}/tflite_runtime/__init__.py"
# Build host tools
if [[ "${TENSORFLOW_TARGET}" != "native" ]]; then
echo "Building for host tools."
HOST_BUILD_DIR="${BUILD_DIR}/cmake_build_host"
mkdir -p "${HOST_BUILD_DIR}"
pushd "${HOST_BUILD_DIR}"
cmake "${TENSORFLOW_LITE_DIR}"
cmake --build . --verbose -j ${BUILD_NUM_JOBS} -t flatbuffers-flatc
popd
fi
# Build python interpreter_wrapper.
mkdir -p "${BUILD_DIR}/cmake_build"
cd "${BUILD_DIR}/cmake_build"
echo "Building for ${TENSORFLOW_TARGET}"
case "${TENSORFLOW_TARGET}" in
armhf_vfpv3)
eval $(${TENSORFLOW_LITE_DIR}/tools/cmake/download_toolchains.sh "${TENSORFLOW_TARGET}")
ARMCC_FLAGS="${ARMCC_FLAGS} ${TF_CXX_FLAGS} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"
cmake \
-DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
-DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
-DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_PROCESSOR=armv7 \
-DTFLITE_ENABLE_XNNPACK=OFF \
-DTFLITE_HOST_TOOLS_DIR="${HOST_BUILD_DIR}" \
"${TENSORFLOW_LITE_DIR}"
;;
armhf)
eval $(${TENSORFLOW_LITE_DIR}/tools/cmake/download_toolchains.sh "${TENSORFLOW_TARGET}")
ARMCC_FLAGS="${ARMCC_FLAGS} ${TF_CXX_FLAGS} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"
cmake \
-DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
-DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
-DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_PROCESSOR=armv7 \
-DTFLITE_ENABLE_XNNPACK=OFF \
-DTFLITE_HOST_TOOLS_DIR="${HOST_BUILD_DIR}" \
"${TENSORFLOW_LITE_DIR}"
;;
rpi0)
eval $(${TENSORFLOW_LITE_DIR}/tools/cmake/download_toolchains.sh "${TENSORFLOW_TARGET}")
ARMCC_FLAGS="${ARMCC_FLAGS} ${TF_CXX_FLAGS} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"
cmake \
-DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
-DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
-DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_PROCESSOR=armv6 \
-DTFLITE_ENABLE_XNNPACK=OFF \
-DTFLITE_HOST_TOOLS_DIR="${HOST_BUILD_DIR}" \
"${TENSORFLOW_LITE_DIR}"
;;
aarch64)
eval $(${TENSORFLOW_LITE_DIR}/tools/cmake/download_toolchains.sh "${TENSORFLOW_TARGET}")
ARMCC_FLAGS="${ARMCC_FLAGS} ${TF_CXX_FLAGS} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"
cmake \
-DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
-DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
-DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
-DXNNPACK_ENABLE_ARM_I8MM=OFF \
-DTFLITE_HOST_TOOLS_DIR="${HOST_BUILD_DIR}" \
"${TENSORFLOW_LITE_DIR}"
;;
native)
BUILD_FLAGS=${BUILD_FLAGS:-"-march=native ${TF_CXX_FLAGS} -I${PYTHON_INCLUDE} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"}
cmake \
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
"${TENSORFLOW_LITE_DIR}"
;;
*)
BUILD_FLAGS=${BUILD_FLAGS:-"${TF_CXX_FLAGS} -I${PYTHON_INCLUDE} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"}
cmake \
-DCMAKE_C_FLAGS="${BUILD_FLAGS}" \
-DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \
"${TENSORFLOW_LITE_DIR}"
;;
esac
cmake --build . --verbose -j ${BUILD_NUM_JOBS} -t _pywrap_tensorflow_interpreter_wrapper
cd "${BUILD_DIR}"
case "${TENSORFLOW_TARGET}" in
windows)
LIBRARY_EXTENSION=".pyd"
;;
*)
LIBRARY_EXTENSION=".so"
;;
esac
cp "${BUILD_DIR}/cmake_build/_pywrap_tensorflow_interpreter_wrapper${LIBRARY_EXTENSION}" \
"${BUILD_DIR}/tflite_runtime"
# Bazel generates the wrapper library with r-x permissions for user.
# At least on Windows, we need write permissions to delete the file.
# Without this, setuptools fails to clean the build directory.
chmod u+w "${BUILD_DIR}/tflite_runtime/_pywrap_tensorflow_interpreter_wrapper${LIBRARY_EXTENSION}"
# Build python wheel.
cd "${BUILD_DIR}"
case "${TENSORFLOW_TARGET}" in
armhf_vfpv3)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-armv7l}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
armhf)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-armv7l}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
rpi0)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-armv6l}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
aarch64)
WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-aarch64}"
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
;;
*)
if [[ -n "${WHEEL_PLATFORM_NAME}" ]]; then
${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \
bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME}
else
${PYTHON} setup.py bdist bdist_wheel
fi
;;
esac
echo "Output can be found here:"
find "${BUILD_DIR}/dist"
# Build debian package.
if [[ "${BUILD_DEB}" != "y" ]]; then
exit 0
fi
PYTHON_VERSION=$(${PYTHON} -c "import sys;print(sys.version_info.major)")
if [[ ${PYTHON_VERSION} != 3 ]]; then
echo "Debian package can only be generated for python3." >&2
exit 1
fi
DEB_VERSION=$(dpkg-parsechangelog --show-field Version | cut -d- -f1)
if [[ "${DEB_VERSION}" != "${PACKAGE_VERSION}" ]]; then
cat << EOF > "${BUILD_DIR}/debian/changelog"
tflite-runtime (${PACKAGE_VERSION}-1) unstable; urgency=low
* Bump version to ${PACKAGE_VERSION}.
-- TensorFlow team <packages@tensorflow.org> $(date -R)
$(<"${BUILD_DIR}/debian/changelog")
EOF
fi
case "${TENSORFLOW_TARGET}" in
armhf_vfpv3)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armhf
;;
armhf)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armhf
;;
rpi0)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armel
;;
aarch64)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a arm64
;;
*)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d
;;
esac
cat "${BUILD_DIR}/debian/changelog"
@@ -0,0 +1,11 @@
tflite-runtime (2.0.0-1) unstable; urgency=low
* Bump version to 2.0.0.
-- TensorFlow team <packages@tensorflow.org> Fri, 18 Oct 2019 13:21:05 -0700
tflite-runtime (1.14.0-1) unstable; urgency=low
* Initial release.
-- TensorFlow team <packages@tensorflow.org> Thu, 29 Aug 2019 14:34:40 -0700
@@ -0,0 +1 @@
9
@@ -0,0 +1,28 @@
Source: tflite-runtime
Maintainer: TensorFlow team <packages@tensorflow.org>
Section: python
Priority: optional
Build-Depends:
debhelper (>= 9),
dh-python,
python3-all,
python3-numpy (>= 1:1.12.1),
python3-setuptools,
python3-wheel
Standards-Version: 3.9.8
Homepage: https://www.tensorflow.org/
Package: python3-tflite-runtime
Architecture: any
Depends:
${misc:Depends},
${python3:Depends},
python3-numpy (>= 1:1.12.1),
libc6 (>= 2.14),
libgcc1 (>= 1:4.7),
libstdc++6 (>= 6)
Description: TensorFlow Lite is for mobile and embedded devices.
TensorFlow Lite is the official solution for running machine learning models on
mobile and embedded devices. It enables on-device machine learning inference
with low latency and a small binary size on Android, iOS, and other operating
systems.
@@ -0,0 +1,7 @@
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: python3-tflite-runtime
Source: https://www.tensorflow.org/
Files: *
Copyright: Copyright 2019 Google, LLC <packages@tensorflow.org>
License: Apache-2.0
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/make -f
export PYBUILD_NAME=tflite_runtime
%:
dh $@ --with python3 --buildsystem=pybuild
override_dh_shlibdeps:
@@ -0,0 +1,68 @@
# Copyright 2020 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.
# ==============================================================================
"""TensorFlow Lite is for mobile and embedded devices.
TensorFlow Lite is the official solution for running machine learning models on
mobile and embedded devices. It enables on-device machine learning inference
with low latency and a small binary size on Android, iOS, and other operating
systems.
"""
import os
from setuptools import find_packages
from setuptools import setup
PACKAGE_NAME = os.environ['PROJECT_NAME']
PACKAGE_VERSION = os.environ['PACKAGE_VERSION']
DOCLINES = __doc__.split('\n')
setup(
name=PACKAGE_NAME.replace('_', '-'),
version=PACKAGE_VERSION,
description=DOCLINES[0],
long_description='\n'.join(DOCLINES[2:]),
url='https://www.tensorflow.org/lite/',
author='Google, LLC',
author_email='packages@tensorflow.org',
license='Apache 2.0',
include_package_data=True,
has_ext_modules=lambda: True,
keywords='tflite tensorflow tensor machine learning',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Software Development',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Libraries :: Python Modules',
],
packages=find_packages(exclude=[]),
package_dir={'': '.'},
package_data={'': ['*.so', '*.pyd']},
install_requires=[
'numpy >= 1.23.2', # Better to keep sync with both TF ci_build
# and OpenCV-Python requirement.
],
)
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Copyright 2019 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.
# ==============================================================================
#!/bin/bash
. /etc/os-release
[[ "${NAME}" == "Ubuntu" ]] || exit 0
sed -i "s/deb\ /deb \[arch=amd64\]\ /g" /etc/apt/sources.list
cat <<EOT >> /etc/apt/sources.list
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME} main universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-updates main universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-security main universe
EOT
+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
# Copyright 2022 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 script is a wrapper creating the same user inside container as the one
# running the ci_build.sh outside the container. It also set the home directory
# for the user inside container to match the same absolute path as the workspace
# outside of container.
# We do this so that the bazel running inside container generate symbolic links
# and user permissions which makes sense outside of container.
# Do not run this manually. It does not make sense. It is intended to be called
# by ci_build.sh only.
set -e
COMMAND=("$@")
if ! touch /this_is_writable_file_system; then
echo "You can't write to your filesystem!"
echo "If you are in Docker you should check you do not have too many images" \
"with too many files in them. Docker has some issue with it."
exit 1
else
rm /this_is_writable_file_system
fi
if [ -n "${CI_BUILD_USER_FORCE_BADNAME}" ]; then
ADDUSER_OPTS="--force-badname"
fi
apt-get install sudo
getent group "${CI_BUILD_GID}" || addgroup ${ADDUSER_OPTS} --gid "${CI_BUILD_GID}" "${CI_BUILD_GROUP}"
getent passwd "${CI_BUILD_UID}" || adduser ${ADDUSER_OPTS} \
--gid "${CI_BUILD_GID}" --uid "${CI_BUILD_UID}" \
--gecos "${CI_BUILD_USER} (generated by with_the_same_user script)" \
--disabled-password --home "${CI_BUILD_HOME}" --quiet "${CI_BUILD_USER}"
usermod -a -G sudo "${CI_BUILD_USER}"
echo "${CI_BUILD_USER} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/90-nopasswd-sudo
if [[ "${TF_NEED_ROCM}" -eq 1 ]]; then
# ROCm requires the video group in order to use the GPU for compute. If it
# exists on the host, add it to the container.
getent group video || addgroup video && adduser "${CI_BUILD_USER}" video
fi
if [ -e /root/.bazelrc ]; then
cp /root/.bazelrc "${CI_BUILD_HOME}/.bazelrc"
chown "${CI_BUILD_UID}:${CI_BUILD_GID}" "${CI_BUILD_HOME}/.bazelrc"
fi
sudo -u "#${CI_BUILD_UID}" --preserve-env "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" \
"HOME=${CI_BUILD_HOME}" ${COMMAND[@]}