简体   繁体   English

如何在Ubuntu 16.04上使用Theano和GPU?

[英]How can I use Theano with GPU on Ubuntu 16.04?

I use the following script to test if GPU is working: 我使用以下脚本来测试GPU是否正常工作:

#!/usr/bin/env python
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print f.maker.fgraph.toposort()
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

When I run it, I get: 当我运行它时,我得到:

http://pastebin.com/wM9jaGMF http://pastebin.com/wM9jaGMF

The interesting part is at the end: 有趣的部分是在最后:

ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: ('nvcc return status', 1, 'for cmd', 'nvcc -shared -O3 -m64 -Xcompiler -DCUDA_NDARRAY_CUH=c72d035fdf91890f3b36710688069b2e,-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,-fPIC,-fvisibility=hidden -Xlinker -rpath,/home/moose/.theano/compiledir_Linux-4.4--generic-x86_64-with-Ubuntu-16.04-xenial-x86_64-2.7.11+-64/cuda_ndarray -I/home/moose/.local/lib/python2.7/site-packages/theano/sandbox/cuda -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -I/home/moose/.local/lib/python2.7/site-packages/theano/gof -o /home/moose/.theano/compiledir_Linux-4.4--generic-x86_64-with-Ubuntu-16.04-xenial-x86_64-2.7.11+-64/cuda_ndarray/cuda_ndarray.so mod.cu -L/usr/lib -lcublas -lpython2.7 -lcudart')
WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available  (error: cuda unavailable)

My system 我的系统

  • I use Ubuntu 16.04. 我使用的是Ubuntu 16.04。
  • I've installed CUDA through the standard repos ( V7.5.17 ). 我通过标准回购( V7.5.17 )安装了CUDA。 nvcc --version works. nvcc --version有效。
  • I've installed Theano via pip 我通过pip安装了Theano
  • I have CuDNN 4 (works with TensorFlow) 我有CuDNN 4(与TensorFlow一起使用)
  • I set CUDA_ROOT=/usr/bin/ and LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/ (I'm not sure if that is correct) 我设置了CUDA_ROOT=/usr/bin/LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/ (我不确定这是否正确)

My ~/.theanorc is 我的~/.theanorc

[global]
exception_verbosity=high
device=gpu
floatX=float32

[cuda]
root=/usr/bin/

Paths 路径

I think the installation from the standard repos might make things different from a manual installation. 我认为标准回购的安装可能会与手动安装有所不同。 Here are some paths which might uncover some problems: 以下是一些可能会发现一些问题的路径:

/usr/bin/nvcc
/usr/lib/x86_64-linux-gnu/libcuda.so
/usr/lib/x86_64-linux-gnu/libcudart.so
/usr/lib/nvidia-cuda-toolkit
/usr/include/cudnn.h

Question

How can I make it work? 我怎样才能使它工作?

I'm not exactly sure what solved the issue, but one or both of the following ( source ) 我不确定是什么解决了这个问题,但是以下一个或两个( 来源

sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev libblas-dev git
echo -e "\n[nvcc]\nflags=-D_FORCE_INLINES\n" >> ~/.theanorc

I am writing a more general answer here in case others find themselves in a similar situation. 我在这里写一个更一般的答案,以防其他人发现自己处于类似情况。 First, see that theano dependencies are installed, as explained here . 首先,看到theano依赖安装,如解释在这里 you should install nvidia driver as described here , use sudo ubuntu-drivers devices to determine which driver is recommended and install it with sudo apt-get install nvidia-xxx (xxx = 375 at this time). 您应该按照此处的说明安装nvidia驱动程序,使用sudo ubuntu-drivers devices确定推荐的驱动程序,并使用sudo apt-get install nvidia-xxx (此时xxx = 375)安装它。 Then see that the nvidia driver is being used by opening "additional drivers" window (from terminal software-properties-gtk --open-tab=4 ). 然后通过打开“其他驱动程序”窗口(从终端software-properties-gtk --open-tab=4 )看到正在使用nvidia驱动程序。 Setup a ~/.theanorc text file as follows: 设置〜/ .theanorc文本文件如下:

[global]
exception_verbosity=high
device=gpu
floatX=float32

[cuda]
root=/usr/bin/

[nvcc]
flags=-D_FORCE_INLINES

[lib]
cnmem = 1

[lib] section is not necessary but on my laptop performance was about 2 times faster when adding it to .theanorc. [lib]部分不是必需的,但在我的笔记本电脑上,将它添加到.theanorc时,性能提高了大约2倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM