Skip to content

Commit be9ae28

Browse files
authored
Add ability to set RunOptions config entries to C# API. (microsoft#13939)
### Description <!-- Describe your changes. --> Add ability to set RunOptions config entries. Largely a cut-and-paste of the existing code for setting SessionOptions config entries. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> microsoft#13936
1 parent 7d20d88 commit be9ae28

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ static NativeMethods()
341341
OrtRunOptionsGetRunTag = (DOrtRunOptionsGetRunTag)Marshal.GetDelegateForFunctionPointer(api_.RunOptionsGetRunTag, typeof(DOrtRunOptionsGetRunTag));
342342
OrtRunOptionsSetTerminate = (DOrtRunOptionsSetTerminate)Marshal.GetDelegateForFunctionPointer(api_.RunOptionsSetTerminate, typeof(DOrtRunOptionsSetTerminate));
343343
OrtRunOptionsUnsetTerminate = (DOrtRunOptionsUnsetTerminate)Marshal.GetDelegateForFunctionPointer(api_.RunOptionsUnsetTerminate, typeof(DOrtRunOptionsUnsetTerminate));
344+
OrtAddRunConfigEntry = (DOrtAddRunConfigEntry)Marshal.GetDelegateForFunctionPointer(api_.AddRunConfigEntry, typeof(DOrtAddRunConfigEntry));
344345

345346
OrtCreateArenaCfg = (DOrtCreateArenaCfg)Marshal.GetDelegateForFunctionPointer(api_.CreateArenaCfg, typeof(DOrtCreateArenaCfg));
346347
OrtReleaseArenaCfg = (DOrtReleaseArenaCfg)Marshal.GetDelegateForFunctionPointer(api_.ReleaseArenaCfg, typeof(DOrtReleaseArenaCfg));
@@ -1071,9 +1072,22 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca
10711072
public delegate IntPtr /*(OrtStatus*)*/ DOrtRunOptionsUnsetTerminate(IntPtr /* OrtRunOptions* */ options);
10721073
public static DOrtRunOptionsUnsetTerminate OrtRunOptionsUnsetTerminate;
10731074

1074-
#endregion
10751075

1076-
#region Allocator/MemoryInfo API
1076+
/// <summary>
1077+
/// Add run config entry
1078+
/// </summary>
1079+
/// <param name="options">Native RunOptions instance</param>
1080+
/// <param name="configKey">Config key</param>
1081+
/// <param name="configValue">Config value</param>
1082+
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
1083+
public delegate IntPtr /*(OrtStatus*)*/ DOrtAddRunConfigEntry(IntPtr /* OrtRunOptions* */ options,
1084+
IntPtr /* const char* */configKey,
1085+
IntPtr /* const char* */ configValue);
1086+
public static DOrtAddRunConfigEntry OrtAddRunConfigEntry;
1087+
1088+
#endregion
1089+
1090+
#region Allocator/MemoryInfo API
10771091

10781092
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
10791093
public delegate IntPtr /* (OrtStatus*)*/ DOrtCreateMemoryInfo(

csharp/src/Microsoft.ML.OnnxRuntime/RunOptions.shared.cs

+18
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ public bool Terminate
119119
}
120120
private bool _terminate = false; //value set to default value of the C++ RunOptions
121121

122+
/// <summary>
123+
/// Set a single run configuration entry as a pair of strings
124+
/// If a configuration with same key exists, this will overwrite the configuration with the given configValue.
125+
/// </summary>
126+
/// <param name="configKey">config key name</param>
127+
/// <param name="configValue">config key value</param>
128+
public void AddRunConfigEntry(string configKey, string configValue)
129+
{
130+
using (var pinnedConfigKeyName = new PinnedGCHandle(
131+
GCHandle.Alloc(NativeOnnxValueHelper.StringToZeroTerminatedUtf8(configKey), GCHandleType.Pinned)))
132+
using (var pinnedConfigValueName = new PinnedGCHandle(
133+
GCHandle.Alloc(NativeOnnxValueHelper.StringToZeroTerminatedUtf8(configValue), GCHandleType.Pinned)))
134+
{
135+
NativeApiStatus.VerifySuccess(
136+
NativeMethods.OrtAddRunConfigEntry(handle,
137+
pinnedConfigKeyName.Pointer, pinnedConfigValueName.Pointer));
138+
}
139+
}
122140

123141
#region SafeHandle
124142
/// <summary>

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

+5
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ public void TestRunOptions()
189189

190190
opt.LogId = "MyLogTag";
191191
Assert.Equal("MyLogTag", opt.LogId);
192+
193+
opt.AddRunConfigEntry("key", "value");
194+
195+
var ex = Assert.Throws<OnnxRuntimeException>(() => { opt.AddRunConfigEntry("", "missing key"); });
196+
Assert.Contains("[ErrorCode:InvalidArgument] Config key is empty", ex.Message);
192197
}
193198
}
194199

0 commit comments

Comments
 (0)