After jumping through many hoops, my Ubuntu 16.04 LTS with NVidia GTX 1080 runs tensorflow 0.9 with GPU support. !! At the time of this writing, a simple “pip install tensorflow” does not get your the goodies of the GPU. Hopefully, someone will benefit from a recollection of my experiences.
The first step is actually get the nvidia display drivers working on Ubuntu 16.04 LTS for the GTX 1080. Finish that step first and then get started on this adventure to install tensorflow on your machine.
Note: If in spite of installing the correct NVidia Driver’s for Ubuntu 16.04 LTS, you are staring at a black screen, it is possible that is a display resolution related issue. Please check out the detailed and helpful comment by Constantin below for the fix.
Step 1: Install NVidia Cuda Toolkit
- Download the latest cuda toolkit. From the Nvidia website, I would recommend the latest 0.80 cuda toolkit version as that has support for the Pascal architecture that the GTX 1080 has. You will need to become a member of the NVidia Accelerated computing developer program, but that merely means providing an email address. Easy.
chmod +x cuda_8.0_your-version_linux.run ./cuda_8.0_your-version_linux.run
Know where you are installing it. Better to use the default “/usr/local/cuda”
- Test that it actually works by running one of the examples in /usr/local/cuda/samples
Step 2: Install NVidia CuDNN
- Now download the latest CuDNN library. At the time of this writing, the latest version was CuDNN v5.1
- Extract the files and copy the entire contents of the directory to their final destinations
cp -r cuda/* /usr/local/cuda/
- Set appropriate permissions
chmod a+r /usr/local/cuda/include/cudnn.h chmod a+r /usr/local/cuda/lib64/libcudnn*
- add these libraries to your path
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64" export CUDA_HOME=/usr/local/cuda'
Step 3: Install Tensorflow dependencies
apt-get install swig python-dev python-numpy python-wheel
- Google uses its own build tool bazel for building tensorflow. Download bazel. Unfortunately, the pre-packaged apt-get versions are not available for Ubuntu 16.04 LTS, so it will not work for you. Instead download the packages listed at the bottom. One the bazel installer itself, and one the appropriate jdk version. Here’s two files that I downloaded.
- Installed pre-requisities:
apt-get install pkg-config zip g++ zlib1g-dev unzip
- Make the installer executable and install it. Perform similar steps for the jdk installer.
chmod +x bazel-version-installer-os.sh ./bazel-version-installer-os.sh --user
Step 4. Configure and build tensorflow
- Get the latest version of tensorflow
- While this step is not mentioned on the tensorflow installation site, it is crucial, else your installation will fail. Open the file under the tensorflow source directory at third_party/gpus/crosstool/CROSSTOOL. In that file, add the following directive below every location you see a cxx_builtin_include_directory
. Save the file and get back to the command prompt
- Build the tensorflow package using bazel. From the source tree of the tensorflow directory
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
- Package the built package into a pip installable
- Install the package using pip
pip install /tmp/tensorflow_pkg/tensorflow-0.9.0-py2-none-any.whl
Now, if all the steps are complete, you should have a working tensorflow installation. Easy to find out if it is indeed working for you. Trying this simple short script which should tell if you GPU is being used as one of the devices
#!/usr/bin/env python # Author: Abhay Harpale # Website: abhay.harpale.net/blog/ """ gpu test for tensorflow """ import tensorflow as tf # Creates a graph. a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b') c = tf.matmul(a, b) # Creates a session with log_device_placement set to True. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. print sess.run(c)
If the system is indeed using the GPU as the device, it should output:
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so.5 locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so.8.0 locally 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_init.cc:102] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate (GHz) 1.835 pciBusID 0000:01:00.0 Total memory: 7.92GiB Free memory: 7.56GiB I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 I tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0: Y I tensorflow/core/common_runtime/gpu/gpu_device.cc:838] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0) I tensorflow/core/common_runtime/direct_session.cc:175] Device mapping: /job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0 I tensorflow/core/common_runtime/simple_placer.cc:818] MatMul: /job:localhost/replica:0/task:0/gpu:0 I tensorflow/core/common_runtime/simple_placer.cc:818] b: /job:localhost/replica:0/task:0/gpu:0 I tensorflow/core/common_runtime/simple_placer.cc:818] a: /job:localhost/replica:0/task:0/gpu:0 Device mapping: /job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0 MatMul: /job:localhost/replica:0/task:0/gpu:0 b: /job:localhost/replica:0/task:0/gpu:0 a: /job:localhost/replica:0/task:0/gpu:0 [[ 22. 28.] [ 49. 64.]]
Now, go fire up some deep learning jobs and take it to the next level.
If this article has worked for you, it is highly likely that some of your friends and followers will benefit from it too. Please share by clicking on the appropriate channels.
Please comment about your experience with getting Tensorflow to work with Ubuntu 16.04 LTS and new GPU’s from the NVidia Pascal line-up, including GTX 1080, GTX 1070, GTX Titan X, and GTX 1060. If you had to customize these steps for your system, please also provide those specifics so that other people visiting this blog as can benefit. Thanks for visiting!