forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQnnImplementation.cpp
More file actions
272 lines (241 loc) · 7.97 KB
/
QnnImplementation.cpp
File metadata and controls
272 lines (241 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) Qualcomm Innovation Center, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <executorch/backends/qualcomm/runtime/backends/QnnImplementation.h>
#include "QnnInterface.h"
namespace torch {
namespace executor {
namespace qnn {
template <typename Fn>
Fn loadQnnFunction(void* handle, const char* function_name) {
#ifdef _WIN32
return reinterpret_cast<Fn>(GetProcAddress(reinterpret_cast<HMODULE>(handle), function_name));
#else
return reinterpret_cast<Fn>(dlsym(handle, function_name)); // NOLINT
#endif
}
Error QnnImplementation::InitBackend(
void* const lib_handle,
const QnnSaver_Config_t** saver_config) {
Qnn_ErrorHandle_t error = QNN_SUCCESS;
// saver_config must be set before backend initialization
auto saver_initialize =
loadQnnFunction<QnnSaverInitializeFn*>(lib_handle, "QnnSaver_initialize");
if (saver_initialize != nullptr) {
error = saver_initialize(saver_config);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
"[Qnn Delegate] QnnSaver Backend Failed to "
"saver_initialize. Error %d",
QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
}
return Error::Ok;
}
// instantiate static members
// NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
std::unordered_map<std::string, QnnImplementation::BackendIdType>
QnnImplementation::lib_path_to_backend_id_;
// NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
std::unordered_map<QnnImplementation::BackendIdType, const QnnInterface_t*>
QnnImplementation::loaded_backend_;
// NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
std::unordered_map<QnnImplementation::BackendIdType, void*>
QnnImplementation::loaded_lib_handle_;
// NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
std::mutex QnnImplementation::be_init_mutex_;
Error QnnImplementation::StartBackend(
const std::string& lib_path,
const QnnSaver_Config_t** saver_config) {
Qnn_ErrorHandle_t error = QNN_SUCCESS;
#ifdef _WIN32
void* lib_handle = LoadLibrary(lib_path.c_str());
#else
void* lib_handle = dlopen(lib_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
#endif
if (lib_handle == nullptr) {
#ifdef _WIN32
QNN_EXECUTORCH_LOG_ERROR(
"Cannot Open QNN library %s, with error: %d",
lib_path.c_str(),
GetLastError());
#else
QNN_EXECUTORCH_LOG_ERROR(
"Cannot Open QNN library %s, with error: %s",
lib_path.c_str(),
dlerror());
#endif
return Error::Internal;
}
// load get_provider function
auto get_providers = loadQnnFunction<QnnInterfaceGetProvidersFn*>(
lib_handle, "QnnInterface_getProviders");
if (get_providers == nullptr) {
QNN_EXECUTORCH_LOG_ERROR(
"QnnImplementation::Load Cannot load symbol "
"QnnInterface_getProviders : %s",
#ifdef _WIN32
GetLastError());
#else
dlerror());
#endif
return Error::Internal;
}
// Get QnnInterface Providers
std::uint32_t num_providers;
const QnnInterface_t** provider_list = nullptr;
error = get_providers(&provider_list, &num_providers);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
"Qnn Interface failed to get providers. Error %d",
QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
if (num_providers != required_num_providers_) {
QNN_EXECUTORCH_LOG_ERROR(
"Qnn Interface Num Providers is "
"%d instead of required %d",
num_providers,
required_num_providers_);
return Error::Internal;
}
BackendIdType backend_id = provider_list[0]->backendId;
// store everything
lib_path_to_backend_id_[lib_path] = backend_id;
// we use lib_path as the first unique key.
// Users can get wrong like, he or she assigns
// library_path=libQnnHtp_1.so
// library_path=libQnnHtp_2.so
// for different QnnBackend instances.
// So we warning out here.
if (loaded_backend_.count(backend_id) > 0) {
QNN_EXECUTORCH_LOG_WARN(
"lib_path %s is loaded, but backend %d "
"already exists. Overwriting previous loaded backend...",
lib_path.c_str(),
backend_id);
}
loaded_backend_[backend_id] = provider_list[0];
if (loaded_lib_handle_.count(backend_id) > 0) {
QNN_EXECUTORCH_LOG_WARN("closing %pK...", loaded_lib_handle_[backend_id]);
#ifdef _WIN32
if (FreeLibrary(reinterpret_cast<HMODULE>(loaded_lib_handle_[backend_id])) == 0) {
QNN_EXECUTORCH_LOG_WARN(
"Sadly, fail to close %pK with error %d",
loaded_lib_handle_[backend_id],
GetLastError());
}
#else
int dlclose_error = dlclose(loaded_lib_handle_[backend_id]);
if (dlclose_error != 0) {
QNN_EXECUTORCH_LOG_WARN(
"Sadly, fail to close %pK with error %s",
loaded_lib_handle_[backend_id],
dlerror());
}
#endif
}
loaded_lib_handle_[backend_id] = lib_handle;
// Saver backend need initialization.
Error be_init_st = InitBackend(loaded_lib_handle_[backend_id], saver_config);
if (be_init_st != Error::Ok) {
// backend init fails. clear things
lib_path_to_backend_id_.erase(lib_path);
loaded_backend_.erase(backend_id);
#ifdef _WIN32
if (FreeLibrary(reinterpret_cast<HMODULE>(loaded_lib_handle_[backend_id])) == 0) {
QNN_EXECUTORCH_LOG_WARN(
"fail to close %pK after backend-init "
"failure, with error %d",
loaded_lib_handle_[backend_id],
GetLastError());
}
#else
int dlclose_error = dlclose(loaded_lib_handle_[backend_id]);
if (dlclose_error != 0) {
QNN_EXECUTORCH_LOG_WARN(
"fail to close %pK after backend-init "
"failure, with error %s",
loaded_lib_handle_[backend_id],
dlerror());
}
#endif
loaded_lib_handle_.erase(backend_id);
return be_init_st;
}
return Error::Ok;
}
Error QnnImplementation::TerminateAllBackends() {
Error ret_status = Error::Ok;
loaded_backend_.clear();
for (auto& it : loaded_lib_handle_) {
#ifdef _WIN32
if (FreeLibrary(reinterpret_cast<HMODULE>(it.second)) == 0) {
QNN_EXECUTORCH_LOG_ERROR(
"Fail to close QNN backend %d with error %d",
it.first,
GetLastError());
ret_status = Error::Internal;
}
#else
int dlclose_error = dlclose(it.second);
if (dlclose_error != 0) {
QNN_EXECUTORCH_LOG_ERROR(
"Fail to close QNN backend %d with error %s", it.first, dlerror());
ret_status = Error::Internal;
}
#endif
}
loaded_lib_handle_.clear();
lib_path_to_backend_id_.clear();
return ret_status;
}
Error QnnImplementation::Load(const QnnSaver_Config_t** saver_config) {
BackendIdType backend_id = QNN_BACKEND_ID_NULL;
{
const std::lock_guard<std::mutex> lock(be_init_mutex_);
if (lib_path_to_backend_id_.count(lib_path_) == 0) {
Error st = StartBackend(lib_path_, saver_config);
ET_CHECK_OR_RETURN_ERROR(
st == Error::Ok, Internal, "Fail to start backend");
}
// Get backend ID
backend_id = lib_path_to_backend_id_[lib_path_];
// really don't expect.
if (loaded_backend_.count(backend_id) == 0 ||
loaded_lib_handle_.count(backend_id) == 0) {
QNN_EXECUTORCH_LOG_ERROR(
"library %s is loaded but "
"loaded backend count=%zu, "
"loaded lib_handle count=%zu",
lib_path_.c_str(),
loaded_backend_.count(backend_id),
loaded_lib_handle_.count(backend_id));
return Error::Internal;
}
} // be_init_mutex_ release.
// Connect QnnInterface
qnn_interface_.SetQnnInterface(loaded_backend_[backend_id]);
return Error::Ok;
}
const QnnInterface& QnnImplementation::GetQnnInterface() const {
if (!qnn_interface_.IsLoaded()) {
QNN_EXECUTORCH_LOG_WARN(
"GetQnnInterface, returning a QNN interface "
"which is not loaded yet.");
}
return qnn_interface_;
}
} // namespace qnn
} // namespace executor
} // namespace torch