在 Windows 11 上使用 ONNX Runtime 和 C# 为 GPU 配置 CUDA 和 cuDNN

先决条件

  • Windows 11
  • Visual Studio 2019 或 2022

在 Windows 11 上使用 C# 为 ONNX Runtime 配置 CUDA 和 cuDNN 的步骤

请参阅此表了解支持的版本:

ONNX Runtime 版本 CUDA 工具包版本 cuDNN 版本
1.13 - 1.16 11.6 8.5.0.96
1.9 - 1.12 11.4 8.2.2.26

注意:完整表格可在此处找到:此处

  • 请遵循第 2 节“在 Windows 上安装 cuDNN”。注意:跳过第 2.3 节中关于更新 Visual Studio 设置的第 5 步,这仅适用于 C++ 项目。

  • 重启您的计算机,并通过运行以下命令或使用 PyTorch 在 Python 中验证安装。

nvcc --version
import torch
torch.cuda.is_available()
  • 现在,您可以使用以下代码在 C# ONNX Runtime API 中启用 GPU:
// keep in mind almost all of the classes are disposable.
using var gpuSessionOptions = SessionOptions.MakeSessionOptionWithCudaProvider(0);
using var session = new InferenceSession(modelPath, gpuSessionOptions);

查看更多 C# ONNX Runtime 资源