// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace Microsoft.ML.OnnxRuntime
{
internal struct GlobalOptions //Options are currently not accessible to user
{
public string LogId { get; set; }
public LogLevel LogLevel { get; set; }
}
///
/// Logging level used to specify amount of logging when
/// creating environment. The lower the value is the more logging
/// will be output. A specific value output includes everything
/// that higher values output.
///
public enum LogLevel
{
Verbose = 0, // Everything
Info = 1, // Informational
Warning = 2, // Warnings
Error = 3, // Errors
Fatal = 4 // Results in the termination of the application.
}
///
/// Language projection property for telemetry event for tracking the source usage of ONNXRUNTIME
///
public enum OrtLanguageProjection
{
ORT_PROJECTION_C = 0,
ORT_PROJECTION_CPLUSPLUS = 1 ,
ORT_PROJECTION_CSHARP = 2,
ORT_PROJECTION_PYTHON = 3,
ORT_PROJECTION_JAVA = 4,
ORT_PROJECTION_WINML = 5,
}
///
/// This class initializes the process-global ONNX Runtime environment instance (OrtEnv)
///
public sealed class OrtEnv : SafeHandle
{
private static readonly Lazy _instance = new Lazy(()=> new OrtEnv());
private static LogLevel envLogLevel = LogLevel.Warning;
#region private methods
private OrtEnv() //Problem: it is not possible to pass any option for a Singleton
: base(IntPtr.Zero, true)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateEnv(envLogLevel, @"CSharpOnnxRuntime", out handle));
try
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtSetLanguageProjection(handle, OrtLanguageProjection.ORT_PROJECTION_CSHARP));
}
catch (OnnxRuntimeException)
{
ReleaseHandle();
throw;
}
}
#endregion
#region internal methods
///
/// Returns a handle to the native `OrtEnv` instance held by the singleton C# `OrtEnv` instance
/// Exception caching: May throw an exception on every call, if the `OrtEnv` constructor threw an exception
/// during lazy initialization
///
internal static IntPtr Handle
{
get
{
return _instance.Value.handle;
}
}
#endregion
#region public methods
///
/// Returns an instance of OrtEnv
/// It returns the same instance on every call - `OrtEnv` is singleton
///
/// Returns a singleton instance of OrtEnv that represents native OrtEnv object
public static OrtEnv Instance() { return _instance.Value; }
///
/// Enable platform telemetry collection where applicable
/// (currently only official Windows ORT builds have telemetry collection capabilities)
///
public void EnableTelemetryEvents()
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtEnableTelemetryEvents(Handle));
}
///
/// Disable platform telemetry collection
///
public void DisableTelemetryEvents()
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtDisableTelemetryEvents(Handle));
}
///
/// Create and register an allocator to the OrtEnv instance
/// so as to enable sharing across all sessions using the OrtEnv instance
/// OrtMemoryInfo instance to be used for allocator creation
/// OrtArenaCfg instance that will be used to define the behavior of the arena based allocator
///
public void CreateAndRegisterAllocator(OrtMemoryInfo memInfo, OrtArenaCfg arenaCfg)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateAndRegisterAllocator(Handle, memInfo.Pointer, arenaCfg.Pointer));
}
///
/// Queries all the execution providers supported in the native onnxruntime shared library
///
/// an array of strings that represent execution provider names
public string[] GetAvailableProviders()
{
IntPtr availableProvidersHandle = IntPtr.Zero;
int numProviders;
NativeApiStatus.VerifySuccess(NativeMethods.OrtGetAvailableProviders(out availableProvidersHandle, out numProviders));
var availableProviders = new string[numProviders];
try
{
for(int i=0; i
/// Get/Set log level property of OrtEnv instance
///
/// env log level
public LogLevel EnvLogLevel
{
get { return envLogLevel; }
set
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtUpdateEnvWithCustomLogLevel(Handle, value));
envLogLevel = value;
}
}
#endregion
#region SafeHandle
///
/// Overrides SafeHandle.IsInvalid
///
/// returns true if handle is equal to Zero
public override bool IsInvalid
{
get
{
return (handle == IntPtr.Zero);
}
}
///
/// Overrides SafeHandle.ReleaseHandle() to properly dispose of
/// the native instance of OrtEnv
///
/// always returns true
protected override bool ReleaseHandle()
{
NativeMethods.OrtReleaseEnv(handle);
handle = IntPtr.Zero;
return true;
}
#endregion
}
}