Skip to content

Commit c6f5559

Browse files
committed
Ensure all blobs are available at epctx export
1 parent 07bf632 commit c6f5559

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

onnxruntime/core/providers/openvino/backend_manager.cc

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ ov::CompiledModel BackendManager::GetOVCompiledModel() {
3737
return ov::CompiledModel();
3838
}
3939

40+
static bool ShouldExportEpContext(const SessionContext& session_context, const SubGraphContext& subgraph_context) {
41+
return session_context.so_context_enable && (subgraph_context.is_ep_ctx_ovir_encapsulated || !subgraph_context.is_ep_ctx_graph);
42+
}
43+
4044
BackendManager::BackendManager(SessionContext& session_context,
4145
SharedContextManager& shared_context_manager,
4246
const onnxruntime::Node& fused_node,
@@ -188,43 +192,37 @@ BackendManager::BackendManager(SessionContext& session_context,
188192
}
189193
}
190194
}
195+
196+
if (ShouldExportEpContext(session_context_, subgraph_context_)) {
197+
if (concrete_backend_) {
198+
shared_context_->AddNativeBlob(subgraph_context_.subgraph_name, concrete_backend_->GetOVCompiledModel());
199+
} else {
200+
ORT_THROW(
201+
"Exporting dynamically compiled models at runtime is not supported. "
202+
"Cannot export blobs of dynamic models that request static shape inference. "
203+
"To export this model, set disable_dynamic_shapes to False");
204+
}
205+
}
191206
}
192207

193208
// Call EPContext model exporter here if the provider option for exporting
194209
// precompiled blob is set. If that's the case:
195210
// By default, create model in embed mode where the blob stream is exported as data within
196211
// the EPContext node.
197212
void BackendManager::TryExportCompiledBlobAsEPCtxNode(const onnxruntime::GraphViewer& graph_body_viewer, bool include_embed_data) {
198-
bool should_export = session_context_.so_context_enable && (subgraph_context_.is_ep_ctx_ovir_encapsulated || !subgraph_context_.is_ep_ctx_graph);
199-
200-
if (!should_export) {
201-
// skip exporting.
213+
if (!ShouldExportEpContext(session_context_, subgraph_context_) || !concrete_backend_) {
202214
return;
203215
}
204216

205-
if (!concrete_backend_) {
206-
ORT_THROW("[OpenVINO-EP] Cannot export compiled blob as EPCtx Node: Backend not initialized.");
207-
}
208-
209-
if (session_context_.disable_dynamic_shapes && subgraph_context_.has_dynamic_input_shape) {
210-
std::string exception_str =
211-
"Exporting dynamically compiled models at runtime is not supported. "
212-
"Cannot export blobs of dynamic models that request static shape inference. "
213-
"To export this model, set disable_dynamic_shapes to False";
214-
ORT_THROW(exception_str);
215-
}
216-
217217
// If embed_mode, then pass on the serialized blob
218218
// If not embed_mode, dump the blob here and only pass on the path to the blob
219219
std::string model_blob_str;
220220
auto compiled_model = concrete_backend_->GetOVCompiledModel();
221221
if (session_context_.so_context_embed_mode) { // Internal blob
222-
auto shared_context = shared_context_manager_.GetOrCreateActiveSharedContext("");
223-
shared_context->AddNativeBlob(subgraph_context_.subgraph_name, compiled_model);
224222
if (include_embed_data) {
225223
std::stringstream ss;
226-
shared_context->Serialize(ss);
227-
model_blob_str = ss.str();
224+
shared_context_->Serialize(ss);
225+
model_blob_str = std::move(ss).str();
228226
}
229227
} else { // External blob
230228
// Build name by combining EpCtx model name (if available) and subgraph name. Model
@@ -235,10 +233,7 @@ void BackendManager::TryExportCompiledBlobAsEPCtxNode(const onnxruntime::GraphVi
235233
}
236234
ORT_ENFORCE(!name.empty());
237235

238-
auto bin_filename = session_context_.GetOutputBinPath();
239-
auto shared_context = shared_context_manager_.GetOrCreateActiveSharedContext(bin_filename);
240-
shared_context->AddNativeBlob(subgraph_context_.subgraph_name, compiled_model);
241-
model_blob_str = shared_context->GetBinPath().filename().string();
236+
model_blob_str = shared_context_->GetBinPath().filename().string();
242237
}
243238

244239
auto status = ep_ctx_handle_.AddOVEPCtxNodeToGraph(graph_body_viewer,

0 commit comments

Comments
 (0)