keras with gpu

I need to speed up my Machine Learning - so I am looking at moving my ML backend from Tensorflow to Theano. In theory keeping the keras framework ill mean that my ML Models do not have to be altered. However seeing will be believing.

As a side note, this would be a lot easier if my GPUs were NVIDIA based; However they are not, so lets get on with it.

The underlying pre-requisite is a Linux package called libgpuarray

Libgpuarray

As I am using a Virtual Python Environment, I switch to that and then add the conda python package.

source ~\pe36\bin\activate
pip install conda

I However need some Ubuntu packages at a system level, these may need to be altered as I already have a host of AMD GPU libraries installed for some crypto-mining research.

sudo apt-get install python3-dev libpython3-dev

I now try using the conda package manager (never used this before) install the larget libgpuarray.

conda libgpuarray

This now fails - as there seems to be a difference between the pip version, and conda as an application.

Conda as an Application.

I pul the conda for linux install script from [conda.io] it is a 50M download

Installing is easy

bash Miniconda3-latest-Linux-x86_64.sh

Then log out/in and test you have conda by typing

conda

Installing pygpu using conda

So with a Conda env instaled we type

conda install pygpu

Adter some license info we see the following

conda install pygpu
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment /home/tim/miniconda3:

The following NEW packages will be INSTALLED:

    intel-openmp:   2018.0.0-hc7b2577_8  
    libgfortran-ng: 7.2.0-h9f7466a_2     
    libgpuarray:    0.7.5-h14c3975_0     
    mako:           1.0.7-py36h0727276_0 
    markupsafe:     1.0-py36hd9260cd_1   
    mkl:            2018.0.1-h19d6760_4  
    numpy:          1.14.0-py36h3dfced4_1
    pygpu:          0.7.5-py36h14c3975_0 

The following packages will be UPDATED:

    conda:          4.3.31-py36_0         --> 4.4.8-py36_0

Proceed ([y]/n)? y

This takes a little time - to install - but I now see

intel-openmp-2 100% |##################################################################| Time: 0:00:00   1.49 MB/s
libgfortran-ng 100% |##################################################################| Time: 0:00:00 500.03 kB/s
libgpuarray-0. 100% |##################################################################| Time: 0:00:00   1.35 MB/s
mkl-2018.0.1-h 100% |##################################################################| Time: 0:02:41   1.20 MB/s
markupsafe-1.0 100% |##################################################################| Time: 0:00:00 807.16 kB/s
numpy-1.14.0-p 100% |##################################################################| Time: 0:00:03   1.40 MB/s
mako-1.0.7-py3 100% |##################################################################| Time: 0:00:00   1.54 MB/s
pygpu-0.7.5-py 100% |##################################################################| Time: 0:00:00   1.37 MB/s
conda-4.4.8-py 100% |##################################################################| Time: 0:00:00   1.32 MB/s

libgpuarray

This is the next piece of software, but it needs to be installed at a machine (not a Python env) level.

We also need a few more Conda Libraries

conda install cython

Download and install

A fairly standard install process here.

cd ~\
git clone https://github.com/Theano/libgpuarray.git
cd libgpuarray

Compiling time

I needed to add a few extra system modules

sudo apt-get install check mpi-default-dev

Again a standard cmake build process


```bash
cd ~\libgpuarray
mkdir Build
cd Build
# you can pass -DCMAKE_INSTALL_PREFIX=/path/to/somewhere to install to an alternate location
cmake .. -DCMAKE_BUILD_TYPE=Release # or Debug if you are investigating a crash
make
make install
cd ..

Add to Conda

We need to switch to our Conda Enviroment.

This assumes you have no Conda env, but you have installed Conda at System level, plus the other modules.

conda create --name myenv
source activate myenv

We now go to the source of libgpuarray

cd ~\libgpuarray

And build python libraries

python setup.py build
python setup.py install

Please note: This is installling into your private python enviroment.

Getting Conda Setup

With my virtual Enviroment active (source activate myenv)

conda install keras
conda install theano

Using Teano as a backend

Switching from one backend to another

If you have run Keras at least once, you will find the Keras configuration file at:

$HOME/.keras/keras.json

If it isn't there, you can create it.

The default configuration file looks like this:

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

Simply change the field backend to "theano", "tensorflow", or "cntk", and Keras will use the new configuration next time you run any Keras code.

Problems

I did has issues - I eventually found this due to the missing blas library (no clue what this does).

On Ubuntu this is fixed by

sudo apt-get install libblas-dev

You may then have to remove and theano (it was installed without this setting). Pygpu is also complaining.

But at the moment it is working.