Skip to content

Commit

Permalink
Update NPU device exception handling approach
Browse files Browse the repository at this point in the history
* Changes failure status code to exception (std::runtime_error)

* Capture all NPU related errors

* Throw minimal error message with error type and error code for Release
  builds
  • Loading branch information
srirammaswamy-intel committed Sep 2, 2024
1 parent 55d4fac commit 5429e55
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
24 changes: 22 additions & 2 deletions onnxruntime/core/providers/openvino/backend_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <cassert>
#include <fstream>
#include <regex>

Check notice on line 8 in onnxruntime/core/providers/openvino/backend_manager.cc

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] onnxruntime/core/providers/openvino/backend_manager.cc#L8

<regex> is an unapproved C++11 header. [build/c++11] [5]
Raw output
onnxruntime/core/providers/openvino/backend_manager.cc:8:  <regex> is an unapproved C++11 header.  [build/c++11] [5]
#include <sstream>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -107,13 +108,14 @@ BackendManager::BackendManager(const GlobalContext& global_context,
subgraph_context_,
ep_ctx_handle_);
} catch (const OnnxRuntimeException& ex) {
std::string exception_str = ex.what();
#if defined(OPENVINO_DISABLE_NPU_FALLBACK)
ORT_THROW(ex.what());
ORT_THROW(exception_str);
#else
if (device_type.find("NPU") != std::string::npos &&
!GetGlobalContext().disable_cpu_fallback &&
!ep_ctx_handle_.IsValidOVEPCtxGraph()) {
LOGS_DEFAULT(WARNING) << ex.what();
LOGS_DEFAULT(WARNING) << exception_str;
LOGS_DEFAULT(WARNING) << "Model compilation failed at OV NPU."
<< "Falling back to OV CPU for execution";
GetGlobalContext().device_type = "CPU";
Expand All @@ -126,6 +128,24 @@ BackendManager::BackendManager(const GlobalContext& global_context,
} catch (std::string const& msg) {
ORT_THROW(msg);
}
} else if (device_type.find("NPU") != std::string::npos &&
exception_str.find("intel_npu") != std::string::npos) {
// Handle NPU device related errors
#ifndef NDEBUG
ORT_THROW(exception_str + "\nModel needs to be recompiled\n");
#endif
std::string error_message = "UNKNOWN NPU ERROR";
std::string error_code = "code 0x0";
std::regex error_message_pattern(R"(\bZE_\w*\b)");
std::regex error_code_pattern("code 0x[0-9a-fA-F]+");
std::smatch matches;
if (std::regex_search(exception_str, matches, error_message_pattern)) {
error_message = matches[0];
}
if (std::regex_search(exception_str, matches, error_code_pattern)) {
error_code = matches[0];
}
throw std::runtime_error(error_message + ", " + error_code + "\nModel needs to be recompiled\n");
} else {
ORT_THROW(ex.what());
}
Expand Down
76 changes: 33 additions & 43 deletions onnxruntime/core/providers/openvino/openvino_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,50 +139,40 @@ common::Status OpenVINOExecutionProvider::Compile(
// During backend creation, we check if user wants to use precompiled blob onnx model or the original model
// For precompiled blob, directly load the model instead of compiling the model
// For original model, check if the user wants to export a model with pre-compiled blob
try {
std::shared_ptr<openvino_ep::BackendManager> backend_manager =
std::make_shared<openvino_ep::BackendManager>(*global_context_,
fused_node,
graph_body_viewer,
*GetLogger(),
ep_ctx_handle_);
compute_info.create_state_func =
[backend_manager](ComputeContext* context, FunctionState* state) {
OpenVINOEPFunctionState* p = new OpenVINOEPFunctionState();
p->allocate_func = context->allocate_func;
p->destroy_func = context->release_func;
p->allocator_handle = context->allocator_handle;
p->backend_manager = backend_manager;
*state = static_cast<FunctionState>(p);
return 0;
};
compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) {
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());
}
return Status::OK();
};

compute_info.release_state_func =
[](FunctionState state) {
if (state) {
OpenVINOEPFunctionState* function_state = static_cast<OpenVINOEPFunctionState*>(state);
delete function_state;
}
};
node_compute_funcs.push_back(compute_info);
} catch (const OnnxRuntimeException& ex) {
std::string exception_str = ex.what();
if (exception_str.find("ZE_RESULT_ERROR_UNKNOWN") != std::string::npos ||
exception_str.find("ZE_RESULT_ERROR_UNINITIALIZED") != std::string::npos) {
return Status(common::ONNXRUNTIME, common::EP_FAIL, "Model needs to be recompiled");
} else {
ORT_THROW(exception_str);
std::shared_ptr<openvino_ep::BackendManager> backend_manager =
std::make_shared<openvino_ep::BackendManager>(*global_context_,
fused_node,
graph_body_viewer,
*GetLogger(),
ep_ctx_handle_);
compute_info.create_state_func =
[backend_manager](ComputeContext* context, FunctionState* state) {
OpenVINOEPFunctionState* p = new OpenVINOEPFunctionState();
p->allocate_func = context->allocate_func;
p->destroy_func = context->release_func;
p->allocator_handle = context->allocator_handle;
p->backend_manager = backend_manager;
*state = static_cast<FunctionState>(p);
return 0;
};
compute_info.compute_func = [](FunctionState state, const OrtApi* /* api */, OrtKernelContext* context) {
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());
}
}
return Status::OK();
};

compute_info.release_state_func =
[](FunctionState state) {
if (state) {
OpenVINOEPFunctionState* function_state = static_cast<OpenVINOEPFunctionState*>(state);
delete function_state;
}
};
node_compute_funcs.push_back(compute_info);
}

return Status::OK();
Expand Down

0 comments on commit 5429e55

Please sign in to comment.