Skip to content

Commit b95e4b3

Browse files
authored
Qualcomm AI Engine Direct - Check the version QNN API and backend API (#4998)
Summary: QNN API version format is major.minor.patch. If major given by the user does not match the built major, it will return and show error message. If minor does not match, it will show warning message.
1 parent 1263964 commit b95e4b3

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

backends/qualcomm/runtime/QnnManager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <executorch/backends/qualcomm/runtime/QnnManager.h>
99
#include <executorch/backends/qualcomm/runtime/SharedBuffer.h>
1010
#include <executorch/backends/qualcomm/runtime/Utils.h>
11+
#include <executorch/backends/qualcomm/runtime/backends/QnnBackendCommon.h>
1112
#include <executorch/backends/qualcomm/runtime/backends/QnnImplementation.h>
1213
#include <algorithm>
1314
#include <cstdlib>
@@ -281,6 +282,8 @@ Error QnnManager::Init() {
281282
options_->backend_options()->backend_type());
282283
backend_params_ptr_ = QnnBackendFactory().Create(
283284
qnn_loaded_backend_, logger_.get(), qnn_context_blob_, options_);
285+
ET_CHECK_OR_RETURN_ERROR(
286+
backend_params_ptr_ != nullptr, Internal, "Failed to load Qnn backend.")
284287
ET_CHECK_OR_RETURN_ERROR(
285288
backend_params_ptr_->qnn_backend_ptr_->Configure() == Error::Ok,
286289
Internal,

backends/qualcomm/runtime/backends/QnnBackendCommon.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,85 @@ Error QnnBackend::Configure() {
5353
}
5454
return Error::Ok;
5555
}
56+
57+
Error QnnBackend::VerifyQNNSDKVersion(
58+
const QnnExecuTorchBackendType backend_id) {
59+
const QnnInterface& qnn_interface = implementation_.GetQnnInterface();
60+
61+
Qnn_ApiVersion_t qnn_version = {QNN_VERSION_INIT};
62+
Qnn_ErrorHandle_t error =
63+
qnn_interface.qnn_backend_get_api_version(&qnn_version);
64+
if (error != QNN_SUCCESS) {
65+
QNN_EXECUTORCH_LOG_ERROR("Failed to get Qnn API version.");
66+
return Error::Internal;
67+
}
68+
69+
Qnn_ApiVersion_t expected_version = {QNN_VERSION_INIT};
70+
expected_version.coreApiVersion.major = QNN_API_VERSION_MAJOR;
71+
expected_version.coreApiVersion.minor = QNN_API_VERSION_MINOR;
72+
expected_version.coreApiVersion.patch = QNN_API_VERSION_PATCH;
73+
expected_version.backendApiVersion = GetExpectedBackendVersion();
74+
const char* backend_type = EnumNameQnnExecuTorchBackendType(backend_id);
75+
76+
Error status = VersionChecker(
77+
qnn_version.coreApiVersion, expected_version.coreApiVersion, "Qnn API");
78+
if (status == Error::Ok) {
79+
status = VersionChecker(
80+
qnn_version.backendApiVersion,
81+
expected_version.backendApiVersion,
82+
backend_type);
83+
}
84+
85+
return status;
86+
}
87+
88+
Error QnnBackend::VersionChecker(
89+
const Qnn_Version_t& qnn_version,
90+
const Qnn_Version_t& expected,
91+
const std::string& prefix) {
92+
if (qnn_version.major != expected.major) {
93+
QNN_EXECUTORCH_LOG_ERROR(
94+
"%s version %u.%u.%u is not supported. "
95+
"The minimum supported version is %u.%u.%u. Please make "
96+
"sure you have the correct backend library version.",
97+
prefix.c_str(),
98+
qnn_version.major,
99+
qnn_version.minor,
100+
qnn_version.patch,
101+
expected.major,
102+
expected.minor,
103+
expected.patch);
104+
return Error::Internal;
105+
}
106+
if (qnn_version.major == QNN_API_VERSION_MAJOR &&
107+
qnn_version.minor < expected.minor) {
108+
QNN_EXECUTORCH_LOG_WARN(
109+
"%s version %u.%u.%u is mismatched. "
110+
"The minimum supported version is %u.%u.%u. Please make "
111+
"sure you have the correct backend library version.",
112+
prefix.c_str(),
113+
qnn_version.major,
114+
qnn_version.minor,
115+
qnn_version.patch,
116+
expected.major,
117+
expected.minor,
118+
expected.patch);
119+
}
120+
if ((qnn_version.major == QNN_API_VERSION_MAJOR &&
121+
qnn_version.minor > expected.minor)) {
122+
QNN_EXECUTORCH_LOG_WARN(
123+
"%s version %u.%u.%u is used. "
124+
"The version is tested against %u.%u.%u.",
125+
prefix.c_str(),
126+
qnn_version.major,
127+
qnn_version.minor,
128+
qnn_version.patch,
129+
expected.major,
130+
expected.minor,
131+
expected.patch);
132+
}
133+
return Error::Ok;
134+
}
56135
} // namespace qnn
57136
} // namespace executor
58137
} // namespace torch

backends/qualcomm/runtime/backends/QnnBackendCommon.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
#include <vector>
1515

16+
#include "HTP/QnnHtpCommon.h"
1617
#include "QnnBackend.h"
1718
#include "QnnCommon.h"
19+
#include "QnnTypes.h"
1820
namespace torch {
1921
namespace executor {
2022
namespace qnn {
@@ -43,7 +45,10 @@ class QnnBackend {
4345
return handle_;
4446
}
4547

48+
Error VerifyQNNSDKVersion(const QnnExecuTorchBackendType backend_id);
49+
4650
protected:
51+
virtual Qnn_Version_t GetExpectedBackendVersion() const = 0;
4752
virtual Error MakeConfig(std::vector<const QnnBackend_Config_t*>& config) {
4853
return Error::Ok;
4954
};
@@ -52,6 +57,10 @@ class QnnBackend {
5257
Qnn_BackendHandle_t handle_;
5358
const QnnImplementation& implementation_;
5459
QnnLogger* logger_;
60+
Error VersionChecker(
61+
const Qnn_Version_t& qnn_version,
62+
const Qnn_Version_t& expected,
63+
const std::string& prefix);
5564
};
5665
} // namespace qnn
5766
} // namespace executor

backends/qualcomm/runtime/backends/QnnBackendFactory.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
1616
const QnnExecuTorchContextBinary& qnn_context_blob,
1717
const QnnExecuTorchOptions* options) {
1818
auto backend_params = std::make_unique<BackendConfigParameters>();
19+
1920
switch (options->backend_options()->backend_type()) {
2021
case QnnExecuTorchBackendType::kHtpBackend: {
2122
auto htp_options = options->backend_options()->htp_options();
@@ -51,6 +52,7 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
5152
}
5253
backend_params->qnn_backend_ptr_ =
5354
std::make_unique<HtpBackend>(implementation, logger);
55+
5456
backend_params->qnn_device_ptr_ = std::make_unique<HtpDevice>(
5557
implementation, logger, options->soc_info(), htp_options);
5658

@@ -72,7 +74,6 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
7274
backend_params->qnn_mem_manager_ptr_ = std::make_unique<QnnMemManager>(
7375
implementation, backend_params->qnn_context_ptr_.get());
7476
backend_params->backend_init_state_ = BackendInitializeState::INITIALIZED;
75-
return backend_params;
7677
} break;
7778
case QnnExecuTorchBackendType::kGpuBackend:
7879
case QnnExecuTorchBackendType::kDspBackend:
@@ -81,7 +82,11 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
8182
return nullptr;
8283
}
8384

84-
// should not reach here
85+
if (backend_params->qnn_backend_ptr_->VerifyQNNSDKVersion(
86+
options->backend_options()->backend_type()) == Error::Ok) {
87+
return backend_params;
88+
}
89+
8590
return nullptr;
8691
}
8792
} // namespace qnn

backends/qualcomm/runtime/backends/htpbackend/HtpBackend.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#pragma once
99

1010
#include <executorch/backends/qualcomm/runtime/backends/QnnBackendCommon.h>
11+
#include "HTP/QnnHtpCommon.h"
1112
#include "HTP/QnnHtpProfile.h"
13+
#include "QnnTypes.h"
1214
namespace torch {
1315
namespace executor {
1416
namespace qnn {
@@ -24,6 +26,14 @@ class HtpBackend : public QnnBackend {
2426
event_type == QNN_HTP_PROFILE_EVENTTYPE_GRAPH_EXECUTE_ACCEL_TIME_CYCLE);
2527
}
2628

29+
Qnn_Version_t GetExpectedBackendVersion() const override {
30+
Qnn_Version_t backend_version;
31+
backend_version.major = QNN_HTP_API_VERSION_MAJOR;
32+
backend_version.minor = QNN_HTP_API_VERSION_MINOR;
33+
backend_version.patch = QNN_HTP_API_VERSION_PATCH;
34+
return backend_version;
35+
}
36+
2737
protected:
2838
Error MakeConfig(std::vector<const QnnBackend_Config_t*>& config) override {
2939
return Error::Ok;

0 commit comments

Comments
 (0)