Update tflite-runtime build scripts

PiperOrigin-RevId: 276629962
Change-Id: I42c5e05361660297c78f1717d9021134abe6f0c2
This commit is contained in:
Dmitry Kovalev 2019-10-24 22:12:03 -07:00 committed by TensorFlower Gardener
parent 087b39e079
commit bd422caa62
3 changed files with 72 additions and 47 deletions

View File

@ -6,6 +6,8 @@ PYTHON ?= python3
TENSORFLOW_TARGET ?= native TENSORFLOW_TARGET ?= native
# Values: n, y # Values: n, y
BUILD_DEB ?= n BUILD_DEB ?= n
# Values: according to https://www.python.org/dev/peps/pep-0440/
VERSION_SUFFIX ?=
MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
TENSORFLOW_DIR := $(MAKEFILE_DIR)/../../../.. TENSORFLOW_DIR := $(MAKEFILE_DIR)/../../../..
@ -20,23 +22,28 @@ TAG_IMAGE := "tflite-runtime-builder-$(subst :,-,$(BASE_IMAGE))"
help: help:
@echo "make docker-image -- build docker image" @echo "make docker-image -- build docker image"
@echo "make docker-shell -- run shell inside the docker image" @echo "make docker-shell -- run shell inside the docker image"
@echo "make docker-build -- build wheel inside the docker image" @echo "make docker-build -- build wheel and deb inside the docker image"
@echo "make clean -- remove built wheel files" @echo "make clean -- remove wheel and deb files"
docker-image: docker-image:
docker build -t $(TAG_IMAGE) --build-arg IMAGE=$(BASE_IMAGE) . docker build -t $(TAG_IMAGE) --build-arg IMAGE=$(BASE_IMAGE) .
docker-shell: docker-image docker-shell: docker-image
docker run --rm -it -v $(TENSORFLOW_DIR):/tensorflow --workdir /tensorflow $(TAG_IMAGE) docker run --rm --interactive --tty \
--volume $(TENSORFLOW_DIR):/tensorflow \
--workdir /tensorflow \
$(TAG_IMAGE)
docker-build: docker-image docker-build: docker-image
docker run \ docker run --user $(shell id -u):$(shell id -g) \
-e "PYTHON=$(PYTHON)" \ --rm --interactive $(shell tty -s && echo --tty) \
-e "TENSORFLOW_TARGET=$(TENSORFLOW_TARGET)" \ --env "PYTHON=$(PYTHON)" \
-e "BUILD_DEB=$(BUILD_DEB)" \ --env "TENSORFLOW_TARGET=$(TENSORFLOW_TARGET)" \
-v $(TENSORFLOW_DIR):/tensorflow \ --env "BUILD_DEB=$(BUILD_DEB)" \
-v $(CURDIR):/out \ --env "VERSION_SUFFIX=$(VERSION_SUFFIX)" \
--rm -it $(TAG_IMAGE) \ --volume $(TENSORFLOW_DIR):/tensorflow \
--volume $(CURDIR):/out \
$(TAG_IMAGE) \
/bin/bash -c "tensorflow/tensorflow/lite/tools/pip_package/build_pip_package.sh && \ /bin/bash -c "tensorflow/tensorflow/lite/tools/pip_package/build_pip_package.sh && \
(cp /tmp/tflite_pip/*.deb /tmp/tflite_pip/$(PYTHON)/dist/*.whl /out 2>/dev/null || true)" (cp /tmp/tflite_pip/*.deb /tmp/tflite_pip/$(PYTHON)/dist/*.whl /out 2>/dev/null || true)"

View File

@ -13,38 +13,43 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================== # ==============================================================================
# TODO(dkovalev): b/140445440 -- Implement test coverage for this script.
set -e set -e
set -x
PYTHON="${PYTHON:-python3}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export TENSORFLOW_SRC_ROOT="${SCRIPT_DIR}/../../../.." PYTHON="${PYTHON:-python3}"
export TENSORFLOW_VERSION=$(grep "_VERSION = " "${TENSORFLOW_SRC_ROOT}/tensorflow/tools/pip_package/setup.py" | cut -d'=' -f 2 | sed "s/[ '-]//g"); VERSION_SUFFIX=${VERSION_SUFFIX:-}
TFLITE_ROOT="${TENSORFLOW_SRC_ROOT}/tensorflow/lite" export TENSORFLOW_DIR="${SCRIPT_DIR}/../../../.."
BUILD_ROOT="/tmp/tflite_pip/${PYTHON}" TENSORFLOW_LITE_DIR="${TENSORFLOW_DIR}/tensorflow/lite"
TENSORFLOW_VERSION=$(grep "_VERSION = " "${TENSORFLOW_DIR}/tensorflow/tools/pip_package/setup.py" | cut -d= -f2 | sed "s/[ '-]//g")
export PACKAGE_VERSION="${TENSORFLOW_VERSION}${VERSION_SUFFIX}"
BUILD_DIR="/tmp/tflite_pip/${PYTHON}"
# Build source tree. # Build source tree.
rm -rf "${BUILD_ROOT}" rm -rf "${BUILD_DIR}" && mkdir -p "${BUILD_DIR}/tflite_runtime"
mkdir -p "${BUILD_ROOT}/tflite_runtime" cp -r "${TENSORFLOW_LITE_DIR}/tools/pip_package/debian" \
cp -r "${TFLITE_ROOT}/tools/pip_package/debian" \ "${TENSORFLOW_LITE_DIR}/tools/pip_package/setup.py" \
"${TFLITE_ROOT}/python/interpreter_wrapper" \ "${TENSORFLOW_LITE_DIR}/tools/pip_package/MANIFEST.in" \
"${TFLITE_ROOT}/tools/pip_package/setup.py" \ "${TENSORFLOW_LITE_DIR}/python/interpreter_wrapper" \
"${TFLITE_ROOT}/tools/pip_package/MANIFEST.in" \ "${BUILD_DIR}"
"${BUILD_ROOT}" cp "${TENSORFLOW_LITE_DIR}/python/interpreter.py" \
cp "${TFLITE_ROOT}/python/interpreter.py" \ "${BUILD_DIR}/tflite_runtime"
"${BUILD_ROOT}/tflite_runtime" echo "__version__ = '${PACKAGE_VERSION}'" >> "${BUILD_DIR}/tflite_runtime/__init__.py"
echo "__version__ = '${TENSORFLOW_VERSION}'" > "${BUILD_ROOT}/tflite_runtime/__init__.py" echo "__git_version__ = '$(git -C "${TENSORFLOW_DIR}" describe)'" >> "${BUILD_DIR}/tflite_runtime/__init__.py"
# Build python wheel. # Build python wheel.
cd "${BUILD_ROOT}" cd "${BUILD_DIR}"
if [[ "${TENSORFLOW_TARGET}" == "rpi" ]]; then case "${TENSORFLOW_TARGET}" in
rpi)
${PYTHON} setup.py bdist_wheel --plat-name=linux-armv7l ${PYTHON} setup.py bdist_wheel --plat-name=linux-armv7l
elif [[ "${TENSORFLOW_TARGET}" == "aarch64" ]]; then ;;
aarch64)
${PYTHON} setup.py bdist_wheel --plat-name=linux-aarch64 ${PYTHON} setup.py bdist_wheel --plat-name=linux-aarch64
else ;;
*)
${PYTHON} setup.py bdist_wheel ${PYTHON} setup.py bdist_wheel
fi ;;
esac
# Build debian package. # Build debian package.
if [[ "${BUILD_DEB}" != "y" ]]; then if [[ "${BUILD_DEB}" != "y" ]]; then
@ -58,15 +63,28 @@ if [[ ${PYTHON_VERSION} != 3 ]]; then
fi fi
DEB_VERSION=$(dpkg-parsechangelog --show-field Version | cut -d- -f1) DEB_VERSION=$(dpkg-parsechangelog --show-field Version | cut -d- -f1)
if [[ "${DEB_VERSION}" != "${TENSORFLOW_VERSION}" ]]; then if [[ "${DEB_VERSION}" != "${PACKAGE_VERSION}" ]]; then
echo "Debian package version (${DEB_VERSION}) doesn't match TensorFlow version (${TENSORFLOW_VERSION})" >&2 cat << EOF > "${BUILD_DIR}/debian/changelog"
exit 1 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 fi
if [[ "${TENSORFLOW_TARGET}" == "rpi" ]]; then case "${TENSORFLOW_TARGET}" in
rpi)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armhf dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armhf
elif [[ "${TENSORFLOW_TARGET}" == "aarch64" ]]; then ;;
aarch64)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a arm64 dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a arm64
else ;;
*)
dpkg-buildpackage -b -rfakeroot -us -uc -tc -d dpkg-buildpackage -b -rfakeroot -us -uc -tc -d
fi ;;
esac
cat "${BUILD_DIR}/debian/changelog"

View File

@ -36,9 +36,9 @@ from setuptools import find_packages
from setuptools import setup from setuptools import setup
from setuptools.command.build_py import build_py from setuptools.command.build_py import build_py
PACKAGE_NAME = 'tflite_runtime' PACKAGE_NAME = 'tflite_runtime'
PACKAGE_VERSION = os.environ['TENSORFLOW_VERSION'] PACKAGE_VERSION = os.environ['PACKAGE_VERSION']
DOCLINES = __doc__.split('\n') DOCLINES = __doc__.split('\n')
TENSORFLOW_DIR = os.environ['TENSORFLOW_SRC_ROOT'] TENSORFLOW_DIR = os.environ['TENSORFLOW_DIR']
# Setup cross compiling # Setup cross compiling
TARGET = os.environ.get('TENSORFLOW_TARGET', None) TARGET = os.environ.get('TENSORFLOW_TARGET', None)