Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors about using c# and TensorRT #19489

Open
PTA00 opened this issue Feb 10, 2024 · 4 comments
Open

Errors about using c# and TensorRT #19489

PTA00 opened this issue Feb 10, 2024 · 4 comments
Labels
api:CSharp issues related to the C# API ep:TensorRT issues related to TensorRT execution provider

Comments

@PTA00
Copy link

PTA00 commented Feb 10, 2024

I tried cuda12 and 11, and the corresponding cudnn version, and all attempts failed.
I guarantee that the path is correct and everything is fine when using only dml and cuda, but wrong when using tensorRT.
Before that, I looked at all the relevant issues, which took me 6 hours.
I was wondering if anyone had successfully generated a program using c# and TensorRT.
On .NET8.0 (c#+cuda+cudnn+tensorRT+windows-x64)

I think my head is going to explode.

Unhandled exception. Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:RuntimeException] 
D:\a\_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1209 onnxruntime::ProviderLibrary::Get 
[ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 "" when trying to load 
"C:\Users\PTA00\source\repos\ConsoleApp1\ConsoleApp1\bin\Debug\net8.0\runtimes\win-x64\native\
onnxruntime_providers_tensorrt.dll"
@PTA00
Copy link
Author

PTA00 commented Feb 10, 2024

Here's the test code, from the example:
I only made a slight modification,Switch to tensorRT.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace Microsoft.ML.OnnxRuntime.ResNet50v2Sample
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Read paths
            string modelFilePath = "resnet50-v2-7.onnx";
            string imageFilePath = "dog.jpeg";

            // Read image
            using Image<Rgb24> image = Image.Load<Rgb24>(imageFilePath);

            // Resize image
            image.Mutate(x =>
            {
                x.Resize(new ResizeOptions
                {
                    Size = new Size(224, 224),
                    Mode = ResizeMode.Crop
                });
            });

            // Preprocess image
            Tensor<float> input = new DenseTensor<float>(new[] { 1, 3, 224, 224 });
            var mean = new[] { 0.485f, 0.456f, 0.406f };
            var stddev = new[] { 0.229f, 0.224f, 0.225f };
            image.ProcessPixelRows(accessor =>
            {
                for (int y = 0; y < accessor.Height; y++)
                {
                    Span<Rgb24> pixelSpan = accessor.GetRowSpan(y);
                    for (int x = 0; x < accessor.Width; x++)
                    {
                        input[0, 0, y, x] = ((pixelSpan[x].R / 255f) - mean[0]) / stddev[0];
                        input[0, 1, y, x] = ((pixelSpan[x].G / 255f) - mean[1]) / stddev[1];
                        input[0, 2, y, x] = ((pixelSpan[x].B / 255f) - mean[2]) / stddev[2];
                    }
                }
            });

            // Setup inputs
            var inputs = new List<NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("data", input)
            };
//change--------------------------------

            using var gpuSessionOptions = SessionOptions.MakeSessionOptionWithTensorrtProvider(0);
            using var session = new InferenceSession(modelFilePath, gpuSessionOptions);

            // Run inference
            //using var session = new InferenceSession(modelFilePath);
//change---------------------------------

            using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);

            // Postprocess to get softmax vector
            IEnumerable<float> output = results.First().AsEnumerable<float>();
            float sum = output.Sum(x => (float)Math.Exp(x));
            IEnumerable<float> softmax = output.Select(x => (float)Math.Exp(x) / sum);

            // Extract top 10 predicted classes
            IEnumerable<Prediction> top10 = softmax.Select((x, i) => new Prediction { Label = LabelMap.Labels[i], Confidence = x })
                               .OrderByDescending(x => x.Confidence)
                               .Take(10);

            // Print results to console
            Console.WriteLine("Top 10 predictions for ResNet50 v2...");
            Console.WriteLine("--------------------------------------------------------------");
            foreach (var t in top10)
            {
                Console.WriteLine($"Label: {t.Label}, Confidence: {t.Confidence}");
            }
        }
    }
}

@PTA00
Copy link
Author

PTA00 commented Feb 11, 2024

Snipaste_2024-02-11_14-33-50

Microsoft.ML.OnnxRuntime.OnnxRuntimeException:“[ErrorCode:RuntimeException] 
D:\a\_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1209 
onnxruntime::ProviderLibrary::Get [ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 ""
 when trying to load "C:\Users\PTA00\Desktop\sample\Microsoft.ML.OnnxRuntime.ResNet50v2Sample
\bin\Debug\net8.0\runtimes\win-x64\native\onnxruntime_providers_tensorrt.dll"

@yuslepukhin
Copy link
Member

This is what I would do.
dumpbin /DEPENDENTS onnxruntime_providers_tensorrt.dll" and then try running where on each of them.
I don't think dependency walker still works well.

@yuslepukhin
Copy link
Member

BTW, I recommend using OrtValue API for direct memory access. This reduces amount of garbage.

@sophies927 sophies927 added ep:TensorRT issues related to TensorRT execution provider api:CSharp issues related to the C# API labels Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:CSharp issues related to the C# API ep:TensorRT issues related to TensorRT execution provider
Projects
None yet
Development

No branches or pull requests

3 participants