Installing theano on Windows 8 with GPU enabled Installing theano on Windows 8 with GPU enabled windows windows

Installing theano on Windows 8 with GPU enabled


Theano is a great tool for machine learning applications, yet I found that its installation on Windows is not trivial especially for beginners (like myself) in programming. In my case, I see 5-6x speedups of my scripts when run on a GPU so it was definitely worth the hassle.

I wrote this guide based on my installation procedure and is meant to be verbose and hopefully complete even for people with no prior understanding of building programs under Windows environment. Most of this guide is based on these instructions but I had to change some of the steps in order for it to work on my system. If there is anything that I do that may not be optimal or that doesn't work on your machine, please, let me know and I will try to modify this guide accordingly.

These are the steps (in order) I followed when installing Theano with GPU enabled on my Windows 8.1 machine:

CUDA Installation

CUDA can be downloaded from here. In my case, I chose 64-bit Notebook version for my NVIDIA Optimus laptop with Geforce 750m.

Verify that your installation was successful by launching deviceQuery from command line. In my case this was located in the following folder: C:\ProgramData\NVIDIA Corporation\CUDA Samples\v6.5\bin\win64\Release . If successful, you should see PASS at the end of the test.

Visual Studio 2010 Installation

I installed this via dreamspark. If you are a student you are entitled for a free version. If not, you can still install the Express version which should work just as well. After install is complete you should be able to call Visual Studio Command Prompt 2010 from the start menu.

Python Installation

At the time of writing, Theano on GPU only allows working with 32-bit floats and is primarily built for 2.7 version of Python. Theano requires most of the basic scientific Python libraries such as scipy and numpy. I found that the easiest way to install these was via WinPython. It installs all the dependencies in a self-contained folder which allows easy reinstall if something goes wrong in the installation process and you get some useful IDE tools such as ipython notebook and Spyder installed for free as well. For ease of use you might want to add the path to your python.exe and path to your Scripts folder in the environment variables.

Git installation

Found here.

MinGW Installation

Setup file is here. I checked all the base installation files during the installation process. This is required if you run into g++ error described below.

Cygwin installation

You can find it here. I basically used this utility only to extract PyCUDA tar file which is already provided in the base install (so the install should be straightforward).

Python distutils fix

Open msvc9compiler.py located in your /lib/distutils/ directory of your Python installation. Line 641 in my case reads: ld_args.append ('/IMPLIB:' + implib_file). Add the following after this line (same indentation):

ld_args.append('/MANIFEST')

PyCUDA installation

Source for PyCUDA is here.

Steps:

Open cygwin and navigate to the PyCUDA folder (i.e. /cygdrive/c/etc/etc) and execute tar -xzf pycuda-2012.1.tar.gz.

Open Visual Studio Command Prompt 2010 and navigate to the directory where tarball was extracted and execute python configure.py

Open the ./siteconf.py and change the values so that it reads (for CUDA 6.5 for instance):

BOOST_INC_DIR = []BOOST_LIB_DIR = []BOOST_COMPILER = 'gcc43'USE_SHIPPED_BOOST = TrueBOOST_PYTHON_LIBNAME = ['boost_python']BOOST_THREAD_LIBNAME = ['boost_thread']CUDA_TRACE = FalseCUDA_ROOT = 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v6.5'CUDA_ENABLE_GL = FalseCUDA_ENABLE_CURAND = TrueCUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib/Win32']CUDADRV_LIBNAME = ['cuda']CUDART_LIB_DIR = ['${CUDA_ROOT}/lib/Win32']CUDART_LIBNAME = ['cudart']CURAND_LIB_DIR = ['${CUDA_ROOT}/lib/Win32']CURAND_LIBNAME = ['curand']CXXFLAGS = ['/EHsc']LDFLAGS = ['/FORCE']

Execute the following commands at the VS2010 command prompt:

set VS90COMNTOOLS=%VS100COMNTOOLS%python setup.py buildpython setup.py install

Create this python file and verify that you get a result:

# from: http://documen.tician.de/pycuda/tutorial.htmlimport pycuda.gpuarray as gpuarrayimport pycuda.driver as cudaimport pycuda.autoinitimport numpya_gpu = gpuarray.to_gpu(numpy.random.randn(4,4).astype(numpy.float32))a_doubled = (2*a_gpu).get()print a_doubledprint a_gpu

Install Theano

Open git bash shell and choose a folder in which you want to place Theano installation files and execute:

git clone git://github.com/Theano/Theano.gitpython setup.py install

Try opening python in VS2010 command prompt and run import theano

If you get a g++ related error, open MinGW msys.bat in my case installed here: C:\MinGW\msys\1.0 and try importing theano in MinGW shell. Then retry importing theano from VS2010 Command Prompt and it should be working now.

Create a file in WordPad (NOT Notepad!), name it .theanorc.txt and put it in C:\Users\Your_Name\ or wherever your users folder is located:

#!sh[global]device = gpufloatX = float32[nvcc]compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin# flags=-m32 # we have this hard coded for now[blas]ldflags =# ldflags = -lopenblas # placeholder for openblas support

Create a test python script and run it:

from theano import function, config, shared, sandboximport theano.tensor as Timport numpyimport timevlen = 10 * 30 * 768  # 10 x #cores x # threads per coreiters = 1000rng = 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 xrange(iters):    r = f()t1 = time.time()print 'Looping %d times took' % iters, t1 - t0, 'seconds'print 'Result is', rif numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):    print 'Used the cpu'else:    print 'Used the gpu'

Verify you got Used the gpu at the end and you're done!


Here are my simple steps for installing theano on a64-bit windows 10 machine. It's tested on the code listed here

(All installation are with default installation path)

  • install anaconda python 3.x distribution (it already includes numpy,scipy, matlibplot, etc.)
  • run 'conda install mingw libpython' in command-line
  • install theano by downloading it from the official website and do `python setup.py install'
  • install lastest CUDA toolkit for 64-bit windows 10 (now is 7.5)
  • install visual studio 2013 (free for windows 10)
  • create .theanorc.txt file under %USERPROFILE% path and here arethe content in the .theanorc.txt file to run theano with GPU

[global]

floatX = float32

device = gpu

[nvcc]

fastmath = True

compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe

[cuda]

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5



Here's a guide to installing theano with CUDA on 64-bit Windows.

It seems straightforward, but I have not actually tested it to ensure that it works.

http://pavel.surmenok.com/2014/05/31/installing-theano-with-gpu-on-windows-64-bit/