Skip to content

Commit aa93078

Browse files
Merge branch 'ovep-develop' into preetha/get_capability
2 parents 8fc21b1 + 866a24c commit aa93078

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
5959
};
6060
// If the EPContext node with OVIR Encapsulation, then create
6161
// an executable network from EP_CACHE_CONTEXT using read_model() & compile_model()
62-
exe_network_ = OVCore::Get()->ImportEPCtxOVIREncapsulation(*model_stream,
62+
exe_network_ = OVCore::Get()->ImportEPCtxOVIREncapsulation(*model_stream->stream_,
6363
hw_target,
6464
device_config,
6565
enable_causallm,

onnxruntime/core/providers/openvino/ibackend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IBackend {
1919
virtual ~IBackend() = default;
2020
virtual void RewindKVCache(size_t index) {}
2121
};
22-
using ptr_stream_t = std::unique_ptr<std::istream>;
22+
using ptr_stream_t = std::unique_ptr<ModelBlobWrapper>;
2323
class BackendFactory {
2424
public:
2525
static std::shared_ptr<IBackend>

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ Status EPCtxHandler::AddOVEPCtxNodeToGraph(const GraphViewer& graph_viewer,
100100
return Status::OK();
101101
}
102102

103-
std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const {
103+
std::unique_ptr<ModelBlobWrapper>
104+
EPCtxHandler::GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const {
104105
auto first_index = *graph_viewer.GetNodesInTopologicalOrder().begin();
105106
auto node = graph_viewer.GetNode(first_index);
106107
ORT_ENFORCE(node != nullptr);
@@ -113,10 +114,11 @@ std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(const std::filesy
113114
bool embed_mode = static_cast<bool>(attrs.at(EMBED_MODE).i());
114115

115116
std::unique_ptr<std::istream> result;
117+
std::filesystem::path blob_filepath{};
116118
if (embed_mode) {
117119
result.reset((std::istream*)new std::istringstream(ep_cache_context));
118120
} else {
119-
auto blob_filepath = so_context_file_path;
121+
blob_filepath = so_context_file_path;
120122
if (blob_filepath.empty() && !graph_viewer.ModelPath().empty()) {
121123
blob_filepath = graph_viewer.ModelPath();
122124
}
@@ -126,16 +128,18 @@ std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(const std::filesy
126128
}
127129

128130
bool isXML = backend_utils::IsModelStreamXML(*result);
131+
std::filesystem::path native_blob_path{};
129132
if (!isXML) {
130133
// If the model stream is not an XML (i.e. precompiled blob), the OpenVINO SDK version that it was
131134
// exported with must match the version that is currently running.
135+
native_blob_path = std::move(blob_filepath);
132136
ORT_ENFORCE((attrs.count(EP_SDK_VER) == 1) && (attrs.at(EP_SDK_VER).s() == openvino_sdk_version_),
133137
"EPCtx blob was exported / is compatible with OpenVINO SDK version " + attrs.at(EP_SDK_VER).s() +
134138
", but OpenVINO SDK version currently in use is " + openvino_sdk_version_);
135139
}
136140

137141
LOGS_DEFAULT(VERBOSE) << "[OpenVINO EP] Read blob from EPContext Node";
138-
return result;
142+
return std::make_unique<ModelBlobWrapper>(std::move(result), native_blob_path);
139143
}
140144

141145
bool EPCtxHandler::CheckForOVEPCtxNodeInGraph(const GraphViewer& graph_viewer) const {

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
namespace onnxruntime {
1313
namespace openvino_ep {
1414

15+
struct ModelBlobWrapper {
16+
ModelBlobWrapper(std::unique_ptr<std::istream> stream, const std::filesystem::path& native_blob_path) : stream_(std::move(stream)), maybe_native_blob_path_(native_blob_path) {}
17+
std::unique_ptr<std::istream> stream_;
18+
std::filesystem::path maybe_native_blob_path_;
19+
};
20+
1521
// Utilities to handle EPContext node export and parsing of an EPContext node
1622
// to create the compiled_model object to infer on
1723
static const char EPCONTEXT_OP[] = "EPContext";
@@ -31,7 +37,7 @@ class EPCtxHandler {
3137
const std::string& graph_name,
3238
const bool embed_mode,
3339
std::string&& model_blob_str) const;
34-
std::unique_ptr<std::istream> GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const;
40+
std::unique_ptr<ModelBlobWrapper> GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const;
3541
InlinedVector<const Node*> GetEPCtxNodes() const;
3642
bool CheckEPCacheContextAttribute(const GraphViewer& graph_viewer, const std::string& target_attr_extn) const;
3743

onnxruntime/core/providers/openvino/ov_interface.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "core/providers/openvino/backend_utils.h"
1212
#include "core/providers/openvino/backends/basic_backend.h"
1313
#include "core/providers/openvino/ov_stateful_patch_utils.h"
14+
#include "core/providers/openvino/onnx_ctx_model_helper.h"
1415

1516
namespace onnxruntime {
1617
namespace openvino_ep {
@@ -191,14 +192,23 @@ OVExeNetwork OVCore::CompileModel(const std::string& onnx_model,
191192
"Exception while Loading Network for graph {}", name);
192193
}
193194

194-
OVExeNetwork OVCore::ImportModel(std::istream& model_stream,
195+
OVExeNetwork OVCore::ImportModel(ModelBlobWrapper& model_blob,
195196
std::string hw_target,
196197
const ov::AnyMap& device_config,
197198
std::string name) {
198199
return OvExceptionBoundary([&]() {
199200
ov::CompiledModel obj;
200-
obj = core.import_model(model_stream, hw_target, device_config);
201+
#if (OPENVINO_VERSION_MAJOR > 2025 || (OPENVINO_VERSION_MAJOR == 2025 && OPENVINO_VERSION_MINOR >= 3))
202+
if (!model_blob.maybe_native_blob_path_.empty()) {
203+
obj = core.import_model(ov::read_tensor_data(model_blob.maybe_native_blob_path_), hw_target, device_config);
204+
} else {
205+
obj = core.import_model(*model_blob.stream_, hw_target, device_config);
206+
}
207+
#else
208+
obj = core.import_model(*model_blob.stream_, hw_target, device_config);
209+
#endif
201210
OVExeNetwork exe(obj, hw_target);
211+
202212
#ifndef NDEBUG
203213
printDebugInfo(exe.Get());
204214
#endif

onnxruntime/core/providers/openvino/ov_interface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace openvino_ep {
2626
class OVCore;
2727
class OVInferRequest;
2828
class OVExeNetwork;
29+
struct ModelBlobWrapper;
2930

3031
typedef ov::Tensor OVTensor;
3132
typedef ov::ProfilingInfo OVProfilingInfo;
@@ -82,7 +83,7 @@ struct OVCore : WeakSingleton<OVCore> {
8283
ov::AnyMap& device_config,
8384
const std::string& name);
8485
// OV Interface for Import model Stream
85-
OVExeNetwork ImportModel(std::istream& model_stream,
86+
OVExeNetwork ImportModel(ModelBlobWrapper& model_blob,
8687
std::string hw_target,
8788
const ov::AnyMap& device_config,
8889
std::string name);

0 commit comments

Comments
 (0)