Skip to content
Open
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
12 changes: 10 additions & 2 deletions onnxruntime/core/providers/openvino/openvino_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <memory>
#include <vector>
#include <charconv>
#include "core/providers/shared_library/provider_api.h"
#include "core/providers/openvino/openvino_execution_provider.h"
#include "core/providers/openvino/contexts.h"
Expand Down Expand Up @@ -170,12 +171,19 @@
return 0;
};

compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) {
compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) -> Status {
auto function_state = static_cast<OpenVINOEPFunctionState*>(state);
try {
function_state->backend_manager.Compute(context);
} catch (const std::exception& ex) {
return common::Status(common::ONNXRUNTIME, common::FAIL, ex.what());
// Extract error code from message with pattern: "<code>|message"
auto get_error_code = [](std::string_view msg) {
if (auto pos = msg.find('|'); pos != std::string_view::npos)

Check notice on line 181 in onnxruntime/core/providers/openvino/openvino_execution_provider.cc

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] onnxruntime/core/providers/openvino/openvino_execution_provider.cc#L181

If/else bodies with multiple statements require braces [readability/braces] [4]
Raw output
onnxruntime/core/providers/openvino/openvino_execution_provider.cc:181:  If/else bodies with multiple statements require braces  [readability/braces] [4]
if (int code = 0; std::from_chars(msg.data(), msg.data() + pos, code).ec == std::errc{})
return code;
return static_cast<int>(common::EP_FAIL);
};
return common::Status(common::ONNXRUNTIME, get_error_code(ex.what()), ex.what());
}
return Status::OK();
};
Expand Down
70 changes: 67 additions & 3 deletions onnxruntime/core/providers/openvino/ov_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include "core/providers/openvino/ov_interface.h"

#include <charconv>
#include <format>
#include <regex>

#define ORT_API_MANUAL_INIT
#include "core/session/onnxruntime_cxx_api.h"
Expand All @@ -16,14 +18,75 @@
namespace onnxruntime {
namespace openvino_ep {

namespace {
// Helper to extract Level Zero error codes from NPU driver exceptions
struct ZEErrorInfo {
uint32_t code{0};
std::string name{"UNKNOWN_NPU_ERROR"};

static ZEErrorInfo ExtractFromMessage(std::string_view ov_exception_msg) {
ZEErrorInfo info;

// Extract hex error code: "code 0x([0-9a-fA-F]+)"
std::regex code_pattern("code 0x([0-9a-fA-F]+)");
std::string msg_str(ov_exception_msg);
std::smatch matches;
if (std::regex_search(msg_str, matches, code_pattern) && matches.size() > 1) {
const auto& code_str = matches[1].str();
std::from_chars(code_str.data(), code_str.data() + code_str.size(), info.code, 16);
}

// Extract ZE error name: R"(\bZE_\w*\b)"
std::regex name_pattern(R"(\bZE_\w*\b)");
if (std::regex_search(msg_str, matches, name_pattern)) {
info.name = matches[0];
}

return info;
}

// Check if this is ZE_RESULT_ERROR_INVALID_NATIVE_BINARY (driver/binary mismatch)
bool IsInvalidNativeBinary() const {
return code == 0x7800000f;
}
};
} // namespace

template <typename Func, typename... Args>
inline auto OvExceptionBoundary(Func&& func, std::format_string<Args...>&& fmt, Args&&... args) {
return OvExceptionBoundary(func, ORT_EP_FAIL, std::forward<std::format_string<Args...>>(fmt), std::forward<Args>(args)...);
}

template <typename Func, typename... Args>
inline auto OvExceptionBoundary(Func&& func, OrtErrorCode error_code, std::format_string<Args...>&& fmt, Args&&... args) {
try {
return func();
} catch (const ov::Exception& e) {
ORT_THROW(log_tag + std::vformat(fmt.get(), std::make_format_args(args...)) + ": " + std::string(e.what()));
std::string ov_msg = e.what();

// For import failures, extract and report ZE error codes for better NPU diagnostics
if (error_code == ORT_INVALID_GRAPH) {
auto ze_info = ZEErrorInfo::ExtractFromMessage(ov_msg);
if (ze_info.IsInvalidNativeBinary()) {
// Enhance message for binary incompatibility (most common import failure)
ov_msg = std::format("{}, code 0x{:x}\nModel needs to be recompiled\n",
ze_info.name, ze_info.code);
} else if (ze_info.code != 0) {
// Include ZE error details for other NPU failures
ov_msg = std::format("{}, code 0x{:x}: {}",
ze_info.name, ze_info.code, ov_msg);
}
}

// Encode error code as prefix: "<code>|message"
auto msg = std::format("{}|{}{}: {}", static_cast<int>(error_code), log_tag,
std::vformat(fmt.get(), std::make_format_args(args...)), ov_msg);
ORT_THROW(msg);
} catch (...) {
ORT_THROW(log_tag + std::vformat(fmt.get(), std::make_format_args(args...)));
// Encode error code as prefix: "<code>|message"
auto msg = std::format("{}|{}{}", static_cast<int>(error_code), log_tag,
std::vformat(fmt.get(), std::make_format_args(args...)));
ORT_THROW(msg);
}
}

Expand Down Expand Up @@ -214,7 +277,8 @@ OVExeNetwork OVCore::ImportModel(ModelBlobWrapper& model_blob,
#endif
return exe;
},
"Exception while Loading Network for graph {}", name);
ORT_INVALID_GRAPH,
"Exception while importing model for graph {}", name);
}

OVExeNetwork OVCore::ImportEPCtxOVIREncapsulation(std::istream& model_stream,
Expand Down
Loading