microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.56k stars 2.91k forks source link

NativeMethod failed with NullReferenceException under c# ONNX in Windows_NT #21309

Open darren-zdc opened 3 months ago

darren-zdc commented 3 months ago

Describe the issue

I have a C# unit test that load an ONNX model and do a prediction. The test works well in my Windows 11 box. But when I tested it in my Windows Server, it failed with a nullreference exception.

I wonder if the ONNX runtime package is compatible with Windows_NT.

Error message:

  Failed Datahunter.Tests.BertSentenceSimilarity.NLPSearchTest.GivenTwoTitles_ThenFindOutSimilarityScore(title1: "Battery technologies mass weighted SDP", title2: "battery life cycle", expectedScore: 0.92900002) [508 ms]
  Error Message:
   System.TypeInitializationException : The type initializer for 'Microsoft.ML.OnnxRuntime.NativeMethods' threw an exception.
---- System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace:
     at Microsoft.ML.OnnxRuntime.SessionOptions..ctor()
   at Microsoft.ML.OnnxRuntime.InferenceSession..ctor(String modelPath)
   at Datahunter.Tests.BertSentenceSimilarity.NLPSearchTest.GivenTwoTitles_ThenFindOutSimilarityScore(String title1, String title2, Single expectedScore) in D:\vsts-agent-win-x64-3.225.0\_work\60\s\DataHunter-Main\tests\Datahunter.Tests\BertSentenceSimilarity\NLPSearchTest.cs:line 95
--- End of stack trace from previous location ---
----- Inner Stack Trace -----
   at Microsoft.ML.OnnxRuntime.NativeMethods..cctor()

System information: OS: Windows_NT OSArchitecture: x64 OSVersion: 10.0.20348

To reproduce

in a Windows_NT box, run a dotnet net unit test to load an ONNX model. I have something like these.

 private string _baseUncasedModelFromPytorchPath = @"BertSentenceSimilarity/bert-base-uncased.onnx";

 public async Task GivenTwoTitles_ThenFindOutSimilarityScore(string title1, string title2, float expectedScore)
 {
     // Arrange
     // Create an InferenceSession from the Model Path.
     using var session = new InferenceSession(Path.GetFullPath(_baseUncasedModelFromPytorchPath));
     using var runOptions = new RunOptions();
 }

Urgency

No response

Platform

Windows

OS Version

10.0.20348

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.18.0

ONNX Runtime API

C#

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

amusingCloud commented 3 weeks ago

I'm also facing same NullReferenceException at Microsoft.ML.OnnxRuntime.NativeMethods..cctor().