Currently if you try to use the TensorFlow rust bindings crate: tensorflow = "0.17.0", this crate's sub-crate: tensorflow-sys will try to build the underlying TensorFlow C library from the source as Google hasn't provided an official release for Apple M1, but unfortunately the building will fail.
This gist provides a full working instructions for you to build the TensorFlow C library on Apple M1 (darwin_arm64), and eventually use it with the TensorFlow rust bindings crate: tensorflow = "0.17.0".
This gist was tested on TensorFlow v2.8.0
, the other versions should work in the same way.
-
Install bazel:
> bazel --version > bazel 4.2.1
-
Create python3.9 venv and install numpy (
Python 3.9.9 + numpy 1.22.3
); -
git clone the TensorFlow source code, and switch to
v2.8.0
branch; -
Go to the cloned
tensorflow/
project, do the compiling, and install it to your system:# Provide the python interpreter path in the venv you just created which has numpy installed, # for me: /private/tmp/venv/bin/python ./configure bazel build --jobs=10 --compilation_mode=opt --copt=-march=native //tensorflow/tools/lib_package:libtensorflow
- After the compiling completed you should be able to find the
bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz
; - Install the tarball to
/usr/local/lib
:sudo mkdir -p /usr/local/lib/libtensorflow-cpu-darwin-arm64-2.8.0 sudo tar -C /usr/local/lib/libtensorflow-cpu-darwin-arm64-2.8.0 -xzf bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz
- After the compiling completed you should be able to find the
-
Configure the installed TensorFlow dylibs to
pkg-config
(if u haven't installed do:brew install pkg-config
):- Generate the
tensorflow.pc
, u could utilize the$TENSORFLOW_SRC/tensorflow/c/generate-pc.sh
script, but i was not able to run it, anyway the contents of thetensorflow.pc
is very straightforward like below (make sure all the paths are correct):prefix=/usr/local/lib/libtensorflow-cpu-darwin-arm64-2.8.0 exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir=${prefix}/include/tensorflow Name: TensorFlow Version: 2.8.0 Description: Library for computation using data flow graphs for scalable machine learning Requires: Libs: -L${libdir} -ltensorflow -ltensorflow_framework Cflags: -I${includedir}
- Put the above
tensorflow.pc
into your systemPKG_CONFIG_PATH
, for me i use~/.pkg_configs
; - Then run the command
PKG_CONFIG_PATH=~/.pkg_configs/ pkg-config --list-all | grep tensorflow
u should be able to see:tensorflow TensorFlow - Library for computation using data flow graphs for scalable machine learning
- Generate the
-
Till here ur systemwide setup is all ok, but unfortunately due to this bug of rust-lang/pkg-config-rs, u need to perform the below steps to make ur rust TensorFlow project work:
- Switch ur project's
tensorflow-rust
bindings dependencies to local source code:tensorflow = { path = "/Users/leonard/projects/rust" }
; - Then upgrade
tensorflow-rust
's dependant create pkg-config = "0.3.19" topkg-config = "0.3.24";
- Switch ur project's
-
Finally all will work:
cargo clean # Note u have to use the absolute path or $(pwd) PKG_CONFIG_PATH=~/.pkg_configs/ cargo build
πππ
Thank you for the build instructions! Just FYI I had to omit
--copt=-march=native
from step 4 to get it working.