在我的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的庫:
- 下載libgpuarray源碼:
git clone https://github.com/Theano/libgpuarray.git
cd libgpuarray
- 安裝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 ..
- 安裝pygpu:
# This must be done after libgpuarray is installed as per instructions above.
python setup.py build
python setup.py install
- 測試是否安裝成功:
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。
- 使用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