// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.ML.OnnxRuntime
{
///
/// Sets various runtime options.
///
public class RunOptions : SafeHandle
{
internal IntPtr Handle
{
get
{
return handle;
}
}
///
/// Default __ctor. Creates default RuntimeOptions
///
public RunOptions()
: base(IntPtr.Zero, true)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateRunOptions(out handle));
}
///
/// Overrides SafeHandle.IsInvalid
///
/// returns true if handle is equal to Zero
public override bool IsInvalid { get { return handle == IntPtr.Zero; } }
///
/// Log Severity Level for the session logs. Default = ORT_LOGGING_LEVEL_WARNING
///
public OrtLoggingLevel LogSeverityLevel
{
get
{
return _logSeverityLevel;
}
set
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtRunOptionsSetRunLogSeverityLevel(handle, value));
_logSeverityLevel = value;
}
}
private OrtLoggingLevel _logSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING;
///
/// Log Verbosity Level for the session logs. Default = 0. Valid values are >=0.
/// This takes into effect only when the LogSeverityLevel is set to ORT_LOGGING_LEVEL_VERBOSE.
///
public int LogVerbosityLevel
{
get
{
return _logVerbosityLevel;
}
set
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtRunOptionsSetRunLogVerbosityLevel(handle, value));
_logVerbosityLevel = value;
}
}
private int _logVerbosityLevel = 0;
///
/// Log tag to be used during the run. default = ""
///
public string LogId
{
get
{
return _logId;
}
set
{
var utf8 = NativeOnnxValueHelper.StringToZeroTerminatedUtf8(value);
NativeApiStatus.VerifySuccess(NativeMethods.OrtRunOptionsSetRunTag(handle, utf8));
_logId = value;
}
}
private string _logId = "";
///
/// Sets a flag to terminate all Run() calls that are currently using this RunOptions object
/// Default = false
///
/// terminate flag value
public bool Terminate
{
get
{
return _terminate;
}
set
{
if (!_terminate && value)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtRunOptionsSetTerminate(handle));
_terminate = true;
}
else if (_terminate && !value)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtRunOptionsUnsetTerminate(handle));
_terminate = false;
}
}
}
private bool _terminate = false; //value set to default value of the C++ RunOptions
///
/// Set a single run configuration entry as a pair of strings
/// If a configuration with same key exists, this will overwrite the configuration with the given configValue.
///
/// config key name
/// config key value
public void AddRunConfigEntry(string configKey, string configValue)
{
var utf8Key = NativeOnnxValueHelper.StringToZeroTerminatedUtf8(configKey);
var utf8Value = NativeOnnxValueHelper.StringToZeroTerminatedUtf8(configValue);
NativeApiStatus.VerifySuccess(NativeMethods.OrtAddRunConfigEntry(handle, utf8Key, utf8Value));
}
///
/// Appends the specified lora adapter to the list of active lora adapters
/// for this RunOptions instance. All run calls with this instant will
/// make use of the activated Lora Adapters. An adapter is considered active
/// if it is added to RunOptions that are used during Run() calls.
///
/// Lora adapter instance
public void AddActiveLoraAdapter(OrtLoraAdapter loraAdapter)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtRunOptionsAddActiveLoraAdapter(handle, loraAdapter.Handle));
}
#region SafeHandle
///
/// Overrides SafeHandle.ReleaseHandle() to properly dispose of
/// the native instance of RunOptions
///
/// always returns true
protected override bool ReleaseHandle()
{
NativeMethods.OrtReleaseRunOptions(handle);
handle = IntPtr.Zero;
return true;
}
#endregion
}
}