Search code examples
pythoncudagputheano

How can I run theano on GPU


If I run the following code with python 3.5

import numpy as np
import time
import theano
A = np.random.rand(1000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices('XY')
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print ("NP time: %f[s], theano time: %f[s] **(times should be close when run
on CPU!)**" %(np_end-np_start, t_end-t_start))
print ("Result difference: %f" % (np.abs(AB-tAB).max(), ))

I get the output

NP time: 0.161123[s], theano time: 0.167119[s] (times should be close when
run on CPU!)
Result difference: 0.000000

it says if the times are close, it means that I am running on my CPU.

How can I run this code on my GPU?

NOTE:

  • I have a workstation with Nvidia Quadro k4200.
  • I have installed Cuda toolkit
  • I have successfully worked an cuda vectorAdd sample project on VS2012.

Solution

  • You configure Theano to use a GPU by specifying the device=gpu in Theano's config. There are two principle methods for setting the config: (1) in the THEANO_FLAGS environment variable, or (2) via the .theanorc file. Both methods, and all of Theano's configuration flags, are documented.

    You will know that Theano is using the GPU if, after calling import theano you see a message that looks something like this

    Using gpu device 0: GeForce GT 640 (CNMeM is disabled)
    

    The details may vary for you but if no message appears at all then Theano is using the CPU only.

    Note also that even if you see the GPU message, your particular computation graph may not run on the GPU. To see which parts of your computation are running on the GPU print its compiled and optimized graph

    f = theano.function(...)
    theano.printing.debugprint(f)
    

    Operations that start with the prefix 'Gpu' will run on the GPU. Operations that do not have that prefix to their name will run on the CPU.