From 8b9065a3967dbf2d8205a18f881d488febc3427a Mon Sep 17 00:00:00 2001 From: yf711 <109183385+yf711@users.noreply.github.com> Date: Fri, 4 Nov 2022 23:46:00 -0500 Subject: [PATCH] Add getter/setter of C# OrtEnv log level (#13402) ### Description * Add getter/setter to access and update C# OrtEnv log level * Add C API about updating ort env with custom log level to support the setter above (Following [pybind implementation](https://github.com/microsoft/onnxruntime/blob/952c99304a764328070ac6c07f510deec06dedf4/onnxruntime/python/onnxruntime_pybind_state.cc#L923-L924)) * Add test case to verify getter & setter ### Motivation and Context * For C++/Python, the log level can be adjusted via OrtEnv, and this feature is missing in C# binding --- .../NativeMethods.shared.cs | 17 +++++++++++++++-- .../OnnxRuntime.shared.cs | 18 +++++++++++++++++- .../InferenceTest.cs | 8 ++++++++ .../core/session/onnxruntime_c_api.h | 8 ++++++++ .../core/session/onnxruntime_cxx_api.h | 4 +++- .../core/session/onnxruntime_cxx_inline.h | 5 +++++ onnxruntime/core/session/onnxruntime_c_api.cc | 14 +++++++++++++- onnxruntime/core/session/ort_apis.h | 1 + 8 files changed, 70 insertions(+), 5 deletions(-) diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs index 350aa380d0..8f7942e6b8 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs @@ -15,6 +15,7 @@ namespace Microsoft.ML.OnnxRuntime // NOTE: The order of the APIs in this struct should match exactly that in // OrtApi ort_api_1_to_ (onnxruntime/core/session/onnxruntime_c_api.cc) + // If syncing your new C API, any other C APIs before yours also need to be synced here if haven't [StructLayout(LayoutKind.Sequential)] public struct OrtApi { @@ -252,6 +253,13 @@ namespace Microsoft.ML.OnnxRuntime public IntPtr ReleaseKernelInfo; public IntPtr GetTrainingApi; + public IntPtr SessionOptionsAppendExecutionProvider_CANN; + public IntPtr CreateCANNProviderOptions; + public IntPtr UpdateCANNProviderOptions; + public IntPtr GetCANNProviderOptionsAsString; + public IntPtr ReleaseCANNProviderOptions; + public IntPtr MemoryInfoGetDeviceType; + public IntPtr UpdateEnvWithCustomLogLevel; } internal static class NativeMethods @@ -427,6 +435,7 @@ namespace Microsoft.ML.OnnxRuntime = (DSessionOptionsAppendExecutionProvider)Marshal.GetDelegateForFunctionPointer( api_.SessionOptionsAppendExecutionProvider, typeof(DSessionOptionsAppendExecutionProvider)); + OrtUpdateEnvWithCustomLogLevel = (DOrtUpdateEnvWithCustomLogLevel)Marshal.GetDelegateForFunctionPointer(api_.UpdateEnvWithCustomLogLevel, typeof(DOrtUpdateEnvWithCustomLogLevel)); } internal class NativeLib @@ -466,9 +475,13 @@ namespace Microsoft.ML.OnnxRuntime public delegate IntPtr /* OrtStatus* */DOrtDisableTelemetryEvents(IntPtr /*(OrtEnv*)*/ env); public static DOrtDisableTelemetryEvents OrtDisableTelemetryEvents; -#endregion Runtime/Environment API + [UnmanagedFunctionPointer(CallingConvention.Winapi)] + public delegate IntPtr /* OrtStatus* */DOrtUpdateEnvWithCustomLogLevel(IntPtr /*(OrtEnv*)*/ env, LogLevel custom_log_level); + public static DOrtUpdateEnvWithCustomLogLevel OrtUpdateEnvWithCustomLogLevel; -#region Provider Options API + #endregion Runtime/Environment API + + #region Provider Options API /// /// Creates native OrtTensorRTProviderOptions instance diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs index 61a3eb680f..4c42710fc7 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs @@ -49,12 +49,13 @@ namespace Microsoft.ML.OnnxRuntime 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(LogLevel.Warning, @"CSharpOnnxRuntime", out handle)); + NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateEnv(envLogLevel, @"CSharpOnnxRuntime", out handle)); try { NativeApiStatus.VerifySuccess(NativeMethods.OrtSetLanguageProjection(handle, OrtLanguageProjection.ORT_PROJECTION_CSHARP)); @@ -150,6 +151,21 @@ namespace Microsoft.ML.OnnxRuntime return availableProviders; } + + + /// + /// 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 diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/InferenceTest.cs index 8704fa4141..5734d90fd7 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/InferenceTest.cs @@ -221,6 +221,14 @@ namespace Microsoft.ML.OnnxRuntime.Tests #endif } + [Fact(DisplayName = "TestUpdatingEnvWithCustomLogLevel")] + public void TestUpdatingEnvWithCustomLogLevel() + { + var ortEnvInstance = OrtEnv.Instance(); + ortEnvInstance.EnvLogLevel = LogLevel.Verbose; + Assert.Equal(LogLevel.Verbose, ortEnvInstance.EnvLogLevel); + } + [Fact(DisplayName = "CanCreateAndDisposeSessionWithModel")] public void CanCreateAndDisposeSessionWithModel() { diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 086aa1e4e8..c5eb8a9cfd 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -3589,6 +3589,14 @@ struct OrtApi { */ void(ORT_API_CALL* MemoryInfoGetDeviceType)(_In_ const OrtMemoryInfo* ptr, _Out_ OrtMemoryInfoDeviceType* out); + /* \brief Update the OrtEnv instance with custom log severity level + * + * \param[in] ort_env The OrtEnv instance being used + * \param[in] log_severity_level The log severity level. + * + * \since Version 1.14. + */ + ORT_API2_STATUS(UpdateEnvWithCustomLogLevel, _In_ OrtEnv* ort_env, OrtLoggingLevel log_severity_level); #ifdef __cplusplus OrtApi(const OrtApi&)=delete; // Prevent users from accidentally copying the API structure, it should always be passed as a pointer diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 9b1aa0527c..c5ee9b28aa 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -366,7 +366,9 @@ struct Env : detail::Base { Env& EnableTelemetryEvents(); ///< Wraps OrtApi::EnableTelemetryEvents Env& DisableTelemetryEvents(); ///< Wraps OrtApi::DisableTelemetryEvents - + + Env& UpdateEnvWithCustomLogLevel(OrtLoggingLevel log_severity_level); ///< Wraps OrtApi::UpdateEnvWithCustomLogLevel + Env& CreateAndRegisterAllocator(const OrtMemoryInfo* mem_info, const OrtArenaCfg* arena_cfg); ///< Wraps OrtApi::CreateAndRegisterAllocator }; diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h index 00b5428f0e..ba90363db2 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h @@ -415,6 +415,11 @@ inline Env& Env::DisableTelemetryEvents() { return *this; } +inline Env& Env::UpdateEnvWithCustomLogLevel(OrtLoggingLevel log_severity_level) { + ThrowOnError(GetApi().UpdateEnvWithCustomLogLevel(p_, log_severity_level)); + return *this; +} + inline Env& Env::CreateAndRegisterAllocator(const OrtMemoryInfo* mem_info, const OrtArenaCfg* arena_cfg) { ThrowOnError(GetApi().CreateAndRegisterAllocator(p_, mem_info, arena_cfg)); return *this; diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index 37e7834c62..db855e0350 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -169,6 +169,16 @@ ORT_API_STATUS_IMPL(OrtApis::DisableTelemetryEvents, _In_ const OrtEnv* ort_env) API_IMPL_END } +ORT_API_STATUS_IMPL(OrtApis::UpdateEnvWithCustomLogLevel, _In_ OrtEnv* ort_env, + OrtLoggingLevel log_severity_level) { + API_IMPL_BEGIN + LoggingManager* default_logging_manager = ort_env->GetLoggingManager(); + int severity_level = static_cast(log_severity_level); + default_logging_manager->SetDefaultLoggerSeverity(static_cast(severity_level)); + return nullptr; + API_IMPL_END +} + ORT_STATUS_PTR CreateTensorImpl(MLDataType ml_type, const int64_t* shape, size_t shape_len, _Inout_ OrtAllocator* allocator, OrtValue& value) { TensorShape tensor_shape(shape, shape_len); @@ -2594,7 +2604,9 @@ static constexpr OrtApi ort_api_1_to_12 = { // End of Version 13 - DO NOT MODIFY ABOVE (see above text for more information) // Start of Version 14 API in progress, safe to modify/rename/rearrange until we ship - &OrtApis::MemoryInfoGetDeviceType}; + &OrtApis::MemoryInfoGetDeviceType, + &OrtApis::UpdateEnvWithCustomLogLevel, +}; diff --git a/onnxruntime/core/session/ort_apis.h b/onnxruntime/core/session/ort_apis.h index a4adc9ce38..01d5b42460 100644 --- a/onnxruntime/core/session/ort_apis.h +++ b/onnxruntime/core/session/ort_apis.h @@ -403,4 +403,5 @@ ORT_API(void, ReleaseCANNProviderOptions, _Frees_ptr_opt_ OrtCANNProviderOptions ORT_API(void, MemoryInfoGetDeviceType, _In_ const OrtMemoryInfo* ptr, _Out_ OrtMemoryInfoDeviceType* out); +ORT_API_STATUS_IMPL(UpdateEnvWithCustomLogLevel, _In_ OrtEnv* ort_env, OrtLoggingLevel log_severity_level); } // namespace OrtApis