Skip to content

Commit 90869ff

Browse files
authored
[CVS-169168] Change name of metadata file and add filepath validation (#717)
* Change name of metadata file and add filepath validation * Save metadata file path in shared context for use across model compilation * Fix metadata file path initialization * Check that metadata file is created
1 parent 278f6a7 commit 90869ff

File tree

9 files changed

+426
-410
lines changed

9 files changed

+426
-410
lines changed

onnxruntime/core/providers/openvino/backend_manager.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BackendManager::BackendManager(SessionContext& session_context,
4545
subgraph_context_.is_ep_ctx_graph = ep_ctx_handle_.CheckForOVEPCtxNodeInGraph(subgraph);
4646
// If the graph contains a OVIR wrapped node, we check if it has matching xml file name attribute
4747
subgraph_context_.is_ep_ctx_ovir_encapsulated = ep_ctx_handle_.CheckEPCacheContextAttribute(subgraph,
48-
session_context_.onnx_model_path_name.filename().replace_extension("xml").string());
48+
session_context_.onnx_model_path_name.filename().replace_extension("xml").string());
4949

5050
subgraph_context_.model_precision = [&](const GraphViewer& graph_viewer) {
5151
// return empty if graph has no inputs or if types are not one of FP32/FP16
@@ -91,21 +91,20 @@ BackendManager::BackendManager(SessionContext& session_context,
9191
std::string device_type = session_context_.device_type;
9292

9393
auto& sw = shared_context_.shared_weights;
94-
if (session_context_.so_share_ep_contexts) {
94+
if (session_context_.so_share_ep_contexts && !sw.metadata.empty()) {
9595
std::filesystem::path weight_filename = session_context_.onnx_model_path_name.parent_path();
96-
if (sw.external_weight_filename.empty() && !sw.metadata.empty()) {
96+
if (sw.external_weight_filename.empty()) {
9797
// Reasonable assumption that all metadata entries have the same external file location
9898
sw.external_weight_filename = sw.metadata.begin()->second.location;
9999
}
100100
weight_filename /= sw.external_weight_filename;
101101
std::ifstream weight_file(weight_filename);
102102

103-
if (weight_file) {
104-
if (!sw.mapped_weights) {
105-
sw.mapped_weights = std::make_unique<SharedContext::SharedWeights::WeightsFile>(weight_filename);
106-
}
107-
backend_utils::CreateOVTensors(session_context_.device_type, sw.metadata, *sw.mapped_weights);
103+
ORT_ENFORCE(weight_file, "Initializer file not found: ", weight_filename.string());
104+
if (!sw.mapped_weights) {
105+
sw.mapped_weights = std::make_unique<SharedContext::SharedWeights::WeightsFile>(weight_filename);
108106
}
107+
backend_utils::CreateOVTensors(session_context_.device_type, sw.metadata, *sw.mapped_weights);
109108
}
110109

111110
if (ModelHasSymbolicInputDims(subgraph)) {
@@ -196,7 +195,7 @@ BackendManager::BackendManager(SessionContext& session_context,
196195
}
197196
}
198197
if (session_context_.so_context_enable &&
199-
(subgraph_context_.is_ep_ctx_ovir_encapsulated || !subgraph_context_.is_ep_ctx_graph)) {
198+
(subgraph_context_.is_ep_ctx_ovir_encapsulated || !subgraph_context_.is_ep_ctx_graph)) {
200199
auto status = onnxruntime::openvino_ep::BackendManager::ExportCompiledBlobAsEPCtxNode(subgraph);
201200
if (!status.IsOK()) {
202201
ORT_THROW(status);

onnxruntime/core/providers/openvino/backends/basic_backend.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,24 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
7878
// specify absolute path for so_context_file_path.
7979
auto model_file_path = [this]() {
8080
if (!session_context_.onnx_model_path_name.empty() &&
81-
std::filesystem::exists(session_context_.onnx_model_path_name)) return session_context_.onnx_model_path_name;
81+
std::filesystem::exists(session_context_.onnx_model_path_name)) return session_context_.onnx_model_path_name;
8282

8383
ORT_ENFORCE(!session_context_.so_context_file_path.empty() &&
84-
std::filesystem::path(session_context_.so_context_file_path).is_absolute() &&
85-
std::filesystem::exists(session_context_.so_context_file_path), log_tag +
86-
"Context file path must be non-empty & absolute, when using CreateSessionFormArray() API explicitly."
87-
" Please set a valid absolute path for ep.context_file_path in session options.");
84+
std::filesystem::path(session_context_.so_context_file_path).is_absolute() &&
85+
std::filesystem::exists(session_context_.so_context_file_path),
86+
log_tag +
87+
"Context file path must be non-empty & absolute, when using CreateSessionFormArray() API explicitly."
88+
" Please set a valid absolute path for ep.context_file_path in session options.");
8889
// Return absolute context file path as input to ImportEPCtxOVIREncapsulation() function.
8990
return session_context_.so_context_file_path;
90-
9191
};
9292
// If the EPContext node with OVIR Encapsulation, then create
9393
// an executable network from EP_CACHE_CONTEXT using read_model() & compile_model()
9494
exe_network_ = OVCore::Get()->ImportEPCtxOVIREncapsulation(*model_stream,
95-
hw_target,
96-
device_config,
97-
enable_causallm,
98-
model_file_path());
95+
hw_target,
96+
device_config,
97+
enable_causallm,
98+
model_file_path());
9999
} else {
100100
// If the blob is held in an EPContext node, then skip FE+Compile
101101
// and directly move on to creating a backend with the executable blob

onnxruntime/core/providers/openvino/contexts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class SharedContext : public WeakSingleton<SharedContext> {
6464
fs::path external_weight_filename;
6565
std::unique_ptr<WeightsFile> mapped_weights;
6666
Metadata::Map metadata;
67+
fs::path metadata_filepath;
6768
} shared_weights;
6869
};
6970

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(const std::filesy
131131
// exported with must match the version that is currently running.
132132
ORT_ENFORCE((attrs.count(EP_SDK_VER) == 1) && (attrs.at(EP_SDK_VER).s() == openvino_sdk_version_),
133133
"EPCtx blob was exported / is compatible with OpenVINO SDK version " + attrs.at(EP_SDK_VER).s() +
134-
", but OpenVINO SDK version currently in use is " + openvino_sdk_version_);
134+
", but OpenVINO SDK version currently in use is " + openvino_sdk_version_);
135135
}
136136

137137
LOGS_DEFAULT(VERBOSE) << "[OpenVINO EP] Read blob from EPContext Node";

onnxruntime/core/providers/openvino/openvino_execution_provider.cc

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,24 @@ common::Status OpenVINOExecutionProvider::Compile(
102102
graph_body_viewer_0.DomainToVersionMap().at(kOnnxDomain);
103103
}
104104

105-
// Temporary code to read metadata before it moves to the .bin
106-
auto& metadata = shared_context_->shared_weights.metadata;
107-
if (session_context_.so_share_ep_contexts && metadata.empty()) {
108-
// Metadata is always read from model location, this could be a source or epctx model
109-
fs::path metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin";
110-
std::ifstream file(metadata_filename, std::ios::binary);
111-
if (file) {
112-
file >> metadata;
105+
// The block below is executed during EP context model inference
106+
auto& metadata = shared_context_->shared_weights.metadata; // Metadata object in memory
107+
if (session_context_.so_share_ep_contexts &&
108+
!session_context_.so_context_enable &&
109+
metadata.empty()) {
110+
fs::path context_model_file_path = session_context_.so_context_file_path;
111+
if (context_model_file_path.empty()) {
112+
// If ep.context_file_path is not set the input model path is used
113+
context_model_file_path = session_context_.onnx_model_path_name;
113114
}
115+
116+
// Metadata is always read from model location, this could be a source or epctx model
117+
fs::path metadata_filename = context_model_file_path.stem().string() + "_metadata.bin";
118+
fs::path metadata_file_path = context_model_file_path.parent_path() / metadata_filename;
119+
std::ifstream file(metadata_file_path, std::ios::binary);
120+
ORT_RETURN_IF_NOT(file, "Metadata file was not found: " + metadata_file_path.string());
121+
shared_context_->shared_weights.metadata_filepath = metadata_file_path;
122+
file >> metadata;
114123
}
115124

116125
struct OpenVINOEPFunctionState {
@@ -173,22 +182,29 @@ common::Status OpenVINOExecutionProvider::Compile(
173182
}
174183
}
175184

176-
if (session_context_.so_share_ep_contexts) {
177-
fs::path metadata_filename;
178-
if (session_context_.so_context_file_path.empty()) {
179-
metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin";
180-
} else {
181-
metadata_filename = session_context_.so_context_file_path.parent_path() / "metadata.bin";
185+
// The block below is executed during EP context model generation
186+
if (session_context_.so_context_enable &&
187+
session_context_.so_share_ep_contexts &&
188+
!metadata.empty()) {
189+
// For models after the first the metadata name comes from the shared context
190+
fs::path metadata_file_path = shared_context_->shared_weights.metadata_filepath;
191+
if (metadata_file_path.empty()) {
192+
metadata_file_path = session_context_.so_context_file_path;
193+
if (metadata_file_path.empty()) {
194+
metadata_file_path = session_context_.onnx_model_path_name;
195+
}
196+
auto metadata_filename = metadata_file_path.stem().string() + "_metadata.bin";
197+
metadata_file_path.replace_filename(metadata_filename);
198+
shared_context_->shared_weights.metadata_filepath = metadata_file_path;
182199
}
183200

184201
// Metadata is generated only for shared contexts
185-
// If saving metadata then save it to the provided path or ose the original model path
202+
// If saving metadata then save it to the provided path or use the original model path
186203
// Multiple calls to Compile() will update the metadata and for the last call
187204
// the resulting file will contain the aggregated content
188-
std::ofstream file(metadata_filename, std::ios::binary);
189-
if (file) {
190-
file << metadata;
191-
}
205+
std::ofstream file{metadata_file_path, std::ios::binary};
206+
ORT_RETURN_IF_NOT(file, "Metadata file could not be written: ", metadata_file_path);
207+
file << metadata;
192208
}
193209

194210
return status;

0 commit comments

Comments
 (0)