From dbd5cefcf8d304cd8b8ee2e8fe47ec4486a30d37 Mon Sep 17 00:00:00 2001 From: Terry Heo Date: Fri, 11 Feb 2022 01:04:11 -0800 Subject: [PATCH] lite: Update tflite_runtime builds Switch to Makefile method which is easy to maintain and less dependent to TensorFlow scripts. PiperOrigin-RevId: 427943932 Change-Id: I0aff1caaf34a890566ad8a745e8a1d417828c20a --- .../lite/g3doc/guide/build_cmake_pip.md | 32 +++------ tensorflow/lite/tools/pip_package/Dockerfile | 46 ------------- .../lite/tools/pip_package/Dockerfile.py3 | 32 +++++++-- tensorflow/lite/tools/pip_package/Makefile | 69 +++++++++++-------- .../build_pip_package_with_bazel.sh | 1 + .../build_pip_package_with_cmake.sh | 1 + .../tools/pip_package/setup_with_binary.py | 6 +- .../lite/tools/pip_package/with_the_same_user | 65 +++++++++++++++++ 8 files changed, 145 insertions(+), 107 deletions(-) delete mode 100644 tensorflow/lite/tools/pip_package/Dockerfile create mode 100755 tensorflow/lite/tools/pip_package/with_the_same_user diff --git a/tensorflow/lite/g3doc/guide/build_cmake_pip.md b/tensorflow/lite/g3doc/guide/build_cmake_pip.md index f6d000109fb079..76e710c5b4abfa 100644 --- a/tensorflow/lite/g3doc/guide/build_cmake_pip.md +++ b/tensorflow/lite/g3doc/guide/build_cmake_pip.md @@ -23,7 +23,7 @@ PYTHON=python3 tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh ``` **Note:** If you have multiple Python interpreters available, specify the exact -Python version with `PYTHON` variable. (Currently, it supports Python 3.5 or +Python version with `PYTHON` variable. (Currently, it supports Python 3.7 or higher) ## ARM cross compilation @@ -32,26 +32,16 @@ For ARM cross compilation, it's recommended to use Docker since it makes easier to setup cross build environment. Also you needs a `target` option to figure out the target architecture. -There is a helper script `tensorflow/tools/ci_build/ci_build.sh` available to -invoke a build command using a pre-defined Docker container. On a Docker host -machine, you can run a build command with the `container` name and the `target` -name as followings. +There is a helper tool in Makefile `tensorflow/lite/tools/pip_package/Makefile` +available to invoke a build command using a pre-defined Docker container. On a +Docker host machine, you can run a build command as followings. ```sh -tensorflow/tools/ci_build/ci_build.sh \ - tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh +make -C tensorflow/lite/tools/pip_package docker-build \ + TENSORFLOW_TARGET= PYTHON_VERSION= ``` -### Available Docker containers - -You need to select ARM cross build container for your target Python interpreter -version. Here is the list of supported containers. - -Container | Supported Python version ------------ | ------------------------ -PI-PYTHON37 | Python 3.7 -PI-PYTHON38 | Python 3.8 -PI-PYTHON39 | Python 3.9 +**Note:** Python version 3.7 or higher is supported. ### Available target names @@ -74,15 +64,15 @@ Here are some example commands you can use. #### armhf target for Python 3.7 ```sh -tensorflow/tools/ci_build/ci_build.sh PI-PYTHON37 \ - tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh armhf +make -C tensorflow/lite/tools/pip_package docker-build \ + TENSORFLOW_TARGET=armhf PYTHON_VERSION=3.7 ``` #### aarch64 target for Python 3.8 ```sh -tensorflow/tools/ci_build/ci_build.sh PI-PYTHON38 \ - tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh aarch64 +make -C tensorflow/lite/tools/pip_package docker-build \ + TENSORFLOW_TARGET=aarch64 PYTHON_VERSION=3.8 ``` #### How to use a custom toolchain? diff --git a/tensorflow/lite/tools/pip_package/Dockerfile b/tensorflow/lite/tools/pip_package/Dockerfile deleted file mode 100644 index 9059f81cc85bcf..00000000000000 --- a/tensorflow/lite/tools/pip_package/Dockerfile +++ /dev/null @@ -1,46 +0,0 @@ -ARG IMAGE -FROM ${IMAGE} - -COPY update_sources.sh / -RUN /update_sources.sh - -RUN dpkg --add-architecture armhf -RUN dpkg --add-architecture arm64 -RUN apt-get update && \ - apt-get install -y \ - debhelper \ - dh-python \ - python-all \ - python-setuptools \ - python-wheel \ - python-numpy \ - python-pip \ - pybind11-dev \ - libpython-dev \ - libpython-dev:armhf \ - libpython-dev:arm64 \ - python3-all \ - python3-setuptools \ - python3-wheel \ - python3-numpy \ - python3-pip \ - libpython3-dev \ - libpython3-dev:armhf \ - libpython3-dev:arm64 \ - crossbuild-essential-armhf \ - crossbuild-essential-arm64 \ - zlib1g-dev \ - zlib1g-dev:armhf \ - zlib1g-dev:arm64 \ - curl \ - unzip \ - git && \ - apt-get clean -RUN pip install pip --upgrade -RUN pip install pybind11 -RUN pip3 install pip --upgrade -RUN pip3 install pybind11 -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 diff --git a/tensorflow/lite/tools/pip_package/Dockerfile.py3 b/tensorflow/lite/tools/pip_package/Dockerfile.py3 index da34f2d39ce242..664f713e4ead44 100644 --- a/tensorflow/lite/tools/pip_package/Dockerfile.py3 +++ b/tensorflow/lite/tools/pip_package/Dockerfile.py3 @@ -1,3 +1,17 @@ +# 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 @@ -5,27 +19,26 @@ ARG PYTHON_VERSION COPY update_sources.sh / RUN /update_sources.sh -RUN dpkg --add-architecture armhf -RUN dpkg --add-architecture arm64 RUN apt-get update && \ apt-get install -y \ + build-essential \ software-properties-common \ - debhelper \ - crossbuild-essential-armhf \ - crossbuild-essential-arm64 \ zlib1g-dev \ - zlib1g-dev:armhf \ - zlib1g-dev:arm64 \ curl \ unzip \ git && \ apt-get clean +RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata +# 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 \ python$PYTHON_VERSION-distutils \ libpython$PYTHON_VERSION-dev \ libpython$PYTHON_VERSION-dev:armhf \ @@ -42,3 +55,8 @@ RUN curl -OL https://github.com/Kitware/CMake/releases/download/v3.16.8/cmake-3. 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 / diff --git a/tensorflow/lite/tools/pip_package/Makefile b/tensorflow/lite/tools/pip_package/Makefile index 4cec686858c2b8..aed48aca5383f2 100644 --- a/tensorflow/lite/tools/pip_package/Makefile +++ b/tensorflow/lite/tools/pip_package/Makefile @@ -1,19 +1,45 @@ +# 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:, ubuntu: -BASE_IMAGE ?= debian:buster -# Values: python, python3 -PYTHON ?= python3 +BASE_IMAGE ?= ubuntu:18.04 +PYTHON_VERSION ?= 3.9 # Values: rpi, aarch64, native TENSORFLOW_TARGET ?= native -# Values: n, y -BUILD_DEB ?= n +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)/../../../.. -OUT_DIR := $(CURDIR)/out/$(PYTHON)/$(subst :,-,$(BASE_IMAGE)) 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 \ @@ -27,33 +53,18 @@ help: @echo "make clean -- remove wheel and deb files" docker-image: -ifeq ($(BASE_IMAGE),ubuntu:16.04) - docker build -t $(TAG_IMAGE) --build-arg IMAGE=$(BASE_IMAGE) --build-arg PYTHON_VERSION=3.8 -f Dockerfile.py3 . -else - docker build -t $(TAG_IMAGE) --build-arg IMAGE=$(BASE_IMAGE) . -endif + docker build -t $(TAG_IMAGE) --build-arg IMAGE=$(BASE_IMAGE) --build-arg PYTHON_VERSION=$(PYTHON_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 \ - --volume $(TENSORFLOW_DIR):/tensorflow \ - --workdir /tensorflow \ - $(TAG_IMAGE) + $(DOCKER_PARAMS) \ + $(TAG_IMAGE) /with_the_same_user /bin/bash docker-build: docker-image - mkdir -p $(OUT_DIR) - docker run --user $(shell id -u):$(shell id -g) \ + mkdir -p $(TENSORFLOW_DIR)/bazel-ci_build-cache + docker run \ --rm --interactive $(shell tty -s && echo --tty) \ - --env "PYTHON=$(PYTHON)" \ - --env "TENSORFLOW_TARGET=$(TENSORFLOW_TARGET)" \ - --env "BUILD_DEB=$(BUILD_DEB)" \ - --env "VERSION_SUFFIX=$(VERSION_SUFFIX)" \ - --volume $(TENSORFLOW_DIR):/tensorflow \ - --volume $(OUT_DIR):/out \ + $(DOCKER_PARAMS) \ $(TAG_IMAGE) \ - /bin/bash -c "/tensorflow/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh && \ - (cp /tensorflow/tensorflow/lite/tools/pip_package/gen/tflite_pip/*.deb \ - /tensorflow/tensorflow/lite/tools/pip_package/gen/tflite_pip/${PYTHON}/dist/{*.whl,*.tar.gz} \ - /out 2>/dev/null || true)" - -clean: - rm -rf $(CURDIR)/out \ No newline at end of file + /with_the_same_user /bin/bash -C /tensorflow/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh $(TENSORFLOW_TARGET) diff --git a/tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh b/tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh index cafb8540264866..9026c3db2c7dbe 100755 --- a/tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh +++ b/tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh @@ -22,6 +22,7 @@ export TENSORFLOW_DIR="${SCRIPT_DIR}/../../../.." 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}" +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 diff --git a/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh b/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh index 35eb3a917fb921..f20765164ead82 100755 --- a/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh +++ b/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh @@ -22,6 +22,7 @@ export TENSORFLOW_DIR="${SCRIPT_DIR}/../../../.." 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}" +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 diff --git a/tensorflow/lite/tools/pip_package/setup_with_binary.py b/tensorflow/lite/tools/pip_package/setup_with_binary.py index 52d8fd4c96dbe1..58f197e773c963 100644 --- a/tensorflow/lite/tools/pip_package/setup_with_binary.py +++ b/tensorflow/lite/tools/pip_package/setup_with_binary.py @@ -24,7 +24,7 @@ from setuptools import find_packages from setuptools import setup -PACKAGE_NAME = 'tflite_runtime' +PACKAGE_NAME = os.environ['PROJECT_NAME'] PACKAGE_VERSION = os.environ['PACKAGE_VERSION'] DOCLINES = __doc__.split('\n') @@ -47,11 +47,9 @@ 'Intended Audience :: Science/Research', 'License :: OSI Approved :: Apache Software License', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', 'Topic :: Scientific/Engineering', 'Topic :: Scientific/Engineering :: Mathematics', 'Topic :: Scientific/Engineering :: Artificial Intelligence', diff --git a/tensorflow/lite/tools/pip_package/with_the_same_user b/tensorflow/lite/tools/pip_package/with_the_same_user new file mode 100755 index 00000000000000..dd64195739d16b --- /dev/null +++ b/tensorflow/lite/tools/pip_package/with_the_same_user @@ -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[@]}