From 7350484b4ad54f44ac0b53f2158a06e30db2d6d9 Mon Sep 17 00:00:00 2001 From: Lagos Date: Tue, 21 Apr 2026 11:51:05 -0400 Subject: [PATCH] Wrap the session's mutable SessionOptions in an OrtSessionOptions before passing it to IExecutionProviderFactory::CreateProvider. This ensures that any modifications made by an EP during creation (e.g., disabling memory pattern optimization) are reflected in the actual session options used during initialization. Previously, EP-side modifications to session options could be lost. --- onnxruntime/core/session/utils.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/session/utils.cc b/onnxruntime/core/session/utils.cc index a354cf26368d4..6ef7370d2b322 100644 --- a/onnxruntime/core/session/utils.cc +++ b/onnxruntime/core/session/utils.cc @@ -423,11 +423,24 @@ OrtStatus* InitializeSession(_In_ const OrtSessionOptions* options, if (has_provider_factories) { std::vector> provider_list; + + // Create OrtSessionOptions wrapper for the CreateProvider call. + // Once the InferenceSession is created, its SessionOptions is the source of truth + // and contains all the values from the user provided OrtSessionOptions. + // We wrap the session's copy so any modifications made by the EP (e.g., DisableMemPattern) + // will affect the actual session options that will be used. + auto& session_options = sess.GetMutableSessionOptions(); + OrtSessionOptions ort_so; + ort_so.value = session_options; + for (auto& factory : options->provider_factories) { - auto provider = factory->CreateProvider(*options, *session_logger->ToExternal()); + auto provider = factory->CreateProvider(ort_so, *session_logger->ToExternal()); provider_list.push_back(std::move(provider)); } + // Copy any modifications made by the EP back to the session's options + session_options = ort_so.value; + // register the providers for (auto& provider : provider_list) { if (provider) {