Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Created January 5, 2018 22:04
Show Gist options
  • Save daniel-j-h/b410dd21d4949a54c83d0c3864437f0c to your computer and use it in GitHub Desktop.
Save daniel-j-h/b410dd21d4949a54c83d0c3864437f0c to your computer and use it in GitHub Desktop.
Builds architecture optimized TensorFlow from source, for https://news.ycombinator.com/item?id=16071001
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
# Usage: ./build_tf_1_4_0.sh broadwell /tmp/wheel
#
# Builds TensorFlow 1.4.0 from source for a specific architecture.
# Assumes Ubuntu 16.04, installs dependencies and the bazel buildsystem.
# Does not build against Intel MKL.
#
# For architecture values see the `-march=` compiler flag in:
# https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
#
# https://www.tensorflow.org/install/install_sources
# https://www.tensorflow.org/performance/performance_guide#building_and_installing_from_source
# https://www.tensorflow.org/performance/performance_guide#optimizing_for_cpu
readonly Arch=${1:-native}
readonly Out=${2:-$(pwd)}
apt-get install -y --no-install-recommends wget python3 python3-dev python3-pip gcc g++ pkg-config zip zlib1g-dev unzip
python3 -m pip install --ignore-installed 'pip==9.0.1' 'wheel==0.30.0' 'setuptools==36.4.0'
python3 -m pip install 'numpy==1.13.1'
wget https://github.com/bazelbuild/bazel/releases/download/0.5.4/bazel-0.5.4-installer-linux-x86_64.sh
echo "9981da210d2d96e68662127ba9d96844ce2822524b18e98fcec54a70345cff9c bazel-0.5.4-installer-linux-x86_64.sh" | sha256sum --check --quiet
chmod a+x bazel-0.5.4-installer-linux-x86_64.sh
./bazel-0.5.4-installer-linux-x86_64.sh
bazel version
rm bazel-0.5.4-installer-linux-x86_64.sh
wget https://github.com/tensorflow/tensorflow/archive/v1.4.0.tar.gz -O tensorflow-1.4.0.tar.gz
echo "8a0ad8d61f8f6c0282c548661994a5ab83ac531bac496c3041dedc1ab021107b tensorflow-1.4.0.tar.gz" | sha256sum --check --quiet
tar xf tensorflow-1.4.0.tar.gz
cd tensorflow-1.4.0
env \
PYTHON_BIN_PATH=$(which python3) \
USE_DEFAULT_PYTHON_LIB_PATH=1 \
TF_NEED_MKL=0 \
CC_OPT_FLAGS="-march=${Arch} -D_GLIBCXX_USE_CXX11_ABI=0" \
TF_NEED_JEMALLOC=0 \
TF_NEED_GCP=0 \
TF_NEED_HDFS=0 \
TF_ENABLE_XLA=0 \
TF_NEED_VERBS=0 \
TF_NEED_OPENCL=0 \
TF_NEED_S3=0 \
TF_NEED_CUDA=0 \
HOST_CXX_COMPILER=$(which g++) \
HOST_C_COMPILER=$(which gcc) \
TF_NEED_MPI=0 \
TF_NEED_GDR=0 \
./configure
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package ${Out}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment