From 2f358b74f0606e810bc47b84e7061636222cb8ed Mon Sep 17 00:00:00 2001 From: jatinwadhwa921 <110383850+jatinwadhwa921@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:14:16 +0530 Subject: [PATCH] Epctx node base path fix and lint fix (#569) * Use ep.context_file_path to get base path when creating session from memory * Fixed lint issues --------- Co-authored-by: Javier E. Martinez --- .../providers/openvino/backend_manager.cc | 20 +++++++++++++------ .../core/providers/openvino/backend_utils.cc | 8 ++++---- .../openvino/onnx_ctx_model_helper.cc | 8 ++++++-- .../openvino/onnx_ctx_model_helper.h | 2 +- .../openvino/openvino_provider_factory.cc | 6 +++--- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/onnxruntime/core/providers/openvino/backend_manager.cc b/onnxruntime/core/providers/openvino/backend_manager.cc index 3740fdc239aea..16a92b43adaf6 100644 --- a/onnxruntime/core/providers/openvino/backend_manager.cc +++ b/onnxruntime/core/providers/openvino/backend_manager.cc @@ -76,7 +76,7 @@ BackendManager::BackendManager(SessionContext& session_context, ptr_stream_t model_stream; std::unique_ptr model_proto; if (subgraph_context_.is_ep_ctx_graph) { - model_stream = ep_ctx_handle_.GetModelBlobStream(subgraph); + model_stream = ep_ctx_handle_.GetModelBlobStream(session_context_.so_context_file_path, subgraph); } else { model_proto = GetModelProtoFromFusedNode(fused_node, subgraph, logger); } @@ -214,21 +214,29 @@ Status BackendManager::ExportCompiledBlobAsEPCtxNode(const onnxruntime::GraphVie // If not embed_mode, dump the blob here and only pass on the path to the blob std::string model_blob_str; auto compiled_model = concrete_backend_->GetOVCompiledModel(); - if (session_context_.so_context_embed_mode) { - // Internal blob + if (session_context_.so_context_embed_mode) { // Internal blob std::ostringstream model_blob_stream; compiled_model.export_model(model_blob_stream); model_blob_str = std::move(model_blob_stream).str(); if (model_blob_str.empty()) { ORT_THROW("Model blob stream is empty after exporting the compiled model."); } - } else { - // External blob + } else { // External blob + // Build name by combining EpCtx model name (if available) and subgraph name. Model + // name is not available in when creating a session from memory + auto name = session_context_.so_context_file_path.stem().string(); + if (!name.empty() && !graph_body_viewer.ModelPath().empty()) { + name = graph_body_viewer.ModelPath().stem().string(); + } + if (!name.empty()) { + name += "_"; + } + name += subgraph_context_.subgraph_name; + std::filesystem::path blob_filename = session_context_.so_context_file_path; if (blob_filename.empty()) { blob_filename = session_context_.onnx_model_path_name; } - const auto name = graph_body_viewer.ModelPath().stem().string() + "_" + subgraph_context_.subgraph_name; blob_filename = blob_filename.parent_path() / name; blob_filename.replace_extension("blob"); std::ofstream blob_file(blob_filename, diff --git a/onnxruntime/core/providers/openvino/backend_utils.cc b/onnxruntime/core/providers/openvino/backend_utils.cc index 7970c98994424..acc3f120b270b 100644 --- a/onnxruntime/core/providers/openvino/backend_utils.cc +++ b/onnxruntime/core/providers/openvino/backend_utils.cc @@ -92,13 +92,13 @@ std::istream& operator>>(std::istream& stream, SharedContext::SharedWeights::Met size_t safe_num_dimensions = num_dimensions; - if(num_dimensions == 0 || safe_num_dimensions > MAX_SAFE_DIMENSIONS) { - ORT_THROW("Invalid number of dimensions provided."); + if (num_dimensions == 0 || safe_num_dimensions > MAX_SAFE_DIMENSIONS) { + ORT_THROW("Invalid number of dimensions provided."); } try { - value.dimensions.resize(safe_num_dimensions); + value.dimensions.resize(safe_num_dimensions); } catch (const std::bad_alloc&) { - ORT_THROW("Error: Memory allocation failed while resizing dimensions."); + ORT_THROW("Error: Memory allocation failed while resizing dimensions."); } for (auto& dim : value.dimensions) { diff --git a/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc b/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc index 244ceaf56fe05..7bd4f8d96cc55 100644 --- a/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc +++ b/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc @@ -99,7 +99,7 @@ Status EPCtxHandler::AddOVEPCtxNodeToGraph(const GraphViewer& graph_viewer, return Status::OK(); } -std::unique_ptr EPCtxHandler::GetModelBlobStream(const GraphViewer& graph_viewer) const { +std::unique_ptr EPCtxHandler::GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const { auto first_index = *graph_viewer.GetNodesInTopologicalOrder().begin(); auto node = graph_viewer.GetNode(first_index); ORT_ENFORCE(node != nullptr); @@ -115,7 +115,11 @@ std::unique_ptr EPCtxHandler::GetModelBlobStream(const GraphViewer if (embed_mode) { result.reset((std::istream*)new std::istringstream(ep_cache_context)); } else { - const auto& blob_filepath = graph_viewer.ModelPath().parent_path() / ep_cache_context; + auto blob_filepath = so_context_file_path; + if (blob_filepath.empty() && !graph_viewer.ModelPath().empty()) { + blob_filepath = graph_viewer.ModelPath(); + } + blob_filepath = blob_filepath.parent_path() / ep_cache_context; ORT_ENFORCE(std::filesystem::exists(blob_filepath), "Blob file not found: ", blob_filepath.string()); result.reset((std::istream*)new std::ifstream(blob_filepath, std::ios_base::binary | std::ios_base::in)); } diff --git a/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h b/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h index f644e2607904d..ff978bd6534d8 100644 --- a/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h +++ b/onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h @@ -31,7 +31,7 @@ class EPCtxHandler { const std::string& graph_name, const bool embed_mode, std::string&& model_blob_str) const; - std::unique_ptr GetModelBlobStream(const GraphViewer& graph_viewer) const; + std::unique_ptr GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const; InlinedVector GetEPCtxNodes() const; private: diff --git a/onnxruntime/core/providers/openvino/openvino_provider_factory.cc b/onnxruntime/core/providers/openvino/openvino_provider_factory.cc index b02392606aba8..1c2d857b6252d 100644 --- a/onnxruntime/core/providers/openvino/openvino_provider_factory.cc +++ b/onnxruntime/core/providers/openvino/openvino_provider_factory.cc @@ -22,9 +22,9 @@ void ParseConfigOptions(ProviderInfo& pi, const ConfigOptions& config_options) { pi.so_context_file_path = config_options.GetConfigOrDefault(kOrtSessionOptionEpContextFilePath, ""); } -void* ParseUint64(const ProviderOptions& provider_options, [[maybe_unused]] std::string option_name) { - if (provider_options.contains("context")) { - uint64_t number = std::strtoull(provider_options.at("context").data(), nullptr, 16); +void* ParseUint64(const ProviderOptions& provider_options, std::string option_name) { + if (provider_options.contains(option_name)) { + uint64_t number = std::strtoull(provider_options.at(option_name).data(), nullptr, 16); return reinterpret_cast(number); } else { return nullptr;