WSLとGPUとWindows

Windows

はじめに

WindowsのWSLを使ってUbuntuをインストールして、Tensorflowを使ったGPUの計算を行いたかったので調べました。
Windows Store Windows Server 2022で環境を作りたかったのですが、結論にあるように無理でした・・・。
以下、Windows11の環境で行います。

本当はDebianで行いたかったのですが、UbuntuにはSingularityのパッケージがあるので採用しました。

結論

WSLのバージョンをダウングレードしないと使えません。
また、ダウングレードできるのはMicrosoft Store版のWSLです。
Windows Store Windows Server 2022はMicrosoft Storeをサポートしていないのでダウングレードができ使えませんでした。

WSLのUbuntuからnvidia-smiを実行すると以下のエラーになります。
WSLのUbuntuからWindows(ホスト)のGPUが認識できないようです。

nvidia-smi

    Failed to initialize NVML: GPU access blocked by the operating system
    Failed to properly shut down NVML: GPU access blocked by the operating system

手順

手順は、
・WindowsにGPUに対応したNVIDIAドライバをインストールする。
・WSLのバージョンをダウングレードする。
・WSLにUbuntuをインストールする。
・Ubuntuにnvidia-container-toolkitをインストールする。
・UbuntuにSingularityをインストールする。

CUDA GPU Containers on Windows with WSL2
This tutorial will show you how to install and use CUDA GPU containers on Windows with WSL2 for effi...

環境

- Windows 11 Pro 64bit
    - NVIDIA Windows driver
        537.13-data-center-tesla-desktop-winserver-2019-2022-dch-international.exe
    - WSL 1.2.3

- Ubuntu 22.04
    - nvidia-container-toolkit 1.14.3-1
    - Singularity 4.0.1

WindowsにNVIDIAドライバをインストール

WindowsにGPUに対応したNVIDIAドライバをインストールします。
インストール後にコマンドプロンプトで「nvidia-smi」を実行してGPUが認識できるか確認します。

WSLのダウングレード

WSLをダウングレードします。
「https://github.com/microsoft/WSL/releases/download/1.2.3/Microsoft.WSL_1.2.3.0_x64_ARM64.msixbundle」からWSL 1.2.3をダウンロードします。
ファイルはCドライブ直下など、空白が入らないディレクトリに置きます。

Releases · microsoft/WSL
Issues found on WSL. Contribute to microsoft/WSL development by creating an account on GitHub.

以下によるとWSL 1.2.4、1.3.10、1.3.11でも良いらしいです。

In WSL2's docker container : GPU access blocked by the operating system ?? Issue #9962 ?? microsoft/WSL
Windows Version Microsoft Windows WSL Version 1.2.0.0 Are you using WSL 1 or WSL 2? WSL 2 WSL 1 Kern...

管理者権限でコマンドプロンプトを起動して行います。

wsl --shutdown

この後に、タスクマネージャーでWSLのプロセスをkillします。
引き続き作業します。

get-appxpackage *linux*

remove-appxpackage [package_name]

cd C:\
add-appxpackage Microsoft.WSL_1.2.3.0_x64_ARM64.msixbundle

wsl

WSLの各種操作

不要かもしれません。
WSLでエラーが出た場合の対処です。
Hyper-V、Linux用Windowsサブシステム、仮想マシンプラットフォームを有効にします。

powershell -command "Start-Process ms-settings:windowsupdate"\
powershell -command "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -n"
powershell -command "Enable-WindowsOptionalFeature -Online -FeatureName HypervisorPlatform -n"
powershell -command "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -n"
powershell -command "Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -n"
shutdown /r /t 0
powershell -command "dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart"
powershell -command "dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart"

Ubuntuのインストール

Ubuntu-22.04をインストールします。

wsl --install -d Ubuntu-22.04

nvidia-container-toolkitのインストール

Ubuntuにnvidia-container-toolkitをインストールします。

Installing the NVIDIA Container Toolkit — NVIDIA Container Toolkit 1.17.3 documentation
url -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list \
  && \
    sudo apt-get update


sudo apt install libnvidia-container-tools

Singularityのインストール

Ubuntu-22.04(jammy)に対応したSingularityの最新版をインストールします。

Releases ?? sylabs/singularity
SingularityCE is the Community Edition of Singularity, an open source container platform designed to...
wget https://github.com/sylabs/singularity/releases/download/v4.0.1/singularity-ce_4.0.1-jammy_amd64.deb

sudo apt install singularity-ce_4.0.1-jammy_amd64.deb

singularity version
    4.0.1-jammy

nvidia-container-cliのパスを設定します。

which nvidia-container-cli

sudo vi /etc/singularity/singularity.conf

        nvidia-container-cli path = /usr/bin/nvidia-container-cli

Singularityのテスト

Tensorflowなどを含んだコンテナをダウンロードします。

singularity run --nv --nvccli library://sylabsdemo/containers/tensorflow:latest-gpu-signed

以下のURLにあるように、ホストのGPUを認識できるかテストします。

CUDA GPU Containers on Windows with WSL2
This tutorial will show you how to install and use CUDA GPU containers on Windows with WSL2 for effi...

Ubuntuの再インストール

失敗した場合などにWSLのUbuntuを削除、再インストールします。

wsl --shutdown

wsl --unregister Ubuntu-22.04

wsl --install -d Ubuntu-22.04

wsl ~

Comments