Skip to content

Commit 25700bb

Browse files
Revert "feat: Enable EpContext OVIR Encapsulation (#704)"
This reverts commit 6d04a2e.
1 parent 6d04a2e commit 25700bb

File tree

9 files changed

+12
-169
lines changed

9 files changed

+12
-169
lines changed

onnxruntime/core/providers/openvino/backend_manager.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ BackendManager::BackendManager(SessionContext& session_context,
4343
session_context_(session_context),
4444
shared_context_{shared_context} {
4545
subgraph_context_.is_ep_ctx_graph = ep_ctx_handle_.CheckForOVEPCtxNodeInGraph(subgraph);
46-
// If the graph contains a OVIR wrapped node, we check if it has matching xml file name attribute
47-
subgraph_context_.is_ep_ctx_ovir_encapsulated = ep_ctx_handle_.CheckEPCacheContextAttribute(subgraph,
48-
session_context_.onnx_model_path_name.filename().replace_extension("xml").string());
4946

5047
subgraph_context_.model_precision = [&](const GraphViewer& graph_viewer) {
5148
// return empty if graph has no inputs or if types are not one of FP32/FP16
@@ -195,10 +192,9 @@ BackendManager::BackendManager(SessionContext& session_context,
195192
}
196193
}
197194
}
198-
if (session_context_.so_context_enable &&
199-
(subgraph_context_.is_ep_ctx_ovir_encapsulated || !subgraph_context_.is_ep_ctx_graph)) {
195+
if (session_context_.so_context_enable && !subgraph_context_.is_ep_ctx_graph) {
200196
auto status = onnxruntime::openvino_ep::BackendManager::ExportCompiledBlobAsEPCtxNode(subgraph);
201-
if (!status.IsOK()) {
197+
if ((!status.IsOK())) {
202198
ORT_THROW(status);
203199
}
204200
}

onnxruntime/core/providers/openvino/backend_utils.cc

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -400,33 +400,6 @@ void DestroyOVTensors(SharedContext::SharedWeights::Metadata::Map& metadata_map)
400400
metadata_map.clear();
401401
}
402402

403-
bool IsModelStreamXML(std::istream& model_stream) {
404-
std::streampos originalPos = model_stream.tellg();
405-
406-
// first, get the total size of model_stream in bytes
407-
model_stream.seekg(0, std::ios::end);
408-
auto end_pos = model_stream.tellg();
409-
// Restore the stream position
410-
model_stream.seekg(originalPos);
411-
auto total_size = end_pos - originalPos;
412-
413-
// Choose 32 bytes to hold content of:
414-
// '<?xml version-"1.0"?> <net '
415-
const std::streamsize header_check_len = 32;
416-
ORT_ENFORCE(total_size > header_check_len);
417-
418-
// read 32 bytes into header
419-
std::string header(header_check_len, '\0');
420-
model_stream.read(&header[0], header_check_len);
421-
// Clear any read errors
422-
model_stream.clear();
423-
// Restore the stream position
424-
model_stream.seekg(originalPos);
425-
426-
// return true if the header starts with '<?xml' and also includes '<net '
427-
return ((header.rfind("<?xml", 0) == 0) && (header.find("<net ") != std::string::npos));
428-
}
429-
430403
} // namespace backend_utils
431404
} // namespace openvino_ep
432405
} // namespace onnxruntime

onnxruntime/core/providers/openvino/backend_utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ void printPerformanceCounts(const std::vector<OVProfilingInfo>& performanceMap,
107107

108108
void printPerformanceCounts(OVInferRequestPtr request, std::ostream& stream, std::string deviceName);
109109

110-
bool IsModelStreamXML(std::istream& model_stream);
111-
112110
} // namespace backend_utils
113111
} // namespace openvino_ep
114112
} // namespace onnxruntime

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,12 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
7272
!session_context_.so_disable_cpu_ep_fallback &&
7373
!subgraph_context_.is_ep_ctx_graph);
7474
if (subgraph_context_.is_ep_ctx_graph) {
75-
if (subgraph_context_.is_ep_ctx_ovir_encapsulated) {
76-
// model_file_path will use so_context_file_path if the onnx_model_path_name is not available,
77-
// especially in case of CreateSessionFormArray() where user must explicitly
78-
// specify absolute path for so_context_file_path.
79-
auto model_file_path = [this]() {
80-
if (!session_context_.onnx_model_path_name.empty() &&
81-
std::filesystem::exists(session_context_.onnx_model_path_name)) return session_context_.onnx_model_path_name;
82-
83-
ORT_ENFORCE(!session_context_.so_context_file_path.empty() &&
84-
std::filesystem::path(session_context_.so_context_file_path).is_absolute() &&
85-
std::filesystem::exists(session_context_.so_context_file_path), log_tag +
86-
"Context file path must be non-empty & absolute, when using CreateSessionFormArray() API explicitly."
87-
" Please set a valid absolute path for ep.context_file_path in session options.");
88-
// Return absolute context file path as input to ImportEPCtxOVIREncapsulation() function.
89-
return session_context_.so_context_file_path;
90-
91-
};
92-
// If the EPContext node with OVIR Encapsulation, then create
93-
// an executable network from EP_CACHE_CONTEXT using read_model() & compile_model()
94-
exe_network_ = OVCore::Get()->ImportEPCtxOVIREncapsulation(*model_stream,
95-
hw_target,
96-
device_config,
97-
enable_causallm,
98-
model_file_path());
99-
} else {
100-
// If the blob is held in an EPContext node, then skip FE+Compile
101-
// and directly move on to creating a backend with the executable blob
102-
exe_network_ = OVCore::Get()->ImportModel(*model_stream,
103-
hw_target,
104-
device_config,
105-
subgraph_context_.subgraph_name);
106-
}
75+
// If the blob is held in an EPContext node, then skip FE+Compile
76+
// and directly move on to creating a backend with the executable blob
77+
exe_network_ = OVCore::Get()->ImportModel(*model_stream,
78+
hw_target,
79+
device_config,
80+
subgraph_context_.subgraph_name);
10781
model_stream.reset(); // Delete stream after it is no longer needed
10882
} else if (!session_context_.has_external_weights &&
10983
!subgraph_context_.has_dynamic_input_shape &&

onnxruntime/core/providers/openvino/contexts.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ struct SubGraphContext {
137137
string_index_map_t output_names;
138138
std::string model_precision;
139139
bool is_ep_ctx_graph = false;
140-
bool is_ep_ctx_ovir_encapsulated = false;
141140
};
142141

143142
} // namespace openvino_ep

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.cc

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <algorithm>
88

99
#include "core/providers/openvino/onnx_ctx_model_helper.h"
10-
#include "core/providers/openvino/backend_utils.h"
1110

1211
namespace onnxruntime {
1312
namespace openvino_ep {
@@ -124,16 +123,6 @@ std::unique_ptr<std::istream> EPCtxHandler::GetModelBlobStream(const std::filesy
124123
ORT_ENFORCE(std::filesystem::exists(blob_filepath), "Blob file not found: ", blob_filepath.string());
125124
result.reset((std::istream*)new std::ifstream(blob_filepath, std::ios_base::binary | std::ios_base::in));
126125
}
127-
128-
bool isXML = backend_utils::IsModelStreamXML(*result);
129-
if (!isXML) {
130-
// If the model stream is not an XML (i.e. precompiled blob), the OpenVINO SDK version that it was
131-
// exported with must match the version that is currently running.
132-
ORT_ENFORCE((attrs.count(EP_SDK_VER) == 1) && (attrs.at(EP_SDK_VER).s() == openvino_sdk_version_),
133-
"EPCtx blob was exported / is compatible with OpenVINO SDK version " + attrs.at(EP_SDK_VER).s() +
134-
", but OpenVINO SDK version currently in use is " + openvino_sdk_version_);
135-
}
136-
137126
LOGS_DEFAULT(VERBOSE) << "[OpenVINO EP] Read blob from EPContext Node";
138127
return result;
139128
}
@@ -153,6 +142,7 @@ bool EPCtxHandler::CheckForOVEPCtxNode(const Node& node) const {
153142
if (node.OpType() == EPCONTEXT_OP) {
154143
auto& attrs = node.GetAttributes();
155144
bool result = (attrs.count(SOURCE) == 1) && (attrs.at(SOURCE).s() == kOpenVINOExecutionProvider);
145+
result &= (attrs.count(EP_SDK_VER) == 1) && (attrs.at(EP_SDK_VER).s() == openvino_sdk_version_);
156146
result &= attrs.count(EMBED_MODE) == 1;
157147
result &= attrs.count(EP_CACHE_CONTEXT) == 1;
158148
return result;
@@ -165,32 +155,5 @@ InlinedVector<const Node*> EPCtxHandler::GetEPCtxNodes() const {
165155
return InlinedVector<const Node*>(epctx_nodes.begin(), epctx_nodes.end());
166156
}
167157

168-
// Check if graph's only node is EPContext & EP_CACHE_CONTEXT attribute has target extension.
169-
// @param graph_viewer: The graph to inspect.
170-
// @param target_attr_extn: The string to search for in the EP_CACHE_CONTEXT attribute.
171-
// @return true if the node exists, is of the correct type, and the attribute contains the extension; false otherwise.
172-
bool EPCtxHandler::CheckEPCacheContextAttribute(const GraphViewer& graph_viewer, const std::string& target_attr_extn) const {
173-
// Only check if the graph has exactly one node
174-
if (graph_viewer.NumberOfNodes() != 1) {
175-
return false;
176-
}
177-
// Get the first node in topological order
178-
auto first_index = *graph_viewer.GetNodesInTopologicalOrder().begin();
179-
const Node* node = graph_viewer.GetNode(first_index);
180-
if (!node) {
181-
return false;
182-
}
183-
// Check OpType and required attributes
184-
if (node->OpType() != EPCONTEXT_OP) {
185-
return false;
186-
}
187-
const auto& attrs = node->GetAttributes();
188-
auto it = attrs.find(EP_CACHE_CONTEXT);
189-
if (it != attrs.end()) {
190-
return it->second().s().find(target_attr_extn) != std::string::npos;
191-
}
192-
return false;
193-
}
194-
195158
} // namespace openvino_ep
196159
} // namespace onnxruntime

onnxruntime/core/providers/openvino/onnx_ctx_model_helper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class EPCtxHandler {
3333
std::string&& model_blob_str) const;
3434
std::unique_ptr<std::istream> GetModelBlobStream(const std::filesystem::path& so_context_file_path, const GraphViewer& graph_viewer) const;
3535
InlinedVector<const Node*> GetEPCtxNodes() const;
36-
bool CheckEPCacheContextAttribute(const GraphViewer& graph_viewer, const std::string& target_attr_extn) const;
3736

3837
private:
3938
const std::string openvino_sdk_version_;

onnxruntime/core/providers/openvino/ov_interface.cc

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void printDebugInfo(const ov::CompiledModel& obj) {
4747
continue;
4848
OPENVINO_SUPPRESS_DEPRECATED_END
4949
std::cout << " " << item2.first << ": " << item2.second.as<std::string>() << std::endl;
50+
}
5051
}
5152
} else {
5253
std::cout << " " << cfg << ": " << prop.as<std::string>() << std::endl;
@@ -100,10 +101,10 @@ OVExeNetwork OVCore::StatefulCompileModel(std::shared_ptr<OVNetwork>& model,
100101
LogBasicModelInfo(model);
101102
}
102103

104+
LOGS_DEFAULT(INFO) << log_tag << "Converting from Stateless OV Model to Stateful OV Model" << std::endl;
103105
bool model_status = IsStateful(model);
104106
LOGS_DEFAULT(INFO) << log_tag << "Model IsStateful() Status:\t" << (model_status ? "True" : "False");
105107
if (!model_status) {
106-
LOGS_DEFAULT(INFO) << log_tag << "Converting from Stateless OV Model to Stateful OV Model" << std::endl;
107108
PatchStatefulDecoder(model);
108109
}
109110

@@ -197,69 +198,15 @@ OVExeNetwork OVCore::ImportModel(std::istream& model_stream,
197198
return OvExceptionBoundary([&]() {
198199
ov::CompiledModel obj;
199200
obj = core.import_model(model_stream, hw_target, device_config);
200-
OVExeNetwork exe(obj, hw_target);
201201
#ifndef NDEBUG
202202
printDebugInfo(exe.Get());
203203
#endif
204+
OVExeNetwork exe(obj, hw_target);
204205
return exe;
205206
},
206207
"Exception while Loading Network for graph {}", name);
207208
}
208209

209-
OVExeNetwork OVCore::ImportEPCtxOVIREncapsulation(std::istream& model_stream,
210-
std::string& hw_target,
211-
const ov::AnyMap& device_config,
212-
bool enable_causallm,
213-
std::filesystem::path model_file_path) {
214-
return OvExceptionBoundary([&]() {
215-
OVExeNetwork exe;
216-
217-
bool isXML = backend_utils::IsModelStreamXML(model_stream);
218-
219-
// Helper function to check if file exists and is readable
220-
const auto check_file_access = [&model_file_path](const std::filesystem::path& path) {
221-
try {
222-
if (!std::filesystem::exists(path) || std::filesystem::is_empty(path)) {
223-
ORT_THROW(log_tag + "Required file missing or empty: " + path.string());
224-
}
225-
std::ifstream file(path);
226-
if (!file) {
227-
ORT_THROW(log_tag + "Required file not readable: " + path.string());
228-
}
229-
} catch (const std::exception& e) {
230-
ORT_THROW(log_tag + "Exception while checking file access for: " + path.string() + " - " + e.what());
231-
}
232-
};
233-
234-
if (isXML) {
235-
// If the model is XML, we need to load it with the XML content in read_model()
236-
// where weights from bin file is directly consumed
237-
auto xml_file_path = model_file_path.parent_path() / (model_file_path.stem().string() + ".xml");
238-
239-
check_file_access(xml_file_path);
240-
241-
LOGS_DEFAULT(INFO) << log_tag << "Reading OVIR from XML file path: " << xml_file_path.string();
242-
243-
// Load the model explicitly with XML contents
244-
std::shared_ptr<ov::Model> model = core.read_model(xml_file_path.string());
245-
246-
if (enable_causallm) {
247-
exe = OVCore::Get()->StatefulCompileModel(model, hw_target, device_config);
248-
} else {
249-
auto obj = core.compile_model(model, hw_target, device_config);
250-
exe = OVExeNetwork(obj, hw_target);
251-
}
252-
}
253-
254-
#ifndef NDEBUG
255-
printDebugInfo(exe.Get());
256-
#endif
257-
return exe;
258-
},
259-
"Exception while Loading Network from OVIR model file: {}", model_file_path.string());
260-
}
261-
262-
263210
void OVCore::SetCache(const std::string& cache_dir_path) {
264211
core.set_property(ov::cache_dir(cache_dir_path));
265212
}

onnxruntime/core/providers/openvino/ov_interface.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ struct OVCore : WeakSingleton<OVCore> {
8686
std::string hw_target,
8787
const ov::AnyMap& device_config,
8888
std::string name);
89-
OVExeNetwork ImportEPCtxOVIREncapsulation(std::istream& model_stream,
90-
std::string& hw_target,
91-
const ov::AnyMap& device_config,
92-
bool enable_causallm,
93-
std::filesystem::path model_file_path);
94-
9589
std::vector<std::string> GetAvailableDevices() const;
9690
std::vector<std::string> GetAvailableDevices(const std::string& device_type) const;
9791
void SetCache(const std::string& cache_dir_path);

0 commit comments

Comments
 (0)