Configure the pytorch GPU acceleration environment on the WSL2 side

Tutorial for Pytorch GPU acceleration on Windows: Steps for Pytorch to use GPU acceleration

Pre-tutorial: WSL2 installation and its python environment configuration


After configuring the WSL2-related environment, in order to perform GPU acceleration on pytorch, the following steps are required:

  1. To update the Windows system, only the version above Win10 21H2, which is the latest Win10 version, is a big pit. The official update website: https://www.microsoft.com/zh-cn/software-download/windows10

  2. After updating the system, we need to update the driver for the computer. We need to install an nvidia driver with WSL2 driver on the Windows side. You only need to install this driver to cover Windows and WSL2 drivers, and there is no need to install drivers inside WSL2. Driver download address: https://developer.nvidia.com/cuda/wsl/download

  3. After installing the driver, open WSL2, enter nvidia-smi, you should be able to see:
    insert image description here

    If not, please check whether the previous steps are correct.

  4. At this time, our driver supports the installation of cuda11.6, but we should not install cuda11.6 directly on the WSL2 side, because the latest cuda is often not supported by torch, and we should install the stable cuda11.3.

    Open the installation address: https://developer.nvidia.com/cuda-11.3.0-download-archive, select the version we need, and then enter the corresponding command in the WSL2 console.

    insert image description here

  5. Install the relevant dependent libraries and execute the command:

    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
    
  6. Excuting an order:

    sudo vim ~/.bashrc
    

    Add at the end:

    export PATH=/usr/local/cuda-11.3/bin${
          
          PATH:+:${
          
          PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
    

    update file:

    source ~/.bashrc
    
  7. At this point, the cuda installation is complete, enter nvcc -V to check, if there is the following output, it means normal:

insert image description here

  1. Install CUDNN, this is some libraries used by cuda. Download address: https://developer.nvidia.com/rdp/cudnn-archive. We choose the 11.x version of v8.2.1, which is suitable for cuda11.3. We choose cuDNN Library for Linux (x86_64) to download.

  2. After downloading, put the file in a place in WSL2, and you can execute the command explorer.exe on the WSL2 side, so that windows can directly access the WSL2 file in a graphical interface. Then drag the installation package to WSL2.

  3. Installing CUDNN actually puts this library into the corresponding CUDA location. Just execute the command:

    tar -zxvf cudnn-11.3-linux-x64-v8.2.1.32.tgz
    sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-11.3/lib64/
    sudo cp cuda/include/cudnn.h /usr/local/cuda-11.3/include/
    sudo chmod a+r /usr/local/cuda-11.3/include/cudnn.h
    sudo chmod a+r /usr/local/cuda-11.3/lib64/libcudnn*
    
  4. To test whether cuda can run normally, execute the command:

    #找到测试项目BlackScholes
    cd /usr/local/cuda/samples/4_Finance/BlackScholes
     
    #进行编译
    sudo make
     
    #在当前目录查看编译结果
    ./BlackScholes
    

    If the following content is output, it means that cuda is running normally:

    [./BlackScholes] - Starting...
    GPU Device 0: "Pascal" with compute capability 6.1
    
    Initializing data...
    ...allocating CPU memory for options.
    ...allocating GPU memory for options.
    ...generating input data in CPU mem.
    ...copying input data to GPU mem.
    Data init done.
    
    Executing Black-Scholes GPU kernel (512 iterations)...
    Options count             : 8000000
    BlackScholesGPU() time    : 0.822840 msec
    Effective memory bandwidth: 97.224265 GB/s
    Gigaoptions per second    : 9.722426
    
    BlackScholes, Throughput = 9.7224 GOptions/s, Time = 0.00082 s, Size = 8000000 options, NumDevsUsed = 1, Workgroup = 128
    
    Reading back GPU results...
    Checking the results...
    ...running CPU calculations.
    
    Comparing the results...
    L1 norm: 1.741792E-07
    Max absolute error: 1.192093E-05
    
    Shutting down...
    ...releasing GPU memory.
    ...releasing CPU memory.
    Shutdown done.
    
    [BlackScholes] - Test Summary
    
    NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
    
  5. Then there is the problem of pytorch itself. We need to install the corresponding pytorch version to successfully ignite the GPU. Visit the official website of pytorch: https://pytorch.org/get-started/locally/https://pytorch.org/get-started/locally/. Choose Linux, Stable version of pytorch. I chose the pip installation myself. There is another pit here. I failed to install the pytorch GPU acceleration corresponding to cuda11.3 here. But I installed the pytorch corresponding to cuda10.2 and successfully ignited the GPU. So the command I executed here is:

    pip3 install torch torchvision torchaudio
    
  6. To check whether the GPU is valid, first execute the python command to enter the python programming environment. Then enter the following code:

    import torch
    print(torch.cuda.is_available())
    

    If the output is True, it means that our deep learning program can successfully use GPU acceleration! ! !

Guess you like

Origin blog.csdn.net/tianjuewudi/article/details/122692068