How to get correct GPU device id for Microsoft.ML.OnnxRuntime.DirectML (.net core 3.1)?

2.3k views Asked by At

I am using Microsoft.ML.OnnxRuntime.DirectML nuget package for image classification like this:

    var options = new SessionOptions();
    options.AppendExecutionProvider_DML( 1 ); // deviceId goes here
    var session = new InferenceSession( _modelPath, options );

And I have one big problem: in IIS integrated video card has deviceId 0 and discrete has deviceId 1. But when my app is running under Kestrel, integrated has deviceId 1 and discrete has deviceId 0, and this is opposite to what Task Manager shows in "GPU engine" column when scoring is in progress.

And right now my integrated card can not be used with this package as it throws this exception (and this is pointless anyway):

Exception Info: Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:RuntimeException] D:\5\s\onnxruntime\core\providers\dml\dml_provider_factory.cc(110)\onnxruntime.DLL!00007FF8C074118F: (caller: 00007FF8C07411C7) Exception(941) tid(35b8) 887A0020 An internal issue prevented the driver from carrying out the specified operation. The driver's state is probably suspect, and the application should not continue.

So I need a reliable way to detect deviceId for discrete video card.

1

There are 1 answers

0
Omni On

Ok, I have found a workaround for now. I can check witch video card is integrated/discrete using this method How get GPU information in C#? and then I can use Vortice.DXGI nuget package to find out which deviceId each video card has - it is basically index

DXGI.CreateDXGIFactory1( out IDXGIFactory1 factory );
factory.EnumAdapters1( 0, out var adapter0 );
factory.EnumAdapters1( 1, out var adapter1 );
factory.EnumAdapters1( 2, out var adapter2 );