-
Notifications
You must be signed in to change notification settings - Fork 38
Copy weights file to epctx output directory #648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ovep-develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,12 +102,13 @@ common::Status OpenVINOExecutionProvider::Compile( | |
graph_body_viewer_0.DomainToVersionMap().at(kOnnxDomain); | ||
} | ||
|
||
const auto metadata_path = session_context_.GetEpContextOutputDirectory() / "metadata.bin"; | ||
|
||
// Temporary code to read metadata before it moves to the .bin | ||
auto& metadata = shared_context_->shared_weights.metadata; | ||
if (session_context_.so_share_ep_contexts && metadata.empty()) { | ||
// Metadata is always read from model location, this could be a source or epctx model | ||
fs::path metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin"; | ||
std::ifstream file(metadata_filename, std::ios::binary); | ||
std::ifstream file(metadata_path, std::ios::binary); | ||
if (file) { | ||
file >> metadata; | ||
} | ||
|
@@ -174,20 +175,30 @@ common::Status OpenVINOExecutionProvider::Compile( | |
} | ||
|
||
if (session_context_.so_share_ep_contexts) { | ||
fs::path metadata_filename; | ||
if (session_context_.so_context_file_path.empty()) { | ||
metadata_filename = session_context_.onnx_model_path_name.parent_path() / "metadata.bin"; | ||
const auto& sw_path_filename = shared_context_->shared_weights.external_weight_filename; | ||
fs::path new_weights_file_path = session_context_.GetNewWeightsFilePath(sw_path_filename); | ||
fs::path original_weights_path = session_context_.GetModelDirectory() / sw_path_filename; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not quite right. There's an ORT option for setting the weights path when for whatever reason the relative path between model and weight was altered from when the model was generated. For example if an initializer in the model has location = "weights.bin" then the file weights.bin is expected to be in the same folder as the model. If the weights are instead in "path\weights.bin" then session option In summary this needs a fs::path GetWeightsDirectory() const {
return so_context_model_external_initializers_file_name.empty() ? GetModelDirectory() : so_so_context_model_external_initializers_file_name;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on yesterday's discussion we're not going to deal with |
||
|
||
if (!std::filesystem::exists(new_weights_file_path)) { | ||
try { | ||
std::filesystem::create_hard_link(original_weights_path, new_weights_file_path); | ||
} catch (const std::filesystem::filesystem_error& e) { | ||
LOGS_DEFAULT(WARNING) << "Failed to create hard link: " << e.what() << " Falling back to copy."; | ||
std::filesystem::copy_file(original_weights_path, new_weights_file_path); | ||
} | ||
} else { | ||
metadata_filename = session_context_.so_context_file_path.parent_path() / "metadata.bin"; | ||
LOGS_DEFAULT(WARNING) << "Weights file already exists: " << new_weights_file_path.string() << " Link/Copy."; | ||
} | ||
|
||
// Metadata is generated only for shared contexts | ||
// If saving metadata then save it to the provided path or ose the original model path | ||
// If saving metadata then save it to the provided path or use the original model path | ||
// Multiple calls to Compile() will update the metadata and for the last call | ||
// the resulting file will contain the aggregated content | ||
std::ofstream file(metadata_filename, std::ios::binary); | ||
std::ofstream file(metadata_path, std::ios::binary); | ||
if (file) { | ||
file << metadata; | ||
} else { | ||
LOGS_DEFAULT(WARNING) << "Failed to write metadata to file: " << metadata_path.string(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as below