【第3回】WSL2にUbuntu 22.04 LTSを導入する(GPUを使ってPyTorchを動かす)

最終更新日:2022年8月24日

CUDA toolkitのインストール

今回はCUDA toolkit 11.4をインストールします。

CUDA toolkit 11.4で「torch==1.11.0+cu113」が問題なく動きます。

まずはNVIDA公式の通りにやりましたがエラーがでました。

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.4.0/local_installers/cuda-repo-wsl-ubuntu-11-4-local_11.4.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-4-local_11.4.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-4-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

エラー内容

The following packages have unmet dependencies:
 libcufile-11-4 : Depends: liburcu6 but it is not installable
E: Unable to correct problems, you have held broken packages.

解決方法

こちらから「liburcu6_0.12.2-1_amd64.deb」をダウンロードしてインストールしました。

sudo dpkg -i liburcu6_0.12.2-1_amd64.deb



その後はNVIDA公式の通りにするとCUDAのインストールが完了します。

hoge@meshify:~$ nvidia-smi
Sat Apr 23 21:46:44 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.52       Driver Version: 511.79       CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0  On |                  N/A |
| 30%   33C    P8     8W / 180W |    775MiB /  8192MiB |      2%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       272      G   /Xwayland                       N/A      |
+-----------------------------------------------------------------------------+

PyTorchを動かす

自分が過去に行った物体検出モデルの学習を行ってみました。
touch-sp.hatenablog.com
何の問題もなく動作しました。

PyTorchを動かすためにcuDNNが必要と書かれたものも散見しますが現在は必要ないと思います。
cuDNNはPyTorchについてくるようです。


cuDNNはインストールしていませんが以下の結果が得られます。

>>> import torch
>>> torch.backends.cudnn.is_available()
True
>>> torch.backends.cudnn.version()
8200

動作環境

使用したPCはこちら

Ubuntu 22.04 LTS on WSL2 (Windows 11)
WSL バージョン: 0.58.3.0
カーネル バージョン: 5.10.102.1
WSLg バージョン: 1.0.33
MSRDC バージョン: 1.2.2924
Direct3D バージョン: 1.601.0
Windows バージョン: 10.0.22000.613
Python 3.10.4

記事一覧

「WSL2にUbuntu 22.04 LTSを導入する」シリーズの記事一覧はこちら