[QNN EP] Redirect Qnn log to Ort log (#16019)

### Description
Redirect Qnn log to Ort log.
Set Qnn log level align with Ort log level
Always output Qnn log as Ort verbose log

### Motivation and Context
Redirect Qnn log to Ort log instead of print to console.

---------

Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
This commit is contained in:
Hector Li 2023-05-23 09:22:14 -07:00 committed by GitHub
parent b457cfaa8f
commit 1b498e414f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 61 deletions

View file

@ -12,6 +12,7 @@
#include "HTP/QnnHtpCommon.h"
#include "core/common/gsl.h"
#include "core/framework/endian_utils.h"
#include "core/common/logging/capture.h"
// Flag to determine if Backend should do node validation for each opNode added
#define DO_GRAPH_NODE_VALIDATIONS 1
@ -128,6 +129,53 @@ Status QnnBackendManager::LoadQnnSystemLib() {
return Status::OK();
}
void QnnLogging(const char* format,
QnnLog_Level_t level,
uint64_t timestamp,
va_list argument_parameter) {
ORT_UNUSED_PARAMETER(level);
ORT_UNUSED_PARAMETER(timestamp);
// Always output Qnn log as Ort verbose log
const auto& logger = ::onnxruntime::logging::LoggingManager::DefaultLogger();
const auto severity = ::onnxruntime::logging::Severity::kVERBOSE;
const auto data_type = ::onnxruntime::logging::DataType::SYSTEM;
if (logger.OutputIsEnabled(severity, data_type)) {
::onnxruntime::logging::Capture(logger,
severity,
::onnxruntime::logging::Category::onnxruntime,
data_type,
ORT_WHERE)
.ProcessPrintf(format, argument_parameter);
}
}
void QnnBackendManager::InitializeQnnLog() {
// Set Qnn log level align with Ort log level
QnnLog_Level_t qnn_log_level = QNN_LOG_LEVEL_WARN;
auto ort_log_level = logger_->GetSeverity();
switch (ort_log_level) {
case logging::Severity::kVERBOSE:
qnn_log_level = QNN_LOG_LEVEL_DEBUG;
break;
case logging::Severity::kINFO:
qnn_log_level = QNN_LOG_LEVEL_INFO;
break;
case logging::Severity::kWARNING:
qnn_log_level = QNN_LOG_LEVEL_WARN;
break;
case logging::Severity::kERROR:
qnn_log_level = QNN_LOG_LEVEL_ERROR;
break;
default:
break;
}
if (QNN_SUCCESS != qnn_interface_.logCreate(QnnLogging, qnn_log_level, &log_handle_)) {
LOGS(*logger_, WARNING) << "Unable to initialize logging in the QNN backend.";
}
}
Status QnnBackendManager::InitializeBackend() {
if (true == backend_initialized_) {
LOGS_DEFAULT(INFO) << "Backend intialized already.";

View file

@ -106,25 +106,7 @@ class QnnBackendManager {
}
}
void InitializeQnnLog() {
const std::map<logging::Severity, QnnLog_Level_t> ort_log_level_to_qnn_log_level = {
{logging::Severity::kVERBOSE, QNN_LOG_LEVEL_DEBUG},
{logging::Severity::kINFO, QNN_LOG_LEVEL_INFO},
{logging::Severity::kWARNING, QNN_LOG_LEVEL_WARN},
{logging::Severity::kERROR, QNN_LOG_LEVEL_ERROR},
{logging::Severity::kFATAL, QNN_LOG_LEVEL_ERROR}};
QnnLog_Level_t qnn_log_level = QNN_LOG_LEVEL_WARN;
auto ort_log_level = logger_->GetSeverity();
auto pos = ort_log_level_to_qnn_log_level.find(ort_log_level);
if (pos != ort_log_level_to_qnn_log_level.end()) {
qnn_log_level = pos->second;
}
if (QNN_SUCCESS != qnn_interface_.logCreate(QnnLogStdoutCallback, qnn_log_level, &log_handle_)) {
LOGS(*logger_, WARNING) << "Unable to initialize logging in the QNN backend.";
}
}
void InitializeQnnLog();
// Terminate logging in the backend
Status TerminateQnnLog() {

View file

@ -19,42 +19,6 @@ size_t memscpy(void* dst, size_t dst_size, const void* src, size_t copy_size) {
return min_size;
}
void QnnLogStdoutCallback(const char* format,
QnnLog_Level_t level,
uint64_t timestamp,
va_list argument_parameter) {
const char* levelStr = "";
switch (level) {
case QNN_LOG_LEVEL_ERROR:
levelStr = " ERROR ";
break;
case QNN_LOG_LEVEL_WARN:
levelStr = "WARNING";
break;
case QNN_LOG_LEVEL_INFO:
levelStr = " INFO ";
break;
case QNN_LOG_LEVEL_DEBUG:
levelStr = " DEBUG ";
break;
case QNN_LOG_LEVEL_VERBOSE:
levelStr = "VERBOSE";
break;
case QNN_LOG_LEVEL_MAX:
levelStr = "UNKNOWN";
break;
}
double ms = (double)timestamp / 1000000.0;
// To avoid interleaved messages
{
std::lock_guard<std::mutex> lock(qnn_log_mutex_);
fprintf(stdout, "%8.1fms [%-7s] ", ms, levelStr);
vfprintf(stdout, format, argument_parameter);
fprintf(stdout, "\n");
}
}
void SetQnnTensorType(Qnn_Tensor_t& qnn_tensor, Qnn_TensorType_t tensor_type) {
if (QNN_TENSOR_VERSION_1 == qnn_tensor.version) {
qnn_tensor.v1.type = tensor_type;

View file

@ -437,12 +437,6 @@ typedef struct GraphConfigInfo {
const QnnGraph_Config_t** graphConfigs;
} GraphConfigInfo_t;
void QnnLogStdoutCallback(const char* format,
QnnLog_Level_t level,
uint64_t timestamp,
va_list argument_parameter);
static std::mutex qnn_log_mutex_;
namespace qnn_def {
const std::string package_name = "qti.aisw";
const std::string dilation = "dilation";