事情的起因是想安装modelscope的官方docker镜像来跑写语音小程序,结果一直cuda找不到,所以一直在网上找解决方案,具体表现是nvidia-smi 显示有:
说明这里容器里是访问的到宿主机的GPU的,但是又出现了下面的情况:
>>> import torch
>>> torch.cuda.is_available()
/usr/local/lib/python3.10/site-packages/torch/cuda/__init__.py:118: UserWarning: CUDA initialization: Unexpected error from cudaGetDeviceCount(). Did you run some cuda functions before calling NumCudaDevices() that might have already set an error? Error 500: named symbol not found (Triggered internally at /torch/pytorch/c10/cuda/CUDAFunctions.cpp:108.)
return torch._C._cuda_getDeviceCount() > 0
------
False
同时,可以看到:
>>> torch.__version__
2'2.3.0+cu121'
>>> torch.cuda.device_count()
1
这里是可以访问到cuda 设备的,一般考虑的是cuda和驱动、pytorch不匹配,但是我这里是官方的容器,所以不太需要考虑这种情况,好吧,谜题揭晓,是docker Desktop的问题,具体的issue见下面的链接:
https://github.com/NVIDIA/nvidia-container-toolkit/issues/520
主要的原因还是显卡的驱动太新了, 解决的方案就是下载最新的版本:
https://docs.docker.com/desktop/release-notes/#4310
BB一句:csdn基本全是垃圾搬运工,搜了很久,基本上都是重复的内容,真恶心。好内容一篇就够了