Last active
February 22, 2024 06:12
-
-
Save venik/9ba962c8b301b0e21f99884cbd35082f to your computer and use it in GitHub Desktop.
Bash script for local building TensorFlow on Mac/Linux with all CPU optimizations (default pip package has only SSE)
This file contains 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 characters
#!/usr/bin/env bash | |
# Author: Sasha Nikiforov | |
# source of inspiration | |
# https://stackoverflow.com/questions/41293077/how-to-compile-tensorflow-with-sse4-2-and-avx-instructions | |
# Detect platform | |
if [ "$(uname)" == "Darwin" ]; then | |
# MacOS | |
raw_cpu_flags=`sysctl -a | grep machdep.cpu.features | cut -d ":" -f 2 | tr '[:upper:]' '[:lower:]'` | |
elif [ "$(uname)" == "Linux" ]; then | |
# GNU/Linux | |
raw_cpu_flags=`grep flags -m1 /proc/cpuinfo | cut -d ":" -f 2 | tr '[:upper:]' '[:lower:]'` | |
else | |
echo "Unknown plaform: $(uname)" | |
exit -1 | |
fi | |
# check if VirtuelEnv activated | |
if [ -z "$VIRTUAL_ENV" ]; then | |
echo "VirtualEnv is not activated" | |
exit -1 | |
fi | |
VENV_BIN=$VIRTUAL_ENV/bin | |
VENV_LIB=$VIRTUAL_ENV/lib | |
# bazel tf needs these env vars | |
export PYTHON_BIN_PATH=$VENV_BIN/python | |
export PYTHON_LIB_PATH=$VENV_LIB/`ls $VENV_LIB | grep python` | |
COPT="--copt=-march=native" | |
for cpu_feature in $raw_cpu_flags | |
do | |
case "$cpu_feature" in | |
"sse4.1" | "sse4.2" | "ssse3" | "fma" | "cx16" | "popcnt" | "maes") | |
COPT+=" --copt=-m$cpu_feature" | |
;; | |
"avx1.0") | |
COPT+=" --copt=-mavx" | |
;; | |
*) | |
# noop | |
;; | |
esac | |
done | |
bazel clean | |
./configure | |
bazel build -c opt $COPT -k //tensorflow/tools/pip_package:build_pip_package | |
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg | |
pip install --upgrade /tmp/tensorflow_pkg/`ls /tmp/tensorflow_pkg/ | grep tensorflow` |
Tensorflow 1.4.1 did not work with bazel 0.9.0 for me. Had to downgrade bazel to 0.8.1
works with tag v1.11.0, commit c19e293 python 3, I wasnt able to build with python 2, but didnt spend much time to do it Note: python 3.7 wont work on MacOS, you need python 3.6 or mb 3.5. More details below
tensorflow/tensorflow#21441
Due to some problem with virtualenv small patch is needed to build TF on virtualenv.
diff --git a/configure.py b/configure.py
index 7edab53964..5987c083cc 100644
--- a/configure.py
+++ b/configure.py
@@ -155,7 +155,7 @@ def get_python_path(environ_cp, python_bin_path):
try:
library_paths = run_shell(
[python_bin_path, '-c',
- 'import site; print("\\n".join(site.getsitepackages()))']).split('\n')
+ 'from distutils.sysconfig import get_python_lib; print(get_python_lib())']).split('\n')
except subprocess.CalledProcessError:
library_paths = [run_shell(
[python_bin_path, '-c',
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rogeriochaves thx :)
@phobrain great, thx for the input