From b86840a8d8082a8d69ac31fb550107ac43a63d01 Mon Sep 17 00:00:00 2001 From: n1harika Date: Thu, 4 Sep 2025 15:16:25 +0530 Subject: [PATCH 1/4] support for stop context sharing --- onnxruntime/core/providers/openvino/contexts.h | 5 +++++ .../core/providers/openvino/openvino_execution_provider.cc | 5 +++++ .../core/providers/openvino/openvino_provider_factory.cc | 1 + 3 files changed, 11 insertions(+) diff --git a/onnxruntime/core/providers/openvino/contexts.h b/onnxruntime/core/providers/openvino/contexts.h index 07b09899ac214..f3ac04d1ca787 100644 --- a/onnxruntime/core/providers/openvino/contexts.h +++ b/onnxruntime/core/providers/openvino/contexts.h @@ -66,6 +66,10 @@ class SharedContext : public WeakSingleton { Metadata::Map metadata; fs::path metadata_filepath; } shared_weights; + + void clear(){ + shared_weights.metadata.clear(); + } }; using config_t = std::map; @@ -109,6 +113,7 @@ struct ProviderInfo { bool so_context_embed_mode{false}; // ORT session option bool so_share_ep_contexts{false}; // ORT session option fs::path so_context_file_path{}; // ORT session option + bool so_stop_share_ep_contexts{false}; // ORT session option const ConfigOptions* config_options{NULL}; const std::unordered_set valid_provider_keys = {"device_type", "device_id", "device_luid", "cache_dir", "precision", "load_config", "context", "num_of_threads", "model_priority", "num_streams", "enable_opencl_throttling", "enable_qdq_optimizer", diff --git a/onnxruntime/core/providers/openvino/openvino_execution_provider.cc b/onnxruntime/core/providers/openvino/openvino_execution_provider.cc index a0fa885cbfc38..3890cbd42f571 100644 --- a/onnxruntime/core/providers/openvino/openvino_execution_provider.cc +++ b/onnxruntime/core/providers/openvino/openvino_execution_provider.cc @@ -65,6 +65,7 @@ OpenVINOExecutionProvider::~OpenVINOExecutionProvider() { backend_manager.ShutdownBackendManager(); } backend_managers_.clear(); + shared_context_.reset(); } std::vector> @@ -214,6 +215,10 @@ common::Status OpenVINOExecutionProvider::Compile( file << metadata; } + if (session_context_.so_stop_share_ep_contexts) { + shared_context_->clear(); + } + return status; } diff --git a/onnxruntime/core/providers/openvino/openvino_provider_factory.cc b/onnxruntime/core/providers/openvino/openvino_provider_factory.cc index 1a10d9849d5cc..f26da37fa7d7e 100644 --- a/onnxruntime/core/providers/openvino/openvino_provider_factory.cc +++ b/onnxruntime/core/providers/openvino/openvino_provider_factory.cc @@ -28,6 +28,7 @@ void ParseConfigOptions(ProviderInfo& pi) { pi.so_context_embed_mode = pi.config_options->GetConfigOrDefault(kOrtSessionOptionEpContextEmbedMode, "0") == "1"; pi.so_share_ep_contexts = pi.config_options->GetConfigOrDefault(kOrtSessionOptionShareEpContexts, "0") == "1"; pi.so_context_file_path = pi.config_options->GetConfigOrDefault(kOrtSessionOptionEpContextFilePath, ""); + pi.so_stop_share_ep_contexts = pi.config_options->GetConfigOrDefault(kOrtSessionOptionStopShareEpContexts, "0") == "1"; if (pi.so_share_ep_contexts) { ov::AnyMap map; From 1f77e29b0515b6e9a13369a7ebe552ec0d99e060 Mon Sep 17 00:00:00 2001 From: Javier Martinez Date: Thu, 4 Sep 2025 15:47:07 -0700 Subject: [PATCH 2/4] Clearing additional SharedWeights members The initial implementation of Clear() did not reset the name of the external metadata file and causes the new context from using the same name as the previous context. --- onnxruntime/core/providers/openvino/contexts.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/openvino/contexts.h b/onnxruntime/core/providers/openvino/contexts.h index f3ac04d1ca787..46f553ad28164 100644 --- a/onnxruntime/core/providers/openvino/contexts.h +++ b/onnxruntime/core/providers/openvino/contexts.h @@ -61,6 +61,15 @@ class SharedContext : public WeakSingleton { size_t weights_size_; }; + void clear() { + metadata.clear(); + metadata_filepath.clear(); + external_weight_filename.clear(); + if (mapped_weights) { + delete mapped_weights.release(); + } + } + fs::path external_weight_filename; std::unique_ptr mapped_weights; Metadata::Map metadata; @@ -68,7 +77,7 @@ class SharedContext : public WeakSingleton { } shared_weights; void clear(){ - shared_weights.metadata.clear(); + shared_weights.clear(); } }; From 397609e10a94470e0a004d4177254deefbf44e19 Mon Sep 17 00:00:00 2001 From: n1harika Date: Tue, 7 Oct 2025 17:56:07 +0530 Subject: [PATCH 3/4] fixing format and unique pointer handling --- onnxruntime/core/providers/openvino/contexts.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/providers/openvino/contexts.h b/onnxruntime/core/providers/openvino/contexts.h index 46f553ad28164..a0dc33ae657c8 100644 --- a/onnxruntime/core/providers/openvino/contexts.h +++ b/onnxruntime/core/providers/openvino/contexts.h @@ -65,9 +65,7 @@ class SharedContext : public WeakSingleton { metadata.clear(); metadata_filepath.clear(); external_weight_filename.clear(); - if (mapped_weights) { - delete mapped_weights.release(); - } + mapped_weights.reset(); } fs::path external_weight_filename; @@ -76,7 +74,7 @@ class SharedContext : public WeakSingleton { fs::path metadata_filepath; } shared_weights; - void clear(){ + void clear() { shared_weights.clear(); } }; From 634e451f06fb46f5b29faf866232207e77363345 Mon Sep 17 00:00:00 2001 From: MayureshV1 <47039074+MayureshV1@users.noreply.github.com> Date: Tue, 7 Oct 2025 14:10:19 -0700 Subject: [PATCH 4/4] Update onnxruntime/core/providers/openvino/openvino_execution_provider.cc Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../core/providers/openvino/openvino_execution_provider.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/openvino/openvino_execution_provider.cc b/onnxruntime/core/providers/openvino/openvino_execution_provider.cc index 3890cbd42f571..ee5298d5b08e2 100644 --- a/onnxruntime/core/providers/openvino/openvino_execution_provider.cc +++ b/onnxruntime/core/providers/openvino/openvino_execution_provider.cc @@ -216,7 +216,9 @@ common::Status OpenVINOExecutionProvider::Compile( } if (session_context_.so_stop_share_ep_contexts) { - shared_context_->clear(); + if (shared_context_) { + shared_context_->clear(); + } } return status;