docker 内 pytorch cuda 不可用
拉的官方 docker 仓库里的镜像
nvidia/cuda:12.4.1-cudnn-devel-rockylinux8
conda 安装2.5.0 的 pytorch 后,使用 cuda 报错
(myenv) [root@000b4321253c opt]# python test1.py
可用 GPU 数量: 1
检测 CUDA 时出错: Unexpected error from cudaGetDeviceCount(). Did you run some cuda functions before calling NumCudaDevices() that might have already set an error? Error 804: forward compatibility was attempted on non supported HW
我的 test1.py 代码
import torch
try:
device_count = torch.cuda.device_count()
print("可用 GPU 数量:", device_count)
for i in range(device_count):
print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
except Exception as e:
print("检测 CUDA 时出错:", e)
nvidia-smi 可以执行
直接贴原因:
NVIDIA 在 /usr/local/cuda/compat/ 目录下提供了一些兼容库 (libcuda.so.1 等),但如果主机的 NVIDIA 驱动版本较新,则不需要这些兼容库。强行使用这些库可能会导致不匹配的 CUDA 版本,从而触发 Error 804。
解决方案:
rm -rf /usr/local/cuda/compat