The following steps are based on a shell script that I wrote. It's not all automatically but you can see it in https://github.com/fredericobenevides/dotfiles/blob/master/files/python/tensorflow/tensorflow_gpu.sh.
macOs: 10.13.3 CUDA Toolkit: 9.1.128 cuDNN: 7.1 Xcode: 8.2 bazel: 0.11.1
- Restart holding command + r
- Execute 'csrutil disable’
- Restart
Note that you need to install anaconda, not miniconda.
pip install six numpy wheel
brew tap caskroom/versions
brew cask install java8 anaconda
brew install coreutils bazel
Login first on apple and download using this link: https://download.developer.apple.com/Developer_Tools/Xcode_8.2/Xcode_8.2.xip
Click twice on Xcode_9.2.xip. It will extract the Xcode.app in your Downloads folder. After extracting, move the Xcode.app from Downlods folder to /Applications/Xcode_8.2.app
mv ~/Downloads/Xcode.app ~/Downloads/Xcode_8.2.app
mv ~/Downloads/Xcode_8.2.app /Applications
xcode-select -s /Applications/Xcode_8.2.app
Download using this link
curl -fsSL https://developer.nvidia.com/compute/cuda/9.1/Prod/local_installers/cuda_9.1.128_mac ~/Downloads/cuda_9.1.128_mac
After downloading, click it twice to install it.
Download the cuDNN 7.1 for CUDA 9.1 from this link https://developer.nvidia.com/cudnn . Direct link with you did the login: https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.0.5/prod/9.1_20171129/cudnn-9.1-osx-x64-v7-ga After downloading, execute:
cudnn_file=`find ~/Downloads/cudnn*`
tar -C ~/Downloads -xvf $cudnn_file
sudo cp -RPf ~/Downloads/cuda/include/cudnn.h /Developer/NVIDIA/CUDA-9.1/include
sudo cp -RPf ~/Downloads/cuda/lib/* /Developer/NVIDIA/CUDA-9.1/lib
sudo ln -s /Developer/NVIDIA/CUDA-9.1/lib/libcudnn* /usr/local/cuda/lib
Install the following environment variables in your .bashrc or .zshrc Reload it after installing with source .bashrc or source .zshrc
export CUDA_HOME=/usr/local/cuda \n
export DYLD_LIBRARY_PATH=$CUDA_HOME/lib:$CUDA_HOME/extras/CUPIT/lib \n
export LD_LIBRARY_PATH=$DYLD_LIBRARY_PATH \n
export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH \n
export PATH=$CUDA_HOME/bin:$PATH
Execute the following command and verify if output it without problem.
sudo make -C /usr/local/cuda/samples/1_Utilities/deviceQuery
/usr/local/cuda/samples/bin/x86_64/darwin/release/deviceQuery
Now we need to clone the tensorflow. We're going to use a commit that didn't have problem to compile.
git clone https://github.com/tensorflow/tensorflow ~/Downloads/tensorflow
cd ~/Downloads/tensorflow
git checkout r1.6
git reset --hard 5aee07fd0462d00c52efb5d3c86bfb955a9d976e
Before installing tensorflow we need to apply a patch to fix protobuf_archive and eigen_archive.
Create a file in ~/Downloads/tensorflow/patch_workspace.bzl with the following content:
--- a/tensorflow/workspace.bzl
+++ b/tensorflow/workspace.bzl
@@ -120,11 +120,11 @@ def tf_workspace(path_prefix="", tf_repo_name=""):
tf_http_archive(
name = "eigen_archive",
urls = [
- "https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/14e1418fcf12.tar.gz",
- "https://bitbucket.org/eigen/eigen/get/14e1418fcf12.tar.gz",
+ "https://mirror.bazel.build/bitbucket.org/dtrebbien/eigen/get/374842a18727.tar.gz",
+ "https://bitbucket.org/dtrebbien/eigen/get/374842a18727.tar.gz",
],
- sha256 = "2b526c6888639025323fd4f2600533c0f982d304ea48e4f1663e8066bd9f6368",
- strip_prefix = "eigen-eigen-14e1418fcf12",
+ sha256 = "fa26e9b9ff3a2692b092d154685ec88d6cb84d4e1e895006541aff8603f15c16",
+ strip_prefix = "dtrebbien-eigen-374842a18727",
build_file = str(Label("//third_party:eigen.BUILD")),
)
@@ -353,11 +353,11 @@ def tf_workspace(path_prefix="", tf_repo_name=""):
tf_http_archive(
name = "protobuf_archive",
urls = [
- "https://mirror.bazel.build/github.com/google/protobuf/archive/396336eb961b75f03b25824fe86cf6490fb75e3a.tar.gz",
- "https://github.com/google/protobuf/archive/396336eb961b75f03b25824fe86cf6490fb75e3a.tar.gz",
+ "https://mirror.bazel.build/github.com/dtrebbien/protobuf/archive/50f552646ba1de79e07562b41f3999fe036b4fd0.tar.gz",
+ "https://github.com/dtrebbien/protobuf/archive/50f552646ba1de79e07562b41f3999fe036b4fd0.tar.gz",
],
- sha256 = "846d907acf472ae233ec0882ef3a2d24edbbe834b80c305e867ac65a1f2c59e3",
- strip_prefix = "protobuf-396336eb961b75f03b25824fe86cf6490fb75e3a",
+ sha256 = "eb16b33431b91fe8cee479575cee8de202f3626aaf00d9bf1783c6e62b4ffbc7",
+ strip_prefix = "protobuf-50f552646ba1de79e07562b41f3999fe036b4fd0",
)
Now apply the patch with the following command
cd ~/Downloads/tensorflow
patch -p1 < patch_workspace.bzl
Run the following commands inside ~/Downloads/tensorflow
sed -i -e "s/ __align__(sizeof(T))//g" tensorflow/core/kernels/concat_lib_gpu_impl.cu.cc
sed -i -e "s/ __align__(sizeof(T))//g" tensorflow/core/kernels/depthwise_conv_op_gpu.cu.cc
sed -i -e "s/ __align__(sizeof(T))//g" tensorflow/core/kernels/split_lib_gpu.cu.cc
Run the following command inside ~/Downloads/tensorflow
sed -i.bu '/linkopts = [“-lgomp”]/d' third_party/gpus/cuda/BUILD.tpl
Before configuring tensorflow, find the Compute Capability of your GPU. You can find in this link https://developer.nvidia.com/cuda-gpus When asking about the cuda version, answer with 9.1 instead the default of 9.0.
Run the following command to configure inside ~/Downloads/tensorflow.
Answer only "y" when asking about if you want to install CUDA. Answer "n" (default answer) for Cuda Compiler.
./configure
Now compile the wheel with bazel. I setup the CPU features using the return from the command
sysctl -a | grep machdep.cpu.features
Now configure the CPU_FEATURES and run bazel.
CPU_FEATURES='--copt=-msse4.2 --copt=-mpopcnt --copt=-maes'
bazel build --config=cuda --config=opt $CPU_FEATURES \
--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
tensorflow_pkg=`find /tmp/tensorflow_pkg/tensorflow*.whl`
sudo pip install $tensorflow_pkg