WSL2端配置pytorch GPU加速环境

Windows端Pytorch GPU加速的教程:Pytorch使用GPU加速的步骤

前置教程:WSL2安装及其python环境配置


配置好WSL2相关环境后,要想对pytorch进行GPU加速,需要进行以下步骤:

  1. 更新Windows系统,只有版本在Win10 21H2以上,也就是目前最新的Win10版本才行,这是一个大坑,官方更新网址:https://www.microsoft.com/zh-cn/software-download/windows10

  2. 在更新系统之后,我们需要给电脑更新驱动,我们需要在Windows端安装一个带有WSL2驱动的nvidia驱动。只需要安装这一个驱动,就可以涵盖Windows和WSL2的驱动,而WSL2内部不需要安装驱动。驱动下载地址:https://developer.nvidia.com/cuda/wsl/download

  3. 安装好驱动后打开WSL2,输入nvidia-smi,应当能够看到:
    在这里插入图片描述

    如果没有请检查前面的步骤是否正确。

  4. 此时我们的驱动支持cuda11.6的安装,但是我们不要在WSL2端直接安装cuda11.6,因为最新的cuda往往是torch尚未支持的,我们应当安装稳定的cuda11.3。

    打开安装地址:https://developer.nvidia.com/cuda-11.3.0-download-archive,选择我们需要的版本,然后在WSL2的控制台输入对应的命令即可。

    在这里插入图片描述

  5. 安装一下相关的依赖库,执行命令:

    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
    
  6. 执行命令:

    sudo vim ~/.bashrc
    

    在最后添加:

    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}}
    

    更新文件:

    source ~/.bashrc
    
  7. 此时cuda安装已完成,输入nvcc -V进行检查,如果有下列输出则表示正常:

在这里插入图片描述

  1. 安装CUDNN,这个是cuda用到的一些库。下载地址:https://developer.nvidia.com/rdp/cudnn-archive。我们选择那个v8.2.1的11.x的版本,这个版本适配cuda11.3。我们选择其中的cuDNN Library for Linux (x86_64)进行下载。

  2. 下载好后把文件放到WSL2中的一个地方,可以在WSL2端执行explorer.exe .的命令,使得windows能直接以图形化界面的方式访问WSL2的文件。然后把这个安装包拖到WSL2中即可。

  3. 安装CUDNN,实际上是把这个库放进对应的CUDA位置。执行命令即可:

    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. 测试cuda是否能够正常运行,执行命令:

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

    如果输出以下内容,代表cuda运行正常:

    [./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. 接下来就剩下pytorch本身的问题了,我们需要安装好对应的pytorch版本就能顺利地点燃GPU。访问pytorch官网:https://pytorch.org/get-started/locally/https://pytorch.org/get-started/locally/。选择Linux,Stable的pytorch版本。我自己选择的是pip的安装。这里又有一个坑,我在这里装cuda11.3对应的pytorch GPU加速失败了。但是我装cuda10.2对应的pytorch成功点燃GPU了。因此这里我执行的命令是:

    pip3 install torch torchvision torchaudio
    
  6. 检查GPU是否有效,先执行python命令进入python编程环境。然后输入下面的代码:

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

    如果输出为True,则说明我们的深度学习的程序可以顺利地利用GPU加速了!!!

猜你喜欢

转载自blog.csdn.net/tianjuewudi/article/details/122692068