如何在 Ubuntu 14.04 x64 上安装 Theano,并配置它以使用 GPU? [英] How to install Theano on Ubuntu 14.04 x64, and configure it so that it uses the GPU?

查看:35
本文介绍了如何在 Ubuntu 14.04 x64 上安装 Theano,并配置它以使用 GPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照在当前 Ubuntu 上轻松安装优化的 Theano 上的说明进行操作但它不起作用:每当我使用 GPU 运行 Theano 脚本时,它都会给我错误消息:

I tried to follow the instructions on Easy Installation of an Optimized Theano on Current Ubuntu but it doesn't work: whenever I run a Theano script using GPU, it gives me the error message:

已安装 CUDA,但设备 gpu 不可用(错误:无法获取可用 gpu 数量:未检测到支持 CUDA 的设备)

CUDA is installed, but device gpu is not available (error: Unable to get the number of gpus available: no CUDA-capable device is detected)

<小时>

更具体地说,按照链接网页中的说明,我执行了以下步骤:


More specifically, following the instructions in the linked webpage, I executed the following steps:

# Install Theano
sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git
sudo pip install Theano

# Install Nvidia drivers and CUDA
sudo apt-get install nvidia-current
sudo apt-get install nvidia-cuda-toolkit

然后我重新启动并尝试运行:

Then I rebooted and tried running: 

THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python gpu_test.py # gpu_test.py comes from http://deeplearning.net/software/theano/tutorial/using_gpu.html

但我明白了:

f@f-Aurora-R4:~$ THEANO_FLAGS=’mode=FAST_RUN,device=gpu,floatX=float32,cuda.root=/usr/lib/nvidia-cuda-toolkit’ python gpu_test.py WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: Unable to get the number of gpus available: no CUDA-capable device is detected) [Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)] Looping 1000 times took 2.199992 seconds Result is [ 1.23178029 1.61879337 1.52278066 ..., 2.20771813 2.29967761 1.62323284] Used the cpu

推荐答案

(我在 Ubuntu 14.04.4 LTS x64 和 Kubuntu 14.04.4 LTS x64 上测试了以下内容,我想它应该适用于大多数 Ubuntu 变体)

(I tested the following on Ubuntu 14.04.4 LTS x64 and Kubuntu 14.04.4 LTS x64, I guess it should work on most Ubuntu variants)

官网上的说明已经过时了.相反,您可以使用以下说明(假设新安装的 Kubuntu 14.04 LTS x64):

The instructions on the official website are outdated. Instead you can use the following instructions (assume a freshly installed Kubuntu 14.04 LTS x64):

# Install Theano
sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git
sudo pip install Theano

# Install Nvidia drivers, CUDA and CUDA toolkit, following some instructions from http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
wget http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb # Got the link at https://developer.nvidia.com/cuda-downloads
sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
sudo apt-get update
sudo apt-get install cuda

sudo reboot

此时,运行 nvidia-smi 应该可以,但运行 nvcc 将不起作用.

At that point, running nvidia-smi should work, but running nvcc won't work.

# Execute in console, or (add in ~/.bash_profile then run "source ~/.bash_profile"):
export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH

此时,nvidia-sminvcc 都应该可以工作.

At that point, both nvidia-smi and nvcc should work.

要测试 Theano 是否能够使用 GPU:

To test whether Theano is able to use the GPU:

将以下内容复制粘贴到 gpu_test.py 中:

Copy-paste the following in gpu_test.py:

# Start gpu_test.py
# From http://deeplearning.net/software/theano/tutorial/using_gpu.html#using-gpu
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 %f seconds" % (iters, t1 - t0))
print("Result is %s" % (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')
# End gpu_test.py

并运行它:

THEANO_FLAGS='mode=FAST_RUN,device=gpu,floatX=float32' python gpu_test.py

应该返回:

f@f-Aurora-R4:~$ THEANO_FLAGS='mode=FAST_RUN,device=gpu,floatX=float32' python gpu_test.py
Using gpu device 0: GeForce GTX 690
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.658292 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu

要了解您的 CUDA 版本:

To know your CUDA version:

​nvcc -V

示例:

username@server:~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2015 NVIDIA Corporation
Built on Tue_Aug_11_14:27:32_CDT_2015
Cuda compilation tools, release 7.5, V7.5.17

<小时>

添加cuDNN

添加 cuDNN(来自 http://deeplearning.net 的说明/software/theano/library/sandbox/cuda/dnn.html):

  1. https://developer.nvidia.com/rdp/cudnn-download<下载 cuDNN(需要注册,免费)
  2. tar -xvf cudnn-7.0-linux-x64-v3.0-prod.tgz
  3. 执行以下操作之一

选项 1:将 *.h 文件复制到 CUDA_ROOT/include 并将 *.so* 文件复制到 CUDA_ROOT/lib64(默认情况下,CUDA_ROOT 在 Linux 上是 /usr/local/cuda).

Option 1: Copy the *.h files to CUDA_ROOT/include and the *.so* files to CUDA_ROOT/lib64 (by default, CUDA_ROOT is /usr/local/cuda on Linux).

sudo cp cuda/lib64/* /usr/local/cuda/lib64/
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/

选项 2:

export LD_LIBRARY_PATH=/home/user/path_to_CUDNN_folder/lib64:$LD_LIBRARY_PATH
export CPATH=/home/user/path_to_CUDNN_folder/include:$CPATH
export LIBRARY_PATH=/home/user/path_to_CUDNN_folder/lib64:$LD_LIBRARY_PATH

默认情况下,Theano 会检测它是否可以使用 cuDNN.如果是这样,它将使用它.如果没有,Theano 优化将不会引入 cuDNN 操作.所以如果用户没有手动引入它们,Theano 仍然可以工作.

By default, Theano will detect if it can use cuDNN. If so, it will use it. If not, Theano optimizations will not introduce cuDNN ops. So Theano will still work if the user did not introduce them manually.

要在 Theano 无法使用 cuDNN 时出现错误,请使用以下 Theano 标志:optimizer_including=cudnn.

To get an error if Theano can not use cuDNN, use this Theano flag: optimizer_including=cudnn.

示例:

THEANO_FLAGS='mode=FAST_RUN,device=gpu,floatX=float32,optimizer_including=cudnn' python gpu_test.py

要了解您的 cuDNN 版本:

To know your cuDNN version:

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

<小时>

添加 CNMeM

CNMeM 库是帮助深度学习框架管理 CUDA 内存的简单库"..


Adding CNMeM

The CNMeM library is a "Simple library to help the Deep Learning frameworks manage CUDA memory.".

# Build CNMeM without the unit tests
git clone https://github.com/NVIDIA/cnmem.git cnmem
cd cnmem
mkdir build
cd build
sudo apt-get install -y cmake
cmake ..
make

# Copy files to proper location
sudo cp ../include/cnmem.h /usr/local/cuda/include
sudo cp *.so /usr/local/cuda/lib64/
cd ../..

要与 Theano 一起使用,您需要添加 lib.cnmem 标志.示例:

To use with Theano, you need to add the lib.cnmem flag. Example:

THEANO_FLAGS='mode=FAST_RUN,device=gpu,floatX=float32,lib.cnmem=0.8,optimizer_including=cudnn' python gpu_test.py

脚本的第一个输出应该是:

The first output of the script should be:

Using gpu device 0: GeForce GTX TITAN X (CNMeM is enabled with initial size: 80.0% of memory, cuDNN 5005)

lib.cnmem=0.8 意味着它可以使用高达 80% 的 GPU.

lib.cnmem=0.8 means that it can use up to 80% of the GPU.

据报道,CNMeM 提供了一些有趣的速度改进,并且得到了 Theano、Torch 和 Caffee 的支持.

CNMeM has been reported to give some interesting speed improvements, and is supported by Theano, Torch, and Caffee.

Theano - 源 1:

加速取决于许多因素,例如形状和模型本身.速度提高了 0 到 2 倍.

The speed up depend of many factor, like the shapes and the model itself. The speed up go from 0 to 2x faster.

Theano - 来源 2:

如果您不更改 Theano 标志 allow_gc,您可以期望在 GPU 上提高 20% 的速度.在某些情况下(小型模型),我们看到速度提高了 50%.

If you don't change the Theano flag allow_gc, you can expect 20% speed up on the GPU. In some case (small models), we saw a 50% speed up.

<小时>

在多个 CPU 内核上运行 Theano

附带说明,您可以使用 OMP_NUM_THREADS=[number_of_cpu_cores] 标志.示例:


Running Theano on multiple CPU cores

As a side note, you can run Theano on multiple CPU cores with the OMP_NUM_THREADS=[number_of_cpu_cores] flag. Example:

OMP_NUM_THREADS=4 python gpu_test.py 

脚本theano/misc/check_blas.py 输出关于使用哪个 BLAS 的信息:

The script theano/misc/check_blas.py outputs information regarding which BLAS is used:

cd [theano_git_directory]
OMP_NUM_THREADS=4 python theano/misc/check_blas.py

<小时>

运行 Theano 的测试套件:


To run Theano's test suite:

nosetests theano

sudo pip install nose-parameterized
import theano
theano.test()

常见问题:

这篇关于如何在 Ubuntu 14.04 x64 上安装 Theano,并配置它以使用 GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆