Skip to content

Commit

Permalink
Adust GetVersionString() GetBuildInfoString() signatures and move the…
Browse files Browse the repository at this point in the history
…m to OrtApi (#15921)

### Description

This PR partially reverts changes introduced in
#15643

We make two API return std::string always in UTF-8.

We also move the entry points from OrtApiBase to OrtApi to make them
versioned.

### Motivation and Context

`GetVersionString` always returns x.y.z numbers that are not subject to
internationalization.
`GetBuildInfoString` can hold international chars, but UTF-8 should be
fine to contain those.
We prefix them with u8"" in case the compiler default charset is not
UTF-8.
Furthermore, creating platform dependent APIs is discouraged.
`ORTCHAR_T` is platform dependent and was created for paths only.
On non-unix platforms would still produce `std::string` that can only
contain UTF-8

The API was introduced after the latest release, and can still be
adjusted.
  • Loading branch information
yuslepukhin committed May 13, 2023
1 parent 9fe6d58 commit 896a963
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 43 deletions.
4 changes: 2 additions & 2 deletions cmake/onnxruntime_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
#cmakedefine HAS_FORMAT_TRUNCATION
#cmakedefine HAS_BITWISE_INSTEAD_OF_LOGICAL
#cmakedefine HAS_REALLOCARRAY
#cmakedefine ORT_VERSION "@ORT_VERSION@"
#cmakedefine ORT_BUILD_INFO "@ORT_BUILD_INFO@"
#cmakedefine ORT_VERSION u8"@ORT_VERSION@"
#cmakedefine ORT_BUILD_INFO u8"@ORT_BUILD_INFO@"
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public struct OrtApi
public IntPtr CastTypeInfoToOptionalTypeInfo;
public IntPtr GetOptionalContainedTypeInfo;
public IntPtr GetResizedStringTensorElementBuffer;
public IntPtr KernelContext_GetAllocator;
}

internal static class NativeMethods
Expand Down Expand Up @@ -1654,7 +1655,6 @@ internal class NativeLib

public static DOrtModelMetadataLookupCustomMetadataMap OrtModelMetadataLookupCustomMetadataMap;


/// <summary>
/// Frees ModelMetadata instance
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Microsoft.ML.OnnxRuntime/OrtEnv.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public static OrtEnv Instance()
/// <param name="options"></param>
/// <returns></returns>
/// <exception cref="OnnxRuntimeException">if the singleton has already been created</exception>
public static OrtEnv CreateInstanceWithOptions(EnvironmentCreationOptions options)
public static OrtEnv CreateInstanceWithOptions(ref EnvironmentCreationOptions options)
{
// Non-thread safe, best effort hopefully helpful check.
// Environment is usually created once per process, so this should be fine.
Expand Down
10 changes: 5 additions & 5 deletions csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/OrtEnvTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void TestUpdatingEnvWithCustomLogLevel()
logLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_FATAL
};

ortEnvInstance = OrtEnv.CreateInstanceWithOptions(envOptions);
ortEnvInstance = OrtEnv.CreateInstanceWithOptions(ref envOptions);
Assert.True(OrtEnv.IsCreated);
Assert.Equal(OrtLoggingLevel.ORT_LOGGING_LEVEL_FATAL, ortEnvInstance.EnvLogLevel);

Expand All @@ -92,7 +92,7 @@ public void TestUpdatingEnvWithCustomLogLevel()
logId = "CSharpOnnxRuntimeTestLogid"
};

ortEnvInstance = OrtEnv.CreateInstanceWithOptions(envOptions);
ortEnvInstance = OrtEnv.CreateInstanceWithOptions(ref envOptions);
Assert.Equal(OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING, ortEnvInstance.EnvLogLevel);

// Change and see if this takes effect
Expand All @@ -118,7 +118,7 @@ public void TestUpdatingEnvWithThreadingOptions()
};

// Make sure we start anew
var env = OrtEnv.CreateInstanceWithOptions(envOptions);
var env = OrtEnv.CreateInstanceWithOptions(ref envOptions);
Assert.True(OrtEnv.IsCreated);
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public void TesEnvWithCustomLogger()

LoggingInvokes = 0;

var env = OrtEnv.CreateInstanceWithOptions(envOptions);
var env = OrtEnv.CreateInstanceWithOptions(ref envOptions);
Assert.True(OrtEnv.IsCreated);

var model = TestDataLoader.LoadModelFromEmbeddedResource("squeezenet.onnx");
Expand Down Expand Up @@ -199,7 +199,7 @@ public void TestEnvWithCustomLoggerAndThredingOptions()

LoggingInvokes = 0;

var env = OrtEnv.CreateInstanceWithOptions(envOptions);
var env = OrtEnv.CreateInstanceWithOptions(ref envOptions);
Assert.True(OrtEnv.IsCreated);

var model = TestDataLoader.LoadModelFromEmbeddedResource("squeezenet.onnx");
Expand Down
22 changes: 20 additions & 2 deletions include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ extern "C" {
#define ORTCHAR_T char
#endif

/// ORTCHAR_T, ORT_TSTR are reserved specifically for path handling.
/// All other strings are UTF-8 encoded, use char and std::string
#ifndef ORT_TSTR
#ifdef _WIN32
#define ORT_TSTR(X) L##X
Expand Down Expand Up @@ -627,11 +629,19 @@ struct OrtApiBase {
* \param[in] version Must be ::ORT_API_VERSION
* \return The ::OrtApi for the version requested, nullptr will be returned if this version is unsupported, for example when using a runtime
* older than the version created with this header file.
*
* One can call GetVersionString() to get the version of the Onnxruntime library for logging
* and error reporting purposes.
*/
const OrtApi*(ORT_API_CALL* GetApi)(uint32_t version)NO_EXCEPTION;
const ORTCHAR_T*(ORT_API_CALL* GetVersionString)(void)NO_EXCEPTION; ///< Returns a null terminated string of the version of the Onnxruntime library (eg: "1.8.1")
const ORTCHAR_T*(ORT_API_CALL* GetBuildInfoString)(void)NO_EXCEPTION; ///< Returns a null terminated string of the build info including git info and cxx flags

/** \brief Returns a null terminated string of the version of the Onnxruntime library (eg: "1.8.1")
*
* \return UTF-8 encoded version string. Do not deallocate the returned buffer.
*/
const char*(ORT_API_CALL* GetVersionString)(void)NO_EXCEPTION;
};

typedef struct OrtApiBase OrtApiBase;

/** \brief The Onnxruntime library's entry point to access the C API
Expand Down Expand Up @@ -4193,6 +4203,14 @@ struct OrtApi {
* \since Version 1.15.
*/
ORT_API2_STATUS(KernelContext_GetAllocator, _In_ const OrtKernelContext* context, _In_ const OrtMemoryInfo* mem_info, _Outptr_ OrtAllocator** out);

/** \brief Returns a null terminated string of the build info including git info and cxx flags
*
* \return UTF-8 encoded version string. Do not deallocate the returned buffer.
*
* \since Version 1.15.
*/
const char*(ORT_API_CALL* GetBuildInfoString)(void);
};

/*
Expand Down
4 changes: 2 additions & 2 deletions include/onnxruntime/core/session/onnxruntime_cxx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ inline const OrtApi& GetApi() noexcept { return *Global<void>::api_; }
/// This function returns the onnxruntime version string
/// </summary>
/// <returns>version string major.minor.rev</returns>
std::basic_string<ORTCHAR_T> GetVersionString();
std::string GetVersionString();

/// <summary>
/// This function returns the onnxruntime build information: including git branch,
/// git commit id, build type(Debug/Release/RelWithDebInfo) and cmake cpp flags.
/// </summary>
/// <returns>string</returns>
std::basic_string<ORTCHAR_T> GetBuildInfoString();
std::string GetBuildInfoString();

/// <summary>
/// This is a C++ wrapper for OrtApi::GetAvailableProviders() and
Expand Down
10 changes: 4 additions & 6 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -1987,14 +1987,12 @@ inline void CustomOpApi::ReleaseKernelInfo(_Frees_ptr_opt_ OrtKernelInfo* info_c
api_.ReleaseKernelInfo(info_copy);
}

inline std::basic_string<ORTCHAR_T> GetVersionString() {
std::basic_string<ORTCHAR_T> result = OrtGetApiBase()->GetVersionString();
return result;
inline std::string GetVersionString() {
return OrtGetApiBase()->GetVersionString();
}

inline std::basic_string<ORTCHAR_T> GetBuildInfoString() {
std::basic_string<ORTCHAR_T> result = OrtGetApiBase()->GetBuildInfoString();
return result;
inline std::string GetBuildInfoString() {
return GetApi().GetBuildInfoString();
}

inline std::vector<std::string> GetAvailableProviders() {
Expand Down
10 changes: 2 additions & 8 deletions java/src/main/native/ai_onnxruntime_OnnxRuntime.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxRuntime_getAvailableProvi
JNIEXPORT jstring JNICALL Java_ai_onnxruntime_OnnxRuntime_initialiseVersion
(JNIEnv * jniEnv, jclass clazz) {
(void)clazz; // required JNI parameter not needed by functions which don't access their host class.
const ORTCHAR_T* version = OrtGetApiBase()->GetVersionString();
const char* version = ORT_VERSION;
assert(version != NULL);
#ifdef _WIN32
jsize len = (jsize)(wcslen(version));
jstring versionStr = (*jniEnv)->NewString(jniEnv, (const jchar*)version, len);
#else
jstring versionStr = (*jniEnv)->NewStringUTF(jniEnv, version);
#endif
return versionStr;
return (*jniEnv)->NewStringUTF(jniEnv, version);
}
23 changes: 12 additions & 11 deletions onnxruntime/core/session/onnxruntime_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2380,9 +2380,7 @@ ORT_API(const OrtTrainingApi*, OrtApis::GetTrainingApi, uint32_t version) {

static constexpr OrtApiBase ort_api_base = {
&OrtApis::GetApi,
&OrtApis::GetVersionString,
&OrtApis::GetBuildInfoString,
};
&OrtApis::GetVersionString};

/* Rules on how to add a new Ort API version
Expand Down Expand Up @@ -2726,7 +2724,8 @@ static constexpr OrtApi ort_api_1_to_16 = {
&OrtApis::CastTypeInfoToOptionalTypeInfo,
&OrtApis::GetOptionalContainedTypeInfo,
&OrtApis::GetResizedStringTensorElementBuffer,
&OrtApis::KernelContext_GetAllocator};
&OrtApis::KernelContext_GetAllocator,
&OrtApis::GetBuildInfoString};
// End of Version 15 - DO NOT MODIFY ABOVE (see above text for more information)

// Asserts to do a some checks to ensure older Versions of the OrtApi never change (will detect an addition or deletion but not if they cancel out each other)
Expand All @@ -2747,7 +2746,7 @@ static_assert(offsetof(OrtApi, SessionOptionsAppendExecutionProvider_MIGraphX) /
static_assert(offsetof(OrtApi, ReleaseKernelInfo) / sizeof(void*) == 218, "Size of version 12 API cannot change");
static_assert(offsetof(OrtApi, ReleaseCANNProviderOptions) / sizeof(void*) == 224, "Size of version 13 API cannot change");
static_assert(offsetof(OrtApi, GetSessionConfigEntry) / sizeof(void*) == 238, "Size of version 14 API cannot change");
static_assert(offsetof(OrtApi, KernelContext_GetAllocator) / sizeof(void*) == 253, "Size of version 15 API cannot change");
static_assert(offsetof(OrtApi, GetBuildInfoString) / sizeof(void*) == 254, "Size of version 15 API cannot change");

// So that nobody forgets to finish an API version, this check will serve as a reminder:
static_assert(std::string_view(ORT_VERSION) == "1.16.0",
Expand All @@ -2761,18 +2760,20 @@ ORT_API(const OrtApi*, OrtApis::GetApi, uint32_t version) {
if (version >= 1 && version <= ORT_API_VERSION)
return &ort_api_1_to_16;

fprintf(stderr, "The given version [%u] is not supported, only version 1 to %u is supported in this build.\n",
version, ORT_API_VERSION);
fprintf(stderr,
"The requested API version [%u] is not available, only API versions [1, %u] are supported in this build."
" Current ORT Version is: %s\n",
version, ORT_API_VERSION, ORT_VERSION);

return nullptr; // Unsupported version
}

ORT_API(const ORTCHAR_T*, OrtApis::GetVersionString) {
return ORT_TSTR_ON_MACRO(ORT_VERSION);
ORT_API(const char*, OrtApis::GetVersionString) {
return ORT_VERSION;
}

ORT_API(const ORTCHAR_T*, OrtApis::GetBuildInfoString) {
return ORT_TSTR_ON_MACRO(ORT_BUILD_INFO);
ORT_API(const char*, OrtApis::GetBuildInfoString) {
return ORT_BUILD_INFO;
}

const OrtApiBase* ORT_API_CALL OrtGetApiBase(void) NO_EXCEPTION {
Expand Down
6 changes: 4 additions & 2 deletions onnxruntime/core/session/ort_apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace OrtApis {

ORT_API(const OrtApi*, GetApi, uint32_t version);
ORT_API(const ORTCHAR_T*, GetVersionString);
ORT_API(const ORTCHAR_T*, GetBuildInfoString);

ORT_API(const char*, GetVersionString);

ORT_API(void, ReleaseEnv, OrtEnv*);
ORT_API(void, ReleaseStatus, _Frees_ptr_opt_ OrtStatus*);
Expand Down Expand Up @@ -465,4 +465,6 @@ ORT_API_STATUS_IMPL(GetResizedStringTensorElementBuffer, _Inout_ OrtValue* value
_In_ size_t index, _In_ size_t length_in_bytes, _Inout_ char**);

ORT_API_STATUS_IMPL(KernelContext_GetAllocator, _In_ const OrtKernelContext* context, _In_ const OrtMemoryInfo* mem_info, _Outptr_ OrtAllocator** out);

ORT_API(const char*, GetBuildInfoString);
} // namespace OrtApis
2 changes: 1 addition & 1 deletion onnxruntime/test/onnx/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using namespace onnxruntime;

namespace {
void usage() {
auto version_string = ToUTF8String(OrtGetApiBase()->GetVersionString());
auto version_string = Ort::GetVersionString();
printf(
"onnx_test_runner [options...] <data_root>\n"
"Options:\n"
Expand Down
10 changes: 8 additions & 2 deletions onnxruntime/test/shared_lib/test_inference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2209,9 +2209,15 @@ TEST(CApiTest, get_available_providers_cpp) {
}

TEST(CApiTest, get_version_string_cpp) {
std::basic_string<ORTCHAR_T> version_string = Ort::GetVersionString();
auto version_string = Ort::GetVersionString();
ASSERT_FALSE(version_string.empty());
ASSERT_EQ(version_string, std::basic_string<ORTCHAR_T>(ORT_TSTR_ON_MACRO(ORT_VERSION)));
ASSERT_EQ(version_string, std::string(ORT_VERSION));
}

TEST(CApiTest, get_build_info_string) {
auto build_info_string = Ort::GetBuildInfoString();
ASSERT_FALSE(build_info_string.empty());
ASSERT_EQ(build_info_string, std::string(ORT_BUILD_INFO));
}

TEST(CApiTest, TestSharedAllocators) {
Expand Down

0 comments on commit 896a963

Please sign in to comment.