Amazon just recently released new AWS EC2 P2 instance types featuring K80 GPUs. In this blog post I’ll cover how to configure and install TensorFlow on a Ubuntu 16.04 targeting these new GPU instance types. This article is building upon previous blog post by John Ramey on installing TensorFlow for older AWS GPU instance types.
The following components will be installed:
- CUDA 8.0
- Download cuDNN v5.1 (August 10, 2016), for CUDA 8.0
- Bazel 0.3.2 (Java 8 is a dependency)
- TensorFlow 0.11
This tutorial was written for a p2.xlarge instance type, however all AWS P2 instance types should work. You’re going to need 16 GiB of storage on the root partition to follow this guide.
Installing dependencies & build tools
First we need to install the dependencies and basic build tools required by TensorFlow. To do this execute the following commands from the ubuntu command prompt.
sudo apt-get update sudo apt-get upgrade sudo apt-get install -y build-essential git python-pip libfreetype6-dev libxft-dev libncurses-dev libopenblas-dev gfortran python-matplotlib libblas-dev liblapack-dev libatlas-base-dev python-dev python-pydot linux-headers-generic linux-image-extra-virtual unzip python-numpy swig python-pandas python-sklearn unzip wget pkg-config zip g++ zlib1g-dev libcurl3-dev sudo pip install -U pip
Installing CUDA 8
Second we wish to use the latest version of the CUDA library with the instance type. The latest version at the date of writing this post is 8.0. To install CUDA download the package from NVIDIA and install it.
wget https://developer.nvidia.com/compute/cuda/8.0/prod/local_installers/cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb rm cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb sudo apt-get update sudo apt-get install -y cuda
Third we want to download and install latest version of cuDNN. Downloading cuDNN requires logging into NVIDIA developer site, so we can’t use wget to fetch the files. Download the following files from NVIDIA and upload them to your AWS instance.
After having uploaded the files to your server, install them using the following commands.
sudo dpkg -i libcudnn5_5.1.5-1+cuda8.0_amd64.deb sudo dpkg -i libcudnn5-dev_5.1.5-1+cuda8.0_amd64.deb
Configure the Environment
Finally we need to configure the environment to work with CUDA and cuDNN. Add to the following lines to your
export CUDA_HOME=/usr/local/cuda export CUDA_ROOT=/usr/local/cuda export PATH=$PATH:$CUDA_ROOT/bin export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_ROOT/lib64
To build TensorFlow we need to install Bazel. Bazel itself depends on Java 8, so we will begin by installing Java 8.
sudo add-apt-repository -y ppa:webupd8team/java sudo apt-get update # Hack to silently agree license agreement echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections sudo apt-get install -y oracle-java8-installer
After you have completed installing Java, you can move forward installing Bazel.
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add - sudo apt-get update && sudo apt-get install bazel sudo apt-get upgrade bazel
Note: You will need a Bazel version 0.3.2. The build will fail with Bazel version 0.4. The version installed using the technique below provides Bazel 0.3.2 at the time of writing this post. To check bazel version type the following command:
Building and Installing TensorFlow
Now all our toolchain for building TensorFlow is ready. We want to build TensorFlow from the sources to be able to use the latest CUDA and cuDNN libraries. The first step is to clone the sources from GitHub.
git clone --recurse-submodules https://github.com/tensorflow/tensorflow
This will download the latest development copy of TensorFlow. If you want a specific release, you can switch to a specific branch.
Next we need to configure TensorFlow build. To do it execute the following two lines of code
cd tensorflow TF_UNOFFICIAL_SETTING=1 ./configure
Answer the default for all other questions except the following
Do you wish to build TensorFlow with GPU support? [y/N] y Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0 Please specify the Cudnn version you want to use. [Leave empty to use system default]: 5.1.5 Please specify the location where cuDNN 5.1.5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/lib/x86_64-linux-gnu Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: "3.5,5.2"]: 3.7
Now we have configured the TensorFlow build and can move forward building it. This will take quite a long time.
bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
Finally we can install TensorFlow. Note that the package filename below depends on the version of TensorFlow you checked out from the Git repository.
sudo pip install --upgrade /tmp/tensorflow_pkg/tensorflow-0.11.0rc0-py2-none-any.whl
Validating the Installation
You should now be completed with the installation process. To validate your install try running the mnist convolutional neural network example shipping with TensorFlow.
If TensorFlow got installed correctly you should see something like this:
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so.5.1.5 locally I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so.8.0 locally Extracting data/train-images-idx3-ubyte.gz Extracting data/train-labels-idx1-ubyte.gz Extracting data/t10k-images-idx3-ubyte.gz Extracting data/t10k-labels-idx1-ubyte.gz I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:925] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero I tensorflow/core/common_runtime/gpu/gpu_device.cc:951] Found device 0 with properties: name: Tesla K80 major: 3 minor: 7 memoryClockRate (GHz) 0.8235 pciBusID 0000:00:1e.0 Total memory: 11.17GiB Free memory: 11.11GiB I tensorflow/core/common_runtime/gpu/gpu_device.cc:972] DMA: 0 I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] 0: Y I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0) Initialized!
Congratulation. You now should have TensorFlow set up for the new P2 instance up. Using p2.xlarge instance TensorFlow should be about 4x faster than with the older g2.2xlarge instance.