diff --git a/csharp/sample/Microsoft.ML.OnnxRuntime.InferenceSample/Program.cs b/csharp/sample/Microsoft.ML.OnnxRuntime.InferenceSample/Program.cs index 888f0f7b14..0025bb8429 100644 --- a/csharp/sample/Microsoft.ML.OnnxRuntime.InferenceSample/Program.cs +++ b/csharp/sample/Microsoft.ML.OnnxRuntime.InferenceSample/Program.cs @@ -24,8 +24,11 @@ namespace CSharpUsage { string modelPath = Directory.GetCurrentDirectory() + @"\squeezenet.onnx"; + // Optional : Create session options and set the graph optimization level for the session + SessionOptions options = new SessionOptions(); + options.SetSessionGraphOptimizationLevel(2); - using (var session = new InferenceSession(modelPath)) + using (var session = new InferenceSession(modelPath, options)) { var inputMeta = session.InputMetadata; var container = new List(); diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs index 756fe203e2..68da5be406 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs @@ -157,6 +157,9 @@ namespace Microsoft.ML.OnnxRuntime [DllImport(nativeLib, CharSet = charSet)] public static extern int OrtSetSessionThreadPoolSize(IntPtr /* OrtSessionOptions* */ options, int sessionThreadPoolSize); + [DllImport(nativeLib, CharSet = charSet)] + public static extern int OrtSetSessionGraphOptimizationLevel(IntPtr /* OrtSessionOptions* */ options, uint graphOptimizationLevel); + ///** // * The order of invocation indicates the preference order as well. In other words call this method diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs index fcd200a777..9a22b1297e 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs @@ -35,6 +35,21 @@ namespace Microsoft.ML.OnnxRuntime _nativePtr = NativeMethods.OrtCreateSessionOptions(); } + /// + /// Sets the graph optimization level for the session. Default is set to 1. + /// + /// optimization level for the session + /// Available options are : 0, 1, 2 + /// 0 -> Disable all optimizations + /// 1 -> Enable basic optimizations + /// 2 -> Enable all optimizations + /// True on success and false otherwise + public bool SetSessionGraphOptimizationLevel(uint optimization_level) + { + var result = NativeMethods.OrtSetSessionGraphOptimizationLevel(_nativePtr, optimization_level); + return result == 0; + } + /// /// Default instance /// diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index 0fb6cd77b7..af019ecbbe 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -50,12 +50,18 @@ namespace Microsoft.ML.OnnxRuntime.Tests } } - [Fact] - private void CanRunInferenceOnAModel() + [Theory] + [InlineData(0)] + [InlineData(2)] + private void CanRunInferenceOnAModel(uint graphOptimizationLevel) { string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "squeezenet.onnx"); - using (var session = new InferenceSession(modelPath)) + // Set the graph optimization level for this session. + SessionOptions options = new SessionOptions(); + Assert.True(options.SetSessionGraphOptimizationLevel(graphOptimizationLevel)); + + using (var session = new InferenceSession(modelPath, options)) { var inputMeta = session.InputMetadata; var container = new List(); @@ -664,8 +670,8 @@ namespace Microsoft.ML.OnnxRuntime.Tests "OrtSessionGetOutputTypeInfo","OrtReleaseSession","OrtCreateSessionOptions","OrtCloneSessionOptions", "OrtEnableSequentialExecution","OrtDisableSequentialExecution","OrtEnableProfiling","OrtDisableProfiling", "OrtEnableMemPattern","OrtDisableMemPattern","OrtEnableCpuMemArena","OrtDisableCpuMemArena", - "OrtSetSessionLogId","OrtSetSessionLogVerbosityLevel","OrtSetSessionThreadPoolSize","OrtSessionOptionsAppendExecutionProvider_CPU", - "OrtCreateAllocatorInfo","OrtCreateCpuAllocatorInfo", + "OrtSetSessionLogId","OrtSetSessionLogVerbosityLevel","OrtSetSessionThreadPoolSize","OrtSetSessionGraphOptimizationLevel", + "OrtSessionOptionsAppendExecutionProvider_CPU","OrtCreateAllocatorInfo","OrtCreateCpuAllocatorInfo", "OrtCreateDefaultAllocator","OrtAllocatorFree","OrtAllocatorGetInfo", "OrtCreateTensorWithDataAsOrtValue","OrtGetTensorMutableData", "OrtReleaseAllocatorInfo", "OrtCastTypeInfoToTensorInfo","OrtGetTensorShapeAndType","OrtGetTensorElementType","OrtGetNumOfDimensions", @@ -677,7 +683,7 @@ namespace Microsoft.ML.OnnxRuntime.Tests var x = GetProcAddress(hModule, ep); Assert.False(x == UIntPtr.Zero, $"Entrypoint {ep} not found in module {module}"); } - } + } static string GetTestModelsDir() { diff --git a/docs/CSharp_API.md b/docs/CSharp_API.md index c67b3d6d25..8f4c309917 100644 --- a/docs/CSharp_API.md +++ b/docs/CSharp_API.md @@ -105,6 +105,12 @@ Constructs a SessionOptions will all options at default/unset values. Accessor to the default static option object #### Methods + SetSessionGraphOptimizationLevel(uint optimization_level); +Sets the graph optimization level for the session. Default is set to 1. Available options are : {0, 1, 2}. + * 0 -> Disable all optimizations + * 1 -> Enable basic optimizations such as redundant node removals and constant folding + * 2 -> Enable all optimizations (includes Level1 and more complex optimizations such as node fusions) + AppendExecutionProvider(ExecutionProvider provider); Appends execution provider to the session. For any operator in the graph the first execution provider that implements the operator will be user. ExecutionProvider is defined as the following enum.