Skip to content

Commit

Permalink
lite: Update tflite_runtime builds
Browse files Browse the repository at this point in the history
Switch to Makefile method which is easy to maintain and less dependent to TensorFlow scripts.

PiperOrigin-RevId: 427943932
Change-Id: I0aff1caaf34a890566ad8a745e8a1d417828c20a
  • Loading branch information
terryheo committed Feb 12, 2022
1 parent 2962417 commit dbd5cef
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 107 deletions.
32 changes: 11 additions & 21 deletions tensorflow/lite/g3doc/guide/build_cmake_pip.md
Expand Up @@ -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
Expand All @@ -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 <container> \
tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh <target>
make -C tensorflow/lite/tools/pip_package docker-build \
TENSORFLOW_TARGET=<target> PYTHON_VERSION=<python3 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

Expand All @@ -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?
Expand Down
46 changes: 0 additions & 46 deletions tensorflow/lite/tools/pip_package/Dockerfile

This file was deleted.

32 changes: 25 additions & 7 deletions tensorflow/lite/tools/pip_package/Dockerfile.py3
@@ -1,31 +1,44 @@
# 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

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 \
Expand All @@ -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 /
69 changes: 40 additions & 29 deletions 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:<version>, ubuntu:<version>
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 \
Expand All @@ -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
/with_the_same_user /bin/bash -C /tensorflow/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh $(TENSORFLOW_TARGET)
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions tensorflow/lite/tools/pip_package/setup_with_binary.py
Expand Up @@ -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')

Expand All @@ -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',
Expand Down
65 changes: 65 additions & 0 deletions 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[@]}

0 comments on commit dbd5cef

Please sign in to comment.