From 8cf2b9c8125bc7f9225656682599dd5c0a3b159a Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:22:10 +0800 Subject: [PATCH 1/8] Added Qnn error message --- .../providers/qnn/builder/qnn_backend_manager.cc | 14 +++++++++++--- .../providers/qnn/builder/qnn_backend_manager.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index f44efb1eba6db..18461903ca633 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -406,9 +406,9 @@ Status QnnBackendManager::CreateDevice() { LOGS_DEFAULT(INFO) << "Create device."; if (nullptr != qnn_interface_.deviceCreate) { - auto result = qnn_interface_.deviceCreate(log_handle_, device_configs_builder.GetQnnConfigs(), &device_handle_); - if (QNN_SUCCESS != result) { - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create device. Error: ", result); + Qnn_ErrorHandle_t result = qnn_interface_.deviceCreate(log_handle_, device_configs_builder.GetQnnConfigs(), &device_handle_); + if (QNN_SUCCESS != result) + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create device. Error: ", QnnErrorHandleToString(result); } } device_created_ = true; @@ -1293,6 +1293,14 @@ const char* QnnBackendManager::QnnProfileErrorToString(QnnProfile_Error_t error) } } +const char* QnnBackendManager::QnnErrorHandleToString(Qnn_ErrorHandle_t error) { + const char* error_msg = nullptr; + if (QNN_SUCCESS == qnn_interface_.errorGetMessage(result, &error_msg)) { + return error_msg; + } + return "Unknown"; +} + const std::string QnnBackendManager::ExtractQnnScalarValue(const Qnn_Scalar_t& scalar) { switch (scalar.dataType) { case QNN_DATATYPE_INT_8: diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.h b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.h index d51e547aeb2fb..a4811b2cb6db3 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.h +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.h @@ -216,6 +216,7 @@ class QnnBackendManager { static const std::string GetEventTypeString(QnnProfile_EventType_t eventType); static const std::string ExtractQnnScalarValue(const Qnn_Scalar_t& scalar); const char* QnnProfileErrorToString(QnnProfile_Error_t error); + const char* QnnErrorHandleToString(Qnn_ErrorHandle_t error); QnnLog_Level_t MapOrtSeverityToQNNLogLevel(logging::Severity ort_log_level); #ifdef _WIN32 void LogQnnProfileEventAsTraceLogging( From 6e0d5b2f89ba1679f4ebe83f563e2612f11e667d Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:30:29 +0800 Subject: [PATCH 2/8] Update --- .../qnn/builder/qnn_backend_manager.cc | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index 18461903ca633..bedbf841b07a0 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -330,8 +330,8 @@ Status QnnBackendManager::InitializeBackend() { return Status::OK(); } - auto result = qnn_interface_.backendCreate(log_handle_, (const QnnBackend_Config_t**)backend_config_, &backend_handle_); - ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to initialize backend"); + Qnn_ErrorHandle_t result = qnn_interface_.backendCreate(log_handle_, (const QnnBackend_Config_t**)backend_config_, &backend_handle_); + ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to initialize backend. Error:", QnnErrorHandleToString(result)); backend_initialized_ = true; return Status::OK(); @@ -408,7 +408,7 @@ Status QnnBackendManager::CreateDevice() { if (nullptr != qnn_interface_.deviceCreate) { Qnn_ErrorHandle_t result = qnn_interface_.deviceCreate(log_handle_, device_configs_builder.GetQnnConfigs(), &device_handle_); if (QNN_SUCCESS != result) - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create device. Error: ", QnnErrorHandleToString(result); + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create device. Error: ", QnnErrorHandleToString(result)); } } device_created_ = true; @@ -422,9 +422,9 @@ Status QnnBackendManager::ReleaseDevice() { } if (nullptr != qnn_interface_.deviceFree) { - auto result = qnn_interface_.deviceFree(device_handle_); + Qnn_ErrorHandle_t result = qnn_interface_.deviceFree(device_handle_); if (QNN_SUCCESS != result) { - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to release device. Error: ", result); + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to release device. Error: ", QnnErrorHandleToString(result)); } } @@ -451,8 +451,8 @@ Status QnnBackendManager::InitializeProfiling() { } else if (ProfilingLevel::DETAILED == profiling_level_merge_) { qnn_profile_level = QNN_PROFILE_LEVEL_DETAILED; } - auto result = qnn_interface_.profileCreate(backend_handle_, qnn_profile_level, &profile_backend_handle_); - ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to create QNN profile!"); + Qnn_ErrorHandle_t result = qnn_interface_.profileCreate(backend_handle_, qnn_profile_level, &profile_backend_handle_); + ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to create QNN profile! Error: ", QnnErrorHandleToString(result)); return Status::OK(); } @@ -525,13 +525,13 @@ Status QnnBackendManager::CreateContext() { const QnnContext_Config_t* context_configs[] = {&qnn_context_config, nullptr}; Qnn_ContextHandle_t context = nullptr; - auto result = qnn_interface_.contextCreate(backend_handle_, + Qnn_ErrorHandle_t result = qnn_interface_.contextCreate(backend_handle_, device_handle_, context_configs, &context); contexts_.push_back(context); - ORT_RETURN_IF(QNN_CONTEXT_NO_ERROR != result, "Failed to create context."); + ORT_RETURN_IF(QNN_CONTEXT_NO_ERROR != result, "Failed to create context. Error: ", QnnErrorHandleToString(result)); context_created_ = true; return Status::OK(); @@ -544,7 +544,7 @@ Status QnnBackendManager::ReleaseContext() { bool failed = false; for (auto context : contexts_) { - auto result = qnn_interface_.contextFree(context, nullptr); + Qnn_ErrorHandle_t result = qnn_interface_.contextFree(context, nullptr); if (QNN_CONTEXT_NO_ERROR != result) { failed = true; } From 15197130f3d0c55149f01b250f46e76e4e3ff17b Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:37:23 +0800 Subject: [PATCH 3/8] Update --- .../core/providers/qnn/builder/qnn_backend_manager.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index bedbf841b07a0..e164594bd1d90 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -331,7 +331,7 @@ Status QnnBackendManager::InitializeBackend() { } Qnn_ErrorHandle_t result = qnn_interface_.backendCreate(log_handle_, (const QnnBackend_Config_t**)backend_config_, &backend_handle_); - ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to initialize backend. Error:", QnnErrorHandleToString(result)); + ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to initialize backend. Error: ", QnnErrorHandleToString(result)); backend_initialized_ = true; return Status::OK(); @@ -407,7 +407,7 @@ Status QnnBackendManager::CreateDevice() { LOGS_DEFAULT(INFO) << "Create device."; if (nullptr != qnn_interface_.deviceCreate) { Qnn_ErrorHandle_t result = qnn_interface_.deviceCreate(log_handle_, device_configs_builder.GetQnnConfigs(), &device_handle_); - if (QNN_SUCCESS != result) + if (QNN_SUCCESS != result) { return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to create device. Error: ", QnnErrorHandleToString(result)); } } @@ -1014,8 +1014,8 @@ Status QnnBackendManager::ExtractBackendProfilingInfo() { const QnnProfile_EventId_t* profile_events{nullptr}; uint32_t num_events{0}; - auto result = qnn_interface_.profileGetEvents(profile_backend_handle_, &profile_events, &num_events); - ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to get profile events."); + Qnn_ErrorHandle_t result = qnn_interface_.profileGetEvents(profile_backend_handle_, &profile_events, &num_events); + ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to get profile events. Error: ", QnnErrorHandleToString(result)); if (num_events > 0) { LOGS(*logger_, VERBOSE) << "profile_events: " << profile_events << " num_events: " << num_events; From 69901a81e7ae4f4192729a399c1896f0a5208b31 Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:43:11 +0800 Subject: [PATCH 4/8] fix --- .../core/providers/qnn/builder/qnn_backend_manager.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index e164594bd1d90..76be8758a37a1 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -1073,8 +1073,8 @@ Status QnnBackendManager::ExtractProfilingSubEvents( bool tracelogging_provider_ep_enabled) { const QnnProfile_EventId_t* profile_sub_events{nullptr}; uint32_t num_sub_events{0}; - auto result = qnn_interface_.profileGetSubEvents(profile_event_id, &profile_sub_events, &num_sub_events); - ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to get profile sub events."); + Qnn_ErrorHandle_t result = qnn_interface_.profileGetSubEvents(profile_event_id, &profile_sub_events, &num_sub_events); + ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to get profile sub events. Error: ", QnnErrorHandleToString(result)); if (num_sub_events > 0) { LOGS(*logger_, VERBOSE) << "profile_sub_events: " << profile_sub_events << " num_sub_events: " << num_sub_events; @@ -1113,7 +1113,7 @@ Status QnnBackendManager::ExtractProfilingEventBasic( std::ofstream& outfile, bool tracelogging_provider_ep_enabled) { QnnProfile_EventData_t event_data; - auto result = qnn_interface_.profileGetEventData(profile_event_id, &event_data); + Qnn_ErrorHandle_t result = qnn_interface_.profileGetEventData(profile_event_id, &event_data); QnnProfile_Error_t errorCode = static_cast(result & 0xFFFF); ORT_RETURN_IF(QNN_PROFILE_NO_ERROR != result, "Failed to get profile event data: " + std::string(QnnProfileErrorToString(errorCode))); @@ -1295,7 +1295,7 @@ const char* QnnBackendManager::QnnProfileErrorToString(QnnProfile_Error_t error) const char* QnnBackendManager::QnnErrorHandleToString(Qnn_ErrorHandle_t error) { const char* error_msg = nullptr; - if (QNN_SUCCESS == qnn_interface_.errorGetMessage(result, &error_msg)) { + if (QNN_SUCCESS == qnn_interface_.errorGetMessage(error, &error_msg)) { return error_msg; } return "Unknown"; From ef2fb77a3bf939d197f1b0ead1d61e21f6e8d030 Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:01:16 +0800 Subject: [PATCH 5/8] More --- .../core/providers/qnn/builder/qnn_backend_manager.cc | 8 ++++---- onnxruntime/core/providers/qnn/builder/qnn_model.cc | 4 ++-- .../core/providers/qnn/builder/qnn_model_wrapper.cc | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index 76be8758a37a1..a27beae0c8dc6 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -279,7 +279,7 @@ Status QnnBackendManager::InitializeQnnLog() { } } - ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to initialize logging in the QNN backend"); + ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to initialize logging in the QNN backend. Error: ", QnnErrorHandleToString(result)); return Status::OK(); } @@ -320,7 +320,7 @@ Status QnnBackendManager::UpdateQnnLogLevel(logging::Severity ort_log_level) { LOGS(*logger_, ERROR) << "Invalid log handle provided to QnnLog_setLogLevel."; } } - ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to set log level in Qnn backend"); + ORT_RETURN_IF(QNN_BACKEND_NO_ERROR != result, "Failed to set log level in Qnn backend. Error: ", QnnErrorHandleToString(result)); return Status::OK(); } @@ -566,7 +566,7 @@ std::unique_ptr QnnBackendManager::GetContextBinaryBuffer(uint6 // Generate all graphs in one single context Qnn_ErrorHandle_t rt = qnn_interface_.contextGetBinarySize(contexts_[0], &required_buffer_size); if (QNN_CONTEXT_NO_ERROR != rt) { - LOGS(*logger_, ERROR) << "Failed to get QNN context binary size. Error code: " << rt; + LOGS(*logger_, ERROR) << "Failed to get QNN context binary size. Error: " << QnnErrorHandleToString(rt); return nullptr; } @@ -581,7 +581,7 @@ std::unique_ptr QnnBackendManager::GetContextBinaryBuffer(uint6 required_buffer_size, &written_buffer_size); if (QNN_CONTEXT_NO_ERROR != rt) { - LOGS(*logger_, ERROR) << "Failed to get context binary."; + LOGS(*logger_, ERROR) << "Failed to get context binary. Error: " << QnnErrorHandleToString(rt); return nullptr; } diff --git a/onnxruntime/core/providers/qnn/builder/qnn_model.cc b/onnxruntime/core/providers/qnn/builder/qnn_model.cc index 503943dfb636b..22ab28d0fa560 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_model.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_model.cc @@ -368,8 +368,8 @@ Status QnnModel::DeserializeGraphInfoFromBinaryInfo(const QnnSystemContext_Graph } Qnn_GraphHandle_t graph; auto qnn_interface = qnn_backend_manager_->GetQnnInterface(); - auto rt = qnn_interface.graphRetrieve(context, graph_name.c_str(), &graph); - ORT_RETURN_IF(QNN_SUCCESS != rt, "Failed to retrieve QNN graph."); + Qnn_ErrorHandle_t rt = qnn_interface.graphRetrieve(context, graph_name.c_str(), &graph); + ORT_RETURN_IF(QNN_SUCCESS != rt, "Failed to retrieve QNN graph. Error: ", QnnErrorHandleToString(rt)); graph_info_ = std::make_unique(graph, graph_name, diff --git a/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc b/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc index f85cdc401a152..96b3a5223f70b 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc @@ -39,7 +39,7 @@ bool QnnModelWrapper::CreateQnnGraph(const Qnn_ContextHandle_t& context, if (rt != QNN_GRAPH_NO_ERROR || graph_ == nullptr) { rt = qnn_interface_.graphRetrieve(context, graph_name_.c_str(), &graph_); if (rt != QNN_GRAPH_NO_ERROR || graph_ == nullptr) { - LOGS(logger_, ERROR) << "Failed to create Qnn graph: " << graph_name; + LOGS(logger_, ERROR) << "Failed to create Qnn graph: " << graph_name << " Error: " << QnnErrorHandleToString(rt); return false; } } From 88542c1a2139b4ff338ccc092657dba8c8519aff Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:03:09 +0800 Subject: [PATCH 6/8] Add comment --- onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index a27beae0c8dc6..29e9f2a9e7239 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -1294,6 +1294,7 @@ const char* QnnBackendManager::QnnProfileErrorToString(QnnProfile_Error_t error) } const char* QnnBackendManager::QnnErrorHandleToString(Qnn_ErrorHandle_t error) { + // From QNN SDK: The memory is statically owned and should not be freed by the caller. const char* error_msg = nullptr; if (QNN_SUCCESS == qnn_interface_.errorGetMessage(error, &error_msg)) { return error_msg; From c88ad6851d7883b46934e479eb7c854a1a475185 Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:08:59 +0800 Subject: [PATCH 7/8] restore --- onnxruntime/core/providers/qnn/builder/qnn_model.cc | 4 ++-- onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_model.cc b/onnxruntime/core/providers/qnn/builder/qnn_model.cc index 22ab28d0fa560..503943dfb636b 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_model.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_model.cc @@ -368,8 +368,8 @@ Status QnnModel::DeserializeGraphInfoFromBinaryInfo(const QnnSystemContext_Graph } Qnn_GraphHandle_t graph; auto qnn_interface = qnn_backend_manager_->GetQnnInterface(); - Qnn_ErrorHandle_t rt = qnn_interface.graphRetrieve(context, graph_name.c_str(), &graph); - ORT_RETURN_IF(QNN_SUCCESS != rt, "Failed to retrieve QNN graph. Error: ", QnnErrorHandleToString(rt)); + auto rt = qnn_interface.graphRetrieve(context, graph_name.c_str(), &graph); + ORT_RETURN_IF(QNN_SUCCESS != rt, "Failed to retrieve QNN graph."); graph_info_ = std::make_unique(graph, graph_name, diff --git a/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc b/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc index 96b3a5223f70b..f85cdc401a152 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_model_wrapper.cc @@ -39,7 +39,7 @@ bool QnnModelWrapper::CreateQnnGraph(const Qnn_ContextHandle_t& context, if (rt != QNN_GRAPH_NO_ERROR || graph_ == nullptr) { rt = qnn_interface_.graphRetrieve(context, graph_name_.c_str(), &graph_); if (rt != QNN_GRAPH_NO_ERROR || graph_ == nullptr) { - LOGS(logger_, ERROR) << "Failed to create Qnn graph: " << graph_name << " Error: " << QnnErrorHandleToString(rt); + LOGS(logger_, ERROR) << "Failed to create Qnn graph: " << graph_name; return false; } } From b33ac3130c6b892db2e48a2307ac0cd9eee1478a Mon Sep 17 00:00:00 2001 From: Chester Liu <4710575+skyline75489@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:06:43 +0800 Subject: [PATCH 8/8] Lint --- .../core/providers/qnn/builder/qnn_backend_manager.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc index 29e9f2a9e7239..0005869f13f66 100644 --- a/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc +++ b/onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc @@ -526,9 +526,9 @@ Status QnnBackendManager::CreateContext() { Qnn_ContextHandle_t context = nullptr; Qnn_ErrorHandle_t result = qnn_interface_.contextCreate(backend_handle_, - device_handle_, - context_configs, - &context); + device_handle_, + context_configs, + &context); contexts_.push_back(context); ORT_RETURN_IF(QNN_CONTEXT_NO_ERROR != result, "Failed to create context. Error: ", QnnErrorHandleToString(result));