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:
1 | python3 -m venv venv |
Install some dependencies for Theano and OpenCL:
1 | 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):
1 | 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):
1 | pip install git+https://code.csdn.net/u010096836/theano.git |
Next step is installing gpuarray for supporting OpenCL:
1 | git clone https://github.com/Theano/libgpuarray.git |
build native part
1 | cmake . -DCMAKE_INSTALL_PREFIX=../venv/ -DCMAKE_BUILD_TYPE=Release |
export some env for Theano’s dynamic compilation
1 | export LIBRARY_PATH=$LIBRARY_PATH:$PWD/../venv/lib |
install pygpu/gpuarray
1 | python setup.py build |
Now, Theano can use OpenCL to accelerate computing.
We can use Theano’s check1.py
to check that OpenCL is available:
1 | from theano import function, config, shared, tensor, sandbox |
Performance:
- Only use CPU (1.662902 s)
1 | $ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python check1.py |
Using the cpu
- OpenCL over CPU (1.057008 s)
1 | $ THEANO_FLAGS=mode=FAST_RUN,device=opencl0:0,floatX=float32 python check1.py |
Using the cpu
- OpenCL over Intel GPU (0.554572 s)
1 | $ THEANO_FLAGS=mode=FAST_RUN,device=opencl0:1,floatX=float32 python check1.py |
Using the Intel gpu
- OpenCL over AMD GPU (0.470640 s)
1 | $ THEANO_FLAGS=mode=FAST_RUN,device=opencl0:2,floatX=float32 python check1.py |
Using the AMD 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 should install development version of Theano.
How to use Theano? (with OpenCL)