Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions onnxruntime/core/providers/openvino/backend_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BackendManager::BackendManager(SessionContext& session_context,
subgraph_context_.is_ep_ctx_graph = ep_ctx_handle_.CheckForOVEPCtxNodeInGraph(subgraph);
// If the graph contains a OVIR wrapped node, we check if it has matching xml file name attribute
subgraph_context_.is_ep_ctx_ovir_encapsulated = ep_ctx_handle_.CheckEPCacheContextAttribute(subgraph,
session_context_.onnx_model_path_name.filename().replace_extension("xml").string());
session_context_.onnx_model_path_name.filename().replace_extension("xml").string());

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

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

if (weight_file) {
if (!sw.mapped_weights) {
sw.mapped_weights = std::make_unique<SharedContext::SharedWeights::WeightsFile>(weight_filename);
}
backend_utils::CreateOVTensors(session_context_.device_type, sw.metadata, *sw.mapped_weights);
ORT_ENFORCE(weight_file, "Initializer file not found: ", weight_filename.string());
if (!sw.mapped_weights) {
sw.mapped_weights = std::make_unique<SharedContext::SharedWeights::WeightsFile>(weight_filename);
}
backend_utils::CreateOVTensors(session_context_.device_type, sw.metadata, *sw.mapped_weights);
}

if (ModelHasSymbolicInputDims(subgraph)) {
Expand Down Expand Up @@ -196,7 +195,7 @@ BackendManager::BackendManager(SessionContext& session_context,
}
}
if (session_context_.so_context_enable &&
(subgraph_context_.is_ep_ctx_ovir_encapsulated || !subgraph_context_.is_ep_ctx_graph)) {
(subgraph_context_.is_ep_ctx_ovir_encapsulated || !subgraph_context_.is_ep_ctx_graph)) {
auto status = onnxruntime::openvino_ep::BackendManager::ExportCompiledBlobAsEPCtxNode(subgraph);
if (!status.IsOK()) {
ORT_THROW(status);
Expand Down
20 changes: 10 additions & 10 deletions onnxruntime/core/providers/openvino/backends/basic_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,24 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
// specify absolute path for so_context_file_path.
auto model_file_path = [this]() {
if (!session_context_.onnx_model_path_name.empty() &&
std::filesystem::exists(session_context_.onnx_model_path_name)) return session_context_.onnx_model_path_name;
std::filesystem::exists(session_context_.onnx_model_path_name)) return session_context_.onnx_model_path_name;

ORT_ENFORCE(!session_context_.so_context_file_path.empty() &&
std::filesystem::path(session_context_.so_context_file_path).is_absolute() &&
std::filesystem::exists(session_context_.so_context_file_path), log_tag +
"Context file path must be non-empty & absolute, when using CreateSessionFormArray() API explicitly."
" Please set a valid absolute path for ep.context_file_path in session options.");
std::filesystem::path(session_context_.so_context_file_path).is_absolute() &&
std::filesystem::exists(session_context_.so_context_file_path),
log_tag +
"Context file path must be non-empty & absolute, when using CreateSessionFormArray() API explicitly."
" Please set a valid absolute path for ep.context_file_path in session options.");
// Return absolute context file path as input to ImportEPCtxOVIREncapsulation() function.
return session_context_.so_context_file_path;

};
// If the EPContext node with OVIR Encapsulation, then create
// an executable network from EP_CACHE_CONTEXT using read_model() & compile_model()
exe_network_ = OVCore::Get()->ImportEPCtxOVIREncapsulation(*model_stream,
hw_target,
device_config,
enable_causallm,
model_file_path());
hw_target,
device_config,
enable_causallm,
model_file_path());
} else {
// If the blob is held in an EPContext node, then skip FE+Compile
// and directly move on to creating a backend with the executable blob
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/core/providers/openvino/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class SharedContext : public WeakSingleton<SharedContext> {
fs::path external_weight_filename;
std::unique_ptr<WeightsFile> mapped_weights;
Metadata::Map metadata;
fs::path metadata_filepath;
} shared_weights;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(const std::filesy
// exported with must match the version that is currently running.
ORT_ENFORCE((attrs.count(EP_SDK_VER) == 1) && (attrs.at(EP_SDK_VER).s() == openvino_sdk_version_),
"EPCtx blob was exported / is compatible with OpenVINO SDK version " + attrs.at(EP_SDK_VER).s() +
", but OpenVINO SDK version currently in use is " + openvino_sdk_version_);
", but OpenVINO SDK version currently in use is " + openvino_sdk_version_);
}

LOGS_DEFAULT(VERBOSE) << "[OpenVINO EP] Read blob from EPContext Node";
Expand Down
54 changes: 35 additions & 19 deletions onnxruntime/core/providers/openvino/openvino_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,24 @@ common::Status OpenVINOExecutionProvider::Compile(
graph_body_viewer_0.DomainToVersionMap().at(kOnnxDomain);
}

// 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);
if (file) {
file >> metadata;
// The block below is executed during EP context model inference
auto& metadata = shared_context_->shared_weights.metadata; // Metadata object in memory
if (session_context_.so_share_ep_contexts &&
!session_context_.so_context_enable &&
metadata.empty()) {
fs::path context_model_file_path = session_context_.so_context_file_path;
if (context_model_file_path.empty()) {
// If ep.context_file_path is not set the input model path is used
context_model_file_path = session_context_.onnx_model_path_name;
}

// Metadata is always read from model location, this could be a source or epctx model
fs::path metadata_filename = context_model_file_path.stem().string() + "_metadata.bin";
fs::path metadata_file_path = context_model_file_path.parent_path() / metadata_filename;
std::ifstream file(metadata_file_path, std::ios::binary);
ORT_RETURN_IF_NOT(file, "Metadata file was not found: " + metadata_file_path.string());
shared_context_->shared_weights.metadata_filepath = metadata_file_path;
file >> metadata;
}

struct OpenVINOEPFunctionState {
Expand Down Expand Up @@ -173,22 +182,29 @@ 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";
} else {
metadata_filename = session_context_.so_context_file_path.parent_path() / "metadata.bin";
// The block below is executed during EP context model generation
if (session_context_.so_context_enable &&
session_context_.so_share_ep_contexts &&
!metadata.empty()) {
// For models after the first the metadata name comes from the shared context
fs::path metadata_file_path = shared_context_->shared_weights.metadata_filepath;
if (metadata_file_path.empty()) {
metadata_file_path = session_context_.so_context_file_path;
if (metadata_file_path.empty()) {
metadata_file_path = session_context_.onnx_model_path_name;
}
auto metadata_filename = metadata_file_path.stem().string() + "_metadata.bin";
metadata_file_path.replace_filename(metadata_filename);
shared_context_->shared_weights.metadata_filepath = metadata_file_path;
}

// 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);
if (file) {
file << metadata;
}
std::ofstream file{metadata_file_path, std::ios::binary};
ORT_RETURN_IF_NOT(file, "Metadata file could not be written: ", metadata_file_path);
file << metadata;
}

return status;
Expand Down
Loading
Loading