This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
BasePreparedModel.cpp
751 lines (671 loc) · 30.6 KB
/
BasePreparedModel.cpp
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "BasePreparedModel.h"
#include <android-base/logging.h>
#include <android/log.h>
#include <cutils/properties.h>
#include <log/log.h>
#include <thread>
#include "ExecutionBurstServer.h"
#include "Utils.h"
#include "ValidateHal.h"
#undef LOG_TAG
#define DISABLE_ALL_QUANT
#define LOG_TAG "BasePreparedModel"
namespace android {
namespace hardware {
namespace neuralnetworks {
namespace nnhal {
using namespace android::nn;
static const Timing kNoTiming = {.timeOnDevice = UINT64_MAX, .timeInDriver = UINT64_MAX};
void BasePreparedModel::deinitialize() {
ALOGV("Entering %s", __func__);
mModelInfo->unmapRuntimeMemPools();
ALOGV("Exiting %s", __func__);
}
template <typename T>
T getScalarData(const RunTimeOperandInfo& info) {
// TODO: Check buffer is at least as long as size of data.
T* data = reinterpret_cast<T*>(info.buffer);
return data[0];
}
bool BasePreparedModel::initialize() {
ALOGV("Entering %s", __func__);
return true;
}
static Return<void> notify(const sp<V1_0::IExecutionCallback>& callback, const ErrorStatus& status,
const hidl_vec<OutputShape>&, Timing) {
return callback->notify(status);
}
static Return<void> notify(const sp<V1_2::IExecutionCallback>& callback, const ErrorStatus& status,
const hidl_vec<OutputShape>& outputShapes, Timing timing) {
return callback->notify_1_2(status, outputShapes, timing);
}
static Return<void> notify(const sp<V1_3::IExecutionCallback>& callback, const ErrorStatus& status,
const hidl_vec<OutputShape>& outputShapes, Timing timing) {
return callback->notify_1_3(convertToV1_3(status), outputShapes, timing);
}
static void floatToUint8(const float* src, uint8_t* dst, size_t size) {
for (uint32_t i = 0; i < size; ++i) {
dst[i] = static_cast<uint8_t>(src[i]);
ALOGV("%s input: %f output: %d ", __func__, src[i], dst[i]);
}
}
static void floatToint8(const float* src, int8_t* dst, size_t size) {
for (uint32_t i = 0; i < size; ++i) {
dst[i] = static_cast<int8_t>(src[i]);
ALOGV("%s input: %f output: %d ", __func__, src[i], dst[i]);
}
}
static void floatToFloat16(const float* src, _Float16* dst, size_t size) {
for (uint32_t i = 0; i < size; ++i) {
dst[i] = src[i];
ALOGV("%s input: %f output: %f ", __func__, src[i], dst[i]);
}
}
static void floatToInt16(const float* src, int16_t* dst, size_t size) {
for (uint32_t i = 0; i < size; ++i) {
dst[i] = static_cast<int16_t>(src[i]);
ALOGV("%s input: %f output: %hd ", __func__, src[i], dst[i]);
}
}
static void floatToUInt16(const float* src, uint16_t* dst, size_t size) {
for (uint32_t i = 0; i < size; ++i) {
dst[i] = static_cast<uint16_t>(src[i]);
ALOGV("%s input: %f output: %hu ", __func__, src[i], dst[i]);
}
}
namespace {
using time_point = std::chrono::steady_clock::time_point;
auto now() { return std::chrono::steady_clock::now(); };
auto microsecondsDuration(decltype(now()) end, decltype(now()) start) {
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
};
} // namespace
template <typename T_IExecutionCallback>
Return<ErrorStatus> executeBase(const Request& request, MeasureTiming measure,
BasePreparedModel* preparedModel,
const sp<T_IExecutionCallback>& callback) {
ALOGV("Entering %s", __func__);
time_point driverStart;
if (measure == MeasureTiming::YES) driverStart = now();
if (callback.get() == nullptr) {
ALOGE("invalid callback passed to execute");
return ErrorStatus::INVALID_ARGUMENT;
}
if (!validateRequest(request, convertToV1_2(preparedModel->getModelInfo()->getModel()))) {
notify(callback, ErrorStatus::INVALID_ARGUMENT, {}, kNoTiming);
return ErrorStatus::INVALID_ARGUMENT;
}
// This thread is intentionally detached because the driver service
// is expected to live forever.
std::thread([preparedModel, request, measure, driverStart, callback] {
asyncExecute(request, measure, preparedModel, driverStart, callback);
}).detach();
ALOGV("Exiting %s", __func__);
return ErrorStatus::NONE;
}
template <typename T_IExecutionCallback>
void asyncExecute(const Request& request, MeasureTiming measure, BasePreparedModel* preparedModel,
time_point driverStart, const sp<T_IExecutionCallback>& callback) {
ALOGV("Entering %s", __func__);
auto modelInfo = preparedModel->getModelInfo();
auto plugin = preparedModel->getPlugin();
auto ngraphNw = preparedModel->getNgraphNwCreator();
time_point driverEnd, deviceStart, deviceEnd;
std::vector<RunTimePoolInfo> requestPoolInfos;
auto errorStatus = modelInfo->setRunTimePoolInfosFromHidlMemories(request.pools);
if (errorStatus != ErrorStatus::NONE) {
ALOGE("Failed to set runtime pool info from HIDL memories");
notify(callback, ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
return;
}
for (size_t i = 0; i < request.inputs.size(); i++) {
uint32_t len;
auto inIndex = modelInfo->getModelInputIndex(i);
void* srcPtr = modelInfo->getBlobFromMemoryPoolIn(request, i, len);
const std::string& inputNodeName = ngraphNw->getNodeName(inIndex);
if (inputNodeName == "") {
ALOGD("Ignorning input at index(%d), since it is invalid", inIndex);
continue;
}
ALOGD("Input index: %d layername : %s", inIndex, inputNodeName.c_str());
auto destBlob = plugin->getBlob(inputNodeName);
if (modelInfo->getOperandType(inIndex) == OperandType::TENSOR_FLOAT16) {
float* dest = destBlob->buffer().as<float*>();
_Float16* src = (_Float16*)srcPtr;
for (unsigned int i = 0; i < len / 2; i++) {
dest[i] = src[i];
}
} else {
uint8_t* dest = destBlob->buffer().as<uint8_t*>();
std::memcpy(dest, (uint8_t*)srcPtr, len);
}
}
ALOGD("%s Run", __func__);
if (measure == MeasureTiming::YES) deviceStart = now();
try {
plugin->infer();
} catch (const std::exception& ex) {
ALOGE("%s Exception !!! %s", __func__, ex.what());
notify(callback, ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
return;
}
if (measure == MeasureTiming::YES) deviceEnd = now();
for (size_t i = 0; i < request.outputs.size(); i++) {
auto outIndex = modelInfo->getModelOutputIndex(i);
ALOGI("OutputIndex: %d", outIndex);
const std::string& outputNodeName = ngraphNw->getNodeName(outIndex);
if (outputNodeName == "") {
ALOGD("Ignorning output at index(%d), since it is invalid", outIndex);
continue;
}
ALOGD("Output index: %d layername : %s", outIndex, outputNodeName.c_str());
auto srcBlob = plugin->getBlob(outputNodeName);
auto operandType = modelInfo->getOperandType(outIndex);
uint32_t actualLength = srcBlob->byteSize();
uint32_t expectedLength = 0;
void* destPtr = modelInfo->getBlobFromMemoryPoolOut(request, i, expectedLength);
auto outputBlobDims = srcBlob->getTensorDesc().getDims();
ALOGD("output precision: %d", static_cast<int>(srcBlob->getTensorDesc().getPrecision()));
switch (operandType) {
case OperandType::TENSOR_BOOL8:
case OperandType::TENSOR_QUANT8_ASYMM:
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
actualLength /= 4;
break;
case OperandType::TENSOR_FLOAT16:
case OperandType::TENSOR_QUANT16_SYMM:
case OperandType::TENSOR_QUANT16_ASYMM:
actualLength /= 2;
break;
default:
ALOGV("Operand type is 4 bytes !!");
break;
}
bool outputSizeMismatch = false;
if (actualLength != expectedLength) {
ALOGE("%s Invalid length at outIndex(%d) Actual:%d Expected:%d", __func__, outIndex,
actualLength, expectedLength);
outputSizeMismatch = true;
}
// TODO: bug identified with OV2021.4 where for Pad operation, if the output dimensions is 1
// output dimension is coming as 0
if ((outputBlobDims.size() == 0) && (actualLength != 0)) {
std::vector<size_t> rdims = {1};
modelInfo->updateOutputshapes(i, rdims, outputSizeMismatch ? false : true);
} else
modelInfo->updateOutputshapes(i, outputBlobDims, outputSizeMismatch ? false : true);
if (outputSizeMismatch) {
ALOGE(
"Mismatch in actual and exepcted output sizes. Return with "
"OUTPUT_INSUFFICIENT_SIZE error");
notify(callback, ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, modelInfo->getOutputShapes(),
kNoTiming);
return;
}
switch (operandType) {
case OperandType::TENSOR_INT32:
case OperandType::TENSOR_FLOAT32: {
std::memcpy((uint8_t*)destPtr, srcBlob->buffer().as<uint8_t*>(),
srcBlob->byteSize());
break;
}
case OperandType::TENSOR_BOOL8: {
floatToUint8(srcBlob->buffer().as<float*>(), (uint8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT8_ASYMM: {
floatToUint8(srcBlob->buffer().as<float*>(), (uint8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
floatToint8(srcBlob->buffer().as<float*>(), (int8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_FLOAT16: {
floatToFloat16(srcBlob->buffer().as<float*>(), (_Float16*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT16_SYMM: {
floatToInt16(srcBlob->buffer().as<float*>(), (int16_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT16_ASYMM: {
floatToUInt16(srcBlob->buffer().as<float*>(), (uint16_t*)destPtr, srcBlob->size());
break;
}
default:
std::memcpy((uint8_t*)destPtr, srcBlob->buffer().as<uint8_t*>(),
srcBlob->byteSize());
break;
}
}
if (!modelInfo->updateRequestPoolInfos()) {
ALOGE("Failed to update the request pool infos");
}
Return<void> returned;
if (measure == MeasureTiming::YES) {
driverEnd = now();
Timing timing = {.timeOnDevice = uint64_t(microsecondsDuration(deviceEnd, deviceStart)),
.timeInDriver = uint64_t(microsecondsDuration(driverEnd, driverStart))};
returned = notify(callback, ErrorStatus::NONE, modelInfo->getOutputShapes(), timing);
} else {
returned = notify(callback, ErrorStatus::NONE, modelInfo->getOutputShapes(), kNoTiming);
}
if (!returned.isOk()) {
ALOGE("hidl callback failed to return properly: %s", returned.description().c_str());
}
ALOGV("Exiting %s", __func__);
}
static std::tuple<ErrorStatus, hidl_vec<V1_2::OutputShape>, Timing> executeSynchronouslyBase(
const Request& request, MeasureTiming measure, BasePreparedModel* preparedModel,
time_point driverStart) {
ALOGV("Entering %s", __func__);
auto modelInfo = preparedModel->getModelInfo();
auto plugin = preparedModel->getPlugin();
auto ngraphNw = preparedModel->getNgraphNwCreator();
time_point driverEnd, deviceStart, deviceEnd;
std::vector<RunTimePoolInfo> requestPoolInfos;
auto errorStatus = modelInfo->setRunTimePoolInfosFromHidlMemories(request.pools);
if (errorStatus != ErrorStatus::NONE) {
ALOGE("Failed to set runtime pool info from HIDL memories");
return {ErrorStatus::GENERAL_FAILURE, {}, kNoTiming};
}
for (size_t i = 0; i < request.inputs.size(); i++) {
uint32_t len;
auto inIndex = modelInfo->getModelInputIndex(i);
void* srcPtr = modelInfo->getBlobFromMemoryPoolIn(request, i, len);
const std::string& inputNodeName = ngraphNw->getNodeName(inIndex);
if (inputNodeName == "") {
ALOGD("Ignorning input at index(%d), since it is invalid", inIndex);
continue;
}
ALOGD("Input index: %d layername : %s", inIndex, inputNodeName.c_str());
auto destBlob = plugin->getBlob(inputNodeName);
if (modelInfo->getOperandType(inIndex) == OperandType::TENSOR_FLOAT16) {
float* dest = destBlob->buffer().as<float*>();
_Float16* src = (_Float16*)srcPtr;
for (unsigned int i = 0; i < len / 2; i++) {
dest[i] = src[i];
}
} else {
uint8_t* dest = destBlob->buffer().as<uint8_t*>();
std::memcpy(dest, (uint8_t*)srcPtr, len);
}
}
ALOGD("%s Run", __func__);
if (measure == MeasureTiming::YES) deviceStart = now();
try {
plugin->infer();
} catch (const std::exception& ex) {
ALOGE("%s Exception !!! %s", __func__, ex.what());
return {ErrorStatus::GENERAL_FAILURE, {}, kNoTiming};
}
if (measure == MeasureTiming::YES) deviceEnd = now();
for (size_t i = 0; i < request.outputs.size(); i++) {
auto outIndex = modelInfo->getModelOutputIndex(i);
ALOGI("OutputIndex: %d", outIndex);
const std::string& outputNodeName = ngraphNw->getNodeName(outIndex);
if (outputNodeName == "") {
ALOGD("Ignorning output at index(%d), since it is invalid", outIndex);
continue;
}
ALOGD("Output index: %d layername : %s", outIndex, outputNodeName.c_str());
auto srcBlob = plugin->getBlob(outputNodeName);
auto operandType = modelInfo->getOperandType(outIndex);
uint32_t actualLength = srcBlob->byteSize();
uint32_t expectedLength = 0;
void* destPtr = modelInfo->getBlobFromMemoryPoolOut(request, i, expectedLength);
auto outputBlobDims = srcBlob->getTensorDesc().getDims();
ALOGD("output precision: %d", static_cast<int>(srcBlob->getTensorDesc().getPrecision()));
switch (operandType) {
case OperandType::TENSOR_BOOL8:
case OperandType::TENSOR_QUANT8_ASYMM:
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
actualLength /= 4;
break;
case OperandType::TENSOR_FLOAT16:
case OperandType::TENSOR_QUANT16_SYMM:
case OperandType::TENSOR_QUANT16_ASYMM:
actualLength /= 2;
break;
default:
ALOGV("Operand type is 4 bytes !!");
break;
}
bool outputSizeMismatch = false;
if (actualLength != expectedLength) {
ALOGE("%s Invalid length at outIndex(%d) Actual:%d Expected:%d", __func__, outIndex,
actualLength, expectedLength);
outputSizeMismatch = true;
}
// TODO: bug identified with OV2021.4 where for Pad operation, if the output dimensions is 1
// output dimension is coming as 0
if ((outputBlobDims.size() == 0) && (actualLength != 0)) {
std::vector<size_t> rdims = {1};
modelInfo->updateOutputshapes(i, rdims, outputSizeMismatch ? false : true);
} else
modelInfo->updateOutputshapes(i, outputBlobDims, outputSizeMismatch ? false : true);
if (outputSizeMismatch) {
ALOGE(
"Mismatch in actual and exepcted output sizes. Return with "
"OUTPUT_INSUFFICIENT_SIZE error");
return {ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, modelInfo->getOutputShapes(), kNoTiming};
}
switch (operandType) {
case OperandType::TENSOR_INT32:
case OperandType::TENSOR_FLOAT32: {
std::memcpy((uint8_t*)destPtr, srcBlob->buffer().as<uint8_t*>(),
srcBlob->byteSize());
break;
}
case OperandType::TENSOR_BOOL8: {
floatToUint8(srcBlob->buffer().as<float*>(), (uint8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT8_ASYMM: {
floatToUint8(srcBlob->buffer().as<float*>(), (uint8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
floatToint8(srcBlob->buffer().as<float*>(), (int8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_FLOAT16: {
floatToFloat16(srcBlob->buffer().as<float*>(), (_Float16*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT16_SYMM: {
floatToInt16(srcBlob->buffer().as<float*>(), (int16_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT16_ASYMM: {
floatToUInt16(srcBlob->buffer().as<float*>(), (uint16_t*)destPtr, srcBlob->size());
break;
}
default:
std::memcpy((uint8_t*)destPtr, srcBlob->buffer().as<uint8_t*>(),
srcBlob->byteSize());
break;
}
}
if (!modelInfo->updateRequestPoolInfos()) {
ALOGE("Failed to update the request pool infos");
return {ErrorStatus::GENERAL_FAILURE, {}, kNoTiming};
}
if (measure == MeasureTiming::YES) {
driverEnd = now();
Timing timing = {.timeOnDevice = uint64_t(microsecondsDuration(deviceEnd, deviceStart)),
.timeInDriver = uint64_t(microsecondsDuration(driverEnd, driverStart))};
return {ErrorStatus::NONE, modelInfo->getOutputShapes(), timing};
}
ALOGV("Exiting %s", __func__);
return {ErrorStatus::NONE, modelInfo->getOutputShapes(), kNoTiming};
}
Return<void> BasePreparedModel::executeSynchronously(const Request& request, MeasureTiming measure,
executeSynchronously_cb cb) {
ALOGV("Entering %s", __func__);
time_point driverStart;
if (measure == MeasureTiming::YES) driverStart = now();
if (!validateRequest(request, convertToV1_2(mModelInfo->getModel()))) {
cb(ErrorStatus::INVALID_ARGUMENT, {}, kNoTiming);
return Void();
}
auto [status, outputShapes, timing] =
executeSynchronouslyBase(request, measure, this, driverStart);
cb(status, std::move(outputShapes), timing);
ALOGV("Exiting %s", __func__);
return Void();
}
Return<void> BasePreparedModel::executeSynchronously_1_3(const V1_3::Request& request,
V1_2::MeasureTiming measure,
const V1_3::OptionalTimePoint&,
const V1_3::OptionalTimeoutDuration&,
executeSynchronously_1_3_cb cb) {
ALOGV("Entering %s", __func__);
time_point driverStart;
if (measure == MeasureTiming::YES) driverStart = now();
if (!validateRequest(convertToV1_0(request), convertToV1_2(mModelInfo->getModel()))) {
cb(V1_3::ErrorStatus::INVALID_ARGUMENT, {}, kNoTiming);
return Void();
}
auto [status, outputShapes, timing] =
executeSynchronouslyBase(convertToV1_0(request), measure, this, driverStart);
cb(convertToV1_3(status), std::move(outputShapes), timing);
ALOGV("Exiting %s", __func__);
return Void();
}
Return<void> BasePreparedModel::configureExecutionBurst(
const sp<V1_2::IBurstCallback>& callback,
const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, configureExecutionBurst_cb cb) {
ALOGV("Entering %s", __func__);
const sp<V1_2::IBurstContext> burst =
ExecutionBurstServer::create(callback, requestChannel, resultChannel, this);
if (burst == nullptr) {
cb(ErrorStatus::GENERAL_FAILURE, {});
ALOGI("%s GENERAL_FAILURE", __func__);
} else {
cb(ErrorStatus::NONE, burst);
ALOGI("%s burst created", __func__);
}
ALOGV("Exiting %s", __func__);
return Void();
}
Return<ErrorStatus> BasePreparedModel::execute(const Request& request,
const sp<V1_0::IExecutionCallback>& callback) {
ALOGV("Entering %s", __func__);
return executeBase(request, MeasureTiming::NO, this, callback);
}
Return<ErrorStatus> BasePreparedModel::execute_1_2(const Request& request, MeasureTiming measure,
const sp<V1_2::IExecutionCallback>& callback) {
ALOGV("Entering %s", __func__);
return executeBase(request, measure, this, callback);
}
Return<V1_3::ErrorStatus> BasePreparedModel::execute_1_3(
const V1_3::Request& request, V1_2::MeasureTiming measure, const V1_3::OptionalTimePoint&,
const V1_3::OptionalTimeoutDuration&, const sp<V1_3::IExecutionCallback>& callback) {
ALOGV("Entering %s", __func__);
return convertToV1_3(executeBase(convertToV1_0(request), measure, this, callback));
}
Return<void> BasePreparedModel::executeFenced(const V1_3::Request& request1_3,
const hidl_vec<hidl_handle>& waitFor,
V1_2::MeasureTiming measure,
const V1_3::OptionalTimePoint& halDeadline,
const V1_3::OptionalTimeoutDuration&,
const V1_3::OptionalTimeoutDuration& duration,
executeFenced_cb cb) {
ALOGV("Entering %s", __func__);
time_point driverStart, driverEnd;
if (measure == MeasureTiming::YES) driverStart = now();
if (!validateRequest(request1_3, mModelInfo->getModel(), /*allowUnspecifiedOutput=*/false)) {
cb(V1_3::ErrorStatus::INVALID_ARGUMENT, hidl_handle(nullptr), nullptr);
return Void();
}
const auto deadline = makeDeadline(halDeadline);
if (hasDeadlinePassed(deadline)) {
cb(V1_3::ErrorStatus::MISSED_DEADLINE_PERSISTENT, hidl_handle(nullptr), nullptr);
return Void();
}
// Wait for the dependent events to signal
for (const auto& fenceHandle : waitFor) {
if (!fenceHandle.getNativeHandle()) {
cb(V1_3::ErrorStatus::INVALID_ARGUMENT, hidl_handle(nullptr), nullptr);
return Void();
}
const int syncFenceFd = fenceHandle.getNativeHandle()->data[0];
if (syncWait(syncFenceFd, -1) != FenceState::SIGNALED) {
ALOGV("%s syncWait failed", __func__);
cb(V1_3::ErrorStatus::GENERAL_FAILURE, hidl_handle(nullptr), nullptr);
return Void();
}
}
auto errorStatus = mModelInfo->setRunTimePoolInfosFromHidlMemories(request1_3.pools);
if (errorStatus != V1_3::ErrorStatus::NONE) {
ALOGE("Failed to set runtime pool info from HIDL memories");
cb(errorStatus, hidl_handle(nullptr), nullptr);
return Void();
}
// rest of the interfaces are based on 1.0 request
auto request = convertToV1_0(request1_3);
time_point driverAfterFence;
if (measure == MeasureTiming::YES) driverAfterFence = now();
for (size_t i = 0; i < request.inputs.size(); i++) {
uint32_t len;
auto inIndex = mModelInfo->getModelInputIndex(i);
void* srcPtr = mModelInfo->getBlobFromMemoryPoolIn(request, i, len);
const std::string& inputNodeName = mNgraphNetCreator->getNodeName(inIndex);
if (inputNodeName == "") {
ALOGD("Ignorning input at index(%d), since it is invalid", inIndex);
continue;
}
ALOGD("Input index: %d layername : %s", inIndex, inputNodeName.c_str());
auto destBlob = mPlugin->getBlob(inputNodeName);
if (mModelInfo->getOperandType(inIndex) == OperandType::TENSOR_FLOAT16) {
float* dest = destBlob->buffer().as<float*>();
_Float16* src = (_Float16*)srcPtr;
for (unsigned int i = 0; i < len / 2; i++) {
dest[i] = src[i];
}
} else {
uint8_t* dest = destBlob->buffer().as<uint8_t*>();
std::memcpy(dest, (uint8_t*)srcPtr, len);
}
}
ALOGD("%s Run", __func__);
time_point deviceStart, deviceEnd;
if (measure == MeasureTiming::YES) deviceStart = now();
try {
mPlugin->infer();
} catch (const std::exception& ex) {
ALOGE("%s Exception !!! %s", __func__, ex.what());
cb(V1_3::ErrorStatus::GENERAL_FAILURE, hidl_handle(nullptr), nullptr);
return Void();
}
if (measure == MeasureTiming::YES) deviceEnd = now();
for (size_t i = 0; i < request.outputs.size(); i++) {
auto outIndex = mModelInfo->getModelOutputIndex(i);
ALOGI("OutputIndex: %d", outIndex);
const std::string& outputNodeName = mNgraphNetCreator->getNodeName(outIndex);
if (outputNodeName == "") {
ALOGD("Ignorning output at index(%d), since it is invalid", outIndex);
continue;
}
ALOGD("Output index: %d layername : %s", outIndex, outputNodeName.c_str());
auto srcBlob = mPlugin->getBlob(outputNodeName);
auto operandType = mModelInfo->getOperandType(outIndex);
uint32_t actualLength = srcBlob->byteSize();
uint32_t expectedLength = 0;
void* destPtr = mModelInfo->getBlobFromMemoryPoolOut(request, i, expectedLength);
auto outDims = srcBlob->getTensorDesc().getDims();
switch (operandType) {
case OperandType::TENSOR_BOOL8:
case OperandType::TENSOR_QUANT8_ASYMM:
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
actualLength /= 4;
break;
case OperandType::TENSOR_FLOAT16:
case OperandType::TENSOR_QUANT16_SYMM:
case OperandType::TENSOR_QUANT16_ASYMM:
actualLength /= 2;
break;
default:
ALOGV("Operand type is 4 bytes !!");
break;
}
if (actualLength != expectedLength) {
ALOGE("%s Invalid length(%d) at outIndex(%d)", __func__, actualLength, outIndex);
// Notify Insufficient Buffer Length to modelInfo
mModelInfo->updateOutputshapes(i, outDims, false);
cb(V1_3::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, hidl_handle(nullptr), nullptr);
return Void();
} else {
mModelInfo->updateOutputshapes(i, outDims);
}
switch (operandType) {
case OperandType::TENSOR_INT32:
case OperandType::TENSOR_FLOAT32: {
std::memcpy((uint8_t*)destPtr, srcBlob->buffer().as<uint8_t*>(),
srcBlob->byteSize());
break;
}
case OperandType::TENSOR_BOOL8: {
floatToUint8(srcBlob->buffer().as<float*>(), (uint8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT8_ASYMM: {
floatToUint8(srcBlob->buffer().as<float*>(), (uint8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
floatToint8(srcBlob->buffer().as<float*>(), (int8_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_FLOAT16: {
floatToFloat16(srcBlob->buffer().as<float*>(), (_Float16*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT16_SYMM: {
floatToInt16(srcBlob->buffer().as<float*>(), (int16_t*)destPtr, srcBlob->size());
break;
}
case OperandType::TENSOR_QUANT16_ASYMM: {
floatToUInt16(srcBlob->buffer().as<float*>(), (uint16_t*)destPtr, srcBlob->size());
break;
}
default:
std::memcpy((uint8_t*)destPtr, srcBlob->buffer().as<uint8_t*>(),
srcBlob->byteSize());
break;
}
}
if (!mModelInfo->updateRequestPoolInfos()) {
ALOGE("Failed to update the request pool infos");
}
Timing timingSinceLaunch = {.timeOnDevice = UINT64_MAX, .timeInDriver = UINT64_MAX};
Timing timingAfterFence = {.timeOnDevice = UINT64_MAX, .timeInDriver = UINT64_MAX};
if (measure == MeasureTiming::YES) {
driverEnd = now();
timingSinceLaunch = {
.timeOnDevice = uint64_t(microsecondsDuration(deviceEnd, deviceStart)),
.timeInDriver = uint64_t(microsecondsDuration(driverEnd, driverStart))};
timingAfterFence = {
.timeOnDevice = uint64_t(microsecondsDuration(deviceEnd, deviceStart)),
.timeInDriver = uint64_t(microsecondsDuration(driverEnd, driverAfterFence))};
}
sp<BaseFencedExecutionCallback> fencedExecutionCallback = new BaseFencedExecutionCallback(
timingSinceLaunch, timingAfterFence, V1_3::ErrorStatus::NONE);
cb(V1_3::ErrorStatus::NONE, hidl_handle(nullptr), fencedExecutionCallback);
ALOGV("Exiting %s", __func__);
return Void();
}
} // namespace nnhal
} // namespace neuralnetworks
} // namespace hardware
} // namespace android