This should work for other TF versions 2.x, but mine was done for 2.3.1 specifically.
kmhoffmann's Gist about compiling TF 2.3.0 on Ubuntu 20.04 as that helped me get started with the steps I needed to start compling my own TF packaget started.
There are many reasons to want to compile your own TF, but for me, I've been playing around with ML for a while myself and recently embarked on the TensorFlow journey. While trying to follow along to the [Predict Fuel Efficiency] regression problem, I ran into the following error: Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA and after a bit of Googling, I found that the default TF package that you install with pip is built without CPU extensions and so I had to compile my own if I wanted to run something a bit more complex on my MacBook. (The example I was following was mean to be run on Colab, which probably has a custom TF package already as well as the option to use a GPU for your training).
So I don't know what I was thinking when I thought that I could compile TF on a linux VM and install that package on my Mac, but that's indeed what I did initially and then had to rebuild on a (less powerful) MacBook instead. Since I'd already done it, I might as well document that as well (see section below on my OS-sepcific build/setups and notes)
Compiling TF (taken mostly from here)
You will need a specific version of Bazel for each TF version, which, in our case is 3.1.0. If you're on a Mac, sometimes the OS security will prevent you from running the install script, so I recommend downloading the binary instead.
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout v2.3.0
python3 -m venv ~/.virtualenvs/tf_dev
source ~/.virtualenvs/tf_dev/bin/activate
pip3 install -U pip six 'numpy<1.19.0' wheel setuptools mock 'future>=0.17.1'
pip3 install -U keras_applications --no-deps
pip3 install -U keras_preprocessing --no-deps
You should still be in the tensorflow/
(root of the repo) directory at this point:
./configure
For me, I took the defaults for all the questions that were asked as I didn't have a GPU with CUDA support or anything like that, but answer appropriately for your setup.
Follwed by:
bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.2 //tensorflow/tools/pip_package:build_pip_package
where I'm building it with AVX, AVX2, FMA and SSE4.2 options, but you can set yours to whatever fits your needs.
After your build completes:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
will product your .whl
file (for me, that was tensorflow-2.3.1-cp38-cp38-macosx_10_15_x86_64.whl
) that you will then install with pip3 install
python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
- 4 CPUs
- 32GB mem
With this setup, it took almost 1.5hrs to compile. I had tried a 4CPU/16GB mem VM but would run into various c++ compiler errors that were tied to OOM errors, and while I could have just as easily limited Bazel's RAM allocation, I figured it's a cloud VM, I only pay for what I use right? So I bumped it up to a 4CPU/32GB mem setup and this built without any problems.
...
...
INFO: Elapsed time: 4900.236s, Critical Path: 233.75s
INFO: 1945 processes: 1945 local.
INFO: Build completed successfully, 1987 total actions
- 2 CPUs
- 16GB memory
I have a 2020 MB Air, but during building, the CPUs would be maxed and the fan would go crazy, so I decided to build it on an older, 2017 dual-core MB Pro instead. and it took almost 6hrs!!
...
...
INFO: Elapsed time: 21062.911s, Critical Path: 443.76s
INFO: 19825 processes: 19825 local.
INFO: Build completed successfully, 21086 total actions