When I first wanted to follow the great course Fast AI (which teaches you how to begin with Deep Learning), they offered a simple way to use an Amazon GPU, for $0.99/hour. It could seem quite cheap. But actually if you use an Amazon GPU 4 hours/day, it will cost you more than $120/month, which is not so cheap!
I have a GPU CUDA compatible on my laptop: a XIAOMI NoteBook Air 13″ with a graphic card NVIDIA GeForce 940MX. So I wanted to use it to do deep learning.
It is not so simple to use GPU with Theano on a laptop with Ubuntu 16.04, so I chose to gather all information I needed to do it in this single blog post.
How to know if my laptop Theano-with-GPU eligible?
Your laptop must have a NVIDIA CUDA compatible graphic card. You could visit this link to ensure you are in this case.
You could already go in a terminal and write:
$ lspci | grep -i nvidia
This code will list all your PCI devices and will filter NVIDIA ones.
If nothing appears, it is not good. In my case, I have:
$ lspci | grep -i nvidia 01:00.0 3D controller: NVIDIA Corporation Device 134b (rev a2)
The last line corresponds to my NVIDIA GeForce 940Mx
What is the content of this post?
In this post, we will see step-by-step how to install Theano in order to do deep learning with the GPU of your laptop, on Ubuntu 16.04.
This post assumes you have a fresh-new installation of Ubuntu 16.04, so you don’t need any specia pre installed software.
All information of this post are available in several another places on the web. I just chose to gather all of them in a single place, here.
I assume you use bash. If you use another shell (for example zsh), you will have to adapt some part of this tutorial.
Ready? Here we go!
NVIDIA proprietary drivers installation
Modern laptops with NVIDIA graphic cards are often provided with Optimus Technology. Optimus Technology means that your laptop has actually 2 graphic chips: The first one is located on the mother board, close to the CPU. We will call it “Intel chip”. The second one is on your NVIDIA card. By default, on Ubuntu 16.04, the NVIDIA card is not used. You have to install specific drivers to use it.
We could choose between two types of drivers: free drivers and proprietary drivers. I did not manage to work with free drivers, so we will use proprietary drivers.
To install them, it is quite simple:
- Go to your System Setting
- Go to Software & Updates
- Click on the tab Additional Drivers
- <Wait a little bit>
On my laptop, I have that:
We could see that the NVIDIA binary driver is not used. To fix it:
- Click on Using NVIDIA binary driver, and on Apply Changes, then enter your password.
- <Wait a little bit>
- Click on Restart…
Congratulations, you could now use your NVIDIA video card!
One important thing: On Windows, the Optimus System switches automatically from the Intel graphic chip to the NVIDIA graphic chip when needed. The Intel graphic chip offers low performances, but also low consumption. The NVIDIA one offers high performances but also high consumption.
On Ubuntu, you have to switch from one chip to the other by yourself, using a the tool called PRIME.
In order to do that:
- Open the software NVIDIA XServer Settings
- Go to PRIME Profile
- Choose NVIDIA (Performance Mode) or Intel (Power Saving Mode)
- Enter your password then log out and log in. (Note you don’t need to reboot!)
Note: There is a system, called Bumblebee, which supports properly NVIDIA Optimus technology by switching automatically between the Intel and the NVIDIA chip without manual configuration, as on Windows. We won’t talk about it in this post.
Now, let’s check that we could use our NVIDIA GPU.
NVIDIA GPU test
To be sure that we are using the NVIDIA GPU, we will use the tool glxgear.
In a terminal, enter:
You should see rotating gears.
- Open the software NVIDIA XServer Settings
- Click on the line GPU <n>, – <x>, where <n> and <x> depends on your system
(On mine, it is GPU 0 – (GeForce 940MX)).
- Check the line GPU Utilization. It should be close to 100%. If you close glxgear, the GPU Utilization should significantly decrease.
Optional: A quick way to switch between the Intel chip and the NVIDIA one
To switch to Intel chip:
$ sudo prime-select intel
To switch to NVIDIA chip:
$ sudo prime-select nvidia
Or you could also use Prime Indicator. To install it, go in a terminal and write:
$ sudo add-apt-repository ppa:kranich/cubuntu $ sudo apt-get update $ sudo apt-get install prime-indicator
Then reboot. Prime indicator will be usable thanks to a little icon in the Ubuntu status bar (in the top right of your screen). The icon is either Intel or NVIDIA, depending the chip your are currently using.
Now we are sure that NVIDIA GPU is correctly working, let’s switch on Theano installation!
This part is mostly inspired from the Theano documentation.
Install conda. conda is a packaging tool and installer. In some parts, it looks like pip. If you want to learn more information on conda vs. pip, you could read this Stack Overflow topic.
To install conda, go on this page, download the installer corresponding to your system (I chose Linux, Python 2.7 – 64-bit (bash installer)), and run the downloaded file. For me, I have to go in my Download directory, then write:
$ bash Miniconda2-latest-Linux-x86_64.sh
Then I answer “yes” to all the questions.
Now let’s install numpy, scipy and mkl, with this command:
$ conda install numpy scipy mkl
Now, we have to install GPU drivers: Go on this link, and choose Linux, x86_64, Ubuntu, 16.04 and then deb (network). Then click on Download and follow installation instructions given on the website.
Now, let’s install theano itself and pygpu.
$ conda install theano pygpu
Now, we have all what we need to test Theano. Let’s do it!
Test of Theano
Follow this link and copy/paste the code in a file called test_theano.py.
Execute this script with
$ python test_theano.py
Normally, this script should say: Used the cpu. Damn, it is not what we wanted.
Actually, we just didn’t say to Theano to use the GPU.
To do that, create directly in your Home folder a file named .theanorc (don’t forget the “.” at the beginning of the file’s name).
In this file, write:
[global] device = cuda floatX = float32
Again, run the test_theano.py script. Here, the script says: Use the gpu. Congrats!
But, we have still the ugly error cannot compile with cuDNN. Let’s install it!
Go to NVIDIA cuDNN website, and click on the Download button. You may need to register (it’s free).
Download the last cuDNN Library for Linux, and extract the downloaded archive.
Copy the content of the include directory in /usr/local/cuda/include.
Copy the content of the lib64 directory in /usr/local/cuda/lib64.
Modify your .theanorc file like that:
[global] device = cuda floatX = float32 [dnn] library_path = /usr/local/cuda/lib64 include_path = /usr/local/cuda/include
And add at the end of your .bashrc file (in your Home folder) the following lines:
export LIBRARY_PATH=/usr/local/cuda/lib64:$LIBRARY_PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Then re-run the test_theano.py script. If the script complains because of the version of cuDNN, install the corresponding version of cuDNN from NVIDIA cuDNN website.
That’s it! You are now ready to use Theano with your laptop on Ubuntu 16.04.