@@ -102,12 +102,13 @@ common::Status OpenVINOExecutionProvider::Compile(
102102 graph_body_viewer_0.DomainToVersionMap ().at (kOnnxDomain );
103103 }
104104
105+ const auto metadata_path = session_context_.GetEpContextOutputDirectory () / " metadata.bin" ;
106+
105107 // Temporary code to read metadata before it moves to the .bin
106108 auto & metadata = shared_context_->shared_weights .metadata ;
107109 if (session_context_.so_share_ep_contexts && metadata.empty ()) {
108110 // 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);
111112 if (file) {
112113 file >> metadata;
113114 }
@@ -173,21 +174,33 @@ common::Status OpenVINOExecutionProvider::Compile(
173174 }
174175 }
175176
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+ }
180189 } 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. " ;
182191 }
192+ }
183193
194+ if (session_context_.so_share_ep_contexts ) {
184195 // 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
186197 // Multiple calls to Compile() will update the metadata and for the last call
187198 // 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);
189200 if (file) {
190201 file << metadata;
202+ } else {
203+ LOGS_DEFAULT (WARNING) << " Failed to write metadata to file: " << metadata_path.string ();
191204 }
192205 }
193206
0 commit comments