在安装了 onnxruntime-gpu
后,使用 CUDA 时报错:
2025-01-02 11:40:03.935340441 [E:onnxruntime:Default, provider_bridge_ort.cc:1862 TryGetProviderInfo_CUDA] /onnxruntime_src/onnxruntime/core/session/provider_bridge_ort.cc:1539 onnxruntime::Provider& onnxruntime::ProviderLibrary::Get() [ONNXRuntimeError] : 1 : FAIL : Failed to load library libonnxruntime_providers_cuda.so with error: libcudnn_adv.so.9: cannot open shared object file: No such file or directory
2025-01-02 11:40:03.935371734 [W:onnxruntime:Default, onnxruntime_pybind_state.cc:993 CreateExecutionProviderInstance] Failed to create CUDAExecutionProvider. Require cuDNN 9.* and CUDA 12.*. Please install all dependencies as mentioned in the GPU requirements page (https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements), make sure they're in the PATH, and that your GPU is supported.
很明显,cudnn 并为正确配置。
首先需要说明:
PyTorch 的许多预编译版本(如通过 pip 或 conda 安装)会自带 CUDA 和 cuDNN。它会直接使用打包的版本,而不是依赖系统中全局安装的库。
ONNX Runtime 需要系统中明确安装且可用的 cuDNN 动态库
安装 cudnn,注意版本对应:
https://developer.nvidia.com/cudnn-downloads
proxychains4 wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-9.1.0.70_cuda12-arch
ive.tar.xz
解压下载的文件:
tar -xvf cudnn-linux-x86_64-9.1.0.70_cuda12-archive.tar.xz
解压后,会看到一个名为 cuda 的目录,包含以下子目录:
- include/: 包含头文件,如 cudnn.h
- lib/: 包含动态库,如 libcudnn.so
将解压出来的文件复制到系统的 CUDA 安装路径(默认路径是 /usr/local/cuda/
):
sudo cp ./cudnn-linux-x86_64-9.1.0.70_cuda12-archive/include/* /usr/local/cuda-12.4/include/
sudo cp ./cudnn-linux-x86_64-9.1.0.70_cuda12-archive/lib/* /usr/local/cuda-12.4/lib64/
更新动态链接库缓存,以确保系统可以正确找到 cuDNN 的动态库文件:
检查 LD_LIBRARY_PATH 是否包含 /usr/local/cuda/lib64:
echo $LD_LIBRARY_PATH
如果没有包含,编辑 ~/.bashrc 文件,添加以下内容:
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
source ~/.bashrc
更新动态库缓存:
sudo ldconfig
验证 cuDNN 是否正确安装:
ls /usr/local/cuda/lib64 | grep libcudnn
输出:
libcudnn_adv.so
libcudnn_adv.so.9
libcudnn_adv.so.9.1.0
libcudnn_adv_static.a
libcudnn_adv_static_v9.a
libcudnn_cnn.so
...
检查 cuDNN 版本:
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
输出:
#define CUDNN_MAJOR 9
#define CUDNN_MINOR 1
#define CUDNN_PATCHLEVEL 0