Onnxruntime не может загрузить `cudnn64_9.dll` в .net ⇐ C#

Место общения программистов C#
Anonymous
Onnxruntime не может загрузить `cudnn64_9.dll` в .net

Сообщение Anonymous »

Я хочу использовать microsoft.ml.onnxruntime.gpu версия 1.20.1 в этом уроке на веб -сайте ONNX в Dotnet 9.0.300. Учебное пособие использует процессор, но я хочу изменить его на GPU. Однако мой модифицированный код - это ошибка. Мой GPU-RTX 2060.

Код: Выделить всё

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
using Microsoft.ML.OnnxRuntime;
using SixLabors.ImageSharp.Formats;

// keep in mind almost all of the classes are disposable.
string modelFilePath = "FasterRCNN-10.onnx";
string imageFilePath = "demo.jpg";
string outImageFilePath = "output.jpg";
using Image image = Image.Load(imageFilePath, out IImageFormat format);
float ratio = 800f / Math.Min(image.Width, image.Height);
using Stream imageStream = new MemoryStream();
image.Mutate(x => x.Resize((int)(ratio * image.Width), (int)(ratio * image.Height)));
image.Save(imageStream, format);
var paddedHeight = (int)(Math.Ceiling(image.Height / 32f) * 32f);
var paddedWidth = (int)(Math.Ceiling(image.Width / 32f) * 32f);
var mean = new[] { 102.9801f, 115.9465f, 122.7717f };
var _sessionOptions = new SessionOptions();
_sessionOptions.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL;
_sessionOptions.AppendExecutionProvider_CUDA();

// Preprocessing image
// We use DenseTensor for multi-dimensional access
DenseTensor input = new(new[] { 3, paddedHeight, paddedWidth });
image.ProcessPixelRows(accessor =>
{
for (int y = paddedHeight - accessor.Height; y < accessor.Height; y++)
{
Span pixelSpan = accessor.GetRowSpan(y);
for (int x = paddedWidth - accessor.Width; x < accessor.Width; x++)
{
input[0, y, x] = pixelSpan[x].B - mean[0];
input[1, y, x] = pixelSpan[x].G - mean[1];
input[2, y, x] = pixelSpan[x].R - mean[2];
}
}
});
using var inputOrtValue = OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance,
input.Buffer, new long[] { 3, paddedHeight, paddedWidth });

var inputs = new Dictionary
{
{ "image", inputOrtValue }
};

using var session = new InferenceSession(modelFilePath,_sessionOptions);
using var runOptions = new RunOptions();
using IDisposableReadOnlyCollection results = session.Run(runOptions, inputs, session.OutputNames);

var boxesSpan = results[0].GetTensorDataAsSpan();
var labelsSpan = results[1].GetTensorDataAsSpan();
var confidencesSpan = results[2].GetTensorDataAsSpan();

const float minConfidence = 0.7f;
var predictions = new List
();

for (int i = 0; i < boxesSpan.Length - 4; i += 4)
{
var index = i / 4;
if (confidencesSpan[index] >= minConfidence)
{
predictions.Add(new Prediction
{
Box = new Box(boxesSpan[i], boxesSpan[i + 1], boxesSpan[i + 2], boxesSpan[i + 3]),
Label = LabelMap.Labels[labelsSpan[index]],
Confidence = confidencesSpan[index]
});
}
}

using var outputImage = File.OpenWrite(outImageFilePath);
Font font = SystemFonts.CreateFont("Arial", 16);
foreach (var p in predictions)
{
image.Mutate(x =>
{
x.DrawPolygon(Color.Red, 2f, new PointF[] {
new PointF(p.Box.Xmin, p.Box.Ymin),
new PointF(p.Box.Xmax, p.Box.Ymin),
new PointF(p.Box.Xmax, p.Box.Ymax),
new PointF(p.Box.Xmin, p.Box.Ymax),
new PointF(p.Box.Xmin, p.Box.Ymin)
});
x.DrawText($"{p.Label}, {p.Confidence:0.00}", font, Color.White, new PointF(p.Box.Xmin, p.Box.Ymin));
});
}
image.Save(outputImage, format);

< /code>
Ошибка < /p>
PS E:\Temp\test-onnx> dotnet run
E:\Temp\test-onnx\Prediction.cs(3,20): warning CS8618: Non-nullable property 'Box' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
E:\Temp\test-onnx\Prediction.cs(4,23): warning CS8618: Non-nullable property 'Label' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
Unhandled exception.  Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:RuntimeException] D:\a\_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1539 onnxruntime::ProviderLibrary::Get [ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 "" when trying to load "E:\Temp\test-onnx\bin\Debug\net9.0\runtimes\win-x64\native\onnxruntime_providers_cuda.dll"

at Microsoft.ML.OnnxRuntime.SessionOptions.AppendExecutionProvider_CUDA(Int32 deviceId)
at Program.$(String[] args) in E:\Temp\test-onnx\Program.cs:line 29
Я попытался установить более новую версию cudnn и cuda toolkit . но я получил ошибку. < /p>
nvcc -v Результат: < /p>

Код: Выделить всё

nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2025 NVIDIA Corporation
Built on Wed_Apr__9_19:29:17_Pacific_Daylight_Time_2025
Cuda compilation tools, release 12.9, V12.9.41
Build cuda_12.9.r12.9/compiler.35813241_0
< /code>
Я попробовал несколько версий Onnx. < /p>
Это информация отладки Onnx python < /p>
import onnxruntime

# Preload necessary DLLs
onnxruntime.preload_dlls()

onnxruntime.print_debug_info()
< /code>
Результат < /p>
Failed to load cublasLt64_12.dll: Could not find module 'cublasLt64_12.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cublas64_12.dll: Could not find module 'cublas64_12.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cufft64_11.dll: Could not find module 'cufft64_11.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudart64_12.dll: Could not find module 'cudart64_12.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn_engines_runtime_compiled64_9.dll: Could not find module 'cudnn_engines_runtime_compiled64_9.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn_engines_precompiled64_9.dll: Could not find module 'cudnn_engines_precompiled64_9.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn_heuristic64_9.dll: Could not find module 'cudnn_heuristic64_9.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn_ops64_9.dll: Could not find module 'cudnn_ops64_9.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn_adv64_9.dll: Could not find module 'cudnn_adv64_9.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn_graph64_9.dll: Could not find module 'cudnn_graph64_9.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Failed to load cudnn64_9.dll: Could not find module 'cudnn64_9.dll' (or one of its dependencies).  Try using the full path with constructor syntax.
Please follow https://onnxruntime.ai/docs/install/#cuda-and-cudnn to install CUDA and CuDNN.
onnxruntime-gpu version: 1.22.0
CUDA version used in build: 12.2
platform: Windows-10-10.0.26100-SP0

Python package, version and location:
onnxruntime-gpu==1.22.0 at E:\Temp\test-ango\.venv\lib\site-packages\onnxruntime
torch not installed
nvidia-cuda-runtime-cu12 not installed
nvidia-cudnn-cu12 not installed
nvidia-cublas-cu12 not installed
nvidia-cufft-cu12 not installed
nvidia-curand-cu12 not installed
nvidia-cuda-nvrtc-cu12 not installed
nvidia-nvjitlink-cu12 not installed

Environment variable:
PATH=c:\Users\behro\.vscode\extensions\ms-python.python-2025.6.1-win32-x64\python_files\deactivate\powershell;E:\Temp\test-ango\.venv\Scripts;C:\Program Files\WindowsApps\Microsoft.PowerShell_7.5.1.0_x64__8wekyb3d8bbwe;C:\Program Files\WindowsApps\Microsoft.PowerShell_7.5.1.0_x64__8wekyb3d8bbwe;c:\Users\behro\.vscode\extensions\ms-python.python-2025.6.1-win32-x64\python_files\deactivate\powershell;E:\Temp\test-ango\.venv\Scripts;C:\Program Files\WindowsApps\Microsoft.PowerShell_7.5.1.0_x64__8wekyb3d8bbwe;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.9\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.9\libnvvp;C:\Program Files\Microsoft\jdk-17.0.14.7-hotspot\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\libnvvp;C:\Program Files\NVIDIA\CUDNN\v9.2\bin;C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Cloudflare\Cloudflare WARP\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\chocolatey\bin;C:\Program Files\GitHub CLI\;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files (x86)\DVC (Data Version Control);C:\Program Files\dotnet\;C:\Program Files\NVIDIA Corporation\NVIDIA app\NvDLISR;C:\Program Files\Git\cmd;C:\Program Files\eSpeak NG\;C:\Program Files\nodejs\;C:\Program Files\NVIDIA Corporation\Nsight Compute 2025.2.0\;C:\Users\behro\.cargo\bin;C:\Users\behro\AppData\Local\Microsoft\WindowsApps;C:\Users\behro\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\OpenSSL-Win64\bin;C:\Program Files (x86)\Android\android-sdk\platform-tools;C:\Users\behro\.dotnet\tools;C:\Users\behro\AppData\Local\Microsoft\WinGet\Links;C:\Users\behro\.lmstudio\bin;C:\Users\behro\AppData\Roaming\npm;C:\Program Files\NVIDIA\CUDNN\v9.10\bin;C:\Users\behro\.dotnet\tools

List of loaded DLLs:
E:\Temp\test-ango\.venv\Lib\site-packages\numpy.libs\msvcp140-263139962577ecda4cd9469ca360a746.dll
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\vcruntime140_1.dll
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\vcruntime140.dll
C:\Windows\System32\msvcp140.dll
C:\Windows\System32\msvcp140_1.dll
please `pip install py-cpuinfo py3nvml` to show device information.
C: \ Program Files \ nvidia \ cudnn \ v9.10 \ bin включена в пути. но требуется DLLS не появляется.


Подробнее здесь: https://stackoverflow.com/questions/796 ... dll-in-net

Вернуться в «C#»