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

Using DML in C++ DLL function called from C# #20546

Closed
Daniel-Butt opened this issue May 2, 2024 · 2 comments
Closed

Using DML in C++ DLL function called from C# #20546

Daniel-Butt opened this issue May 2, 2024 · 2 comments
Labels
ep:DML issues related to the DirectML execution provider platform:windows issues related to the Windows platform

Comments

@Daniel-Butt
Copy link

Describe the issue

I have built a C++ exe application that uses Onnx runtime and DML (through NuGet), and it works perfectly. However, I recently converted the application to a DLL so that I can use the C++ code in a C# application. It runs correctly on the CPU, but if I attempt to use DML, I get the following error: D:\a_work\1\s\onnxruntime\core\providers\dml\dml_provider_factory.cc(471)\onnxruntime.dll!00007FFE41A13386: (caller: 00007FFE41A134E3) Exception(1) tid(503c) 887A0004 The specified device interface or feature level is not supported on this system.

The associated DLL code for performing the ML related tasks is identical to that of the exe. Again, I have tested the exe version on the same PC with the same drivers, and I don't get any errors.

Any ideas as to why I'm having issues with specifically just the DLL?

To reproduce

C++ code:

static bool attemptToUseDML(OrtApi const& ortApi, Ort::SessionOptions &sessionOptions) {

try {

    OrtDmlApi const* ortDmlApi = nullptr;
    ortApi.GetExecutionProviderApi("DML", ORT_API_VERSION, reinterpret_cast<void const**>(&ortDmlApi));
    sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
    sessionOptions.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL); 
    sessionOptions.DisableMemPattern(); 
    
    Ort::ThrowOnError(ortDmlApi->SessionOptionsAppendExecutionProvider_DML(sessionOptions, /*device index*/ 0));
}
catch (Ort::Exception& oe) {

    std::cout << oe.what() << '\n';
    
    return false;
}

return true;

}

Urgency

No response

Platform

Windows

OS Version

10

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.17.3

ONNX Runtime API

C++

Architecture

X64

Execution Provider

DirectML

Execution Provider Library Version

1.13.1

@github-actions github-actions bot added ep:DML issues related to the DirectML execution provider platform:windows issues related to the Windows platform labels May 2, 2024
@Daniel-Butt
Copy link
Author

I have no idea why, but for some reason, the "DirectML.dll" wasn't being loaded despite being in the same directory as all the other dll files being loaded by Onnx. So I explicitly preloaded it, and that solved the problem.

Added the following in the C# code:

IntPtr hModule = LoadLibrary("DirectML.dll");

@yuslepukhin
Copy link
Member

Check which one is loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ep:DML issues related to the DirectML execution provider platform:windows issues related to the Windows platform
Projects
None yet
Development

No branches or pull requests

2 participants