机器视觉-尝试Windows下使用Docker 容器进行AMD ROCm加速(2)

结论:

启动Docker deskop

启动Docker deskop, 通过界面可设置 http/https proxy, 以加快docker镜像源的访问.

准备 Dockerfile

参考 https://github.com/harakas/amd_igpu_yolo_v8 , 该reps 使用的Linux作为宿主机.
在 d:\my_workspace\docker\ 创建一个 Dockfile 文件:

# Based on
#   https://rocm.docs.amd.com/projects/install-on-linux/en/latest/how-to/3rd-party/pytorch-install.html#using-docker-with-pytorch-pre-installed
#   https://pytorch.org/hub/ultralytics_yolov5/

FROM rocm/pytorch:rocm5.7_ubuntu22.04_py3.10_pytorch_2.0.1

RUN apt-get -y update
RUN apt-get -y upgrade

RUN apt-get install -y migraphx
RUN apt-get install -y fonts-freefont-ttf

RUN pip install -U pip
RUN pip install -U 'ultralytics' 'gitpython>=3.1.30' 'Pillow>=10.0.1' 'numpy>=1.23.5' 'scipy>=1.11.4' 'onnx>=1.12.0' 'onnxruntime' 

RUN mkdir /opt/cwd

WORKDIR /opt/cwd

ENTRYPOINT ["/opt/conda/envs/py_3.10/bin/python"]

基于该 Dockfile 文件创建镜像

docer 镜像 tag 为 rocm-pytorch , 我编译耗时近两个小时.

cd D:\my_workspace\docker
d:
"C:\Program Files\Docker\Docker\resources\bin\docker.exe" build -t rocm-pytorch .

运行docker镜像

创建 bash 文件 docker_run.sh , 内容如下, 然后运行

#! /bin/bash
#
# Based on
#   https://rocm.docs.amd.com/projects/install-on-linux/en/latest/how-to/3rd-party/pytorch-install.html#using-docker-with-pytorch-pre-installed
#
# The following environment variables are necessary for ROCM to work properly:
#   -e HSA_ENABLE_SDMA=0
#       So that ROCM would work at all due to missing pce atomics
#   -e HSA_OVERRIDE_GFX_VERSION=9.0.0
#       ROCM/Tensile, etc do no work with gfx90c due to missing profile files
#       I have gfx90c so I override the gfx version for the libraries to work properly
#       See https://github.com/ROCm/ROCm/issues/1743#issuecomment-1149902796
#
# Also you need the following groups for ROCM to run properly (as we do not run as root):
#   --group-add video --group-add _ssh --group-add render
#

docker run -it \
  --cap-add=SYS_PTRACE \
  --security-opt seccomp=unconfined \
  --privileged \
  --device=/dev/kfd \
  --device=/dev/dri \
  --group-add video \
  --group-add _ssh \
  --group-add render \  
  -e HSA_ENABLE_SDMA=0 \
  -e HSA_OVERRIDE_GFX_VERSION=9.0.0 \
  -e PYTHONPATH=/opt/rocm-5.7.0/lib/ \
  --ipc=host \
  --shm-size 16G \
  --name my_docker \    
  -v /d/my_workspace/docker_vol:/docker_vol \  
  rocm-pytorch 

说明:

  • 参数--shm-size是Docker镜像的共享内存大小(Shared Memories Size),如果需要训练很大的模型,可以根据实际需要调大。
  • d:\my_workspace\docker_vol 目录映射到docker中
  • 镜像中已经创建了 /opt/cwd 作为工作目录
  • 也可以增加 --network=host 参数试试
  • 也可以增加 - e DEVICE=cuda 参数试试

当创建 mydocker 容器后, 我们可以通过指定容器tag来运行, 比如:

docker container start my_docker
docker container stop my_docker
docker container rm my_docker

python中是否能检测出假冒的Cuda

命令行验证:

python3 -c 'import torch' 2> /dev/null && echo 'Success' || echo 'Failure'
python3 -c 'import torch; print(torch.cuda.is_available())'

python 解释器中验证:

import torch
torch.cuda.is_available()

结果显示False, 也就是没有被认为是CUDA, 在docker中执行 rocminfo 命令, 显示:

# 执行 rocminfo
ROCk module is NOT loaded, possibly no GPU devices
# 执行 sudo modprobe amdgpu
modprobe: FATAL: Module amdgpu not found in directory /lib/modules/5.15.133.1-microsoft-standard-WSL2

参考: https://unix.stackexchange.com/questions/715847/wsl2-issue-installing-new-kernel/715922#715922 , 发现该问题无法解决, 放弃继续探索

posted @ 2024-03-07 07:27  harrychinese  阅读(552)  评论(0编辑  收藏  举报