Skip to content

Commit 04cce1b

Browse files
committed
fix: Fix logic for parsing model_file_path
1 parent 143f4c1 commit 04cce1b

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,29 @@ BasicBackend::BasicBackend(std::unique_ptr<ONNX_NAMESPACE::ModelProto>& model_pr
7272
!subgraph_context_.is_ep_ctx_graph);
7373
if (subgraph_context_.is_ep_ctx_graph) {
7474
if (subgraph_context_.is_ep_ctx_ovir_encapsulated) {
75+
// model_file_path will use so_context_file_path if the onnx_model_path_name is not available,
76+
// especially in case of CreateSessionFormArray() where user must explicitly
77+
// specify absolute path for so_context_file_path.
78+
auto model_file_path = [this]() {
79+
if (!session_context_.onnx_model_path_name.empty() &&
80+
std::filesystem::exists(session_context_.onnx_model_path_name)) return session_context_.onnx_model_path_name;
81+
82+
ORT_ENFORCE(!session_context_.so_context_file_path.empty() &&
83+
std::filesystem::path(session_context_.so_context_file_path).is_absolute() &&
84+
std::filesystem::exists(session_context_.so_context_file_path),
85+
log_tag + "Context file path must be non-empty & absolute, when using CreateSessionFormArray() API explicitly."
86+
"Please set a valid absolute path for ep.context_file_path in session options.");
87+
// Return absolute context file path as input to ImportEPCtxOVIREncapsulation() function.
88+
return session_context_.so_context_file_path;
89+
90+
};
7591
// If the EPContext node with OVIR Encapsulation, then create
7692
// an executable network from EP_CACHE_CONTEXT using read_model() & compile_model()
7793
exe_network_ = OVCore::Get()->ImportEPCtxOVIREncapsulation(*model_stream,
7894
hw_target,
7995
device_config,
8096
enable_causallm,
81-
session_context_.so_context_file_path,
82-
subgraph_context_.subgraph_name);
97+
model_file_path());
8398
} else {
8499
// If the blob is held in an EPContext node, then skip FE+Compile
85100
// and directly move on to creating a backend with the executable blob

onnxruntime/core/providers/openvino/ov_interface.cc

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ void printDebugInfo(const ov::CompiledModel& obj) {
3838
continue;
3939
OPENVINO_SUPPRESS_DEPRECATED_END
4040
std::cout << " " << item2.first << ": " << item2.second.as<std::string>() << std::endl;
41-
}
4241
}
4342
} else {
4443
std::cout << " " << cfg << ": " << prop.as<std::string>() << std::endl;
@@ -210,29 +209,23 @@ OVExeNetwork OVCore::ImportModel(std::istream& model_stream,
210209
}
211210

212211
OVExeNetwork OVCore::ImportEPCtxOVIREncapsulation(std::istream& model_stream,
213-
std::string hw_target,
214-
const ov::AnyMap& device_config,
215-
bool enable_causallm,
216-
std::filesystem::path context_file_path,
217-
std::string name) {
212+
std::string& hw_target,
213+
const ov::AnyMap& device_config,
214+
bool enable_causallm,
215+
std::filesystem::path model_file_path) {
218216
try {
219217
OVExeNetwork exe;
220218

221219
bool isXML = backend_utils::IsModelStreamXML(model_stream);
222220

223-
ORT_ENFORCE(!context_file_path.string().empty(),
224-
"The session option ep.context_file_path is not set for EPContext node with OVIR Encapsulation. "
225-
"Current value: '" + context_file_path.string() + "'");
226-
227221
// Helper function to check if file exists and is readable
228-
const auto check_file_access = [&context_file_path](const std::filesystem::path& path) {
222+
const auto check_file_access = [&model_file_path](const std::filesystem::path& path) {
229223
try {
230-
const auto status = std::filesystem::status(path);
231-
if (!std::filesystem::exists(status)) {
232-
ORT_THROW(log_tag + "Required file missing: " + path.string());
224+
if (!std::filesystem::exists(path) || std::filesystem::is_empty(path)) {
225+
ORT_THROW(log_tag + "Required file missing or empty: " + path.string());
233226
}
234227
std::ifstream file(path);
235-
if (!file.is_open()) {
228+
if (!file) {
236229
ORT_THROW(log_tag + "Required file not readable: " + path.string());
237230
}
238231
} catch (const std::exception& e) {
@@ -243,7 +236,7 @@ OVExeNetwork OVCore::ImportEPCtxOVIREncapsulation(std::istream& model_stream,
243236
if (isXML) {
244237
// If the model is XML, we need to load it with the XML content in read_model()
245238
// where weights from bin file is directly consumed
246-
auto xml_file_path = context_file_path.parent_path() / (context_file_path.stem().string() + ".xml");
239+
auto xml_file_path = model_file_path.parent_path() / (model_file_path.stem().string() + ".xml");
247240

248241
check_file_access(xml_file_path);
249242

@@ -265,9 +258,9 @@ OVExeNetwork OVCore::ImportEPCtxOVIREncapsulation(std::istream& model_stream,
265258
#endif
266259
return exe;
267260
} catch (const Exception& e) {
268-
ORT_THROW(log_tag + " Exception while Loading Network for graph: " + name + e.what());
261+
ORT_THROW(log_tag + " Exception while Loading Network from OVIR model file: " + model_file_path.string() + e.what());
269262
} catch (...) {
270-
ORT_THROW(log_tag + " Exception while Loading Network for graph " + name);
263+
ORT_THROW(log_tag + " Exception while Loading Network from OVIR model file: " + model_file_path.string());
271264
}
272265
}
273266

onnxruntime/core/providers/openvino/ov_interface.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ struct OVCore : WeakSingleton<OVCore> {
8484
const ov::AnyMap& device_config,
8585
std::string name);
8686
OVExeNetwork ImportEPCtxOVIREncapsulation(std::istream& model_stream,
87-
std::string hw_target,
88-
const ov::AnyMap& device_config,
89-
bool enable_causallm,
90-
std::filesystem::path context_file_path,
91-
std::string name);
87+
std::string& hw_target,
88+
const ov::AnyMap& device_config,
89+
bool enable_causallm,
90+
std::filesystem::path model_file_path);
9291

9392
std::vector<std::string> GetAvailableDevices() const;
9493
std::vector<std::string> GetAvailableDevices(const std::string& device_type) const;

0 commit comments

Comments
 (0)