Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Created September 17, 2017 12:29
Show Gist options
  • Save bzamecnik/ad3397297aea3c0a26c3351c1d086115 to your computer and use it in GitHub Desktop.
Save bzamecnik/ad3397297aea3c0a26c3351c1d086115 to your computer and use it in GitHub Desktop.
Building TensorFlow with AVX

Let's compile TensorFlow ourselves with AVX vector instruction to speed up inference on a CPU.

OSX:

https://www.tensorflow.org/install/install_sources#prepare_environment_for_mac_os tensorflow/tensorflow#12979

  • fix checksum in tensorflow/workspace.bzl
  • e5fdeee6b28cf6c38d61243adff06628baa434a22b5ebb7432d2a7fbabbdb13d but wanted 6d43b9d223ce09e5d4ce8b0060cb8a7513577a35a64c7e3dad10f0703bf3ad93
  • comment the sha256 checksums tensorflow/tensorflow#6729
brew install bazel

export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/cuda/extras/CUPTI/lib
export LD_LIBRARY_PATH=$DYLD_LIBRARY_PATH
export PATH=$DYLD_LIBRARY_PATH:$PATH
bazel build  --config=opt --config=cuda --action_env PATH --action_env LD_LIBRARY_PATH --action_env DYLD_LIBRARY_PATH //tensorflow/tools/pip_package:build_pip_package

error: ld: library not found for -lgomp (missing OpenMP) tensorflow/tensorflow#9072

https://medium.com/@mattias.arro/installing-tensorflow-1-2-from-sources-with-gpu-support-on-macos-4f2c5cab8186

still:

ld: warning: cannot export hidden symbol std::__1::__vector_base<tensorflow::graph_transforms::OpTypePattern, std::__1::allocator<tensorflow::graph_transforms::OpTypePattern> >::__destruct_at_end(tensorflow::graph_transforms::OpTypePattern*) from bazel-out/local_darwin-py3-opt/bin/tensorflow/tools/graph_transforms/libtransforms_lib.pic.lo

Let's build it without CUDA (on OSX we don't need it anyway).

bazel clean
./configure
bazel build  --config=opt --action_env PATH --action_env LD_LIBRARY_PATH --action_env DYLD_LIBRARY_PATH //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-1.3.0-py3-none-any.whl

In the end it worked on OSX without CUDA. It doesn't show SSE/AVX warnings, but no observable speedup.

Linux:

install both libcudnn6 and libcudnn6

git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout r1.3
export CUDA_HOME=/usr/local/cuda
./configure
  • cuda: y
  • cudnn path: /usr/lib/x86_64-linux-gnu
  • select python version you're be using the pip package with (eg. 3.5 vs. 3.6!)
    • otherwise: tensorflow-1.3.0-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform.
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-1.3.0-py3-none-any.whl
# avoid ImportError: No module named 'tensorflow.python.pywrap_tensorflow_internal'
cd
# test installation
python -c 'import tensorflow'

Elapsed time: 3163.185s, Critical Path: 126.07s INFO: Build completed successfully, 4559 total actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment