This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathBasePreparedModel.cpp
More file actions
475 lines (430 loc) · 19.9 KB
/
BasePreparedModel.cpp
File metadata and controls
475 lines (430 loc) · 19.9 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
/*
* 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"
#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(const Model& model) {
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]);
}
}
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;
if (!modelInfo->setRunTimePoolInfosFromHidlMemories(request.pools)) {
notify(callback, ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
return;
}
for (size_t i = 0; i < request.inputs.size(); i++) {
auto inIndex = modelInfo->getModelInputIndex(i);
auto srcBlob = modelInfo->getBlobFromMemoryPoolIn(request, i);
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);
uint8_t* dest = destBlob->buffer().as<uint8_t*>();
uint8_t* src = srcBlob->buffer().as<uint8_t*>();
std::memcpy(dest, src, srcBlob->byteSize());
}
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 expectedLength = srcBlob->byteSize();
uint32_t rActualLength = 0;
void* destPtr = modelInfo->getBlobFromMemoryPoolOut(request, i, rActualLength);
auto outDims = srcBlob->getTensorDesc().getDims();
if (operandType == OperandType::TENSOR_BOOL8 ||
operandType == OperandType::TENSOR_QUANT8_ASYMM ||
operandType == OperandType::TENSOR_QUANT8_SYMM ||
operandType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)
expectedLength /= 4; // 8bit expected instead of 32bit
else if(operandType == OperandType::TENSOR_QUANT16_SYMM)
expectedLength /= 2; // 16bit expected instead of 32bit
if (rActualLength != expectedLength) {
ALOGE("%s Invalid length at outIndex(%d) Actual:%d Expected:%d", __func__, outIndex,
rActualLength, expectedLength);
// Notify Insufficient Buffer Length to modelInfo
modelInfo->updateOutputshapes(i, outDims, false);
notify(callback, ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, modelInfo->getOutputShapes(),
kNoTiming);
return;
} else {
modelInfo->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: {
modelInfo->getOperandScaleZeroPoint(outIndex, sc, zp);
for (int i = 0; i < srcBlob->size() ; i++) {
*((uint8_t*)destPtr + i) = static_cast<uint8_t>(zp + (*(srcBlob->buffer().as<float*>() + i) / sc));
}
break;
}
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
modelInfo->getOperandScaleZeroPoint(outIndex, sc, zp);
for (int i = 0; i < srcBlob->size() ; i++) {
*((int8_t*)destPtr + i) = static_cast<int8_t>(zp + (*(srcBlob->buffer().as<float*>() + i) / sc));
}
break;
}
case OperandType::TENSOR_QUANT16_SYMM: {
modelInfo->getOperandScaleZeroPoint(outIndex, sc, zp);
for (int i = 0; i < srcBlob->size() ; i++) {
*((int16_t*)destPtr + i) = static_cast<int16_t>(zp + (*(srcBlob->buffer().as<float*>() + i) / sc));
}
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;
if (!modelInfo->setRunTimePoolInfosFromHidlMemories(request.pools)) {
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++) {
auto inIndex = modelInfo->getModelInputIndex(i);
auto srcBlob = modelInfo->getBlobFromMemoryPoolIn(request, i);
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);
uint8_t* dest = destBlob->buffer().as<uint8_t*>();
uint8_t* src = srcBlob->buffer().as<uint8_t*>();
std::memcpy(dest, src, srcBlob->byteSize());
}
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 expectedLength = srcBlob->byteSize();
uint32_t rActualLength = 0;
void* destPtr = modelInfo->getBlobFromMemoryPoolOut(request, i, rActualLength);
auto outDims = srcBlob->getTensorDesc().getDims();
if (operandType == OperandType::TENSOR_BOOL8 ||
operandType == OperandType::TENSOR_QUANT8_ASYMM ||
operandType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
operandType == OperandType::TENSOR_QUANT8_SYMM ||
operandType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)
expectedLength /= 4; // 8bit expected instead of 32bit
else if(operandType == OperandType::TENSOR_QUANT16_SYMM)
expectedLength /= 2; // 16bit expected instead of 32bit
if (rActualLength != expectedLength) {
ALOGE("%s Invalid length(%d) at outIndex(%d)", __func__, rActualLength, outIndex);
// Notify Insufficient Buffer Length to modelInfo
modelInfo->updateOutputshapes(i, outDims, false);
return {ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, modelInfo->getOutputShapes(), kNoTiming};
} else
modelInfo->updateOutputshapes(i, outDims);
float sc;
int32_t zp;
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: {
modelInfo->getOperandScaleZeroPoint(outIndex, sc, zp);
for (int i = 0; i < srcBlob->size() ; i++) {
*((uint8_t*)destPtr + i) = static_cast<uint8_t>(zp + (*(srcBlob->buffer().as<float*>() + i) / sc));
}
break;
}
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
modelInfo->getOperandScaleZeroPoint(outIndex, sc, zp);
for (int i = 0; i < srcBlob->size() ; i++) {
*((int8_t*)destPtr + i) = static_cast<int8_t>(zp + (*(srcBlob->buffer().as<float*>() + i) / sc));
}
break;
}
case OperandType::TENSOR_QUANT16_SYMM: {
modelInfo->getOperandScaleZeroPoint(outIndex, sc, zp);
for (int i = 0; i < srcBlob->size() ; i++) {
*((int16_t*)destPtr + i) = static_cast<int16_t>(zp + (*(srcBlob->buffer().as<float*>() + i) / sc));
}
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& deadline,
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, 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& deadline,
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
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& request, const hidl_vec<hidl_handle>& waitFor, V1_2::MeasureTiming measure,
const V1_3::OptionalTimePoint& deadline,
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
const V1_3::OptionalTimeoutDuration& duration, executeFenced_cb cb) {
ALOGV("Entering %s", __func__);
// TODO: Add support
ALOGV("Exiting %s", __func__);
return Void();
}
} // namespace nnhal
} // namespace neuralnetworks
} // namespace hardware
} // namespace android