From 238dcdfdee502ef55b135fb4ac5a89bdd6a5238b Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Thu, 18 Jul 2019 21:30:11 -0700 Subject: [PATCH] Add docker-based build system to cross-compile tflite_runtime wheels. PiperOrigin-RevId: 258903014 --- .../lite/tools/pip_package/Dockerfile.debian | 28 +++++++++++++ .../lite/tools/pip_package/Dockerfile.ubuntu | 31 ++++++++++++++ tensorflow/lite/tools/pip_package/Makefile | 42 +++++++++++++++++++ tensorflow/lite/tools/pip_package/README.md | 20 +++++++++ .../tools/pip_package/build_pip_package.sh | 0 .../lite/tools/pip_package/update_sources.sh | 23 ++++++++++ 6 files changed, 144 insertions(+) create mode 100644 tensorflow/lite/tools/pip_package/Dockerfile.debian create mode 100644 tensorflow/lite/tools/pip_package/Dockerfile.ubuntu create mode 100644 tensorflow/lite/tools/pip_package/Makefile mode change 100644 => 100755 tensorflow/lite/tools/pip_package/build_pip_package.sh create mode 100755 tensorflow/lite/tools/pip_package/update_sources.sh diff --git a/tensorflow/lite/tools/pip_package/Dockerfile.debian b/tensorflow/lite/tools/pip_package/Dockerfile.debian new file mode 100644 index 00000000000..7cb678e5215 --- /dev/null +++ b/tensorflow/lite/tools/pip_package/Dockerfile.debian @@ -0,0 +1,28 @@ +ARG VERSION +FROM debian:${VERSION} + +RUN dpkg --add-architecture armhf +RUN dpkg --add-architecture arm64 +RUN apt-get update && apt-get install -y \ + python \ + python-setuptools \ + python-wheel \ + python-numpy \ + libpython-dev \ + libpython-dev:armhf \ + libpython-dev:arm64 \ + python3 \ + python3-setuptools \ + python3-wheel \ + python3-numpy \ + libpython3-dev \ + libpython3-dev:armhf \ + libpython3-dev:arm64 \ + crossbuild-essential-armhf \ + crossbuild-essential-arm64 \ + zlib1g-dev \ + zlib1g-dev:armhf \ + zlib1g-dev:arm64 \ + swig \ + curl \ + git diff --git a/tensorflow/lite/tools/pip_package/Dockerfile.ubuntu b/tensorflow/lite/tools/pip_package/Dockerfile.ubuntu new file mode 100644 index 00000000000..78a0e195898 --- /dev/null +++ b/tensorflow/lite/tools/pip_package/Dockerfile.ubuntu @@ -0,0 +1,31 @@ +ARG VERSION +FROM ubuntu:${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 \ + python \ + python-setuptools \ + python-wheel \ + python-numpy \ + libpython-dev \ + libpython-dev:armhf \ + libpython-dev:arm64 \ + python3 \ + python3-setuptools \ + python3-wheel \ + python3-numpy \ + libpython3-dev \ + libpython3-dev:armhf \ + libpython3-dev:arm64 \ + crossbuild-essential-armhf \ + crossbuild-essential-arm64 \ + zlib1g-dev \ + zlib1g-dev:armhf \ + zlib1g-dev:arm64 \ + swig \ + curl \ + git diff --git a/tensorflow/lite/tools/pip_package/Makefile b/tensorflow/lite/tools/pip_package/Makefile new file mode 100644 index 00000000000..b19cc09dc2b --- /dev/null +++ b/tensorflow/lite/tools/pip_package/Makefile @@ -0,0 +1,42 @@ +# Values: debian:, ubuntu: +BASE_IMAGE ?= debian:buster +# Values: python, python3 +PYTHON ?= python3 +# Values: rpi, aarch64, native +TENSORFLOW_TARGET ?= native + +ROOT_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) +TENSORFLOW_DIR := $(ROOT_DIR)/../../../.. +NAME := $(word 1, $(subst :, ,$(BASE_IMAGE))) +VERSION := $(word 2, $(subst :, ,$(BASE_IMAGE))) +TAG_IMAGE := "tflite-builder-$(BASE_IMAGE)" + +.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 inside the docker image" + @echo "make clean -- remove built wheel files" + +docker-image: + docker build -t $(TAG_IMAGE) --build-arg VERSION=$(VERSION) -f Dockerfile.$(NAME) . + +docker-shell: docker-image + docker run --rm -it $(TAG_IMAGE) + +docker-build: docker-image + docker run \ + -e "PYTHON=$(PYTHON)" \ + -e "TENSORFLOW_TARGET=$(TENSORFLOW_TARGET)" \ + -v $(TENSORFLOW_DIR):/tensorflow \ + -v $(CURDIR):/out \ + --rm -it $(TAG_IMAGE) \ + /bin/bash -c "bash /tensorflow/tensorflow/lite/tools/pip_package/build_pip_package.sh && cp /tmp/tflite_pip/$(PYTHON)/dist/*.whl /out" + +clean: + rm -f *.whl diff --git a/tensorflow/lite/tools/pip_package/README.md b/tensorflow/lite/tools/pip_package/README.md index adab810126a..f32fa364160 100644 --- a/tensorflow/lite/tools/pip_package/README.md +++ b/tensorflow/lite/tools/pip_package/README.md @@ -15,6 +15,26 @@ That will print out some output and a .whl file. You can then install that pip install --upgrade ``` +You can also build a wheel inside docker container using make tool. For example +the following command will cross-compile tflite_runtime for python2.7 and +python3.7 (from Debian Buster) on Raspberry Pi: +``` +make BASE_IMAGE=debian:buster PYTHON=python TENSORFLOW_TARGET=rpi docker-build +make BASE_IMAGE=debian:buster PYTHON=python3 TENSORFLOW_TARGET=rpi docker-build +``` + +Another option is to cross-compile for python3.5 (from Debian Stretch) on ARM64 +board: +``` +make BASE_IMAGE=debian:stretch PYTHON=python3 TENSORFLOW_TARGET=aarch64 docker-build +``` + +To build for python3.6 (from Ubuntu 18.04) on x86_64 (native to the docker +image) run: +``` +make BASE_IMAGE=ubuntu:18.04 PYTHON=python3 TENSORFLOW_TARGET=native docker-build +``` + Note, unlike tensorflow this will be installed to a tflite_runtime namespace. You can then use the Tensorflow Lite interpreter as. ``` diff --git a/tensorflow/lite/tools/pip_package/build_pip_package.sh b/tensorflow/lite/tools/pip_package/build_pip_package.sh old mode 100644 new mode 100755 diff --git a/tensorflow/lite/tools/pip_package/update_sources.sh b/tensorflow/lite/tools/pip_package/update_sources.sh new file mode 100755 index 00000000000..701a977e155 --- /dev/null +++ b/tensorflow/lite/tools/pip_package/update_sources.sh @@ -0,0 +1,23 @@ +#!/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. +# ============================================================================== + +. /etc/os-release + +sed -i "s/deb\ /deb \[arch=amd64\]\ /g" /etc/apt/sources.list + +echo "deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME} main universe" >> /etc/apt/sources.list +echo "deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-updates main universe" >> /etc/apt/sources.list +echo "deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-security main universe" >> /etc/apt/sources.list