├── src └── update_sources.sh └── README.md /src/update_sources.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2019 Google LLC 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # https://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | . /etc/os-release 16 | 17 | [[ "${NAME}" == "Ubuntu" ]] || exit 0 18 | 19 | sed -i "s/deb\ /deb \[arch=amd64\]\ /g" /etc/apt/sources.list 20 | 21 | if [ ${UBUNTU_CODENAME} == "noble" ]; then 22 | 23 | echo "NOBLE" 24 | 25 | rm /etc/apt/sources.list.d/ubuntu.sources 26 | cat <> /etc/apt/sources.list.d/ubuntu.sources 27 | 28 | Types: deb 29 | URIs: http://archive.ubuntu.com/ubuntu/ 30 | Suites: ${UBUNTU_CODENAME} ${UBUNTU_CODENAME}-updates ${UBUNTU_CODENAME}-backports 31 | Components: main universe restricted multiverse 32 | Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg 33 | Architectures: amd64 34 | 35 | ## Ubuntu security updates. Aside from URIs and Suites, 36 | ## this should mirror your choices in the previous section. 37 | Types: deb 38 | URIs: http://security.ubuntu.com/ubuntu/ 39 | Suites: ${UBUNTU_CODENAME}-security 40 | Components: main universe restricted multiverse 41 | Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg 42 | Architectures: amd64 43 | 44 | Types: deb 45 | URIs: http://ports.ubuntu.com/ubuntu-ports 46 | Suites: ${UBUNTU_CODENAME} ${UBUNTU_CODENAME}-updates ${UBUNTU_CODENAME}-security 47 | Components: main universe 48 | Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg 49 | Architectures: arm64 armhf 50 | 51 | EOT 52 | 53 | else 54 | 55 | cat <> /etc/apt/sources.list 56 | deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME} main universe 57 | deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-updates main universe 58 | deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-security main universe 59 | EOT 60 | 61 | fi 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TFlite-builds 2 | 3 | TFlite cross-platform builds. Current stable version: `2.18.0`. 4 | 5 | ## Why this repo? 6 | 7 | The information provided in the [official tensorflow lite page for building `whl` packages for ARM](https://www.tensorflow.org/lite/guide/build_cmake_pip) to cross-compile TFlite for different architectures is a good start. However there seem to be several issues. This repo aims at providing more info towards successful compilation. And some binaries as well. 8 | 9 | The provided builds are fully compatible with [Coral.ai EdgeTPU through the updated libedgetpu drivers](https://github.com/feranick/libedgetpu). 10 | 11 | ## Building - Docker 12 | 13 | - Install docker: 14 | 15 | ## 16 | sudo apt install docker.io 17 | 18 | - Download tensorflow and checkout the relevant version. 19 | 20 | ## 21 | git clone https://github.com/tensorflow/tensorflow.git 22 | ## 23 | cd tensorflow 24 | ## 25 | git checkout vX.XX.XX 26 | 27 | 28 | If compiling against `python 3.11` or newer, you have to allow installation of whl packages from external sources. Add the following line in `tensorflow/lite/tools/pip_package/Dockerfile.py3`: 29 | ``` 30 | RUN ln -sf /usr/bin/python$PYTHON_VERSION /usr/bin/python3 31 | RUN curl -OL https://bootstrap.pypa.io/get-pip.py 32 | -> RUN rm /usr/lib/python$PYTHON_VERSION/EXTERNALLY-MANAGED 33 | RUN python3 get-pip.py 34 | RUN rm get-pip.py 35 | ``` 36 | Optional: update `cmake` by editing `tensorflow/lite/tools/pip_package/Dockerfile.py3` to replace: 37 | ``` 38 | RUN curl -OL https://github.com/Kitware/CMake/releases/download/v3.16.8/cmake-3.16.8-Linux-x86_64.sh 39 | RUN mkdir /opt/cmake 40 | RUN sh cmake-3.16.8-Linux-x86_64.sh --prefix=/opt/cmake --skip-license 41 | ``` 42 | with: 43 | ``` 44 | RUN curl -OL https://github.com/Kitware/CMake/releases/download/v3.29.6/cmake-3.29.6-linux-x86_64.sh 45 | RUN mkdir /opt/cmake 46 | RUN sh cmake-3.29.6-linux-x86_64.sh --prefix=/opt/cmake --skip-license 47 | ``` 48 | 49 | - Edit Makefile to your system `tensorflow/lite/tools/pip_package/Makefile`: 50 | 51 | For `Python 3.11` and earlier: 52 | ``` 53 | # Values: debian:, ubuntu: 54 | BASE_IMAGE ?= ubuntu:22.04 55 | PYTHON_VERSION ?= 3.11 56 | NUMPY_VERSION ?= 1.24.4 57 | ``` 58 | For `Python 3.12` and later: 59 | ``` 60 | # Values: debian:, ubuntu: 61 | BASE_IMAGE ?= ubuntu:22.04 62 | PYTHON_VERSION ?= 3.12 63 | NUMPY_VERSION ?= 1.26.4 64 | ``` 65 | 66 | ### Changes specific to Ubuntu 24.04 and newer 67 | 68 | - Replace the file `tensorflow/lite/tools/pip_package/update_sources.sh` with the one provided in this repository. 69 | - In the file `tensorflow/lite/tools/pip_package/Makefile` replace: 70 | 71 | ``` 72 | docker-build: docker-image 73 | mkdir -p $(TENSORFLOW_DIR)/bazel-ci_build-cache 74 | docker run \ 75 | --rm --interactive $(shell tty -s && echo --tty) \ 76 | $(DOCKER_PARAMS) \ 77 | $(TAG_IMAGE) \ 78 | /with_the_same_user /bin/bash -C /tensorflow/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh $(TENSORFLOW_TARGET) 79 | ``` 80 | with 81 | ``` 82 | docker-build: docker-image 83 | mkdir -p $(TENSORFLOW_DIR)/bazel-ci_build-cache 84 | docker run \ 85 | --rm --interactive $(shell tty -s && echo --tty) \ 86 | $(DOCKER_PARAMS) \ 87 | $(TAG_IMAGE) \ 88 | /bin/bash -C /tensorflow/tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh $(TENSORFLOW_TARGET) 89 | ``` 90 | 91 | - In the file `tensorflow/lite/tools/pip_package/Dockerfile.py3` remove: 92 | ``` 93 | python$PYTHON_VERSION \ 94 | python$PYTHON_VERSION-dev \ 95 | python$PYTHON_VERSION-venv \ 96 | --> python$PYTHON_VERSION-distutils \ 97 | libpython$PYTHON_VERSION-dev \ 98 | libpython$PYTHON_VERSION-dev:armhf \ 99 | libpython$PYTHON_VERSION-dev:arm64 100 | ``` 101 | 102 | ### Building using Debian 103 | If you are building for `debian:bookworm` or `debian:bullseye`, you need to remove/comment the following line from `tensorflow/lite/tools/pip_package/Dockerfile.py3`, since the added ppa repository is specific only to ubuntu. 104 | 105 | ``` 106 | RUN yes | add-apt-repository ppa:deadsnakes/ppa 107 | ``` 108 | 109 | ### Compilation 110 | 111 | - Run compilation (adjust the values for `TENSORFLOW_TARGET` and `PYTHON_VERSION` to fit your needs: 112 | 113 | ``` 114 | BUILD_NUM_JOBS=4 make -C tensorflow/lite/tools/pip_package docker-build TENSORFLOW_TARGET=aarch64 PYTHON_VERSION=3.11 115 | ``` 116 | Note: You can change the variable `BUILD_NUM_JOBS` from 4 to `$(nproc)` to use the max number of cores in your CPU for fastest compilation. 117 | 118 | These are the supported targets. 119 | 120 | - `armhf`: ARMv7 VFP with Neon Compatible with Raspberry Pi 3 and 4 121 | - `rpi0`: ARMv6 Compatible with Raspberry Pi Zero 122 | - `aarch64`: aarch64 (ARM 64-bit) Coral Mendel Linux 4.0 or Raspberry Pi with Ubuntu Server 20.04.01 LTS 64-bit of 22.04.x LTS 64-bit 123 | - `native`: Your workstation It builds with "-mnative" optimization 124 | 125 | ## Building - Native 126 | ### Using Bazel 127 | 128 | ## 129 | export TF_PYTHON_VERSION=3.12; PYTHON=python3 tensorflow/lite/tools/pip_package/build_pip_package_with_bazel.sh native 130 | 131 | 132 | ### Using CMake 133 | ## 134 | BUILD_NUM_JOBS=4 PYTHON=python3 tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh native 135 | 136 | Note: You can change the variable `BUILD_NUM_JOBS` from 4 to `$(nproc)` to use the max number of cores in your CPU for fastest compilation. 137 | 138 | ## GPU support for native builds 139 | When compiling with either `docker` or native using `cmake` GPU support is disableed by default. To enable it, edit this file: 140 | 141 | ## 142 | nano tensorflow/lite/tools/pip_package/build_pip_package_with_cmake.sh 143 | 144 | and add to line 110: 145 | ``` 146 | native) 147 | BUILD_FLAGS=${BUILD_FLAGS:-"-march=native -I${PYTHON_INCLUDE} -I${PYBIND11_INCLUDE} -I${NUMPY_INCLUDE}"} 148 | cmake \ 149 | -DCMAKE_C_FLAGS="${BUILD_FLAGS}" \ 150 | -DCMAKE_CXX_FLAGS="${BUILD_FLAGS}" \ 151 | ==> -DTFLITE_ENABLE_GPU=ON \ 152 | "${TENSORFLOW_LITE_DIR}" 153 | ``` 154 | --------------------------------------------------------------------------------