Add docker-based build system to cross-compile tflite_runtime wheels.

PiperOrigin-RevId: 258903014
This commit is contained in:
A. Unique TensorFlower 2019-07-18 21:30:11 -07:00 committed by TensorFlower Gardener
parent 632ff99d56
commit 238dcdfdee
6 changed files with 144 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,42 @@
# Values: debian:<version>, ubuntu:<version>
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

View File

@ -15,6 +15,26 @@ That will print out some output and a .whl file. You can then install that
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 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.
```

0
tensorflow/lite/tools/pip_package/build_pip_package.sh Normal file → Executable file
View File

View File

@ -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