8

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.

3 Answers 3

17

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.

8
7

If you are on Linux, create a .theanorc file in your home folder and add the following to set up theano to run on GPU.

[global]
device = gpu
floatx = float32
2
  • 2
    just a detail. it's float32, not floar32 (typo). I cannot edit it :-(
    – FiReTiTi
    Commented Feb 26, 2016 at 19:57
  • for version 1.0.4; device=cuda
    – mokarakaya
    Commented Mar 12, 2019 at 22:18
5

Alternatively, if you want to use the GPU programattically:

import theano.sandbox.cuda
theano.sandbox.cuda.use("gpu0")

You should see a message like this:

Using gpu device 0: Tesla K80

Useful if the environment you are running in isn't easy to configure.

1
  • 3
    This works for theano v0.9. and it is now deprecated. You are importing theano.sandbox.cuda. This is the old GPU back-end and is removed from Theano. Use Theano 0.9 to use it. Even better, transition to the new GPU back-end! See https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29
    – chmodsss
    Commented Jul 16, 2018 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.