Jan 25, 2016

Keras/Theano for Deep Learning on Amazon EC2 GPU instances

This post describes the installation of Keras/Theano on AWS GPU instances.

Although GPUs available with the current generation of AWS g2.2xlarge/8xlarge instance types might not be the fastest in the market, using AWS for GPU computing offers the flexibility of training multiple models simultaneously at minimal upfront cost.


Step 1: Create a g2.2xlarge instance using Ubuntu 14.04 LTS. 
Right now it costs around $0.65 per hour for this instance type. Creating spot instances might offer additional cost savings, with the added caveat that the instance might be reclaimed when demand rises. If you plan on using Spot instances, be sure to check out the pricing history for this instance type/region to ensure that your bid price is appropriately set.


Step 2: Install essentials

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential
sudo apt-get install git vim python-pip python3-pip liblapack-dev cython cython3 gfortran


Step 3: Download cuda
The package we download below includes both the nvidia driver and cuda. However, the nvidia driver version provided in this package is not the latest (aws gpu users require a newer version of the driver that we will download in step -6). We will use the cuda version provided in this package though.

mkdir -p install/cuda
cd install/cuda
wget http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda_7.5.18_linux.run
chmod +x cuda_7.5.18_linux.run
mkdir cuda_installer
./cuda_7.5.18_linux.run -extract=`pwd`/cuda_installer


Step 4: Install additional drivers that are not present in the base kernel package
sudo apt-get install linux-image-extra-virtual
(choose "use local version" if asked about grub version)


Step 5: Disable nouveau since it conflicts with NVIDIA's kernel module
Nouveau is an open-source driver for Nvidia graphics cards. In step-6, we'll be installing the driver provided by nvidia. So we disable the default driver.

$ sudo vi /etc/modprobe.d/blacklist-nouveau.conf
Add the below
----
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
----

echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
sudo update-initramfs -u
sudo reboot

* After reboot
sudo apt-get install linux-source
sudo apt-get install linux-headers-`uname -r`


Step 6: Download and install the NVIDIA driver
NOTE: This uses the newer NVIDIA driver 352.63 that has a fix required for AWS users

cd ~/install/cuda
mkdir cuda_installer-352.63-aws
cd cuda_installer-352.63-aws
wget http://us.download.nvidia.com/XFree86/Linux-x86_64/352.63/NVIDIA-Linux-x86_64-352.63.run

chmod +x NVIDIA-Linux-x86_64-352.63.run 
sudo ./NVIDIA-Linux-x86_64-352.63.run 


Step 7: Verify nvidia driver installation
Run nvidia-smi to view available GPUs

$ nvidia-smi
Tue Jan 19 06:58:00 2016       
+------------------------------------------------------+                       
| NVIDIA-SMI 352.63     Driver Version: 352.63         |                       
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GRID K520           Off  | 0000:00:03.0     Off |                  N/A |
| N/A   34C    P0    36W / 125W |     11MiB /  4095MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+


Step 8: Install CUDA

sudo ./cuda-linux64-rel-7.5.18-19867135.run

Default install path: /usr/local/cuda-7.5
Symbolic link created: /usr/local/cuda -> /usr/local/cuda-7.5


Step 9: Install CUDA samples

sudo ./cuda-samples-linux-7.5.18-19867135.run
Default install path: /usr/local/cuda-7.5/samples


Step 10: In .bashrc, add the following lines
export PATH=$PATH:/usr/local/cuda-7.5/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-7.5/lib64

Remember to source the bashrc file
source ~/.bashrc


Step 11: Install python packages 
You could install a distribution like anaconda, or alternatively install select packages as done below.
sudo pip3 install numpy
sudo pip3 install scipy

sudo apt-get install libyaml-dev
sudo pip3 install --upgrade pyyaml
sudo pip3 install --upgrade six


Step 12: Install and configure theano
sudo pip3 install --upgrade --no-deps git+git://github.com/Theano/Theano.git
vi ~/.theanorc

----
[global]
floatX = float32
device = gpu
optimizer = fast_run

[lib]
cnmem = 0.9

[nvcc]
fastmath = True

[blas]
ldflags = -llapack -lblas
----


Step 13: Install keras
cd ~/install
git clone https://github.com/fchollet/keras.git keras
cd keras
sudo python3 setup.py install


Step 14: Verify
Run the MNIST CNN example to verify that the installation succeeded
cd keras/examples
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python3 mnist_cnn.py

Note that you can monitor GPU usage by using the nvidia-smi command (prints status every sec)
nvidia-smi -l 1

NOTE: You could also install cuDNN for additional speed up, although you would need to apply for the nvidia developer program in order to download it.