Saturday, September 17, 2016

failed to find libcuda.so on this system: Failed precondition: could not dlopen DSO: libcuda.1.dylib

I got that error on my Mac OS X after I tried to do import tensorflow as tf. The solution is a bit hacky, but it's based on the fact that even though the system was unable to find libcuda.1.dylib, if you look in /usr/local/cuda/lib you find that libcuda.dylib does exist. So I simply did:

cd /usr/local/cuda/lib
sudo ln -s libcuda.dylib libcuda.1.dylib

And after that, import tensorflow as tf did not give me an error and I was able to verify that the GPU was in use by following the instructions under "Logging Device Placement" here.

Happy tensorflowing.

ld: framework not found CUDA

On the tensorflow installations instruction page, the following commands are listed as a way to verify the CUDA installation:
$ cp -r /usr/local/cuda/samples ~/cuda-samples
$ pushd ~/cuda-samples
$ make
$ popd
$ ~/cuda-samples/bin/x86_64/darwin/release/deviceQuery

However, when running the make command on my Mac, I got the following error:
ld: framework not found CUDA

Based on the instructions here, I made the following changes to fix it. As stated on that site, for every Makefile in ~/cuda-samples that had
-L$(CUDA_PATH)/lib -framework CUDA
we replace it with
-L$(CUDA_PATH)/lib -F/Library/Frameworks -framework CUDA
as shown below (it seems to be on line 201):


I found the list of files to make the change on using grep:
avmac:cuda-samples$ grep -r "framework CUDA" .
./0_Simple/clock_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./0_Simple/inlinePTX_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./0_Simple/matrixMul_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./0_Simple/simpleAtomicIntrinsics_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./0_Simple/simpleTemplates_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./0_Simple/simpleVoteIntrinsics_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./0_Simple/vectorAdd_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./4_Finance/binomialOptions_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./4_Finance/BlackScholes_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA
./4_Finance/quasirandomGenerator_nvrtc/Makefile: LDFLAGS += -L$(CUDA_PATH)/lib -framework CUDA

However, that wasn't enough. I also had to make the following change on line 139, where I change
-rpath $(CUDA_PATH)/lib
to
-rpath $(CUDA_PATH)/lib -F/Library/Frameworks
as shown below:


I had to make that change on the following files:
./0_Simple/matrixMulDrv/Makefile
./0_Simple/simpleTextureDrv/Makefile
./0_Simple/simpleVoteIntrinsics/Makefile
./0_Simple/vectorAddDrv/Makefile
./1_Utilities/deviceQueryDrv/Makefile
./6_Advanced/ptxjit/Makefile
./6_Advanced/threadMigration/Makefile

After that, the compilations worked and I was able to run deviceQuery. Then I ran sudo pip install --upgrade $TF_BINARY_URL, but I got two errors when I tried to import tensorflow in a python session: one error was about libcuda and the other was about protobuf. The solution to the libcuda problem I have explained in this post. For the protobuf sollution, I had to do a brew install protobuf followed by the following solution from this thread:
pip uninstall protobuf
pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp27-none-any.whl

Sunday, December 6, 2015

fatal error: 'cblas.h' file not found

I once again found myself installing caffe on my Mac, and ran into that error. The solution was to install openblas via "brew install openblas", and then edit the relevant lines in Makefile.config to look like this:

BLAS := open
BLAS_INCLUDE := /usr/local/Cellar/openblas/0.2.14_1/include
BLAS_LIB := /usr/local/Cellar/openblas/0.2.14_1/lib

Also, I was getting some weird permission errors about inability to do the linking (when using brew install). I tried "sudo brew link openblas" and then got a confusing error about sudo. The solution was to execute the following lines:

sudo chown -R `whoami`:admin /usr/local/bin

sudo chown -R `whoami`:admin /usr/local/share

After that I was able to brew link as needed.

Contents of my Makefile.config, largely for my own reference (I think the only other things I modified were the python paths for anaconda: http://pastebin.com/RscLA8j9)

Thursday, December 3, 2015

How to parse a caffe deploy.prototxt file or solver.prototxt file using python.


1) Install google's protobuf by downloading the source from https://developers.google.com/protocol-buffers/docs/downloads (I had protobuf-2.6.1.tar.gz), untarring (tar -zxvf [filename]), and following the instructions in the README

2) Get your caffe .proto file. It lives in [caffe folder]/src/caffe/proto/caffe.proto

3) Compile the caffe.proto file with protoc --python_out=. caffe.proto; it will produce caffe_pb2.py in the same directory.

4.1) For parsing a deploy.prototxt file, use:

import caffe_pb2 #you created this module with the protoc command
from google.protobuf.text_format import Merge
net = caffe_pb2.NetParameter()
Merge((open("deploy.prototxt",'r').read()), net)

4.2) For parsing a solver.prototxt file, use:

import caffe_pb2 #you created this module with the protoc command
from google.protobuf.text_format import Merge
solver = caffe_pb2.SolverParameter()
Merge((open("solver.prototxt",'r').read()), solver)

Remember, you can inspect the attributes of the object using dir(net) or dir(solver)

Tuesday, August 18, 2015

Yaml is really slow in vim

Apparently this is a known issue.

Here I'm documenting some specifics of the fix that weren't clear from the stackoverflow answer. Not totally sure if I did the "right" thing but it works.

1) Download https://raw.githubusercontent.com/stephpy/vim-yaml/master/after/syntax/yaml.vim to ~/.vim
2) Put "au BufNewFile,BufRead *.yaml,*.yml so ~/.vim/yaml.vim" at the top of ~/.vimrc

You should be good to go.

Recursive find and replace

This was way too hard to figure out. I gave up on using sed because I was getting "invalid command code ." and "illegal byte sequence" due to the -i option being interpreted differently on osx, and ended up corrupting my git repository..

find . -type f -print0 | xargs -0 perl -i -pe '$_ =~ s/progressUpdates/progressUpdate/g' 

Tuesday, July 14, 2015

Numpy str on integers extremely slow?

I found a weird performance difference today: create a numpy array of integers, and then convert it to an array of strings in a list comprehension. It's weirdly slow. Cast the numpy integers to python int's before calling str(), and you get nearly a 10x speedup. Observe:


$ python -m timeit 'import numpy as np; newX = [x for x in np.arange(4096)]; [str(x) for x in newX]'
100 loops, best of 3: 10.5 msec per loop
$ python -m timeit 'import numpy as np; newX = [x for x in np.arange(4096)]; [str(int(x)) for x in newX]'
1000 loops, best of 3: 1.23 msec per loop