forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQnnManager.cpp
More file actions
678 lines (616 loc) · 24.5 KB
/
QnnManager.cpp
File metadata and controls
678 lines (616 loc) · 24.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
* 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.
*/
#include <executorch/backends/qualcomm/runtime/QnnBackendOptions.h>
#include <executorch/backends/qualcomm/runtime/QnnManager.h>
#include <executorch/backends/qualcomm/runtime/SharedBuffer.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnBackendCommon.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnCustomProtocol.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnImplementation.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <unordered_map>
namespace executorch {
namespace backends {
namespace qnn {
using executorch::runtime::Error;
bool CompareExportedInput(
const std::shared_ptr<TensorWrapper>& a,
const std::shared_ptr<TensorWrapper>& b) {
// Using the order of the nodes as external_id in AOT
// to extract the right arg from *args at runtime
int numA = std::stoi(a->GetName().substr(a->GetName().find('_') + 1));
int numB = std::stoi(b->GetName().substr(b->GetName().find('_') + 1));
return numA < numB;
}
int ExtractMutableBufferNumber(const std::string& name) {
std::string prefix = "mutbuf_";
size_t startPos = name.find(prefix);
if (startPos != std::string::npos) {
startPos += prefix.length();
return std::stoi(name.substr(startPos));
}
return -1;
}
QnnManager::~QnnManager() {
Destroy();
}
QnnManager::QnnManager(
const QnnExecuTorchOptions* options,
const QnnExecuTorchContextBinary& qnn_executorch_context_binary)
: qnn_context_blob_(qnn_executorch_context_binary), options_(options) {
QnnExecuTorchBackendType backend_type =
options->backend_options()->backend_type();
if (get_option(options_->log_level(), QNN_RUNTIME_LOG_LEVEL) >=
QnnExecuTorchLogLevel::kLogLevelInfo) {
QNN_EXECUTORCH_LOG_INFO(
"soc_model in soc_info: %s",
EnumNameQcomChipset(options_->soc_info()->soc_model()));
QNN_EXECUTORCH_LOG_INFO(
"backend_type: %s", EnumNameQnnExecuTorchBackendType(backend_type));
QNN_EXECUTORCH_LOG_INFO(
"library_path: %s", options->library_path()->str().c_str());
QNN_EXECUTORCH_LOG_INFO("dump intermediate outputs: %s", IsTensorDump());
QNN_EXECUTORCH_LOG_INFO(
"log_level: %s",
EnumNameQnnExecuTorchLogLevel(
get_option(options_->log_level(), QNN_RUNTIME_LOG_LEVEL)));
QNN_EXECUTORCH_LOG_INFO(
"profile_level: %s",
EnumNameQnnExecuTorchProfileLevel(
get_option(options_->profile_level(), QNN_RUNTIME_PROFILE_LEVEL)));
QNN_EXECUTORCH_LOG_INFO(
"the size of qnn context binary: %d",
qnn_executorch_context_binary.nbytes);
QNN_EXECUTORCH_LOG_INFO(
"Is on-device graph construction: %d", options->online_prepare());
QNN_EXECUTORCH_LOG_INFO(
"Enable shared buffer: %d", options->shared_buffer());
QNN_EXECUTORCH_LOG_INFO(
"The number of op packages: %d",
options_->op_package_options()->op_package_infos()->size());
}
backend_params_ptr_ = std::make_unique<BackendConfigParameters>();
backend_bundle_ptr_ = std::make_shared<QnnBackendBundle>();
qnn_dlc_manager_ =
std::make_shared<QnnDlcManager>(qnn_context_blob_, options_);
}
Error QnnManager::RegisterMem(
void* data_ptr,
const std::shared_ptr<TensorWrapper>& tensor_wrapper) {
SharedBuffer& shared_buffer_manager = SharedBuffer::GetSharedBufferManager();
// Not enable shared buffer
if (!options_->shared_buffer()) {
return Error::Internal;
}
if (backend_params_ptr_->qnn_mem_manager_ptr_ == nullptr) {
QNN_EXECUTORCH_LOG_WARN(
"Backend %s doesn't supported shared buffer.",
EnumNameQnnExecuTorchBackendType(
options_->backend_options()->backend_type()));
return Error::Internal;
}
void* custom_mem_base = shared_buffer_manager.GetCustomMemBase(data_ptr);
if (custom_mem_base != nullptr) {
return RegisterCustomMem(data_ptr, custom_mem_base, tensor_wrapper);
}
return RegisterIonMem(data_ptr, tensor_wrapper);
}
Error QnnManager::RegisterIonMem(
void* data_ptr,
const std::shared_ptr<TensorWrapper>& tensor_wrapper) {
SharedBuffer& shared_buffer_manager = SharedBuffer::GetSharedBufferManager();
if (!shared_buffer_manager.IsAllocated(data_ptr)) {
// It means two scenarios here:
// 1. the input and output partitioned graph
// 2. Actually, user doesn't allocate shared buffer with
// QnnExecuTorchAllocCustomMem API
return Error::Internal;
} else if (backend_params_ptr_->qnn_mem_manager_ptr_->IsRegistered(
tensor_wrapper->GetMemHandle(), data_ptr)) {
if (get_option(options_->log_level(), QNN_RUNTIME_LOG_LEVEL) >=
QnnExecuTorchLogLevel::kLogLevelInfo)
QNN_EXECUTORCH_LOG_INFO(
"Tensor name %s has been registered shared memory.",
tensor_wrapper->GetName().c_str());
return Error::Ok;
}
int32_t mem_fd = shared_buffer_manager.MemToFd(data_ptr);
if (mem_fd == -1) {
QNN_EXECUTORCH_LOG_WARN(
"Tensor name %s is failed to get file descriptor.",
tensor_wrapper->GetName().c_str());
return Error::Internal;
}
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_mem_manager_ptr_->RegisterIonMem(
tensor_wrapper, mem_fd, data_ptr) == Error::Ok,
Internal,
"Fail to register to shared memory.");
return Error::Ok;
}
Error QnnManager::RegisterCustomMem(
void* data_ptr,
void* custom_mem_base,
const std::shared_ptr<TensorWrapper>& tensor_wrapper) {
if (backend_params_ptr_->qnn_mem_manager_ptr_->IsRegistered(
tensor_wrapper->GetMemHandle(), data_ptr)) {
if (get_option(options_->log_level(), QNN_RUNTIME_LOG_LEVEL) >=
QnnExecuTorchLogLevel::kLogLevelInfo)
QNN_EXECUTORCH_LOG_INFO(
"Tensor name %s has been registered shared memory.",
tensor_wrapper->GetName().c_str());
return Error::Ok;
}
CustomMemTensorInfo info{
custom_mem_base,
data_ptr,
static_cast<size_t>(
static_cast<char*>(data_ptr) - static_cast<char*>(custom_mem_base)),
tensor_wrapper->GetBytes(),
tensor_wrapper->GetDims(),
tensor_wrapper->GetRank(),
qnn_dtype_to_scalar_type_[tensor_wrapper->GetDataType()]};
Qnn_MemHandle_t pre_registered_handle =
backend_params_ptr_->qnn_mem_manager_ptr_->GetPreRegisteredHandle(info);
// If this memory block has already been registered, we can use it directly.
// This applies when running llama in lookahead mode with the same AR-N model
// handling both the prompt processor and the token generator.
if (pre_registered_handle != nullptr) {
if (get_option(options_->log_level(), QNN_RUNTIME_LOG_LEVEL) >=
QnnExecuTorchLogLevel::kLogLevelInfo) {
QNN_EXECUTORCH_LOG_INFO(
"Tensor name %s found a pre-registered memHandle.",
tensor_wrapper->GetName().c_str());
}
return backend_params_ptr_->qnn_mem_manager_ptr_->SetMemHandle(
tensor_wrapper, data_ptr, pre_registered_handle);
}
SharedBuffer& shared_buffer_manager = SharedBuffer::GetSharedBufferManager();
size_t tensor_offset = info.pos;
size_t total_custom_mem_size =
shared_buffer_manager.GetAllocatedSize(custom_mem_base);
int32_t mem_fd = shared_buffer_manager.MemToFd(custom_mem_base);
// Note: If obtaining the file descriptor fails, it may be due to memory not
// being released with QnnExecuTorchFreeCustomMem. In this situation, we could
// consider adding a map to monitor it.
if (mem_fd == -1) {
QNN_EXECUTORCH_LOG_WARN(
"Tensor name %s failed to get file descriptor.",
tensor_wrapper->GetName().c_str());
return Error::Internal;
}
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_mem_manager_ptr_->RegisterCustomMem(
tensor_wrapper,
mem_fd,
data_ptr,
total_custom_mem_size,
tensor_offset,
info) == Error::Ok,
Internal,
"Fail to register to shared memory.");
return Error::Ok;
}
Error QnnManager::InitBackend() {
// Get or create the shared backend bundle
Error err = QnnBackendUnifiedRegistry::GetInstance().GetOrCreateBackendBundle(
options_, backend_bundle_ptr_);
ET_CHECK_OR_RETURN_ERROR(
err == Error::Ok,
Internal,
"Fail to get or create shared Qnn backend bundle. Error code: %d",
static_cast<int>(err));
return Error::Ok;
}
Error QnnManager::InitContext(
std::optional<std::vector<std::string>> graph_names) {
if (backend_params_ptr_->backend_init_state_ ==
BackendInitializeState::UNINITIALIZED) {
QNN_EXECUTORCH_LOG_INFO(
"Initialize Qnn backend "
"parameters for Qnn executorch backend type %d",
options_->backend_options()->backend_type());
backend_params_ptr_ = QnnBackendFactory().Create(
backend_bundle_ptr_->implementation.get(),
backend_bundle_ptr_->qnn_backend_ptr.get(),
backend_bundle_ptr_->qnn_device_ptr.get(),
qnn_context_blob_,
options_,
qnn_dlc_manager_.get());
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_ != nullptr,
Internal,
"Failed to load Qnn backend.");
// Note: For online_prepare or deserialization, the graph name will be
// obtained from the binary.
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_backend_cache_ptr_->Configure(
graph_names.value_or(std::vector<std::string>{})) == Error::Ok,
Internal,
"Fail to configure Qnn backend cache");
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_context_ptr_->Configure() == Error::Ok,
Internal,
"Fail to configure Qnn context");
for (const std::string& graph_name :
backend_params_ptr_->qnn_context_ptr_->GetGraphNames()) {
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_graph_ptr_->Configure(graph_name) ==
Error::Ok,
Internal,
"Fail to configure Qnn graph");
}
backend_params_ptr_->backend_init_state_ =
BackendInitializeState::INITIALIZED;
}
if (IsOnlinePrepare()) {
// Check whether the QNN version supports the DLC format.
Qnn_ApiVersion_t qnn_version = {QNN_VERSION_INIT};
backend_bundle_ptr_->implementation->GetQnnInterface()
.qnn_backend_get_api_version(&qnn_version);
ET_CHECK_OR_RETURN_ERROR(
qnn_dlc_manager_->SetUpDlcEnvironment(
qnn_version.coreApiVersion,
graph_names.value_or(std::vector<std::string>{})) == Error::Ok,
Internal,
"Fail to setup Dlc environment");
}
return Error::Ok;
}
Error QnnManager::InitContextCache() {
if (backend_params_ptr_->backend_init_state_ ==
BackendInitializeState::UNINITIALIZED) {
QNN_EXECUTORCH_LOG_INFO(
"Initialize Qnn backend "
"parameters for Qnn executorch backend type %d",
options_->backend_options()->backend_type());
backend_params_ptr_ = QnnBackendFactory().Create(
backend_bundle_ptr_->implementation.get(),
backend_bundle_ptr_->qnn_backend_ptr.get(),
backend_bundle_ptr_->qnn_device_ptr.get(),
qnn_context_blob_,
options_,
qnn_dlc_manager_.get());
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_ != nullptr,
Internal,
"Failed to load Qnn backend.");
// Note: For online_prepare or deserialization, the graph name will be
// obtained from the binary.
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_backend_cache_ptr_->Configure({}) == Error::Ok,
Internal,
"Fail to configure Qnn backend cache");
backend_params_ptr_->backend_init_state_ =
BackendInitializeState::INITIALIZED;
}
return Error::Ok;
}
Error QnnManager::AllocateTensor(const std::string& graph_name) {
std::vector<Qnn_Tensor_t> input_tensors =
backend_params_ptr_->qnn_context_ptr_->GetGraphInputs(graph_name);
std::vector<Qnn_Tensor_t> output_tensors =
backend_params_ptr_->qnn_context_ptr_->GetGraphOutputs(graph_name);
// Mapping memory address for the input and output of mutable buffer
std::unordered_map<int, const void*> mutable_buffer_id_to_memory_map;
for (auto& tensor : input_tensors) {
std::shared_ptr<TensorWrapper> tensor_wrapper = CreateTensorWrapper(tensor);
tensor_wrapper->UpdateQnnTensorMeta(tensor);
int mutable_buffer_id =
ExtractMutableBufferNumber(tensor_wrapper->GetName());
if (mutable_buffer_id != -1) {
// Delegate maintains the memory for mutable buffer
tensor_wrapper->AllocateDataBuffer();
mutable_buffer_id_to_memory_map[mutable_buffer_id] =
tensor_wrapper->GetStaticTensorData();
}
input_tensors_[graph_name].emplace_back(std::move(tensor_wrapper));
}
if (!options_->is_from_context_binary()) {
std::sort(
input_tensors_[graph_name].begin(),
input_tensors_[graph_name].end(),
CompareExportedInput);
}
for (size_t i = 0; i < output_tensors.size(); ++i) {
std::shared_ptr<TensorWrapper> tensor_wrapper =
CreateTensorWrapper(output_tensors[i]);
tensor_wrapper->UpdateQnnTensorMeta(output_tensors[i]);
const std::string& tensor_name = tensor_wrapper->GetName();
// this is required by identifying shared buffer mechanism
// info might be missed if context binary came from qnn_converter
if (options_->is_from_context_binary() &&
tensor_name.find("output_") == std::string::npos) {
tensor_wrapper->SetName("output_" + tensor_name);
}
if (IsTensorDump()) {
tensor_wrapper->AllocateDataBuffer();
}
int mutable_buffer_id =
ExtractMutableBufferNumber(tensor_wrapper->GetName());
if (mutable_buffer_id != -1 &&
mutable_buffer_id_to_memory_map.find(mutable_buffer_id) !=
mutable_buffer_id_to_memory_map.end()) {
// Fill the same memory for I/O of mutable buffer
tensor_wrapper->FillDataBuffer(
mutable_buffer_id_to_memory_map[mutable_buffer_id]);
}
output_tensors_[graph_name].emplace_back(std::move(tensor_wrapper));
}
return Error::Ok;
}
Error QnnManager::AllocateTensor(
const std::string& graph_name,
std::vector<std::shared_ptr<TensorWrapper>>& inputs,
std::vector<std::shared_ptr<TensorWrapper>>& outputs) {
input_tensors_[graph_name] = std::move(inputs);
// TODO: suuport per-tensor dump in online prepare mode
// should be achievable with some pre-process
if (!options_->is_from_context_binary()) {
std::sort(
input_tensors_[graph_name].begin(),
input_tensors_[graph_name].end(),
CompareExportedInput);
}
output_tensors_[graph_name] = std::move(outputs);
return Error::Ok;
}
Error QnnManager::Execute(
const std::string& graph_name,
const std::vector<Qnn_Tensor_t>& input_tensor_structs,
std::vector<Qnn_Tensor_t>& output_tensor_structs,
executorch::runtime::EventTracer* event_tracer) {
Qnn_ErrorHandle_t error = QNN_SUCCESS;
error = backend_params_ptr_->qnn_graph_ptr_->GraphExecute(
graph_name, input_tensor_structs, output_tensor_structs);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
"qnn_graph_execute failed. Error %d", QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
if (IsTensorDump()) {
// TODO: Need to handle the graph which is partitioned.
// Maybe we could use graph name.
for (std::size_t out_idx = 0; out_idx < output_tensor_structs.size();
++out_idx) {
const Qnn_Tensor_t& output_tensor = output_tensor_structs[out_idx];
std::vector<executorch::aten::SizesType> sizes(
QNN_TENSOR_VER_PTR(output_tensor)->dimensions,
QNN_TENSOR_VER_PTR(output_tensor)->dimensions +
QNN_TENSOR_VER_PTR(output_tensor)->rank);
// Compute contiguous strides from sizes (e.g. [2,3,4] -> [12,4,1]).
std::vector<executorch::aten::StridesType> stride_size(sizes.size());
if (!sizes.empty()) {
stride_size.back() = 1;
for (int i = sizes.size() - 2; i >= 0; --i) {
stride_size[i] = stride_size[i + 1] * sizes[i + 1];
}
}
// Avoid using from_blob as it significantly increases shared library
// size.
executorch::aten::TensorImpl tensor_impl(
qnn_dtype_to_scalar_type_[QNN_TENSOR_VER_PTR(output_tensor)
->dataType],
sizes.size(),
sizes.data(),
QNN_TENSOR_VER_PTR(output_tensor)->clientBuf.data,
nullptr,
stride_size.data());
executorch::runtime::event_tracer_log_output_delegate<
executorch::aten::Tensor>(
event_tracer,
QNN_TENSOR_VER_PTR(output_tensor)->name,
/*delegate_debug_id=*/
static_cast<executorch::runtime::DebugHandle>(-1),
executorch::aten::Tensor(&tensor_impl));
}
}
return Error::Ok;
}
Error QnnManager::ProfileExecuteData(
const std::string& graph_name,
executorch::runtime::EventTracer* event_tracer) {
Qnn_ErrorHandle_t error = QNN_SUCCESS;
if (get_option(options_->profile_level(), QNN_RUNTIME_PROFILE_LEVEL) !=
QnnExecuTorchProfileLevel::kProfileOff) {
error = backend_params_ptr_->qnn_graph_ptr_->ProfileExecuteData(
graph_name, event_tracer);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
" Failed to profile. Error %d", QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
}
return Error::Ok;
}
void QnnManager::Destroy() {
backend_params_ptr_.reset(new BackendConfigParameters());
backend_bundle_ptr_.reset(new QnnBackendBundle());
qnn_dlc_manager_->Destroy();
}
void QnnManager::DestroyContext() {
backend_params_ptr_.reset(new BackendConfigParameters());
qnn_dlc_manager_->Destroy();
}
bool QnnManager::IsNodeSupportedByBackend(
std::vector<std::shared_ptr<OpWrapper>>& op_wrappers) {
Qnn_ErrorHandle_t error = QNN_SUCCESS;
for (std::shared_ptr<OpWrapper>& op_wrapper : op_wrappers) {
for (const auto& param : op_wrapper->GetParams()) {
// unused?
// auto* p_tensor_param = dynamic_cast<TensorParamWrapper*>(param.get());
if (param->PopulateQnnParam() != Error::Ok) {
QNN_EXECUTORCH_LOG_WARN(
"Qnn Backend op validation failed "
"with PopulateQnnParam: %d",
QNN_GET_ERROR_CODE(error));
return false;
}
}
error = backend_bundle_ptr_->qnn_backend_ptr->BackendValidateOpConfig(
op_wrapper->GetOpConfig());
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_WARN(
"Qnn Backend op validation failed with error: %d",
QNN_GET_ERROR_CODE(error));
return false;
}
}
return true;
}
Error QnnManager::GetContextBinary(
QnnExecuTorchContextBinary& qnn_executorch_context_binary) {
if (IsOnlinePrepare() &&
qnn_dlc_manager_->backend_params_ptr_->qnn_context_ptr_.get() !=
nullptr) {
ET_CHECK_OR_RETURN_ERROR(
qnn_dlc_manager_->backend_params_ptr_->qnn_context_ptr_
->GetContextBinary(qnn_executorch_context_binary) == Error::Ok,
Internal,
"Fail to get context binary.");
}
else {
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_->qnn_context_ptr_->GetContextBinary(
qnn_executorch_context_binary) == Error::Ok,
Internal,
"Fail to get context binary.");
}
return Error::Ok;
}
Error QnnManager::CompileDlc() {
Qnn_ErrorHandle_t error;
auto qnn_dlc_graph_info = qnn_dlc_manager_->GetQnnDlcGraphInfoPtr();
uint32_t qnn_dlc_graph_info_num = qnn_dlc_manager_->GetQnnDlcGraphInfoNum();
for (uint32_t i = 0; i < qnn_dlc_graph_info_num; ++i) {
auto& graphInfo = (*qnn_dlc_graph_info)[i];
backend_params_ptr_->qnn_graph_ptr_->SetGraphHandle(
graphInfo.graphName, graphInfo.graph);
error =
backend_params_ptr_->qnn_graph_ptr_->GraphFinalize(graphInfo.graphName);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
"Failed to finalize Qnn Graph with error: %d",
QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
std::vector<std::shared_ptr<TensorWrapper>> graph_inputs, graph_outputs,
tensors;
// Mapping memory address for the input and output of mutable buffer
std::unordered_map<int, const void*> mutable_buffer_id_to_memory_map;
for (uint32_t i = 0; i < graphInfo.numInputTensors; ++i) {
auto tw = CreateTensorWrapper(graphInfo.inputTensors[i]);
tw->UpdateQnnTensorMeta(graphInfo.inputTensors[i]);
int mutable_buffer_id = ExtractMutableBufferNumber(tw->GetName());
if (mutable_buffer_id != -1) {
// Delegate maintains the memory for mutable buffer
tw->AllocateDataBuffer();
mutable_buffer_id_to_memory_map[mutable_buffer_id] =
tw->GetStaticTensorData();
}
graph_inputs.push_back(tw);
}
for (uint32_t i = 0; i < graphInfo.numOutputTensors; ++i) {
auto tw = CreateTensorWrapper(graphInfo.outputTensors[i]);
tw->UpdateQnnTensorMeta(graphInfo.outputTensors[i]);
int mutable_buffer_id = ExtractMutableBufferNumber(tw->GetName());
if (mutable_buffer_id != -1 &&
mutable_buffer_id_to_memory_map.find(mutable_buffer_id) !=
mutable_buffer_id_to_memory_map.end()) {
// Fill the same memory for I/O of mutable buffer
tw->FillDataBuffer(mutable_buffer_id_to_memory_map[mutable_buffer_id]);
}
graph_outputs.push_back(tw);
}
ET_CHECK_OR_RETURN_ERROR(
AllocateTensor(graphInfo.graphName, graph_inputs, graph_outputs) ==
Error::Ok,
Internal,
"Fail to allocate tensor for Dlc with graph_name: %s",
graphInfo.graphName);
}
return Error::Ok;
}
Error QnnManager::Compile(
const std::string& graph_name,
std::vector<std::shared_ptr<OpWrapper>>& op_wrappers) {
Qnn_ErrorHandle_t error = QNN_SUCCESS;
QnnGraph* qnn_graph_ptr = backend_params_ptr_->qnn_graph_ptr_.get();
if (IsOnlinePrepare() &&
qnn_dlc_manager_->backend_params_ptr_->qnn_graph_ptr_.get() != nullptr) {
qnn_graph_ptr = qnn_dlc_manager_->backend_params_ptr_->qnn_graph_ptr_.get();
}
for (std::shared_ptr<OpWrapper>& op_wrapper : op_wrappers) {
for (const auto& tensor_wrapper : op_wrapper->GetInputTensors()) {
ET_CHECK_OR_RETURN_ERROR(
qnn_graph_ptr->EnsureTensorInQnnGraph(graph_name, tensor_wrapper) ==
Error::Ok,
Internal,
"Tensor name %s isn't added to Qnn Graph",
tensor_wrapper->GetName().c_str());
}
for (const auto& tensor_wrapper : op_wrapper->GetOutputTensors()) {
ET_CHECK_OR_RETURN_ERROR(
qnn_graph_ptr->EnsureTensorInQnnGraph(graph_name, tensor_wrapper) ==
Error::Ok,
Internal,
"Tensor name %s isn't added to Qnn Graph",
tensor_wrapper->GetName().c_str());
}
for (const auto& param : op_wrapper->GetParams()) {
auto* p_tensor_param = dynamic_cast<TensorParamWrapper*>(param.get());
if (p_tensor_param != nullptr) {
ET_CHECK_OR_RETURN_ERROR(
qnn_graph_ptr->EnsureTensorInQnnGraph(
graph_name, p_tensor_param->GetTensorWrapper()) == Error::Ok,
Internal,
"Param tensor name %s isn't added to Qnn Graph",
p_tensor_param->GetName().c_str());
}
ET_CHECK_OR_RETURN_ERROR(
param->PopulateQnnParam() == Error::Ok,
Internal,
"Fail to configure Qnn backend");
}
error = qnn_graph_ptr->GraphAddNode(graph_name, op_wrapper->GetOpConfig());
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
"Failed to add node to Qnn Graph with error: %d",
QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
}
error = qnn_graph_ptr->GraphFinalize(graph_name);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_ERROR(
"Failed to finalize Qnn Graph with error: %d",
QNN_GET_ERROR_CODE(error));
return Error::Internal;
}
return Error::Ok;
}
} // namespace qnn
} // namespace backends
} // namespace executorch
void* QnnExecuTorchAllocCustomMem(size_t bytes, size_t alignment) {
void* buffer_ptr =
executorch::backends::qnn::SharedBuffer::GetSharedBufferManager()
.AllocMem(bytes, alignment);
return buffer_ptr;
}
void QnnExecuTorchFreeCustomMem(void* buffer_ptr) {
executorch::backends::qnn::SharedBuffer::GetSharedBufferManager().FreeMem(
buffer_ptr);
}
void QnnExecuTorchAddCustomMemTensorAddr(void* tensor_addr, void* custom_mem) {
executorch::backends::qnn::SharedBuffer::GetSharedBufferManager()
.AddCusomMemTensorAddr(tensor_addr, custom_mem);
}