Skip to content

Commit 914c07a

Browse files
authored
[OVEP] Enable Session Option to Stop Context Sharing (#822)
This PR enables the session option: StopShareEpContexts ("ep.stop_share_ep_contexts" = 1/0), to explicitly clear the globally shared EP context after a session completes. This allows controlled cleanup of shared weights, metadata, its file path, and other cached artifacts stored in SharedContext. For example, for model abc and model xyz: model_abc(ep.share_ep_contexts=1, ep.stop_share_ep_contexts=1) After this session, all existing shared context data is cleared. model_xyz(ep.share_ep_contexts=1) This model starts with a fresh shared context, without any leftover metadata or weights from model_abc. Fixed format and unique pointer handling.
1 parent 5c40da5 commit 914c07a

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

onnxruntime/core/providers/openvino/contexts.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,22 @@ class SharedContext : public WeakSingleton<SharedContext> {
6161
size_t weights_size_;
6262
};
6363

64+
void clear() {
65+
metadata.clear();
66+
metadata_filepath.clear();
67+
external_weight_filename.clear();
68+
mapped_weights.reset();
69+
}
70+
6471
fs::path external_weight_filename;
6572
std::unique_ptr<WeightsFile> mapped_weights;
6673
Metadata::Map metadata;
6774
fs::path metadata_filepath;
6875
} shared_weights;
76+
77+
void clear() {
78+
shared_weights.clear();
79+
}
6980
};
7081

7182
using config_t = std::map<std::string, ov::AnyMap>;
@@ -109,6 +120,7 @@ struct ProviderInfo {
109120
bool so_context_embed_mode{false}; // ORT session option
110121
bool so_share_ep_contexts{false}; // ORT session option
111122
fs::path so_context_file_path{}; // ORT session option
123+
bool so_stop_share_ep_contexts{false}; // ORT session option
112124
const ConfigOptions* config_options{NULL};
113125
const std::unordered_set<std::string> valid_provider_keys = {"device_type", "device_id", "device_luid", "cache_dir", "precision",
114126
"load_config", "context", "num_of_threads", "model_priority", "num_streams", "enable_opencl_throttling", "enable_qdq_optimizer",

onnxruntime/core/providers/openvino/openvino_execution_provider.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ OpenVINOExecutionProvider::~OpenVINOExecutionProvider() {
6565
backend_manager.ShutdownBackendManager();
6666
}
6767
backend_managers_.clear();
68+
shared_context_.reset();
6869
}
6970

7071
std::vector<std::unique_ptr<ComputeCapability>>
@@ -214,6 +215,12 @@ common::Status OpenVINOExecutionProvider::Compile(
214215
file << metadata;
215216
}
216217

218+
if (session_context_.so_stop_share_ep_contexts) {
219+
if (shared_context_) {
220+
shared_context_->clear();
221+
}
222+
}
223+
217224
return status;
218225
}
219226

onnxruntime/core/providers/openvino/openvino_provider_factory.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void ParseConfigOptions(ProviderInfo& pi) {
2828
pi.so_context_embed_mode = pi.config_options->GetConfigOrDefault(kOrtSessionOptionEpContextEmbedMode, "0") == "1";
2929
pi.so_share_ep_contexts = pi.config_options->GetConfigOrDefault(kOrtSessionOptionShareEpContexts, "0") == "1";
3030
pi.so_context_file_path = pi.config_options->GetConfigOrDefault(kOrtSessionOptionEpContextFilePath, "");
31+
pi.so_stop_share_ep_contexts = pi.config_options->GetConfigOrDefault(kOrtSessionOptionStopShareEpContexts, "0") == "1";
3132

3233
if (pi.so_share_ep_contexts) {
3334
ov::AnyMap map;

0 commit comments

Comments
 (0)