@@ -102,12 +102,13 @@ common::Status OpenVINOExecutionProvider::Compile(
102
102
graph_body_viewer_0.DomainToVersionMap ().at (kOnnxDomain );
103
103
}
104
104
105
+ const auto metadata_path = session_context_.GetEpContextOutputDirectory () / " metadata.bin" ;
106
+
105
107
// Temporary code to read metadata before it moves to the .bin
106
108
auto & metadata = shared_context_->shared_weights .metadata ;
107
109
if (session_context_.so_share_ep_contexts && metadata.empty ()) {
108
110
// 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
+ std::ifstream file (metadata_path, std::ios::binary);
111
112
if (file) {
112
113
file >> metadata;
113
114
}
@@ -173,21 +174,33 @@ common::Status OpenVINOExecutionProvider::Compile(
173
174
}
174
175
}
175
176
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" ;
177
+ if (session_context_.so_context_enable && session_context_.has_external_weights ) {
178
+ const auto & sw_path_filename = shared_context_->shared_weights .external_weight_filename ;
179
+ fs::path new_weights_file_path = session_context_.GetNewWeightsFilePath (sw_path_filename);
180
+ fs::path original_weights_path = session_context_.GetModelDirectory () / sw_path_filename;
181
+
182
+ if (!std::filesystem::exists (new_weights_file_path)) {
183
+ try {
184
+ std::filesystem::create_hard_link (original_weights_path, new_weights_file_path);
185
+ } catch (const std::filesystem::filesystem_error& e) {
186
+ LOGS_DEFAULT (WARNING) << " Failed to create hard link: " << e.what () << " Falling back to copy." ;
187
+ std::filesystem::copy_file (original_weights_path, new_weights_file_path);
188
+ }
180
189
} else {
181
- metadata_filename = session_context_. so_context_file_path . parent_path () / " metadata.bin " ;
190
+ LOGS_DEFAULT (WARNING) << " Weights file already exists: " << new_weights_file_path. string () << " Link/Copy. " ;
182
191
}
192
+ }
183
193
194
+ if (session_context_.so_share_ep_contexts ) {
184
195
// Metadata is generated only for shared contexts
185
- // If saving metadata then save it to the provided path or ose the original model path
196
+ // If saving metadata then save it to the provided path or use the original model path
186
197
// Multiple calls to Compile() will update the metadata and for the last call
187
198
// the resulting file will contain the aggregated content
188
- std::ofstream file (metadata_filename , std::ios::binary);
199
+ std::ofstream file (metadata_path , std::ios::binary);
189
200
if (file) {
190
201
file << metadata;
202
+ } else {
203
+ LOGS_DEFAULT (WARNING) << " Failed to write metadata to file: " << metadata_path.string ();
191
204
}
192
205
}
193
206
0 commit comments