Lynn Le
Lynn Le

Reputation: 113

How do I switch from CPU to GPU on google colab using chainer?

I followed the instructions on the Chainer doc, which led me to an error when I ran my code:

RuntimeErrorTraceback (most recent call last)
<ipython-input-9-ffb21f9880f0> in <module>()
      ...
      6 model = Classifier(CompetitionNetwork(n_units = 64))
----> 7 model.to_gpu()
      ...
RuntimeError: CUDA environment is not correctly set up
(see https://github.com/chainer/chainer#installation).No module named cupy

Then I tried installing cupy in many different ways, one of them being

!apt -y install libcusparse8.0 libnvrtc8.0 libnvtoolsext1
!ln -snf /usr/lib/x86_64-linux-gnu/libnvrtc-builtins.so.8.0 /usr/lib/x86_64-linux-gnu/libnvrtc-builtins.so
!pip install cupy-cuda80 chainer

which keep giving me the same error after importing cupy and then running my code:

RuntimeError: CUDA environment is not correctly set up (see
https://github.com/chainer/chainer#installation).No module named cupy

Next I tried installing cuda using this:

!wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb

!dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb

!apt-key add /var/cuda-repo-<version>/7fa2af80.pub

!apt-get update

!apt-get install cuda

Which took a very long time, and seemed to work however in the end still gave me the same error.

It seems that it is very difficult to use Chainer on Google Colab's GPU, unless I am doing something wrong. With Tensorflow it is much easier. Does anyone have experience with using Chainer on Google's GPU?

Upvotes: 3

Views: 6049

Answers (3)

Yuki Hashimoto
Yuki Hashimoto

Reputation: 1073

I checked whether chainer on python2 works on google-colab.

https://colab.research.google.com/gist/fiarabbit/a44a8b3ff25afc78849c62c2f75b25dd

I confirmed chainer w/ python2 worked on google-colab.

The point is that you do not need to install cupy via wget as the mnist-example given by kmaehashi does.

Also, do not forget to turn GPU on.

Upvotes: 0

kmaehashi
kmaehashi

Reputation: 1045

If you are messed up in Google Colab environment,

  1. First try restarting the Runtime by selecting "Restart runtime..." from Runtime menu.
  2. If it does not solve the issue, run the following code to destroy & re-create the container (note: you will lose files created on the container), so that you can start over from clean state. You may need to wait for a minute after running the command and refresh the browser to reload the notebook.

    !kill -9 -1
    

Your installation steps to install Chainer/CuPy is correct. https://github.com/kmaehashi/chainer-colab

You don't have to manually install CUDA Toolkit; Colab container provides it by default.

Upvotes: 1

korakot
korakot

Reputation: 40838

You may want to look at this Chainer Example.

https://colab.research.google.com/drive/1SsxHvQdSz23kaVov8yKizVD3_2tkXdZM

Upvotes: 1

Related Questions