雑記: [PyTorch on WSL2] CUDA 11.6 環境に CUDA 11.5 用 wheel を入れても動いたというだけの話

お気付きの点がありましたらご指摘いただけますと幸いです。

💜

f:id:cookie-box:20211229165400p:plain:w70
  • WSL2 + Ubuntu20.04 + CUDA 11.6 + cuDNN v8.2.0 環境に PyTorch を入れたいです。native Windows 側には既に PyTorch が入っているのですが、PyTorch 以外の子たちが native Windows 対応していないせいで WSL2 に移行するはめに……まあそれで、参考文献 [1] の画面で Linux, Pip を選択すると以下のコマンドが画面に表示されますが、これでは CUDA のバージョンが手元と違いそうですね。
  • pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
    

  • cu116 の wheel を探しますか……あれ、cu115 までしかない……。

    https://download.pytorch.org/whl/torch/

  • cu115 だったら以下で入るのでしょうね……CUDA をダウングレードするべきでしょうか……。
  • pipenv install "https://download.pytorch.org/whl/cu115/torch-1.11.0%2Bcu115-cp38-cp38-linux_x86_64.whl"
    

  • ……面倒なのでワンチャン上の wheel で動きませんかね。上のコマンドでインストールはできますね。しかし、import torch で警告が出ます。ただこれは単に numpy がインストールされていないからのようですね。
  • $ pipenv run python
    >>> import torch
    python3.8/site-packages/torch/_masked/__init__.py:223: 
    UserWarning: Failed to initialize NumPy: numpy.core.multiarray failed to import 
    (Triggered internally at  ../torch/csrc/utils/tensor_numpy.cpp:68.)
    >>> import numpy
    ModuleNotFoundError: No module named 'numpy'
    

  • pipenv install numpy してやり直しましょう(インストールされたバージョンは 1.22.3 ですね)。今度は警告は出ませんね。そして GPU を利用できそうにみえます……?
  • $ pipenv install numpy
    $ pipenv run python
    >>> import torch
    >>> torch.cuda.is_available()
    True
    

  • 利用できそうなんですが、いざモデルを訓練する段になって「cu116 だったんでやっぱりだめです」となったら悲しいですね。何でもいいからモデルを用意して誤差逆伝搬してみましょう。これは、できているような……?
  • import torch
    import torch.nn as nn
    import torch.nn.functional as F
    
    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.fc1 = nn.Linear(4, 8)
            self.fc2 = nn.Linear(8, 2)
    
        def forward(self, x):
            x = F.relu(self.fc1(x))
            x = F.relu(self.fc2(x))
            return x
    
    model = Model().to('cuda:0')
    x = torch.randn(1, 4).to('cuda:0')
    y = torch.randn(1, 2).to('cuda:0')
    pred = model(x)
    criterion = nn.MSELoss()
    loss = criterion(pred, y)
    print(loss)
    model.zero_grad()
    loss.backward()
    print(model.fc2.weight.grad)
    print(model.fc2.bias.grad)
    
    $ pipenv run python hoge.py
    tensor(2.0922, device='cuda:0', grad_fn=)
    tensor([[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
            [-0.2290,  0.0000, -0.2433,  0.0000, -2.2712, -0.5655, -0.9550, -1.2746]],
           device='cuda:0')
    tensor([ 0.0000, -2.0381], device='cuda:0')
    

おわり?



以下は CUDA をダウングレードしようとする世界線の話(cuda115 にダウングレードしなくても cuda115 用 PyTorch が機能したようなので pending)。
  • CUDA をダウングレードするといっても、Ubuntu におけるパッケージの管理方法など知りません……他のディストリビューションにおける管理方法なら知っているかというと知りませんが……まずそもそも現在入っているパッケージを確認する方法が以下ですか。なるほど cuda 関連らしきパッケージが……39 個もありませんかこれ……。
  • $ sudo apt list --installed
    cuda-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-cccl-11-6/unknown,now 11.6.55-1 amd64 [installed,automatic]
    cuda-command-line-tools-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-compiler-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-cudart-11-6/unknown,now 11.6.55-1 amd64 [installed,automatic]
    cuda-cudart-dev-11-6/unknown,now 11.6.55-1 amd64 [installed,automatic]
    cuda-cuobjdump-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-cupti-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-cupti-dev-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-cuxxfilt-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-demo-suite-11-6/unknown,now 11.6.55-1 amd64 [installed,automatic]
    cuda-documentation-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-driver-dev-11-6/unknown,now 11.6.55-1 amd64 [installed,automatic]
    cuda-gdb-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-libraries-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-libraries-dev-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-memcheck-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nsight-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nsight-compute-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-nsight-systems-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-nvcc-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvdisasm-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvml-dev-11-6/unknown,now 11.6.55-1 amd64 [installed,automatic]
    cuda-nvprof-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvprune-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvrtc-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvrtc-dev-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvtx-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-nvvp-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-runtime-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-samples-11-6/unknown,now 11.6.101-1 amd64 [installed,automatic]
    cuda-sanitizer-11-6/unknown,now 11.6.124-1 amd64 [installed,automatic]
    cuda-toolkit-11-6-config-common/unknown,now 11.6.55-1 all [installed,automatic]
    cuda-toolkit-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-toolkit-11-config-common/unknown,now 11.6.55-1 all [installed,automatic]
    cuda-toolkit-config-common/unknown,now 11.6.55-1 all [installed,automatic]
    cuda-tools-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda-visual-tools-11-6/unknown,now 11.6.2-1 amd64 [installed,automatic]
    cuda/unknown,now 11.6.2-1 amd64 [installed]