From 08dc00b32d89edc3809a87e1c432a1c7579d5cdd Mon Sep 17 00:00:00 2001 From: Adrian Lizarraga Date: Tue, 5 May 2026 15:18:36 -0700 Subject: [PATCH 1/4] Add OrtApi::GetMemPatternEnabled() --- .../onnxruntime/core/session/onnxruntime_c_api.h | 13 +++++++++++++ .../core/session/onnxruntime_cxx_api.h | 1 + .../core/session/onnxruntime_cxx_inline.h | 7 +++++++ onnxruntime/core/session/abi_session_options.cc | 4 ++++ onnxruntime/core/session/onnxruntime_c_api.cc | 2 ++ onnxruntime/core/session/ort_apis.h | 1 + .../test/shared_lib/test_session_options.cc | 15 +++++++++++++++ 7 files changed, 43 insertions(+) diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index ad058a5ba9cfe..0c2542a52175d 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -7436,6 +7436,19 @@ struct OrtApi { _In_ const OrtThreadPoolCallbacksConfig* config); /// @} + + /** \brief Get the current state of the memory pattern optimization setting. + * + * \param[in] options + * \param[out] out Set to 1 if the memory pattern optimization is enabled, 0 otherwise. + * + * \snippet{doc} snippets.dox OrtStatus Return Value + * + * \since Version 1.26. + * + * \see OrtApi::EnableMemPattern, OrtApi::DisableMemPattern + */ + ORT_API2_STATUS(GetMemPatternEnabled, _In_ const OrtSessionOptions* options, _Out_ int* out); }; /* diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 7f0b16e2fdee7..1ea4856b77b03 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -1594,6 +1594,7 @@ struct SessionOptionsImpl : ConstSessionOptionsImpl { SessionOptionsImpl& EnableMemPattern(); ///< Wraps OrtApi::EnableMemPattern SessionOptionsImpl& DisableMemPattern(); ///< Wraps OrtApi::DisableMemPattern + bool GetMemPatternEnabled() const; ///< Wraps OrtApi::GetMemPatternEnabled SessionOptionsImpl& SetExecutionMode(ExecutionMode execution_mode); ///< Wraps OrtApi::SetSessionExecutionMode diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h index cb145c2b6c10a..a59def53f4711 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h @@ -1409,6 +1409,13 @@ inline SessionOptionsImpl& SessionOptionsImpl::DisableMemPattern() { return *this; } +template +inline bool SessionOptionsImpl::GetMemPatternEnabled() const { + int out = 0; + ThrowOnError(GetApi().GetMemPatternEnabled(this->p_, &out)); + return out != 0; +} + template inline SessionOptionsImpl& SessionOptionsImpl::EnableCpuMemArena() { ThrowOnError(GetApi().EnableCpuMemArena(this->p_)); diff --git a/onnxruntime/core/session/abi_session_options.cc b/onnxruntime/core/session/abi_session_options.cc index 3df6d37d63794..46073f62f8bff 100644 --- a/onnxruntime/core/session/abi_session_options.cc +++ b/onnxruntime/core/session/abi_session_options.cc @@ -148,6 +148,10 @@ ORT_API_STATUS_IMPL(OrtApis::DisableMemPattern, _In_ OrtSessionOptions* options) options->value.enable_mem_pattern = false; return nullptr; } +ORT_API_STATUS_IMPL(OrtApis::GetMemPatternEnabled, _In_ const OrtSessionOptions* options, _Out_ int* out) { + *out = options->value.enable_mem_pattern ? 1 : 0; + return nullptr; +} // enable the memory arena on CPU // Arena may pre-allocate memory for future usage. diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index 3f28529e7a847..2e28a900b15ca 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -4894,6 +4894,8 @@ static constexpr OrtApi ort_api_1_to_26 = { &OrtApis::KernelInfoGetAttributeArray_string, &OrtApis::SetPerSessionThreadPoolCallbacks, // End of Version 25 - DO NOT MODIFY ABOVE (see above text for more information) + + &OrtApis::GetMemPatternEnabled, }; // OrtApiBase can never change as there is no way to know what version of OrtApiBase is returned by OrtGetApiBase. diff --git a/onnxruntime/core/session/ort_apis.h b/onnxruntime/core/session/ort_apis.h index 3cc55ee01a3fe..d5a110ad5a23e 100644 --- a/onnxruntime/core/session/ort_apis.h +++ b/onnxruntime/core/session/ort_apis.h @@ -64,6 +64,7 @@ ORT_API_STATUS_IMPL(EnableProfiling, _In_ OrtSessionOptions* options, _In_ const ORT_API_STATUS_IMPL(DisableProfiling, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(EnableMemPattern, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(DisableMemPattern, _In_ OrtSessionOptions* options); +ORT_API_STATUS_IMPL(GetMemPatternEnabled, _In_ const OrtSessionOptions* options, _Out_ int* out); ORT_API_STATUS_IMPL(EnableCpuMemArena, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(DisableCpuMemArena, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(SetSessionLogId, _In_ OrtSessionOptions* options, const char* logid); diff --git a/onnxruntime/test/shared_lib/test_session_options.cc b/onnxruntime/test/shared_lib/test_session_options.cc index ba58344e1e3e2..e400eb0262b0f 100644 --- a/onnxruntime/test/shared_lib/test_session_options.cc +++ b/onnxruntime/test/shared_lib/test_session_options.cc @@ -22,6 +22,21 @@ TEST(CApiTest, session_options_deterministic_compute) { options.SetDeterministicCompute(true); } +TEST(CApiTest, session_options_get_mem_pattern_enabled) { + Ort::SessionOptions options; + + // Memory pattern is enabled by default + ASSERT_TRUE(options.GetMemPatternEnabled()); + + // Disable and verify + options.DisableMemPattern(); + ASSERT_FALSE(options.GetMemPatternEnabled()); + + // Re-enable and verify + options.EnableMemPattern(); + ASSERT_TRUE(options.GetMemPatternEnabled()); +} + #if !defined(ORT_MINIMAL_BUILD) && !defined(ORT_EXTENDED_MINIMAL_BUILD) && !defined(ORT_NO_EXCEPTIONS) TEST(CApiTest, session_options_oversized_affinity_string) { From 1a12e41ccacb4af3d3c9f84fc22056acb7a3c2de Mon Sep 17 00:00:00 2001 From: Adrian Lizarraga Date: Tue, 5 May 2026 15:31:46 -0700 Subject: [PATCH 2/4] Add OrtApi::GetSessionExecutionMode() --- .../onnxruntime/core/session/onnxruntime_c_api.h | 13 +++++++++++++ .../core/session/onnxruntime_cxx_api.h | 1 + .../core/session/onnxruntime_cxx_inline.h | 7 +++++++ onnxruntime/core/session/abi_session_options.cc | 4 ++++ onnxruntime/core/session/onnxruntime_c_api.cc | 1 + onnxruntime/core/session/ort_apis.h | 1 + .../test/shared_lib/test_session_options.cc | 15 +++++++++++++++ 7 files changed, 42 insertions(+) diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 0c2542a52175d..f67b5e157b56d 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -7449,6 +7449,19 @@ struct OrtApi { * \see OrtApi::EnableMemPattern, OrtApi::DisableMemPattern */ ORT_API2_STATUS(GetMemPatternEnabled, _In_ const OrtSessionOptions* options, _Out_ int* out); + + /** \brief Get the current execution mode setting. + * + * \param[in] options + * \param[out] out Set to the current execution mode (ORT_SEQUENTIAL or ORT_PARALLEL). + * + * \snippet{doc} snippets.dox OrtStatus Return Value + * + * \since Version 1.26. + * + * \see OrtApi::SetSessionExecutionMode + */ + ORT_API2_STATUS(GetSessionExecutionMode, _In_ const OrtSessionOptions* options, _Out_ ExecutionMode* out); }; /* diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 1ea4856b77b03..e336ef87e3089 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -1597,6 +1597,7 @@ struct SessionOptionsImpl : ConstSessionOptionsImpl { bool GetMemPatternEnabled() const; ///< Wraps OrtApi::GetMemPatternEnabled SessionOptionsImpl& SetExecutionMode(ExecutionMode execution_mode); ///< Wraps OrtApi::SetSessionExecutionMode + ExecutionMode GetExecutionMode() const; ///< Wraps OrtApi::GetSessionExecutionMode SessionOptionsImpl& SetLoadCancellationFlag(bool value); ///< Wraps OrtApi::SessionOptionsSetLoadCancellationFlag diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h index a59def53f4711..43e2686ca89e4 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h @@ -1434,6 +1434,13 @@ inline SessionOptionsImpl& SessionOptionsImpl::SetExecutionMode(ExecutionM return *this; } +template +inline ExecutionMode SessionOptionsImpl::GetExecutionMode() const { + ExecutionMode out; + ThrowOnError(GetApi().GetSessionExecutionMode(this->p_, &out)); + return out; +} + template inline SessionOptionsImpl& SessionOptionsImpl::SetLoadCancellationFlag(bool value) { ThrowOnError(GetApi().SessionOptionsSetLoadCancellationFlag(this->p_, value)); diff --git a/onnxruntime/core/session/abi_session_options.cc b/onnxruntime/core/session/abi_session_options.cc index 46073f62f8bff..483976cfbae41 100644 --- a/onnxruntime/core/session/abi_session_options.cc +++ b/onnxruntime/core/session/abi_session_options.cc @@ -152,6 +152,10 @@ ORT_API_STATUS_IMPL(OrtApis::GetMemPatternEnabled, _In_ const OrtSessionOptions* *out = options->value.enable_mem_pattern ? 1 : 0; return nullptr; } +ORT_API_STATUS_IMPL(OrtApis::GetSessionExecutionMode, _In_ const OrtSessionOptions* options, _Out_ ExecutionMode* out) { + *out = options->value.execution_mode; + return nullptr; +} // enable the memory arena on CPU // Arena may pre-allocate memory for future usage. diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index 2e28a900b15ca..69618f31bb786 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -4896,6 +4896,7 @@ static constexpr OrtApi ort_api_1_to_26 = { // End of Version 25 - DO NOT MODIFY ABOVE (see above text for more information) &OrtApis::GetMemPatternEnabled, + &OrtApis::GetSessionExecutionMode, }; // OrtApiBase can never change as there is no way to know what version of OrtApiBase is returned by OrtGetApiBase. diff --git a/onnxruntime/core/session/ort_apis.h b/onnxruntime/core/session/ort_apis.h index d5a110ad5a23e..00ec258cef91e 100644 --- a/onnxruntime/core/session/ort_apis.h +++ b/onnxruntime/core/session/ort_apis.h @@ -65,6 +65,7 @@ ORT_API_STATUS_IMPL(DisableProfiling, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(EnableMemPattern, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(DisableMemPattern, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(GetMemPatternEnabled, _In_ const OrtSessionOptions* options, _Out_ int* out); +ORT_API_STATUS_IMPL(GetSessionExecutionMode, _In_ const OrtSessionOptions* options, _Out_ ExecutionMode* out); ORT_API_STATUS_IMPL(EnableCpuMemArena, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(DisableCpuMemArena, _In_ OrtSessionOptions* options); ORT_API_STATUS_IMPL(SetSessionLogId, _In_ OrtSessionOptions* options, const char* logid); diff --git a/onnxruntime/test/shared_lib/test_session_options.cc b/onnxruntime/test/shared_lib/test_session_options.cc index e400eb0262b0f..7e29c6f9a71d1 100644 --- a/onnxruntime/test/shared_lib/test_session_options.cc +++ b/onnxruntime/test/shared_lib/test_session_options.cc @@ -37,6 +37,21 @@ TEST(CApiTest, session_options_get_mem_pattern_enabled) { ASSERT_TRUE(options.GetMemPatternEnabled()); } +TEST(CApiTest, session_options_get_execution_mode) { + Ort::SessionOptions options; + + // Default is sequential + ASSERT_EQ(options.GetExecutionMode(), ORT_SEQUENTIAL); + + // Set to parallel and verify + options.SetExecutionMode(ORT_PARALLEL); + ASSERT_EQ(options.GetExecutionMode(), ORT_PARALLEL); + + // Set back to sequential and verify + options.SetExecutionMode(ORT_SEQUENTIAL); + ASSERT_EQ(options.GetExecutionMode(), ORT_SEQUENTIAL); +} + #if !defined(ORT_MINIMAL_BUILD) && !defined(ORT_EXTENDED_MINIMAL_BUILD) && !defined(ORT_NO_EXCEPTIONS) TEST(CApiTest, session_options_oversized_affinity_string) { From eec3a0712d090894c317a3b907a91c13ae6b4e9e Mon Sep 17 00:00:00 2001 From: Adrian Lizarraga Date: Tue, 5 May 2026 16:22:16 -0700 Subject: [PATCH 3/4] Address review comments and merge main --- .../core/session/onnxruntime_c_api.h | 4 +-- .../core/session/onnxruntime_cxx_api.h | 5 ++-- .../core/session/onnxruntime_cxx_inline.h | 28 +++++++++---------- .../core/session/abi_session_options.cc | 18 +++++++++--- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index c26b046b55e7d..920ff60152c5d 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -7444,7 +7444,7 @@ struct OrtApi { * * \snippet{doc} snippets.dox OrtStatus Return Value * - * \since Version 1.26. + * \since Version 1.27. * * \see OrtApi::EnableMemPattern, OrtApi::DisableMemPattern */ @@ -7457,7 +7457,7 @@ struct OrtApi { * * \snippet{doc} snippets.dox OrtStatus Return Value * - * \since Version 1.26. + * \since Version 1.27. * * \see OrtApi::SetSessionExecutionMode */ diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 47cf97f5ff08e..37920cf0d58f3 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -1621,6 +1621,9 @@ struct ConstSessionOptionsImpl : Base { std::string GetConfigEntry(const char* config_key) const; ///< Wraps OrtApi::GetSessionConfigEntry bool HasConfigEntry(const char* config_key) const; ///< Wraps OrtApi::HasSessionConfigEntry std::string GetConfigEntryOrDefault(const char* config_key, const std::string& def) const; + + bool GetMemPatternEnabled() const; ///< Wraps OrtApi::GetMemPatternEnabled + ExecutionMode GetExecutionMode() const; ///< Wraps OrtApi::GetSessionExecutionMode }; template @@ -1645,10 +1648,8 @@ struct SessionOptionsImpl : ConstSessionOptionsImpl { SessionOptionsImpl& EnableMemPattern(); ///< Wraps OrtApi::EnableMemPattern SessionOptionsImpl& DisableMemPattern(); ///< Wraps OrtApi::DisableMemPattern - bool GetMemPatternEnabled() const; ///< Wraps OrtApi::GetMemPatternEnabled SessionOptionsImpl& SetExecutionMode(ExecutionMode execution_mode); ///< Wraps OrtApi::SetSessionExecutionMode - ExecutionMode GetExecutionMode() const; ///< Wraps OrtApi::GetSessionExecutionMode SessionOptionsImpl& SetLoadCancellationFlag(bool value); ///< Wraps OrtApi::SessionOptionsSetLoadCancellationFlag diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h index 8647a1cc61c0c..f32e560b433e6 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h @@ -1400,6 +1400,20 @@ inline std::string ConstSessionOptionsImpl::GetConfigEntryOrDefault(const cha return this->GetConfigEntry(config_key); } +template +inline bool ConstSessionOptionsImpl::GetMemPatternEnabled() const { + int out = 0; + ThrowOnError(GetApi().GetMemPatternEnabled(this->p_, &out)); + return out != 0; +} + +template +inline ExecutionMode ConstSessionOptionsImpl::GetExecutionMode() const { + ExecutionMode out{}; + ThrowOnError(GetApi().GetSessionExecutionMode(this->p_, &out)); + return out; +} + template inline SessionOptionsImpl& SessionOptionsImpl::SetIntraOpNumThreads(int intra_op_num_threads) { ThrowOnError(GetApi().SetIntraOpNumThreads(this->p_, intra_op_num_threads)); @@ -1460,13 +1474,6 @@ inline SessionOptionsImpl& SessionOptionsImpl::DisableMemPattern() { return *this; } -template -inline bool SessionOptionsImpl::GetMemPatternEnabled() const { - int out = 0; - ThrowOnError(GetApi().GetMemPatternEnabled(this->p_, &out)); - return out != 0; -} - template inline SessionOptionsImpl& SessionOptionsImpl::EnableCpuMemArena() { ThrowOnError(GetApi().EnableCpuMemArena(this->p_)); @@ -1485,13 +1492,6 @@ inline SessionOptionsImpl& SessionOptionsImpl::SetExecutionMode(ExecutionM return *this; } -template -inline ExecutionMode SessionOptionsImpl::GetExecutionMode() const { - ExecutionMode out; - ThrowOnError(GetApi().GetSessionExecutionMode(this->p_, &out)); - return out; -} - template inline SessionOptionsImpl& SessionOptionsImpl::SetLoadCancellationFlag(bool value) { ThrowOnError(GetApi().SessionOptionsSetLoadCancellationFlag(this->p_, value)); diff --git a/onnxruntime/core/session/abi_session_options.cc b/onnxruntime/core/session/abi_session_options.cc index 483976cfbae41..06bd5c4d84089 100644 --- a/onnxruntime/core/session/abi_session_options.cc +++ b/onnxruntime/core/session/abi_session_options.cc @@ -118,6 +118,15 @@ ORT_API_STATUS_IMPL(OrtApis::SetSessionExecutionMode, _In_ OrtSessionOptions* op return nullptr; } +ORT_API_STATUS_IMPL(OrtApis::GetSessionExecutionMode, _In_ const OrtSessionOptions* options, _Out_ ExecutionMode* out) { + API_IMPL_BEGIN + ORT_API_RETURN_IF(options == nullptr, ORT_INVALID_ARGUMENT, "'options' parameter must not be NULL"); + ORT_API_RETURN_IF(out == nullptr, ORT_INVALID_ARGUMENT, "'out' parameter must not be NULL"); + *out = options->value.execution_mode; + return nullptr; + API_IMPL_END +} + // set filepath to save optimized onnx model. ORT_API_STATUS_IMPL(OrtApis::SetOptimizedModelFilePath, _In_ OrtSessionOptions* options, _In_ const ORTCHAR_T* optimized_model_filepath) { options->value.optimized_model_filepath = optimized_model_filepath; @@ -148,13 +157,14 @@ ORT_API_STATUS_IMPL(OrtApis::DisableMemPattern, _In_ OrtSessionOptions* options) options->value.enable_mem_pattern = false; return nullptr; } + ORT_API_STATUS_IMPL(OrtApis::GetMemPatternEnabled, _In_ const OrtSessionOptions* options, _Out_ int* out) { + API_IMPL_BEGIN + ORT_API_RETURN_IF(options == nullptr, ORT_INVALID_ARGUMENT, "'options' parameter must not be NULL"); + ORT_API_RETURN_IF(out == nullptr, ORT_INVALID_ARGUMENT, "'out' parameter must not be NULL"); *out = options->value.enable_mem_pattern ? 1 : 0; return nullptr; -} -ORT_API_STATUS_IMPL(OrtApis::GetSessionExecutionMode, _In_ const OrtSessionOptions* options, _Out_ ExecutionMode* out) { - *out = options->value.execution_mode; - return nullptr; + API_IMPL_END } // enable the memory arena on CPU From 611e0365a342e6b6b7ffea419aee4b47154a21ee Mon Sep 17 00:00:00 2001 From: Adrian Lizarraga Date: Tue, 5 May 2026 16:28:27 -0700 Subject: [PATCH 4/4] wording fix --- include/onnxruntime/core/session/onnxruntime_c_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 920ff60152c5d..28df8e69b5925 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -7437,7 +7437,7 @@ struct OrtApi { /// @} - /** \brief Get the current state of the memory pattern optimization setting. + /** \brief Check if the memory pattern optimization is enabled in the session options. * * \param[in] options * \param[out] out Set to 1 if the memory pattern optimization is enabled, 0 otherwise.