13

I am building tensorflow on my Mac(an hackintosh, so I have a GPU, and already installed CUDA8.0. It works fine with building caffe, so I am sure it works.) I have already set up the environment variables as following(I have put these in .zshrc,.bash_profile and .bashrc):

export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$CUDA_HOME/lib:$CUDA_HOME/extras/CUPTI/lib"

./configure works fine. Then I start build using command bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package. Then I got this error:

    ERROR: /Development/tensorflow/tensorflow/python/BUILD:572:1: Executing genrule //tensorflow/python:array_ops_pygenrule failed: bash failed: error executing command /bin/bash -c ... (remaining 1 argument(s) skipped): com.google.devtools.build.lib.shell.AbnormalTerminationException: Process terminated by signal 5.
dyld: Library not loaded: @rpath/libcudart.8.0.dylib
  Referenced from: /private/var/tmp/_bazel_zarzen/bdf1cb43f3ff02468b610730bd03f348/execroot/tensorflow/bazel-out/host/bin/tensorflow/python/gen_array_ops_py_wrappers_cc
  Reason: image not found
/bin/bash: line 1: 92702 Trace/BPT trap: 5       bazel-out/host/bin/tensorflow/python/gen_array_ops_py_wrappers_cc @tensorflow/python/ops/hidden_ops.txt 1 > bazel-out/local_darwin-opt/genfiles/tensorflow/python/ops/gen_array_ops.py
Target //tensorflow/tools/pip_package:build_pip_package failed to build

I can make sure the missed library is there. And I also tried install pre-built binary(I know it only support CUDA7.5, so I set up the PATH to point to CUDA7.5, but it doesn't work. when I try to import tensorflow, similar error Library not loaded: @rpath/libcudart.7.5.dylib, only version number changed).

I don't know why it cannot find the lib. Anyone can help? or any suggestions?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Zen
  • 133
  • 1
  • 7
  • I've seen SIP interfere with rpath dyld loading, you could try disabling it if you are on El Capitan (system integrity protection) – Yaroslav Bulatov Oct 05 '16 at 04:40
  • Have you been able to resolve this? Same error here. – Stefan Nov 19 '16 at 23:39
  • no, but follow the instructions by Vijay below, can lead you pass the build phase. however in `import` stage still cannot find the `dylib` – Zen Nov 20 '16 at 01:33
  • disable SIP do help! – Zen Mar 06 '17 at 05:35
  • You can find a tuto to build TensorFlow on macOS + a lot more discussions on this topic on this github thread : https://github.com/tensorflow/tensorflow/issues/9072#issue – Ericvulpi Aug 05 '17 at 08:34

2 Answers2

9

The following should fix the error.

Find the file "genrule-setup.sh". The file should be in

<tensorflow source dir>/bazel-tensorflow/external/bazel_tools/tools/genrule/

If the timestamp of this file changes then bazel build will fail saying the file is corrupted. So before modifying this file make a note of the timestamp

stat genrule-setup.sh

You should get an output like this:

16777220 25929227 -rwxr-xr-x 1 user wheel 0 242 "Oct 12 23:46:28 2016" "Oct 10 21:49:39 2026" "Oct 12 21:49:39 2016" "Oct 12 21:49:38 2016" 4096 8 0 genrule-setup.sh

Note down the second timestamp "Oct 10 21:49:39 2026" from the above output

edit the genrule-setup.sh file

nano genrule-setup.sh

and add the environment configuration to the end of the file

export DYLD_LIBRARY_PATH=/usr/local/cuda/lib

save and close the editor.

Then change the timestamp to the original timestamp

touch -t YYYYMMDDhhmm.SS genrule-setup.sh

for e.g.

touch -t 202610102149.39 genrule-setup.sh

Finally, create a symbolic link to avoid "Segmentation fault: 11" error

ln -sf /usr/local/cuda/lib/libcuda.dylib /usr/local/cuda/lib/libcuda.1.dylib

Now restart the build

bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
Vijay
  • 1,506
  • 2
  • 9
  • 8
  • 2
    Now I can build. But after build and installation. I try to import tensorflow, again `image not found`; ``` – Zen Oct 13 '16 at 00:37
  • detailed `ImportError: dlopen(/usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so, 10): Library not loaded: @rpath/libcudart.8.0.dylib` – Zen Oct 13 '16 at 00:38
  • check if DYLD_LIBRARY_PATH env variable is set to /usr/local/cuda/lib. also check the /usr/local/cuda/lib folder contains libcudart.8.0.dylib – Vijay Oct 13 '16 at 11:25
  • Confirmed that DYLD_LIBRARY_PATH is set to `/usr/local/cuda/lib` and `libcudart.8.0.dylib` is there. Also I have checked the `python` path, I have set `PYTHONPATH="/usr/local/cuda/lib:/usr/local/lib/python2.7/site-packages/tensorflow//python:$PYTHONPATH"` – Zen Oct 13 '16 at 14:14
  • After `ImportError` there is another error, which I forgot mention: `Error importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its source directory; please exit the tensorflow source tree, and relaunch your python interpreter from there.` – Zen Oct 13 '16 at 16:38
  • as the error message says, you should not run python from the tensorflow directory. change to a different directory and try it again – Vijay Oct 14 '16 at 08:28
  • are you running some third party library or application using Tensorflow? To check if tensorflow is installed properly run `python -c "import tensorflow as f"` and see if there are no error messages – Vijay Oct 15 '16 at 11:42
  • It has the error `ImportError: dlopen(/usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so, 10): Library not loaded: @rpath/libcudart.8.0.dylib Referenced from: /usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so Reason: image not found Error importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its source directory; please exit the tensorflow source tree, and relaunch your python interpreter`. I don't have third party lib, I just want to run tensoflow GPU version – Zen Oct 15 '16 at 18:51
  • When do you get this error? I mean which program or code are you running to test tensorflow GPU? – Vijay Oct 17 '16 at 14:04
  • No other code, I just want to `import tensorflow` under `python REPL` – Zen Oct 18 '16 at 00:29
  • after the build, I hope you installed it with the following command ```sudo pip install /tmp/tensorflow_pkg/tensorflow-0.11.0rc0-py2-none-any.whl``` Sometimes you might see CUDA 3.0 compatibility issue which can be resolved by selecting 3.0,3.5 when running the ./configure script What is your OSX version and GPU card? – Vijay Oct 18 '16 at 04:25
  • I have installed using that command. I am using CUDA8.0, and I am pretty sure that I have chosen the right version at `./configure` phase. The Cudnn version is 5, and it already installed in `/usr/local/cuda/lib`. – Zen Oct 18 '16 at 19:26
0

When running ./configure, make sure you specify the correct cuda version. You can find this out by running:

/usr/local/cuda/bin/nvcc --version

You can pass environment variables to bazel during build using the action_env flag.

bazel build -c opt --config=cuda \
    --action_env=DYLD_LIBRARY_PATH=/usr/local/cuda/lib \
    //tensorflow/tools/pip_package:build_pip_package

https://bazel.build/designs/2016/06/21/environment.html