Add GetVersionSting API for C++, C# and Python (#14873)

### Description
Added APIs.

### Motivation and Context
Addresses https://github.com/microsoft/onnxruntime/issues/14584

Cc: @Craigacp cp
This commit is contained in:
Dmitri Smirnov 2023-03-02 17:11:07 -08:00 committed by GitHub
parent 6e2ca15140
commit 8d87fdcfa1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 58 additions and 3 deletions

View file

@ -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));

View file

@ -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
/// </summary>
public sealed class OrtEnv : SafeHandle
{
private static readonly Lazy<OrtEnv> _instance = new Lazy<OrtEnv>(()=> new OrtEnv());
private static readonly Lazy<OrtEnv> _instance = new Lazy<OrtEnv>(() => 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));
}
/// <summary>
/// This function returns the onnxruntime version string
/// </summary>
/// <returns>version string</returns>
public string GetVersionString()
{
IntPtr versionString = NativeMethods.OrtGetVersionString();
return NativeOnnxValueHelper.StringFromNativeUtf8(versionString);
}
/// <summary>
/// Queries all the execution providers supported in the native onnxruntime shared library
/// </summary>
@ -137,7 +147,7 @@ namespace Microsoft.ML.OnnxRuntime
try
{
var availableProviders = new string[numProviders];
for (int i=0; i<numProviders; ++i)
for (int i = 0; i < numProviders; ++i)
{
availableProviders[i] = NativeOnnxValueHelper.StringFromNativeUtf8(Marshal.ReadIntPtr(availableProvidersHandle, IntPtr.Size * i));
}

View file

@ -209,6 +209,14 @@ namespace Microsoft.ML.OnnxRuntime.Tests
ortEnvInstance.EnableTelemetryEvents();
}
[Fact(DisplayName = "GetVersionString")]
public void GetVersionString()
{
var ortEnvInstance = OrtEnv.Instance();
string versionString = ortEnvInstance.GetVersionString();
Assert.False(versionString.Length == 0);
}
[Fact(DisplayName = "GetAvailableProviders")]
public void GetAvailableProviders()
{

View file

@ -120,6 +120,12 @@ const OrtApi* Global<T>::api_ = OrtGetApiBase()->GetApi(ORT_API_VERSION);
/// This returns a reference to the OrtApi interface in use
inline const OrtApi& GetApi() noexcept { return *Global<void>::api_; }
/// <summary>
/// This function returns the onnxruntime version string
/// </summary>
/// <returns>version string major.minor.rev</returns>
std::string GetVersionString();
/// <summary>
/// This is a C++ wrapper for OrtApi::GetAvailableProviders() and
/// returns a vector of strings representing the available execution providers.

View file

@ -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<std::string> GetAvailableProviders() {
char** providers;
int len;

View file

@ -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,

View file

@ -4,6 +4,7 @@
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
#include <pybind11/stl.h>
#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

View file

@ -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()

View file

@ -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);

View file

@ -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();
},