Created
March 26, 2022 04:59
-
-
Save lnshi/eb3dea05d99daba5c932bbc786cc3701 to your computer and use it in GitHub Desktop.
Revisions
-
lnshi created this gist
Mar 26, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ ## Background Currently if you try to use the TensorFlow rust bindings crate: [tensorflow = "0.17.0"](https://crates.io/crates/tensorflow), this crate's sub-crate: [tensorflow-sys](https://github.com/tensorflow/rust/tree/master/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"](https://crates.io/crates/tensorflow). This gist was tested on TensorFlow `v2.8.0`, the other versions should work in the same way. ## Steps 1. Install bazel: ```bash > bazel --version > bazel 4.2.1 ``` 2. Create python3.9 venv and install numpy (`Python 3.9.9 + numpy 1.22.3`); 3. git clone the TensorFlow source code, and switch to `v2.8.0` branch; 4. Go to the cloned `tensorflow/` project, do the compiling, and install it to your system: ```bash # 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`: ```bash 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 ``` 5. 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 the `tensorflow.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 system `PKG_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 ``` 6. Till here ur systemwide setup is all ok, but unfortunately due to this [bug of rust-lang/pkg-config-rs](https://github.com/rust-lang/pkg-config-rs/pull/105), 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"](https://github.com/tensorflow/rust/blob/master/tensorflow-sys/Cargo.toml#L27) to `pkg-config = "0.3.24";` 7. Finally all will work: ```bash cargo clean # Note u have to use the absolute path or $(pwd) PKG_CONFIG_PATH=~/.pkg_configs/ cargo build ``` πππ