2018-11-20 00:48:22 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-12-18 08:30:27 +00:00
|
|
|
using System.Text;
|
2018-11-23 04:56:43 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2019-08-14 19:02:02 +00:00
|
|
|
using System.IO;
|
2018-11-23 04:56:43 +00:00
|
|
|
|
2018-11-20 00:48:22 +00:00
|
|
|
namespace Microsoft.ML.OnnxRuntime
|
|
|
|
|
{
|
2019-08-15 00:12:08 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// TODO Add documentation about which optimizations are enabled for each value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum GraphOptimizationLevel
|
|
|
|
|
{
|
|
|
|
|
ORT_DISABLE_ALL = 0,
|
|
|
|
|
ORT_ENABLE_BASIC = 1,
|
|
|
|
|
ORT_ENABLE_EXTENDED = 2,
|
|
|
|
|
ORT_ENABLE_ALL = 99
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-14 16:48:19 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Controls whether you want to execute operators in the graph sequentially or in parallel.
|
|
|
|
|
/// Usually when the model has many branches, setting this option to ExecutionMode.ORT_PARALLEL
|
|
|
|
|
/// will give you better performance.
|
|
|
|
|
/// See [ONNX_Runtime_Perf_Tuning.md] for more details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum ExecutionMode
|
|
|
|
|
{
|
|
|
|
|
ORT_SEQUENTIAL = 0,
|
|
|
|
|
ORT_PARALLEL = 1,
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 06:46:21 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Holds the options for creating an InferenceSession
|
|
|
|
|
/// </summary>
|
2020-08-27 16:17:42 +00:00
|
|
|
public class SessionOptions : SafeHandle
|
2018-11-20 00:48:22 +00:00
|
|
|
{
|
2020-02-27 22:43:16 +00:00
|
|
|
private static string[] cudaDelayLoadedLibs = { "cublas64_10.dll", "cudnn64_7.dll", "curand64_10.dll" };
|
2018-11-20 00:48:22 +00:00
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#region Constructor and Factory methods
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2018-11-26 06:46:21 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs an empty SessionOptions
|
|
|
|
|
/// </summary>
|
2018-11-20 00:48:22 +00:00
|
|
|
public SessionOptions()
|
2020-08-27 16:17:42 +00:00
|
|
|
:base(IntPtr.Zero, true)
|
2018-11-20 00:48:22 +00:00
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSessionOptions(out handle));
|
2018-11-20 00:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 00:23:14 +00:00
|
|
|
/// <summary>
|
2020-02-29 05:46:57 +00:00
|
|
|
/// A helper method to construct a SessionOptions object for CUDA execution.
|
|
|
|
|
/// Use only if CUDA is installed and you have the onnxruntime package specific to this Execution Provider.
|
2019-04-03 00:23:14 +00:00
|
|
|
/// </summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
/// <returns>A SessionsOptions() object configured for execution on deviceId=0</returns>
|
|
|
|
|
public static SessionOptions MakeSessionOptionWithCudaProvider()
|
2019-04-03 00:23:14 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
return MakeSessionOptionWithCudaProvider(0);
|
2019-04-03 00:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 01:43:40 +00:00
|
|
|
/// <summary>
|
2020-02-29 05:46:57 +00:00
|
|
|
/// A helper method to construct a SessionOptions object for CUDA execution.
|
|
|
|
|
/// Use only if CUDA is installed and you have the onnxruntime package specific to this Execution Provider.
|
2019-08-13 01:43:40 +00:00
|
|
|
/// </summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
/// <param name="deviceId"></param>
|
|
|
|
|
/// <returns>A SessionsOptions() object configured for execution on deviceId</returns>
|
|
|
|
|
public static SessionOptions MakeSessionOptionWithCudaProvider(int deviceId = 0)
|
2019-08-13 01:43:40 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
CheckCudaExecutionProviderDLLs();
|
|
|
|
|
SessionOptions options = new SessionOptions();
|
2020-08-29 22:18:50 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options.Handle, deviceId));
|
|
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options.Handle, 1));
|
2019-08-14 19:02:02 +00:00
|
|
|
return options;
|
|
|
|
|
}
|
2020-02-28 02:00:17 +00:00
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A helper method to construct a SessionOptions object for Nuphar execution.
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="settings">settings string, comprises of comma separated key:value pairs. default is empty</param>
|
|
|
|
|
/// <returns>A SessionsOptions() object configured for execution with Nuphar</returns>
|
|
|
|
|
public static SessionOptions MakeSessionOptionWithNupharProvider(String settings = "")
|
|
|
|
|
{
|
|
|
|
|
SessionOptions options = new SessionOptions();
|
2020-08-29 22:18:50 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Nuphar(options.Handle, 1, settings));
|
2020-02-29 05:46:57 +00:00
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#endregion
|
2019-09-09 18:17:44 +00:00
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#region ExecutionProviderAppends
|
2019-09-09 18:17:44 +00:00
|
|
|
public void AppendExecutionProvider_CPU(int useArena)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(handle, useArena));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
2019-12-03 15:34:23 +00:00
|
|
|
public void AppendExecutionProvider_Dnnl(int useArena)
|
2019-09-09 18:17:44 +00:00
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Dnnl(handle, useArena));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
2019-09-09 18:17:44 +00:00
|
|
|
public void AppendExecutionProvider_CUDA(int deviceId)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(handle, deviceId));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-29 22:18:50 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void AppendExecutionProvider_Dml(int deviceId)
|
|
|
|
|
{
|
|
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Dml(handle, deviceId));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
2019-09-09 18:17:44 +00:00
|
|
|
public void AppendExecutionProvider_NGraph(string nGraphBackendType)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_NGraph(handle, nGraphBackendType));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
2020-04-24 11:06:02 +00:00
|
|
|
public void AppendExecutionProvider_OpenVINO(string deviceId = "")
|
2019-09-09 18:17:44 +00:00
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_OpenVINO(handle, deviceId));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
2019-09-09 18:17:44 +00:00
|
|
|
public void AppendExecutionProvider_Tensorrt(int deviceId)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Tensorrt(handle, deviceId));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-26 20:24:59 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void AppendExecutionProvider_MIGraphX(int deviceId)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_MIGraphX(handle, deviceId));
|
2020-05-26 20:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-29 05:46:57 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
|
|
|
|
/// </summary>
|
2019-09-09 18:17:44 +00:00
|
|
|
public void AppendExecutionProvider_Nnapi()
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Nnapi(handle));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-02 06:01:47 +00:00
|
|
|
/// <summary>
|
2020-02-29 05:46:57 +00:00
|
|
|
/// Use only if you have the onnxruntime package specific to this Execution Provider.
|
2019-09-02 06:01:47 +00:00
|
|
|
/// </summary>
|
2019-09-09 18:17:44 +00:00
|
|
|
public void AppendExecutionProvider_Nuphar(string settings = "")
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSessionOptionsAppendExecutionProvider_Nuphar(handle, 1, settings));
|
2019-09-09 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
#endregion //ExecutionProviderAppends
|
2019-10-14 16:48:19 +00:00
|
|
|
|
2019-11-21 23:45:49 +00:00
|
|
|
#region Public Methods
|
|
|
|
|
public void RegisterCustomOpLibrary(string libraryPath)
|
|
|
|
|
{
|
|
|
|
|
IntPtr libraryHandle = IntPtr.Zero;
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtRegisterCustomOpsLibrary(handle, libraryPath, out libraryHandle));
|
2019-11-21 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-05 11:40:38 +00:00
|
|
|
public void AddSessionConfigEntry(string configKey, string configValue)
|
|
|
|
|
{
|
|
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtAddSessionConfigEntry(handle, configKey, configValue));
|
|
|
|
|
}
|
2019-11-21 23:45:49 +00:00
|
|
|
#endregion
|
2019-08-14 19:02:02 +00:00
|
|
|
|
|
|
|
|
internal IntPtr Handle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
return handle;
|
2019-08-14 19:02:02 +00:00
|
|
|
}
|
2019-08-13 01:43:40 +00:00
|
|
|
}
|
2020-08-10 20:33:49 +00:00
|
|
|
#region Public Properties
|
2020-08-27 16:17:42 +00:00
|
|
|
|
|
|
|
|
public override bool IsInvalid { get { return handle == IntPtr.Zero; } }
|
|
|
|
|
|
2019-04-05 19:05:56 +00:00
|
|
|
/// <summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
/// Enables the use of the memory allocation patterns in the first Run() call for subsequent runs. Default = true.
|
2019-04-05 19:05:56 +00:00
|
|
|
/// </summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
public bool EnableMemoryPattern
|
2019-04-05 19:05:56 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _enableMemoryPattern;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!_enableMemoryPattern && value)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtEnableMemPattern(handle));
|
2019-08-14 19:02:02 +00:00
|
|
|
_enableMemoryPattern = true;
|
|
|
|
|
}
|
|
|
|
|
else if (_enableMemoryPattern && !value)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtDisableMemPattern(handle));
|
2019-08-14 19:02:02 +00:00
|
|
|
_enableMemoryPattern = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-05 19:05:56 +00:00
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
private bool _enableMemoryPattern = true;
|
2019-04-05 19:05:56 +00:00
|
|
|
|
2019-06-19 18:17:21 +00:00
|
|
|
/// <summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
/// Path prefix to use for output of profiling data
|
2019-06-19 18:17:21 +00:00
|
|
|
/// </summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
public string ProfileOutputPathPrefix
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
} = "onnxruntime_profile_"; // this is the same default in C++ implementation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enables profiling of InferenceSession.Run() calls. Default is false
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool EnableProfiling
|
2019-06-19 18:17:21 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _enableProfiling;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!_enableProfiling && value)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtEnableProfiling(handle, NativeMethods.GetPlatformSerializedString(ProfileOutputPathPrefix)));
|
2019-08-14 19:02:02 +00:00
|
|
|
_enableProfiling = true;
|
|
|
|
|
}
|
|
|
|
|
else if (_enableProfiling && !value)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtDisableProfiling(handle));
|
2019-08-14 19:02:02 +00:00
|
|
|
_enableProfiling = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-19 18:17:21 +00:00
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
private bool _enableProfiling = false;
|
2019-06-19 18:17:21 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
/// Set filepath to save optimized model after graph level transformations. Default is empty, which implies saving is disabled.
|
2019-06-19 18:17:21 +00:00
|
|
|
/// </summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
public string OptimizedModelFilePath
|
2019-06-19 18:17:21 +00:00
|
|
|
{
|
2019-08-15 00:12:08 +00:00
|
|
|
get
|
2019-08-14 19:02:02 +00:00
|
|
|
{
|
|
|
|
|
return _optimizedModelFilePath;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != _optimizedModelFilePath)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetOptimizedModelFilePath(handle, NativeMethods.GetPlatformSerializedString(value)));
|
2019-08-14 19:02:02 +00:00
|
|
|
_optimizedModelFilePath = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-19 18:17:21 +00:00
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
private string _optimizedModelFilePath = "";
|
2019-06-19 18:17:21 +00:00
|
|
|
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2019-08-15 00:12:08 +00:00
|
|
|
|
2018-11-26 06:46:21 +00:00
|
|
|
/// <summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
/// Enables Arena allocator for the CPU memory allocations. Default is true.
|
2018-11-26 06:46:21 +00:00
|
|
|
/// </summary>
|
2019-08-14 19:02:02 +00:00
|
|
|
public bool EnableCpuMemArena
|
2018-11-20 00:48:22 +00:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
return _enableCpuMemArena;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!_enableCpuMemArena && value)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtEnableCpuMemArena(handle));
|
2019-08-14 19:02:02 +00:00
|
|
|
_enableCpuMemArena = true;
|
2019-08-15 00:12:08 +00:00
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
else if (_enableCpuMemArena && !value)
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtDisableCpuMemArena(handle));
|
2019-08-14 19:02:02 +00:00
|
|
|
_enableCpuMemArena = false;
|
|
|
|
|
}
|
2018-11-20 00:48:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
private bool _enableCpuMemArena = true;
|
|
|
|
|
|
2018-11-20 00:48:22 +00:00
|
|
|
|
2019-08-14 19:02:02 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Log Id to be used for the session. Default is empty string.
|
|
|
|
|
/// TODO: Should it be named LogTag as in RunOptions?
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string LogId
|
2018-11-20 00:48:22 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _logId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetSessionLogId(handle, value));
|
2019-08-14 19:02:02 +00:00
|
|
|
_logId = value;
|
|
|
|
|
}
|
2018-11-20 00:48:22 +00:00
|
|
|
}
|
2019-08-14 19:02:02 +00:00
|
|
|
private string _logId = "";
|
|
|
|
|
|
2020-05-08 21:31:06 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Log Severity Level for the session logs. Default = ORT_LOGGING_LEVEL_WARNING
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OrtLoggingLevel LogSeverityLevel
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _logSeverityLevel;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetSessionLogSeverityLevel(handle, value));
|
2020-05-08 21:31:06 +00:00
|
|
|
_logSeverityLevel = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private OrtLoggingLevel _logSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING;
|
2018-11-20 00:48:22 +00:00
|
|
|
|
2018-12-17 21:18:48 +00:00
|
|
|
/// <summary>
|
2020-05-08 21:31:06 +00:00
|
|
|
/// 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.
|
2018-12-17 21:18:48 +00:00
|
|
|
/// </summary>
|
2020-05-08 21:31:06 +00:00
|
|
|
public int LogVerbosityLevel
|
2018-12-17 21:18:48 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _logVerbosityLevel;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetSessionLogVerbosityLevel(handle, value));
|
2019-08-14 19:02:02 +00:00
|
|
|
_logVerbosityLevel = value;
|
|
|
|
|
}
|
2018-12-17 21:18:48 +00:00
|
|
|
}
|
2020-05-08 21:31:06 +00:00
|
|
|
private int _logVerbosityLevel = 0;
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2018-12-17 21:18:48 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2019-09-19 05:36:23 +00:00
|
|
|
// Sets the number of threads used to parallelize the execution within nodes
|
|
|
|
|
// A value of 0 means ORT will pick a default
|
2018-12-17 21:18:48 +00:00
|
|
|
/// </summary>
|
2019-09-19 05:36:23 +00:00
|
|
|
public int IntraOpNumThreads
|
2018-12-17 21:18:48 +00:00
|
|
|
{
|
2019-08-14 19:02:02 +00:00
|
|
|
get
|
|
|
|
|
{
|
2019-09-19 05:36:23 +00:00
|
|
|
return _intraOpNumThreads;
|
2019-08-14 19:02:02 +00:00
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetIntraOpNumThreads(handle, value));
|
2019-09-19 05:36:23 +00:00
|
|
|
_intraOpNumThreads = value;
|
2019-08-14 19:02:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-19 05:36:23 +00:00
|
|
|
private int _intraOpNumThreads = 0; // set to what is set in C++ SessionOptions by default;
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
/// <summary>
|
|
|
|
|
// Sets the number of threads used to parallelize the execution of the graph (across nodes)
|
|
|
|
|
// If sequential execution is enabled this value is ignored
|
|
|
|
|
// A value of 0 means ORT will pick a default
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int InterOpNumThreads
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _interOpNumThreads;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetInterOpNumThreads(handle, value));
|
2019-09-19 05:36:23 +00:00
|
|
|
_interOpNumThreads = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private int _interOpNumThreads = 0; // set to what is set in C++ SessionOptions by default;
|
2019-08-14 19:02:02 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2020-01-13 22:05:38 +00:00
|
|
|
/// Sets the graph optimization level for the session. Default is set to ORT_ENABLE_ALL.
|
2019-08-14 19:02:02 +00:00
|
|
|
/// </summary>
|
2019-08-15 00:12:08 +00:00
|
|
|
public GraphOptimizationLevel GraphOptimizationLevel
|
2019-08-14 19:02:02 +00:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _graphOptimizationLevel;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetSessionGraphOptimizationLevel(handle, value));
|
2019-08-14 19:02:02 +00:00
|
|
|
_graphOptimizationLevel = value;
|
|
|
|
|
}
|
2018-12-17 21:18:48 +00:00
|
|
|
}
|
2020-01-13 22:05:38 +00:00
|
|
|
private GraphOptimizationLevel _graphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL;
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2019-10-14 16:48:19 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the execution mode for the session. Default is set to ORT_SEQUENTIAL.
|
|
|
|
|
/// See [ONNX_Runtime_Perf_Tuning.md] for more details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ExecutionMode ExecutionMode
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _executionMode;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetSessionExecutionMode(handle, value));
|
2019-10-14 16:48:19 +00:00
|
|
|
_executionMode = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private ExecutionMode _executionMode = ExecutionMode.ORT_SEQUENTIAL;
|
2020-08-07 16:39:19 +00:00
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#endregion
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#region Private Methods
|
2019-08-14 19:02:02 +00:00
|
|
|
|
2018-11-23 04:56:43 +00:00
|
|
|
|
2018-12-18 08:30:27 +00:00
|
|
|
// Declared, but called only if OS = Windows.
|
2018-12-17 21:18:48 +00:00
|
|
|
[DllImport("kernel32.dll")]
|
|
|
|
|
private static extern IntPtr LoadLibrary(string dllToLoad);
|
|
|
|
|
|
2018-12-18 08:30:27 +00:00
|
|
|
[DllImport("kernel32.dll")]
|
|
|
|
|
static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);
|
2018-12-17 21:18:48 +00:00
|
|
|
private static bool CheckCudaExecutionProviderDLLs()
|
|
|
|
|
{
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
foreach (var dll in cudaDelayLoadedLibs)
|
|
|
|
|
{
|
|
|
|
|
IntPtr handle = LoadLibrary(dll);
|
2018-12-18 08:30:27 +00:00
|
|
|
if (handle != IntPtr.Zero)
|
2019-08-15 00:12:08 +00:00
|
|
|
continue;
|
2018-12-18 08:30:27 +00:00
|
|
|
var sysdir = new StringBuilder(String.Empty, 2048);
|
|
|
|
|
GetSystemDirectory(sysdir, (uint)sysdir.Capacity);
|
|
|
|
|
throw new OnnxRuntimeException(
|
2019-08-15 00:12:08 +00:00
|
|
|
ErrorCode.NoSuchFile,
|
2019-03-06 01:20:51 +00:00
|
|
|
$"kernel32.LoadLibrary():'{dll}' not found. CUDA is required for GPU execution. " +
|
2018-12-18 08:30:27 +00:00
|
|
|
$". Verify it is available in the system directory={sysdir}. Else copy it to the output folder."
|
2019-08-15 00:12:08 +00:00
|
|
|
);
|
2018-12-17 21:18:48 +00:00
|
|
|
}
|
2019-08-15 00:12:08 +00:00
|
|
|
}
|
2018-12-17 21:18:48 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2018-11-26 06:46:21 +00:00
|
|
|
|
2019-02-21 19:34:44 +00:00
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#endregion
|
2020-08-27 16:17:42 +00:00
|
|
|
#region SafeHandle
|
2018-11-26 06:46:21 +00:00
|
|
|
|
2020-08-27 16:17:42 +00:00
|
|
|
protected override bool ReleaseHandle()
|
2018-11-26 06:46:21 +00:00
|
|
|
{
|
2020-08-27 16:17:42 +00:00
|
|
|
NativeMethods.OrtReleaseSessionOptions(handle);
|
|
|
|
|
handle = IntPtr.Zero;
|
|
|
|
|
return true;
|
2018-11-26 06:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-19 05:36:23 +00:00
|
|
|
#endregion
|
2018-11-20 00:48:22 +00:00
|
|
|
}
|
|
|
|
|
}
|