How to use Theano? (with OpenCL)

I have a MacBook Pro with AMD display card, so I want Theano use OpenCL.

First, create virtualenv and activate it:

python3 -m venv venv
source venv/bin/activate

Install some dependencies for Theano and OpenCL:

pip install cython nose

those dependencies isn’t in setup.py of Theano (or pygpu).

Install Theano (latest development version, so far, 0.8.2 doesn’t support OpenCL):

pip install git+https://github.com/Theano/Theano.git

If github is temporary unavailable, try my personal csdn mirror(CSDN CODE can sync github repo conveniently):

pip install git+https://code.csdn.net/u010096836/theano.git

Next step is installing gpuarray for supporting OpenCL:

git clone https://github.com/Theano/libgpuarray.git
cd libgpuarray

# build native part
cmake . -DCMAKE_INSTALL_PREFIX=../venv/ -DCMAKE_BUILD_TYPE=Release
make
make install

# export some env for Theano's dynamic compilation
export LIBRARY_PATH=$LIBRARY_PATH:$PWD/../venv/lib
export CPATH=$CPATH:$PWD/../venv/

# install pygpu/gpuarray
python setup.py build
python setup.py install

Now, Theano can use OpenCL to accelerate computing.


We can use Theano’s check1.py to check that OpenCL is available:

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')

Performance:

  • Only use CPU (1.662902 s)
$ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python check1.py 
[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)]
Looping 1000 times took 1.662902 seconds
Result is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761
  1.62323284]
Used the cpu
  • CPU over OpenCL (1.057008 s)
$ THEANO_FLAGS=mode=FAST_RUN,device=opencl0:0,floatX=float32 python check1.py 
Mapped name None to device opencl0:0: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, (False,))>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 1.057008 seconds
Result is [ 1.23178029  1.61879325  1.52278078 ...,  2.20771813  2.29967737
  1.62323272]
Used the gpu
  • Intel GPU over OpenCL (0.554572 s)
$ THEANO_FLAGS=mode=FAST_RUN,device=opencl0:1,floatX=float32 python check1.py 
Mapped name None to device opencl0:1: Iris Pro
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, (False,))>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.554572 seconds
Result is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761
  1.62323284]
Used the gpu
  • AMD GPU over OpenCL (0.470640 s)
$ THEANO_FLAGS=mode=FAST_RUN,device=opencl0:2,floatX=float32 python check1.py 
Mapped name None to device opencl0:2: AMD Radeon R9 M370X Compute Engine
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, (False,))>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.470640 seconds
Result is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761
  1.62323284]
Used the gpu

If you using stable(0.8.2) Theano, you will meet this error when you try to use OpenCL:

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

so, you’d better install development version of Theano.

8 thoughts on “How to use Theano? (with OpenCL)”

  1. 博主你好,我的笔记本是win7的,装了一张AMD的入门显卡,最近也在用theano做深度学习的小实验,在GPU上耗费了两天时间,没有解决问题,恰好看到了博主的文章。我在装libgpuarray时遇到了下面的问题,不知道您遇到过没有。
    *********************************************************
    [email protected] MINGW64 /d
    $ cd libgpuarray

    [email protected] MINGW64 /d/libgpuarray (master)
    $ cmake . -DCMAKE_INSTALL_PREFIX=../venv/ -DCMAKE_BUILD_TYPE=Release
    — The C compiler identification is unknown
    CMake Error at CMakeLists.txt:3 (PROJECT):
    The CMAKE_C_COMPILER:

    cl

    is not a full path and was not found in the PATH.

    To use the NMake generator with Visual C++, cmake must be run from a shell
    that can use the compiler cl from the command line. This environment is
    unable to invoke the cl compiler. To fix this problem, run cmake from the
    Visual Studio Command Prompt (vcvarsall.bat).

    Tell CMake where to find the compiler by setting either the environment
    variable “CC” or the CMake cache entry CMAKE_C_COMPILER to the full path to
    the compiler, or to the compiler name if it is in the PATH.

    — Configuring incomplete, errors occurred!
    See also “D:/libgpuarray/CMakeFiles/CMakeOutput.log”.
    See also “D:/libgpuarray/CMakeFiles/CMakeError.log”.

    [email protected] MINGW64 /d/libgpuarray (master)
    $
    ********************************************************
    另外,是不是在装libgpuarray之前要装好AMD APP SDK,望您赐教。
    这是我的邮箱:[email protected],如果你恰巧碰到过这样的问题,望您赐教。
    祝好

      1. 的确,手动添加了VS的一堆环境变量就解决了这个问题。但是,执行nmake时又有了下面的问题,我没有学过makefile,不知道您知不知道这是哪儿的问题。报错如下:
        **************************************************
        [email protected] MINGW64 /d/libgpuarray (master)
        $ nmake

        Microsoft (R) ▒▒▒▒ά▒▒ʵ▒ù▒▒▒ 11.00.50727.1 ▒▒
        ▒▒Ȩ▒▒▒▒ (C) Microsoft Corporation▒▒▒▒▒▒▒▒▒▒Ȩ▒▒▒▒

        makefile(4) : fatal error U1034: ▒﷨▒▒▒▒: ȱ▒ٷָ▒▒▒
        Stop.

        [email protected] MINGW64 /d/libgpuarray (master)
        $ nmake install

        Microsoft (R) ▒▒▒▒ά▒▒ʵ▒ù▒▒▒ 11.00.50727.1 ▒▒
        ▒▒Ȩ▒▒▒▒ (C) Microsoft Corporation▒▒▒▒▒▒▒▒▒▒Ȩ▒▒▒▒

        makefile(4) : fatal error U1034: ▒﷨▒▒▒▒: ȱ▒ٷָ▒▒▒
        Stop.

        [email protected] MINGW64 /d/libgpuarray (master)
        ***************************************************
        里边还有乱码。为了能实现GPU运算,捣鼓了好几天了,到现在都没弄好。望大神赐教。
        祝好。

        1. U1034看起来是路径分隔符的问题,你检查下环境变量,然后看下报错,这些网上也应该有人遇到,google一般会有解决方案。

          1. 谢谢您的帮助,之前在windows上没能成功,需要装很多没有接触过的东西。后来看到网上有人在ubuntu上安装成功了,于是装了ubuntu14.04的双系统,重新试了一下。
            我在装好AMD APP SDK3.0后终端运行clinfo显示两个计算设备,一个是AMD独显,一个是CPU。但是在装libgpuarray时在cmake一步显示没有找到opencl,这是不是说AMDSDK没有配置好啊,具体输出信息如下:
            ************************************************
            [email protected]:~/myvenv/hehe/libgpuarray/build$ cmake ..
            — The C compiler identification is GNU 4.8.4
            — Check for working C compiler: /usr/bin/cc
            — Check for working C compiler: /usr/bin/cc — works
            — Detecting C compiler ABI info
            — Detecting C compiler ABI info – done
            — Detecting C compile features
            — Detecting C compile features – done
            CUDA_TOOLKIT_ROOT_DIR not found or specified
            — Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_NVCC_EXECUTABLE CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY)
            — Could NOT find OpenCL (missing: OPENCL_LIBRARIES OPENCL_INCLUDE_DIRS)
            — Looking for strlcat
            — Looking for strlcat – not found
            — Looking for mkstemp
            — Looking for mkstemp – found
            — Found PkgConfig: /usr/bin/pkg-config (found version “0.26”)
            — Checking for one of the modules ‘check’
            — Configuring done
            — Generating done
            — Build files have been written to: /home/marcovaldo/myvenv/hehe/libgpuarray/build
            ************************************************
            另外在执行THEANO_FLAGS=mode=FAST_RUN,device=opencl0:0,floatX=float32 python check1.py时报错如下:
            ************************************************
            ERROR (theano.sandbox.gpuarray): Could not initialize pygpu, support disabled
            Traceback (most recent call last):
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/theano/sandbox/gpuarray/__init__.py”, line 95, in
            init_dev(config.device)
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/theano/sandbox/gpuarray/__init__.py”, line 46, in init_dev
            “Make sure Theano and libgpuarray/pygpu ”
            RuntimeError: (‘Wrong major API version for gpuarray:’, -9997, ‘Make sure Theano and libgpuarray/pygpu are in sync.’)
            ************************************************
            还有,在python虚拟环境中运行pygpu.test()时报错如下:
            ************************************************
            ERROR: Failure: GpuArrayException (Invalid value or operation: 0)
            ———————————————————————-
            Traceback (most recent call last):
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/nose/loader.py”, line 418, in loadTestsFromName
            addr.filename, addr.module)
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/nose/importer.py”, line 47, in importFromPath
            return self.importFromDir(dir_path, fqname)
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/nose/importer.py”, line 94, in importFromDir
            mod = load_module(part_fqname, fh, filename, desc)
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/pygpu-0.2.1-py2.7-linux-x86_64.egg/pygpu/tests/test_blas.py”, line 4, in
            from .support import (guard_devsup, gen_gpuarray, context)
            File “/home/marcovaldo/myvenv/venv/local/lib/python2.7/site-packages/pygpu-0.2.1-py2.7-linux-x86_64.egg/pygpu/tests/support.py”, line 32, in
            context = gpuarray.init(get_env_dev())
            File “pygpu/gpuarray.pyx”, line 562, in pygpu.gpuarray.init (pygpu/gpuarray.c:8359)
            File “pygpu/gpuarray.pyx”, line 508, in pygpu.gpuarray.pygpu_init (pygpu/gpuarray.c:7974)
            File “pygpu/gpuarray.pyx”, line 965, in pygpu.gpuarray.GpuContext.__cinit__ (pygpu/gpuarray.c:12332)
            GpuArrayException: Invalid value or operation: 0
            ************************************************
            不知道您有没有遇到过这个问题,请您再帮我看看,小弟不胜感激。

  2. Hi,

    Im running into an issue once everything is installed. I am getting an error saying ‘ [CL_DEVICE_NOT_AVAILABLE]: OpenCL Error : Error: build program driver returned (-3)
    OpenCL Warning : clBuildProgram failed: could not build program for 0xffffffff (Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz) (err:-3)’

    Any thoughts? I’ve scoured the net and don’t seem to be able to find a solution at the moment.

    Thanks.

    Anthony

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 11 = 12