Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2dbce4e

Browse files
authored
csharp api for graph transformers (microsoft#741)
* add graph optimization level to csharp api * format documentation * changes per review comments
1 parent 0688843 commit 2dbce4e

5 files changed

Lines changed: 40 additions & 7 deletions

File tree

csharp/sample/Microsoft.ML.OnnxRuntime.InferenceSample/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ static void UseApi()
2424
{
2525
string modelPath = Directory.GetCurrentDirectory() + @"\squeezenet.onnx";
2626

27+
// Optional : Create session options and set the graph optimization level for the session
28+
SessionOptions options = new SessionOptions();
29+
options.SetSessionGraphOptimizationLevel(2);
2730

28-
using (var session = new InferenceSession(modelPath))
31+
using (var session = new InferenceSession(modelPath, options))
2932
{
3033
var inputMeta = session.InputMetadata;
3134
var container = new List<NamedOnnxValue>();

csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca
157157
[DllImport(nativeLib, CharSet = charSet)]
158158
public static extern int OrtSetSessionThreadPoolSize(IntPtr /* OrtSessionOptions* */ options, int sessionThreadPoolSize);
159159

160+
[DllImport(nativeLib, CharSet = charSet)]
161+
public static extern int OrtSetSessionGraphOptimizationLevel(IntPtr /* OrtSessionOptions* */ options, uint graphOptimizationLevel);
162+
160163

161164
///**
162165
// * The order of invocation indicates the preference order as well. In other words call this method

csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ public SessionOptions()
3535
_nativePtr = NativeMethods.OrtCreateSessionOptions();
3636
}
3737

38+
/// <summary>
39+
/// Sets the graph optimization level for the session. Default is set to 1.
40+
/// </summary>
41+
/// <param name="optimization_level">optimization level for the session</param>
42+
/// Available options are : 0, 1, 2
43+
/// 0 -> Disable all optimizations
44+
/// 1 -> Enable basic optimizations
45+
/// 2 -> Enable all optimizations
46+
/// <returns>True on success and false otherwise</returns>
47+
public bool SetSessionGraphOptimizationLevel(uint optimization_level)
48+
{
49+
var result = NativeMethods.OrtSetSessionGraphOptimizationLevel(_nativePtr, optimization_level);
50+
return result == 0;
51+
}
52+
3853
/// <summary>
3954
/// Default instance
4055
/// </summary>

csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,18 @@ public void CanCreateAndDisposeSessionWithModelPath()
5050
}
5151
}
5252

53-
[Fact]
54-
private void CanRunInferenceOnAModel()
53+
[Theory]
54+
[InlineData(0)]
55+
[InlineData(2)]
56+
private void CanRunInferenceOnAModel(uint graphOptimizationLevel)
5557
{
5658
string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "squeezenet.onnx");
5759

58-
using (var session = new InferenceSession(modelPath))
60+
// Set the graph optimization level for this session.
61+
SessionOptions options = new SessionOptions();
62+
Assert.True(options.SetSessionGraphOptimizationLevel(graphOptimizationLevel));
63+
64+
using (var session = new InferenceSession(modelPath, options))
5965
{
6066
var inputMeta = session.InputMetadata;
6167
var container = new List<NamedOnnxValue>();
@@ -664,8 +670,8 @@ private void VerifyNativeMethodsExist()
664670
"OrtSessionGetOutputTypeInfo","OrtReleaseSession","OrtCreateSessionOptions","OrtCloneSessionOptions",
665671
"OrtEnableSequentialExecution","OrtDisableSequentialExecution","OrtEnableProfiling","OrtDisableProfiling",
666672
"OrtEnableMemPattern","OrtDisableMemPattern","OrtEnableCpuMemArena","OrtDisableCpuMemArena",
667-
"OrtSetSessionLogId","OrtSetSessionLogVerbosityLevel","OrtSetSessionThreadPoolSize","OrtSessionOptionsAppendExecutionProvider_CPU",
668-
"OrtCreateAllocatorInfo","OrtCreateCpuAllocatorInfo",
673+
"OrtSetSessionLogId","OrtSetSessionLogVerbosityLevel","OrtSetSessionThreadPoolSize","OrtSetSessionGraphOptimizationLevel",
674+
"OrtSessionOptionsAppendExecutionProvider_CPU","OrtCreateAllocatorInfo","OrtCreateCpuAllocatorInfo",
669675
"OrtCreateDefaultAllocator","OrtAllocatorFree","OrtAllocatorGetInfo",
670676
"OrtCreateTensorWithDataAsOrtValue","OrtGetTensorMutableData", "OrtReleaseAllocatorInfo",
671677
"OrtCastTypeInfoToTensorInfo","OrtGetTensorShapeAndType","OrtGetTensorElementType","OrtGetNumOfDimensions",
@@ -677,7 +683,7 @@ private void VerifyNativeMethodsExist()
677683
var x = GetProcAddress(hModule, ep);
678684
Assert.False(x == UIntPtr.Zero, $"Entrypoint {ep} not found in module {module}");
679685
}
680-
}
686+
}
681687

682688
static string GetTestModelsDir()
683689
{

docs/CSharp_API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ Constructs a SessionOptions will all options at default/unset values.
105105
Accessor to the default static option object
106106

107107
#### Methods
108+
SetSessionGraphOptimizationLevel(uint optimization_level);
109+
Sets the graph optimization level for the session. Default is set to 1. Available options are : {0, 1, 2}.
110+
* 0 -> Disable all optimizations
111+
* 1 -> Enable basic optimizations such as redundant node removals and constant folding
112+
* 2 -> Enable all optimizations (includes Level1 and more complex optimizations such as node fusions)
113+
108114
AppendExecutionProvider(ExecutionProvider provider);
109115
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.
110116

0 commit comments

Comments
 (0)