AI-折腾记-01-(wsl2环境下运行docker+cuda:pytorch/onnxruntime)

AI-折腾记-01-(wsl2环境下运行docker+cuda:pytorch/onnxruntime)

玖亖伍
2024-06-15 / 0 评论 / 11 阅读 / 正在检测是否收录...

注意:

以下过程无难度,故省略:

  1. 安装 Nvidia 驱动
  2. 启用 WSL
  3. 配置使用 WSL2 版本
  4. WSL 安装 Ubuntu

上述可参考:

准备就绪后的表现为:

  • 可以在 WSL 的 Ubuntu 中执行 which nvidia-smi, 能得到如下输出

    /usr/lib/wsl/lib/nvidia-smi
  • 可以在 WSL 的 Ubuntu 中执行 nvidia-smi, 能正常输出

以下所有操作均在上述过程就绪之后


环境

  • 系统: Windows 11 + WSL2 + Ubuntu 22.04
  • GPU:

    • NVIDIA GeForce RTX 3070
    • Driver Version: 555.99 (Windows 11 安装 Geforce Game Ready 驱动 555.99)
    • NVIDIA-SMI: 555.99
    • CUDA Version: 12.5

Docker CUDA 配置

参考: Installing Docker and The Docker Utility Engine for NVIDIA GPUs

  • Install Docker Engine on Ubuntu | Docker Documentation

    • Set up Docker's apt repository.

      # Add Docker's official GPG key:
      sudo apt-get update
      sudo apt-get install ca-certificates curl
      sudo install -m 0755 -d /etc/apt/keyrings
      sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
      sudo chmod a+r /etc/apt/keyrings/docker.asc
      
      # Add the repository to Apt sources:
      echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
        $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      sudo apt-get update
    • Install the Latest Docker packages.

      sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    • Verify that the Docker Engine installation is successful

      sudo docker run -it --rm hello-world
  • Installing the NVIDIA Container Toolkit | NVIDIA Documentation

    • Configure the apt production repository

      curl -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
    • Install the NVIDIA Container Toolkit packages:

      sudo apt-get update
      sudo apt-get install -y nvidia-container-toolkit
    • Configure the container runtime by using the nvidia-ctk command:

      sudo nvidia-ctk runtime configure --runtime=docker
      sudo systemctl restart docker
  • Testing Docker and NVIDIA Container Runtime: Running a Sample Workload | NVIDIA Documentation

    sudo docker run -it --rm --runtime=nvidia --gpus all ubuntu nvidia-smi

PyTorch-CUDA 测试

# 运行 pytorch-cuda 容器
sudo docker pull pytorch/pytorch:2.3.1-cuda12.1-cudnn8-runtime
sudo docker run -it --rm --runtime=nvidia --gpus all pytorch/pytorch:2.3.1-cuda12.1-cudnn8-runtime python
# python 测试代码
import torch

print(torch.cuda.is_available())
print(torch.cuda._is_compiled())
print(torch.__config__.show())

Docker 构建 ONNX Runtime CUDA 镜像

参考: https://github.com/microsoft/onnxruntime/blob/main/dockerfiles/README.md#cuda

# Build the docker image
git clone https://github.com/microsoft/onnxruntime
cd onnxruntime/dockerfiles/
sudo docker build -t onnxruntime-cuda -f Dockerfile.cuda ..
# Run the Docker image
sudo docker run -it --rm --runtime=nvidia --gpus all onnxruntime-cuda
# or
sudo nvidia-docker run -it --rm onnxruntime-cuda
0

评论 (0)

取消