AWS P2 GPU instance configuration

August 19, 2017

AWS P2 instances are now available in Ireland, so I decided to set up a fooling-around instance a bit closer to home.

I pulled the configuration steps from various logfiles after the fact, so you might have to fudge things around a bit to get everything to work.

This time, we’ll do it the easy way, with Ubuntu 16.04 LTS, with which all the ML tools have been tested.

OK, so first of all, we want to upgrade the Ubuntu LTS instance’s kernel to 4.11 for Docker compatibility. Google for LTS HWE.

Basic

locale-gen fi_FI.UTF-8
apt update && apt -y dist-upgrade
apt -y install linux-generic-hwe-16.04-edge linux-cloud-tools-generic-hwe-16.04-edge

shutdown -r now

apt -y autoremove

NVidia

Then we proceed to installing the various NVidia drivers, CUDA and cuDNN.

Got this from the NVidia Dockerfiles.

echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 /" > /etc/apt/sources.list.d/nvidia-ml.list
echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /" > /etc/apt/sources.list.d/cuda.list
apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
apt update && apt -y dist-upgrade

apt -y install nvidia-384 nvidia-384-dev libcuda1-384 build-essential
apt -y install cuda-nvrtc-* cuda-nvgraph-* cuda-cusolver-* cuda-cublas-* cuda-cufft-* cuda-curand-* cuda-cusparse-* cuda-npp-* cuda-cudart-* 
apt -y install libcudnn6

Docker and nvidia-docker

Many of the good ML tools are available as Docker images, especially meant to be run using the nvidia-docker tool, which allows the image to access the graphics cards for CUDA.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

apt update && apt -y install docker-ce nvidia-modprobe

wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb
sudo dpkg -i /tmp/nvidia-docker*.deb && rm /tmp/nvidia-docker*.deb

usermod -a -G docker ubuntu
usermod -a -G nvidia-docker ubuntu

Python

Let’s take advantage pip’s ability to install Python packages in the user directory.

apt -y install python-dev python-pip python-numpy python-scipy python-pillow
pip install --user --upgrade tensorflow-gpu

Managing the instances from the command line of your laptop

aws --profile swat --region eu-west-1 ec2 describe-instances \
  --filters Name=instance-type,Values=p2.* \
   | jq '.Reservations[].Instances[] | select(.State.Name = "running") |
     {id: .InstanceId, ip: .PublicIpAddress, zone: .Placement.AvailabilityZone, tags: .Tags}'

aws --profile swat --region eu-west-1 ec2 start-instances --instance-ids $ID