From 5a8b892bdca6cfec3b8d05dc9da362569118c73a Mon Sep 17 00:00:00 2001 From: cloudhan Date: Thu, 18 May 2023 11:53:54 +0800 Subject: [PATCH] [C#] Address the concern of append EP throw (#15973) --- .../SessionOptions.shared.cs | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs index 9380820ea0..eb5685e3ab 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs @@ -66,8 +66,16 @@ namespace Microsoft.ML.OnnxRuntime { CheckCudaExecutionProviderDLLs(); SessionOptions options = new SessionOptions(); - options.AppendExecutionProvider_CUDA(deviceId); - return options; + try + { + options.AppendExecutionProvider_CUDA(deviceId); + return options; + } + catch (Exception) + { + options.Dispose(); + throw; + } } /// @@ -148,9 +156,16 @@ namespace Microsoft.ML.OnnxRuntime public static SessionOptions MakeSessionOptionWithTvmProvider(String settings = "") { SessionOptions options = new SessionOptions(); - options.AppendExecutionProvider_Tvm(settings); - - return options; + try + { + options.AppendExecutionProvider_Tvm(settings); + return options; + } + catch (Exception) + { + options.Dispose(); + throw; + } } ///