在 Windows 11 上使用 ONNX Runtime 和 C# 為 GPU 配置 CUDA 和 cuDNN
先決條件
- Windows 11
- Visual Studio 2019 或 2022
在 Windows 11 上使用 C# 為 ONNX Runtime 配置 CUDA 和 cuDNN 的步驟
-
根據 ONNX Runtime 版本支援的版本,下載並安裝 CUDA 工具包。
-
根據 ONNX Runtime 版本支援的版本,下載並安裝 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);