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) {