diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs index 67de9a3630..57ae4c2905 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs @@ -3,6 +3,7 @@ using System; using System.Runtime.InteropServices; +using static Microsoft.ML.OnnxRuntime.NativeMethods; namespace Microsoft.ML.OnnxRuntime { @@ -269,12 +270,18 @@ namespace Microsoft.ML.OnnxRuntime [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate ref OrtApi DOrtGetApi(UInt32 version); + [UnmanagedFunctionPointer(CallingConvention.Winapi)] + public delegate IntPtr DOrtGetVersionString(); + + public static DOrtGetVersionString OrtGetVersionString; + static NativeMethods() { DOrtGetApi OrtGetApi = (DOrtGetApi)Marshal.GetDelegateForFunctionPointer(OrtGetApiBase().GetApi, typeof(DOrtGetApi)); // TODO: Make this save the pointer, and not copy the whole structure across api_ = (OrtApi)OrtGetApi(4 /*ORT_API_VERSION*/); + OrtGetVersionString = (DOrtGetVersionString)Marshal.GetDelegateForFunctionPointer(OrtGetApiBase().GetVersionString, typeof(DOrtGetVersionString)); OrtCreateEnv = (DOrtCreateEnv)Marshal.GetDelegateForFunctionPointer(api_.CreateEnv, typeof(DOrtCreateEnv)); OrtReleaseEnv = (DOrtReleaseEnv)Marshal.GetDelegateForFunctionPointer(api_.ReleaseEnv, typeof(DOrtReleaseEnv)); diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs index 7cab9e89b6..0f70013b0e 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/OnnxRuntime.shared.cs @@ -36,7 +36,7 @@ namespace Microsoft.ML.OnnxRuntime public enum OrtLanguageProjection { ORT_PROJECTION_C = 0, - ORT_PROJECTION_CPLUSPLUS = 1 , + ORT_PROJECTION_CPLUSPLUS = 1, ORT_PROJECTION_CSHARP = 2, ORT_PROJECTION_PYTHON = 3, ORT_PROJECTION_JAVA = 4, @@ -52,7 +52,7 @@ namespace Microsoft.ML.OnnxRuntime /// public sealed class OrtEnv : SafeHandle { - private static readonly Lazy _instance = new Lazy(()=> new OrtEnv()); + private static readonly Lazy _instance = new Lazy(() => new OrtEnv()); private static LogLevel envLogLevel = LogLevel.Warning; #region private methods @@ -124,6 +124,16 @@ namespace Microsoft.ML.OnnxRuntime NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateAndRegisterAllocator(Handle, memInfo.Pointer, arenaCfg.Pointer)); } + /// + /// This function returns the onnxruntime version string + /// + /// version string + public string GetVersionString() + { + IntPtr versionString = NativeMethods.OrtGetVersionString(); + return NativeOnnxValueHelper.StringFromNativeUtf8(versionString); + } + /// /// Queries all the execution providers supported in the native onnxruntime shared library /// @@ -137,7 +147,7 @@ namespace Microsoft.ML.OnnxRuntime try { var availableProviders = new string[numProviders]; - for (int i=0; i::api_ = OrtGetApiBase()->GetApi(ORT_API_VERSION); /// This returns a reference to the OrtApi interface in use inline const OrtApi& GetApi() noexcept { return *Global::api_; } +/// +/// This function returns the onnxruntime version string +/// +/// version string major.minor.rev +std::string GetVersionString(); + /// /// This is a C++ wrapper for OrtApi::GetAvailableProviders() and /// returns a vector of strings representing the available execution providers. diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h index 255e4729a3..b2b400fa9b 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h @@ -1848,6 +1848,11 @@ inline void CustomOpApi::ReleaseKernelInfo(_Frees_ptr_opt_ OrtKernelInfo* info_c api_.ReleaseKernelInfo(info_copy); } +inline std::string GetVersionString() { + std::string result = OrtGetApiBase()->GetVersionString(); + return result; +} + inline std::vector GetAvailableProviders() { char** providers; int len; diff --git a/onnxruntime/__init__.py b/onnxruntime/__init__.py index 5e28b60faa..5e8b04cdc8 100644 --- a/onnxruntime/__init__.py +++ b/onnxruntime/__init__.py @@ -40,6 +40,7 @@ try: get_all_providers, get_available_providers, get_device, + get_version_string, set_default_logger_severity, set_default_logger_verbosity, set_seed, diff --git a/onnxruntime/python/onnxruntime_pybind_module.cc b/onnxruntime/python/onnxruntime_pybind_module.cc index 28434e0404..c224bec218 100644 --- a/onnxruntime/python/onnxruntime_pybind_module.cc +++ b/onnxruntime/python/onnxruntime_pybind_module.cc @@ -4,6 +4,7 @@ #include "onnxruntime_pybind.h" // must use this for the include of #include #include "core/providers/get_execution_providers.h" +#include "onnxruntime_config.h" namespace onnxruntime { namespace python { @@ -19,6 +20,8 @@ PYBIND11_MODULE(onnxruntime_pybind11_state, m) { "Return list of available Execution Providers in this installed version of Onnxruntime. " "The order of elements represents the default priority order of Execution Providers " "from highest to lowest."); + + m.def("get_version_string", []() -> std::string { return ORT_VERSION; }); } } // namespace python } // namespace onnxruntime diff --git a/onnxruntime/test/python/onnxruntime_test_python.py b/onnxruntime/test/python/onnxruntime_test_python.py index 8232044a29..9e4d1f6a80 100644 --- a/onnxruntime/test/python/onnxruntime_test_python.py +++ b/onnxruntime/test/python/onnxruntime_test_python.py @@ -59,6 +59,9 @@ class TestInferenceSession(unittest.TestCase): self.assertTrue(tvm is not None) + def testGetVersionString(self): + self.assertIsNot(onnxrt.get_version_string(), None) + def testModelSerialization(self): try: so = onnxrt.SessionOptions() diff --git a/onnxruntime/test/shared_lib/test_inference.cc b/onnxruntime/test/shared_lib/test_inference.cc index b984c90164..d61114ee5a 100644 --- a/onnxruntime/test/shared_lib/test_inference.cc +++ b/onnxruntime/test/shared_lib/test_inference.cc @@ -21,6 +21,8 @@ #include "core/session/onnxruntime_session_options_config_keys.h" #include "core/session/onnxruntime_run_options_config_keys.h" #include "core/util/thread_utils.h" + +#include "onnxruntime_config.h" #include "providers.h" #include "test_allocator.h" #include "test_fixture.h" @@ -2122,6 +2124,13 @@ TEST(CApiTest, get_available_providers_cpp) { ASSERT_TRUE(std::find(providers.begin(), providers.end(), "CUDAExecutionProvider") != providers.end()); #endif } + +TEST(CApiTest, get_version_string_cpp) { + std::string version_string = Ort::GetVersionString(); + ASSERT_FALSE(version_string.empty()); + ASSERT_EQ(version_string, ORT_VERSION); +} + TEST(CApiTest, TestSharedAllocators) { OrtEnv* env_ptr = (OrtEnv*)(*ort_env); diff --git a/orttraining/orttraining/python/orttraining_python_module.cc b/orttraining/orttraining/python/orttraining_python_module.cc index 0049932826..f2dbb27072 100644 --- a/orttraining/orttraining/python/orttraining_python_module.cc +++ b/orttraining/orttraining/python/orttraining_python_module.cc @@ -9,6 +9,7 @@ #include "core/common/path_string.h" #include "core/providers/get_execution_providers.h" #include "core/session/provider_bridge_ort.h" +#include "onnxruntime_config.h" #ifdef ENABLE_EAGER_MODE #include "orttraining/python/orttraining_python_module_eager.h" @@ -354,6 +355,8 @@ PYBIND11_MODULE(onnxruntime_pybind11_state, m) { "The order of elements represents the default priority order of Execution Providers " "from highest to lowest."); + m.def("get_version_string", []() -> std::string { return ORT_VERSION; }); + m.def("clear_training_ep_instances", []() -> void { ort_training_env->ClearExecutionProviderInstances(); },