LoginSignup
32
26

More than 5 years have passed since last update.

Ubuntu16.04を入れて、nvidia-docker2を入れて、Chainerを動かす

Last updated at Posted at 2018-01-29

以前に入れた、Ubuntu16.10の環境が壊れたので、改めてGTX 1080 搭載のデスクトップPCを買って、 Ubuntu 16.04 を入れて、nvidia-docker2主体の環境を作り直してみました。前半はUbuntu16.10の記事とほぼ同じですが、御了承ください
以前は、16.10を入れて居ましたが、16.04.3ですと、WiFiもすんなり認識したので、今回は16.04です。

USBドライブを使ったインストールメディアの作り方

ここではMacでのやり方です。
基本的に以下に従う。
https://www.ubuntu.com/download/desktop/create-a-usb-stick-on-macos

  1. Ubuntu をダウンロード https://www.ubuntu.com/download/desktop
  2. Etcher をダウンロードしてインストール https://etcher.io/
  3. USBドライブをさす
  4. Etcher を起動、ディスクイメージを選択し、先ほどダウンロードしたISOを選択
  5. ドライブでUSBドライブのものを選択
  6. OKをクリック
  7. 完了までかなり時間がかかるので待ちましょう(15分ほど)

Ubuntuをインストール

画面に従い、普通にインストールしてください。その後、普通に

$ sudo apt update
$ sudo apt upgrade

以下、お好みで

$ sudo apt install screen ssh git
$ gsettings set org.gnome.Vino require-encryption false # mac の vnc からつながらないため
  • "Desktop Sharing preference"アプリを開いて、VNC接続を有効に

NVIDIAドライバのインストール

参考: http://qiita.com/IshitaTakeshi/items/3efce820986f4560765d
参考:公式ドキュメント https://developer.nvidia.com/compute/cuda/8.0/prod/docs/sidebar/CUDA_Installation_Guide_Linux-pdf

1. Nouveau kernel driver をdisableにする

  • /etc/modprobe.d/blacklist-nouveau.conf を作成
blacklist nouveau
options nouveau modeset=0
$ sudo update-initramfs -u
$ sudo reboot

2. ドライバインストール

以下から、ドライバをダウンロード。
http://www.nvidia.com/Download/index.aspx?lang=en-us
GTX1080 の場合は、以下のように設定して検索すれば良いです。
ここで注意なのは、CUDA を使ってドライバーを入れないことです。CUDAは、Nvidia Tesla などGPGPU用に作られているようで、CUDAに同梱されているドライバでは、GeForceで動かないかもしれません。必ず、手持ちのGPUのドライバをインストールして、CUDA同梱のドライバは入れないようにして下さい。

スクリーンショット 2017-07-02 17.32.32.png

$ sudo service lightdm stop # x を止める
$ sudo ./NVIDIA-Linux-x86_64-384.111.run
$ sudo reboot

なお、CUDAは、ホスト側に入れる必要はありません。
もし、CUDAをコンテナ側でのみ使うのであれば、上記のドライバのみでOKです。

Dockerのインストール

参考: https://docs.docker.com/install/linux/docker-ce/ubuntu/

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

fingerprint が9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88である事を確認

$ sudo apt-key fingerprint 0EBFCD88
pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce

試しに実行してみる

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

ついでに、docker を一般ユーザーで動くようにしておきます。

参考: https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user

$ sudo usermod -aG docker $USER
$ sudo /sbin/reboot

nvidia-docker2のインストール

参考: https://github.com/NVIDIA/nvidia-docker#quickstart

$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
  sudo apt-key add -
$ curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-docker.list | \
 sudo tee /etc/apt/sources.list.d/nvidia-docker.list
$ sudo apt-get update
$ sudo apt-get install -y nvidia-docker2
$ sudo pkill -SIGHUP dockerd

試しに実行してみる

$ docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi

nvidia-dockerとGPUドライバのバージョンとの関係

ここで注意が必要なのは、最初に入れたドライバとnvidia-dockerで動かすCUDAのバージョンには対応関係があります。

※ 上記URLより転載

CUDA toolkit version Driver version GPU architecture
6.5 >= 340.29 >= 2.0 (Fermi)
7.0 >= 346.46 >= 2.0 (Fermi)
7.5 >= 352.39 >= 2.0 (Fermi)
8.0 == 361.93 or >= 375.51 == 6.0 (P100)
8.0 >= 367.48 >= 2.0 (Fermi)
9.0 >= 384.81 >= 3.0 (Kepler)
9.1 >= 387.26 >= 3.0 (Kepler)
9.2 >= 396.26 >= 3.0 (Kepler)
10.0 >= 384.130, < 385.00 Tesla GPUs
10.0 >= 410.48 >= 3.0 (Kepler)

後方互換性があるので、新しいドライバを入れておけば、それまでのCUDAで使えます。

また、GeforceのLinuxのドライバはリリースが少し遅いようで、2018/01/29 現在、 CUDA 9.1 が使える 387.26 以上のドライバーはリリースされていません。
このCUDAのバージョンと機械学習フレームワークのバージョンには対応があるので、注意が必要です。例えば、TensorFlow 1.5(tensorflow-gpu==1.5) は、CUDA 9.1 でないと使えません。

Chainerをnvidia-dockerで動かしてみる

$ sudo docker run --runtime=nvidia -it --rm chainer/chainer:v4.0.0b3 bash

$ apt update && apt install -y git 
$ cd
$ git clone https://github.com/chainer/chainer.git
$ cd chainer/examples/mnist/
$ python train_mnist.py --gpu 0
/usr/local/lib/python2.7/dist-packages/cupy/core/fusion.py:659: FutureWarning: cupy.core.fusion is experimental. The interface can change in the future.
  util.experimental('cupy.core.fusion')
GPU: 0
# unit: 1000
# Minibatch-size: 100
# epoch: 20

Downloading from http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz...
Downloading from http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz...
/usr/local/lib/python2.7/dist-packages/chainer/training/extensions/plot_report.py:25: UserWarning: matplotlib is not installed on your environment, so nothing will be plotted at this time. Please install matplotlib to plot figures.

  $ pip install matplotlib

  warnings.warn('matplotlib is not installed on your environment, '
epoch       main/loss   validation/main/loss  main/accuracy  validation/main/accuracy  elapsed_time
1           0.191562    0.0984969             0.94325        0.9681                    7.7457
2           0.0727045   0.078326              0.977199       0.9761                    9.44116
3           0.0475951   0.0953064             0.984532       0.9715                    11.1401
4           0.0360086   0.0823658             0.988865       0.9786                    12.8432
5           0.0272196   0.0794587             0.991049       0.9771                    14.537
6           0.0242206   0.0740276             0.991665       0.9804                    16.225
7           0.022245    0.0832645             0.992348       0.9797                    17.9534
8           0.0192461   0.068595              0.993699       0.9838                    19.6568
9           0.0138185   0.0844866             0.995365       0.9806                    21.3518
10          0.0157227   0.0762108             0.994649       0.9832                    23.0431
11          0.0127363   0.0801768             0.995815       0.9821                    24.7315
12          0.0121151   0.081401              0.996299       0.9821                    26.4256
13          0.012959    0.0779125             0.995699       0.9835                    28.13
14          0.0108018   0.086303              0.996782       0.9825                    29.8367
15          0.00961264  0.0751394             0.997182       0.9839                    31.5322
16          0.00940392  0.0908331             0.997032       0.9823                    33.228
17          0.00934081  0.0928618             0.997416       0.9822                    34.9274
18          0.00880869  0.119626              0.997266       0.9772                    36.6393
19          0.0114004   0.125807              0.996866       0.9779                    38.3455
20          0.00832069  0.108752              0.997849       0.9807                    40.0682
32
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
26