在我的Macbook Pro上安装能使用GPU加速的Theano

目的

最近深度学习的应用非常火爆,有意向要在NLP上应用深度学习的我要对各种开源的深度学习库进行探索,目前比较流行的Python语言的深度学习库有Theano、Google开源的Tensorflow、keras等。

由于我日常使用的机器都是Macbook Pro,没有N卡,只有一张Intel Iris(TM) Graphics 6100(本人对硬件不是很了解),所以不能使用cuda,只能使用opencl的库。所以想使用GPU加速训练的话,这电脑的硬件已经不能支持Tensorflow了(Tensorflow只支持cuda,计算能力要在3.5以上,不太懂),我还是选择Theano(既支持cuda也支持opencl)做做实验吧。

前提依赖

我使用的python版本是2.7。Theano库需要用到依赖库为g++,numpy,scipy,Cython,BLAS,我们只需要使用pip install 把他们都安装好就行。这个可以参考官方文档。

安装Theano

参考:
官方安装文档:http://deeplearning.net/software/theano/install.html
gpuarray安装文档:http://deeplearning.net/software/libgpuarray/installation.html

先安装GPU的库:

  1. 下载libgpuarray源码:
git clone https://github.com/Theano/libgpuarray.git
    cd libgpuarray
  1. 安装libgpuarray:
cd <dir>
mkdir Build
cd Build
# you can pass -DCMAKE_INSTALL_PREFIX=/path/to/somewhere to install to an alternate location
cmake .. -DCMAKE_BUILD_TYPE=Release # or Debug if you are investigating a crash
make
make install
cd ..
  1. 安装pygpu:
# This must be done after libgpuarray is installed as per instructions above.
python setup.py build
python setup.py install
  1. 测试是否安装成功:
python -c "import pygpu;pygpu.test()"

再安装Theano

在终端中输入

pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

(这里用git的原因后面提到的坑再说)

到这里基本上已经安装好的所有的库。

后面附上测试代码(test.py):

from theano import function, config, shared, tensor, sandbox
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([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(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, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

直接运行会显示如下信息:

$python test.py
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 1.492254 seconds
Result is [ 1.23178032  1.61879341  1.52278065 ...,  2.20771815  2.29967753
1.62323285]
Used the cpu
$python test.py
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 1.492254 seconds
Result is [ 1.23178032  1.61879341  1.52278065 ...,  2.20771815  2.29967753
  1.62323285]
Used the cpu

可以开始上述的程序是通过cpu运行的,还没有使用gpu。
那么如何用上gpu呢?这时候,我们需要加上一些环境变量。(由于我们之前已经安装了libgpuarray,所以theano可以支持opencl。)
运行命令:
1. 使用OpenCL和CPU:

$THEANO_FLAGS=device=opencl0:0 python test.py
Mapped name None to device opencl0:0: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, (False,))>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 1.65111804008 seconds
Result is [ 1.23178029  1.61879325  1.52278078 ...,  2.20771813  2.29967737
  1.62323272]
Used the gpu
chenyutongdeMacBook-Pro:dl_test Derrick$ THEANO_FLAGS=device=opencl0:0,floatX=float32  python theano_test.py 
Mapped name None to device opencl0:0: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, (False,))>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 1.72186207771 seconds
Result is [ 1.23178029  1.61879325  1.52278078 ...,  2.20771813  2.29967737
  1.62323272]
Used the gpu

虽然代码显示”used the gpu”,但是实际上看得上是使用CPU。

  1. 使用OpenCL和GPU:
$ THEANO_FLAGS=device=opencl0:1,floatX=float32  python theano_test.py 
Mapped name None to device opencl0:1: Intel(R) Iris(TM) Graphics 6100
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, (False,))>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 1.09479188919 seconds
Result is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761
  1.62323284]
Used the gpu

看得上这程序已经使用上我MacbookPro的显卡了。

遇到的坑

一开始我按照官方的教程一路安装下来:
安装Theano时使用的命令是pip install theano。(此命令安装的是theano发布的最新版本,注意是发布的版本,我写的时候是v0.8.2,但github上托管的代码已经是v0.9.0版了)
libgpuarray同样也是下载源码编译安装,版本是-9998(我不太清楚为什么这样定义)
都安装完之后,当运行THEANO_FLAGS=device=opencl0:1,floatX=float32 python theano_test.py 命令时,出现了这样的错误:

Wrong major API version for gpuarray:-9998 Make sure Theano and libgpuarray/pygpu are in sync.

于是我们google了一下这样的错误,搜不到提问,搜到了源码(有github上的,也有其他网站)只能查一下源码看看哪里蹦出来的错误。
发现在这个网站上的代码里显示的是需要-10000版本,而github上官方最新的是-9998。所以我就怀疑我安装的theano不是最新的版本。通过使用theano.test()命令查看版本后果然如此,安装的是v0.8.2。所以后来我使用了

pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

命令就好了。

参考:
http://codechina.org/2016/04/how-to-install-theano-on-mac-os-x-ei-caption-with-opencl-support/
http://deeplearning.net/software/theano/tutorial/using_gpu.html#using-gpu
http://deeplearning.net/software/libgpuarray/installation.html
http://deeplearning.net/software/theano/install.html
https://github.com/Theano/Theano