From 64d52ae47d1d8a1d8b3e768ccd7e818f6503354a Mon Sep 17 00:00:00 2001 From: Hariharan Seshadri Date: Sat, 29 Aug 2020 15:18:50 -0700 Subject: [PATCH] Support creating sessions using DML EP via C# (#4955) --- .../src/Microsoft.ML.OnnxRuntime/NativeMethods.cs | 3 +++ .../src/Microsoft.ML.OnnxRuntime/SessionOptions.cs | 14 +++++++++++--- .../InferenceTest.cs | 6 ++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs index 4741889bbb..615cc63737 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs @@ -512,6 +512,9 @@ namespace Microsoft.ML.OnnxRuntime [DllImport(nativeLib, CharSet = charSet)] public static extern IntPtr /*(OrtStatus*)*/ OrtSessionOptionsAppendExecutionProvider_CUDA(IntPtr /*(OrtSessionOptions*) */ options, int device_id); + [DllImport(nativeLib, CharSet = charSet)] + public static extern IntPtr /*(OrtStatus*)*/ OrtSessionOptionsAppendExecutionProvider_Dml(IntPtr /*(OrtSessionOptions*) */ options, int device_id); + [DllImport(nativeLib, CharSet = charSet)] public static extern IntPtr /*(OrtStatus*)*/ OrtSessionOptionsAppendExecutionProvider_NGraph(IntPtr /*(OrtSessionOptions*) */ options, string /*(const char*)*/ ng_backend_type); diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs index 16ff02aed0..b9118ccc23 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs @@ -69,8 +69,8 @@ namespace Microsoft.ML.OnnxRuntime { CheckCudaExecutionProviderDLLs(); SessionOptions options = new SessionOptions(); - NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options.Handle, deviceId); - NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options.Handle, 1); + NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options.Handle, deviceId)); + NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options.Handle, 1)); return options; } @@ -83,7 +83,7 @@ namespace Microsoft.ML.OnnxRuntime public static SessionOptions MakeSessionOptionWithNupharProvider(String settings = "") { SessionOptions options = new SessionOptions(); - NativeMethods.OrtSessionOptionsAppendExecutionProvider_Nuphar(options.Handle, 1, settings); + NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Nuphar(options.Handle, 1, settings)); return options; } @@ -111,6 +111,14 @@ namespace Microsoft.ML.OnnxRuntime NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(handle, deviceId)); } + /// + /// Use only if you have the onnxruntime package specific to this Execution Provider. + /// + public void AppendExecutionProvider_Dml(int deviceId) + { + NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Dml(handle, deviceId)); + } + /// /// Use only if you have the onnxruntime package specific to this Execution Provider. /// diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index 6fbb6055e9..812d3c3c61 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -91,6 +91,9 @@ namespace Microsoft.ML.OnnxRuntime.Tests #if USE_CUDA opt.AppendExecutionProvider_CUDA(0); #endif +#if USE_DML + opt.AppendExecutionProvider_Dml(0); +#endif #if USE_NGRAPH opt.AppendExecutionProvider_NGraph("CPU"); //TODO: this API should be refined #endif @@ -1830,6 +1833,9 @@ namespace Microsoft.ML.OnnxRuntime.Tests #if USE_CUDA ,"OrtSessionOptionsAppendExecutionProvider_CUDA" #endif +#if USE_DML + ,"OrtSessionOptionsAppendExecutionProvider_Dml" +#endif #if USE_NGRAPH ,"OrtSessionOptionsAppendExecutionProvider_NGraph" #endif