Skip to content

Instantly share code, notes, and snippets.

@apivovarov
Last active October 22, 2024 10:13
Show Gist options
  • Save apivovarov/6274e4f0afd294c112102868c9e84d94 to your computer and use it in GitHub Desktop.
Save apivovarov/6274e4f0afd294c112102868c9e84d94 to your computer and use it in GitHub Desktop.
Compile Tensorflow on ARMv8 aarch64 arm64

Intro

This wiki explains how to build Tensorflow 1.13.1 for ARMv8 platform (aarch64 arm64)

Build

Preparation

Start a1.4xlarge instance with Ubuntu 16.04 OS and 64GB disk

sudo apt update
sudo apt upgrade
sudo reboot
sudo apt install build-essential

sudo apt install python3 python3-dev

sudo apt install pkg-config zip zlib1g-dev unzip curl tmux wget vim git htop

sudo apt install openjdk-8-jdk

sudo apt install liblapack3 libblas3 libhdf5-dev
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python3 get-pip.py
sudo pip3 install six mock numpy grpcio
sudo pip3 install h5py==2.10.0
sudo pip3 install keras_applications --no-deps
sudo pip3 install keras_preprocessing --no-deps

Bazel

wget https://github.com/bazelbuild/bazel/releases/download/0.19.2/bazel-0.19.2-dist.zip
mkdir bazel-0.19.2
unzip bazel-0.19.2-dist.zip -d bazel-0.19.2
cd bazel-0.19.2

# open tmux session
tmux
./compile.sh

# Copy bazel to $PATH
sudo cp output/bazel /usr/local/bin/

cd ..

Tensorflow

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v1.13.1

# run configure. It will ask several questions
# change python path to /usr/bin/python3 
# hit enter for all other questions (default settings)

./configure
# Monolithic option will produce 90 MB wheel file. Without monolithic - 60MB
# --config=monolithic

# Make sure you are still in tmux session. Build will take about 1.5 hours

bazel build -c opt \
--copt=-O3 \
--copt=-std=c++11 \
--copt=-funsafe-math-optimizations \
--copt=-ftree-vectorize \
--copt=-fomit-frame-pointer \
--copt=-DRASPBERRY_PI \
--host_copt=-DRASPBERRY_PI \
--verbose_failures \
--config=noaws \
--config=nogcp \
--config=nohdfs \
--config=noignite \
--config=nokafka \
--config=nonccl \
//tensorflow/tools/pip_package:build_pip_package
# Build a universal wheel.
BDIST_OPTS="--universal" bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/tensorflow_pkg

sudo pip3 install ~/tensorflow_pkg/tensorflow-1.13.1-cp35-cp35m-linux_aarch64.whl

Test

import tensorflow as tf
tf.__version__
tf.__path__

a = tf.placeholder(tf.float32, shape=[2, 2], name="input1")
b = tf.placeholder(tf.float32, shape=[2, 2], name="input2")
ab = tf.matmul(a, b, name="output")

inp1 = [[4., 7.], [2., 6.]]
inp2 = [[0.6, -0.7], [-0.2, 0.4]]

with tf.Session() as sess:
    res = sess.run(ab, feed_dict={a: inp1, b: inp2})

print(res)

Links

https://www.tensorflow.org/install/source https://collaborate.linaro.org/display/BDTS/Building+and+Installing+Tensorflow+on+AArch64 https://github.com/lhelontra/tensorflow-on-arm/releases

@nhochamvui
Copy link

I followed your instruction but it hang the system after running for about 2 hours :(, 4gb ram and 1gb swap are full! I tried to build tensorflow2.3.0 with bazel on aarch64 linux (raspberry 4 - python3.8). Could you please help me any solution that I can try!

@apivovarov
Copy link
Author

try to use 3 cores our of 4 --local_cpu_resources=3

@Kachasukintim
Copy link

~/tensorflow$ bazel build -c opt \

--copt=-O3
--copt=-std=c++11
--copt=-funsafe-math-optimizations
--copt=-ftree-vectorize
--copt=-fomit-frame-pointer
--copt=-DRASPBERRY_PI
--host_copt=-DRASPBERRY_PI
--verbose_failures
--config=noaws
--config=nogcp
--config=nohdfs
--config=noignite
--config=nokafka
--config=nonccl
//tensorflow/tools/pip_package:build_pip_package
WARNING: ignoring LD_PRELOAD in environment.
Starting local Bazel server and connecting to it...

ERROR: /home/guitar/tensorflow/tensorflow/tools/pip_package/BUILD:149:1: no such package '@icu//': java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/unicode-org/icu/archive/release-62-1.tar.gz, https://github.com/unicode-org/icu/archive/release-62-1.tar.gz] to /home/guitar/.cache/bazel/_bazel_guitar/e0ac497cdf93112707a19bb545085e17/external/icu/release-62-1.tar.gz: Checksum was 86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181 but wanted e15ffd84606323cbad5515bf9ecdf8061cc3bf80fb883b9e6aa162e485aa9761 and referenced by '//tensorflow/tools/pip_package:licenses'

ERROR: Analysis of target '//tensorflow/tools/pip_package:build_pip_package' failed; build aborted: no such package '@icu//': java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/unicode-org/icu/archive/release-62-1.tar.gz, https://github.com/unicode-org/icu/archive/release-62-1.tar.gz] to /home/guitar/.cache/bazel/_bazel_guitar/e0ac497cdf93112707a19bb545085e17/external/icu/release-62-1.tar.gz: Checksum was 86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181 but wanted e15ffd84606323cbad5515bf9ecdf8061cc3bf80fb883b9e6aa162e485aa9761
INFO: Elapsed time: 81.058s
INFO: 0 processes.

FAILED: Build did NOT complete successfully (338 packages loaded, 18753 target
s configured)
guitar@guitar-desktop:~/tensorflow$

Help me ! I followed you every step what happened.

@haraldkubota
Copy link

haraldkubota commented May 11, 2022

For everyone trying to follow this guide: It works basically. Small (and some obvious) changes needed:

  • m6g.xlarge (4 CPU, 16GB RAM). 8 GB was not enough as some compile jobs used over 6GB.
  • Ubuntu 20.04 (different version gives you different GLIBC versions)
  • TF 2.8
  • Bazel 4.2.2 (from wget https://github.com/bazelbuild/bazel/releases/download/4.2.2/bazel-4.2.2-linux-arm64)
  • I removed the nonccl, nokafka, noignite and nohdfs options
  • When using Debian for test purposes, I used openjdk-11-jdk instead of 8 and it seemed to make no difference
  • I installed additionally those 2 Python packages: wheel and packaging as otherwise I got errors during the package build process at the end

I then ran into unrelated CPython issues (wrong CPython version on my target system, and too-new GLIBC), but beside this, all worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment