Physnodes - 7) Using Theano and Lasagne on the physnodes

Loading the modules and configuring theano

To use theano and lasagne you need to load the  module:

theano module
module load theano/0.8.2

For theano and lasagne to operate correctly a .theanorc file should be created in your home directory:

~/.theanorc
$ cat ~/.theanorc
[global]
base_compiledir=/scratch/<userid>/theano/compiledir
[blas]
ldflags = -L/usr/lib64 -lopenblas -lopenblas

Please replace "<userid>" with your username. You also need a /scratch directory, see: /scratch/<userid> and you should create the subdirectories "theano/compiledir".

Testing theano and lasagne

Theano test program

theano-test.py
from theano import function, config, shared, sandbox
import theano.tensor as T
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([], T.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, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

Theano test job

theano-test.job
#$ -cwd -V
#$ -l h_rt=00:15:00
#$ -l h_vmem=16G


echo `date`: executing theano-test on host ${HOSTNAME} with ${CPU_BINDING} cores
echo
THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python theano-test.py


Using the GPU cards

Load the cuda module

cuda module
module load cuda/7.5.18

GPU theano test job

theano-test-gpu.job
#$ -cwd -V
#$ -l h_rt=01:15:00
#$ -l h_vmem=32G
#$ -l nvidia_k20=1

echo `date`: executing theano-test-gpu on host ${HOSTNAME} with ${CPU_BINDING} cores
echo
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python theano-test.py