forked from intel/nn-hal
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.cpp
497 lines (439 loc) · 15.3 KB
/
utils.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
/*
* 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 "utils.h"
#include <android-base/logging.h>
#include <android/hardware_buffer.h>
#include <android/log.h>
#include <hidlmemory/mapping.h>
#include <log/log.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <vndk/hardware_buffer.h>
#include <fstream>
#undef LOG_TAG
#define LOG_TAG "Utils"
// inline unsigned int debugMask = ((1 << (L1 + 1)) - 1);
// F32: exp_bias:127 SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM.
// F16: exp_bias:15 SEEEEEMM MMMMMMMM
#define EXP_MASK_F32 0x7F800000U
#define EXP_MASK_F16 0x7C00U
using namespace InferenceEngine;
namespace android::hardware::neuralnetworks::nnhal {
unsigned int debugMask = ((1 << (L1 + 1)) - 1);
unsigned short float2half(unsigned f) {
unsigned f_exp, f_sig;
unsigned short h_sgn, h_exp, h_sig;
h_sgn = (unsigned short)((f & 0x80000000u) >> 16);
f_exp = (f & 0x7f800000u);
/* Exponent overflow/NaN converts to signed inf/NaN */
if (f_exp >= 0x47800000u) {
if (f_exp == 0x7f800000u) {
/* Inf or NaN */
f_sig = (f & 0x007fffffu);
if (f_sig != 0) {
/* NaN - propagate the flag in the significand... */
unsigned short ret = (unsigned short)(0x7c00u + (f_sig >> 13));
/* ...but make sure it stays a NaN */
if (ret == 0x7c00u) {
ret++;
}
return h_sgn + ret;
} else {
/* signed inf */
return (unsigned short)(h_sgn + 0x7c00u);
}
} else {
/* overflow to signed inf */
#if NPY_HALF_GENERATE_OVERFLOW
npy_set_floatstatus_overflow();
#endif
return (unsigned short)(h_sgn + 0x7c00u);
}
}
/* Exponent underflow converts to a subnormal half or signed zero */
if (f_exp <= 0x38000000u) {
/*
* Signed zeros, subnormal floats, and floats with small
* exponents all convert to signed zero halfs.
*/
if (f_exp < 0x33000000u) {
#if NPY_HALF_GENERATE_UNDERFLOW
/* If f != 0, it underflowed to 0 */
if ((f & 0x7fffffff) != 0) {
npy_set_floatstatus_underflow();
}
#endif
return h_sgn;
}
/* Make the subnormal significand */
f_exp >>= 23;
f_sig = (0x00800000u + (f & 0x007fffffu));
#if NPY_HALF_GENERATE_UNDERFLOW
/* If it's not exactly represented, it underflowed */
if ((f_sig & (((unsigned)1 << (126 - f_exp)) - 1)) != 0) {
npy_set_floatstatus_underflow();
}
#endif
f_sig >>= (113 - f_exp);
/* Handle rounding by adding 1 to the bit beyond half precision */
#if NPY_HALF_ROUND_TIES_TO_EVEN
/*
* If the last bit in the half significand is 0 (already even), and
* the remaining bit pattern is 1000...0, then we do not add one
* to the bit after the half significand. In all other cases, we do.
*/
if ((f_sig & 0x00003fffu) != 0x00001000u) {
f_sig += 0x00001000u;
}
#else
f_sig += 0x00001000u;
#endif
h_sig = (unsigned short)(f_sig >> 13);
/*
* If the rounding causes a bit to spill into h_exp, it will
* increment h_exp from zero to one and h_sig will be zero.
* This is the correct result.
*/
return (unsigned short)(h_sgn + h_sig);
}
/* Regular case with no overflow or underflow */
h_exp = (unsigned short)((f_exp - 0x38000000u) >> 13);
/* Handle rounding by adding 1 to the bit beyond half precision */
f_sig = (f & 0x007fffffu);
#if NPY_HALF_ROUND_TIES_TO_EVEN
/*
* If the last bit in the half significand is 0 (already even), and
* the remaining bit pattern is 1000...0, then we do not add one
* to the bit after the half significand. In all other cases, we do.
*/
if ((f_sig & 0x00003fffu) != 0x00001000u) {
f_sig += 0x00001000u;
}
#else
f_sig += 0x00001000u;
#endif
h_sig = (unsigned short)(f_sig >> 13);
/*
* If the rounding causes a bit to spill into h_exp, it will
* increment h_exp by one and h_sig will be zero. This is the
* correct result. h_exp may increment to 15, at greatest, in
* which case the result overflows to a signed inf.
*/
#if NPY_HALF_GENERATE_OVERFLOW
h_sig += h_exp;
if (h_sig == 0x7c00u) {
npy_set_floatstatus_overflow();
}
return h_sgn + h_sig;
#else
return h_sgn + h_exp + h_sig;
#endif
}
void floattofp16(short* dst, float* src, unsigned nelem) {
unsigned i;
unsigned short* _dst = (unsigned short*)dst;
unsigned* _src = (unsigned*)src;
for (i = 0; i < nelem; i++) _dst[i] = float2half(_src[i]);
}
// small helper function to represent uint32_t value as float32
float asfloat(uint32_t v) { return *reinterpret_cast<float*>(&v); }
// Function to convert F32 into F16
float f16tof32(short x) {
// this is storage for output result
uint32_t u = x;
// get sign in 32bit format
uint32_t s = ((u & 0x8000) << 16);
// check for NAN and INF
if ((u & EXP_MASK_F16) == EXP_MASK_F16) {
// keep mantissa only
u &= 0x03FF;
// check if it is NAN and raise 10 bit to be align with intrin
if (u) {
u |= 0x0200;
}
u <<= (23 - 10);
u |= EXP_MASK_F32;
u |= s;
} else if ((x & EXP_MASK_F16) ==
0) { // check for zero and denormals. both are converted to zero
u = s;
} else {
// abs
u = (u & 0x7FFF);
// shift mantissa and exp from f16 to f32 position
u <<= (23 - 10);
// new bias for exp (f16 bias is 15 and f32 bias is 127)
u += ((127 - 15) << 23);
// add sign
u |= s;
}
// finaly represent result as float and return
return *reinterpret_cast<float*>(&u);
}
// This function convert f32 to f16 with rounding to nearest value to minimize error
// the denormal values are converted to 0.
short f32tof16(float x) {
// create minimal positive normal f16 value in f32 format
// exp:-14,mantissa:0 -> 2^-14 * 1.0
static float min16 = asfloat((127 - 14) << 23);
// create maximal positive normal f16 value in f32 and f16 formats
// exp:15,mantissa:11111 -> 2^15 * 1.(11111)
static float max16 = asfloat(((127 + 15) << 23) | 0x007FE000);
static uint32_t max16f16 = ((15 + 15) << 10) | 0x3FF;
// define and declare variable for intermidiate and output result
// the union is used to simplify representation changing
union {
float f;
uint32_t u;
} v;
v.f = x;
// get sign in 16bit format
uint32_t s = (v.u >> 16) & 0x8000; // sign 16: 00000000 00000000 10000000 00000000
// make it abs
v.u &= 0x7FFFFFFF; // abs mask: 01111111 11111111 11111111 11111111
// check NAN and INF
if ((v.u & EXP_MASK_F32) == EXP_MASK_F32) {
if (v.u & 0x007FFFFF) {
return s | (v.u >> (23 - 10)) | 0x0200; // return NAN f16
} else {
return s | (v.u >> (23 - 10)); // return INF f16
}
}
// to make f32 round to nearest f16
// create halfULP for f16 and add it to origin value
float halfULP = asfloat(v.u & EXP_MASK_F32) * asfloat((127 - 11) << 23);
v.f += halfULP;
// if input value is not fit normalized f16 then return 0
// denormals are not covered by this code and just converted to 0
if (v.f < min16 * 0.5F) {
return s;
}
// if input value between min16/2 and min16 then return min16
if (v.f < min16) {
return s | (1 << 10);
}
// if input value more than maximal allowed value for f16
// then return this maximal value
if (v.f >= max16) {
return max16f16 | s;
}
// change exp bias from 127 to 15
v.u -= ((127 - 15) << 23);
// round to f16
v.u >>= (23 - 10);
return v.u | s;
}
void f16tof32Arrays(float* dst, const short* src, uint32_t& nelem, float scale, float bias) {
ALOGD("convert f16tof32Arrays...\n");
const short* _src = reinterpret_cast<const short*>(src);
for (uint32_t i = 0; i < nelem; i++) {
dst[i] = f16tof32(_src[i]) * scale + bias;
}
}
void f32tof16Arrays(short* dst, const float* src, uint32_t& nelem, float scale, float bias) {
ALOGD("convert f32tof16Arrays...");
for (uint32_t i = 0; i < nelem; i++) {
dst[i] = f32tof16(src[i] * scale + bias);
}
}
uint32_t getNumberOfElements(const vec<uint32_t>& dims) {
uint32_t count = 1;
for (size_t i = 0; i < dims.size(); i++) {
count *= dims[i];
}
return count;
}
size_t getSizeFromInts(int lower, int higher) {
return (uint32_t)(lower) + ((uint64_t)(uint32_t)(higher) << 32);
}
TensorDims toDims(const vec<uint32_t>& dims) {
TensorDims td;
for (auto d : dims) td.push_back(d);
return td;
}
template <typename T>
size_t product(const vec<T>& dims) {
size_t rc = 1;
for (auto d : dims) rc *= d;
return rc;
}
TensorDims permuteDims(const TensorDims& src, const vec<unsigned int>& order) {
TensorDims ret;
for (size_t i = 0; i < src.size(); i++) {
ret.push_back(src[order[i]]);
}
return ret;
}
// IRBlob::Ptr Permute(IRBlob::Ptr ptr, const vec<unsigned int> &order)
IRBlob::Ptr Permute(IRBlob::Ptr ptr, const vec<unsigned int>& order) {
ALOGD("Permute");
auto orig_dims = ptr->getTensorDesc().getDims();
auto dims = permuteDims(orig_dims, order);
ptr->getTensorDesc().setDims(dims);
return ptr;
}
size_t sizeOfTensor(const TensorDims& dims) {
size_t ret = dims[0];
for (size_t i = 1; i < dims.size(); ++i) ret *= dims[i];
return ret;
}
// TODO: short term, make share memory mapping and updating a utility function.
// TODO: long term, implement mmap_fd as a hidl IMemory service.
bool RunTimePoolInfo::set(const hidl_memory& hidlMemory) {
this->hidlMemory = hidlMemory;
buffer = nullptr;
auto memType = hidlMemory.name();
if (memType == "ashmem") {
memory = mapMemory(hidlMemory);
if (memory == nullptr) {
ALOGE("Can't map shared memory.");
return false;
}
memory->update();
buffer = reinterpret_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
if (buffer == nullptr) {
ALOGE("Can't access shared memory.");
return false;
}
return true;
} else if (memType == "mmap_fd") {
size_t size = hidlMemory.size();
int fd = hidlMemory.handle()->data[0];
int prot = hidlMemory.handle()->data[1];
size_t offset = getSizeFromInts(hidlMemory.handle()->data[2], hidlMemory.handle()->data[3]);
buffer = static_cast<uint8_t*>(mmap(nullptr, size, prot, MAP_SHARED, fd, offset));
if (buffer == MAP_FAILED) {
ALOGE("Can't mmap the file descriptor.");
return false;
}
return true;
}
#if __ANDROID__
else if (memType == "hardware_buffer_blob") {
AHardwareBuffer* hardwareBuffer = nullptr;
auto handle = hidlMemory.handle();
auto format = AHARDWAREBUFFER_FORMAT_BLOB;
auto usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
const uint32_t width = hidlMemory.size();
const uint32_t height = 1; // height is always 1 for BLOB mode AHardwareBuffer.
const uint32_t layers = 1; // layers is always 1 for BLOB mode AHardwareBuffer.
const uint32_t stride = hidlMemory.size();
AHardwareBuffer_Desc desc{
.width = width,
.format = format,
.height = height,
.layers = layers,
.usage = usage,
.stride = stride,
};
status_t status = AHardwareBuffer_createFromHandle(
&desc, handle, AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE, &hardwareBuffer);
if (status != NO_ERROR) {
LOG(ERROR) << "RunTimePoolInfo Can't create AHardwareBuffer from handle. Error: "
<< status;
return false;
}
void* gBuffer = nullptr;
status = AHardwareBuffer_lock(hardwareBuffer, usage, -1, nullptr, &gBuffer);
if (status != NO_ERROR) {
LOG(ERROR) << "RunTimePoolInfo Can't lock the AHardwareBuffer. Error: " << status;
return false;
}
buffer = static_cast<uint8_t*>(gBuffer);
return true;
}
#endif
else {
ALOGE("unsupported hidl_memory type");
return false;
}
}
bool RunTimePoolInfo::unmap_mem() {
if (buffer) {
const size_t size = hidlMemory.size();
if (hidlMemory.name() == "mmap_fd") {
if (munmap(buffer, size)) {
ALOGE("Unmap failed\n");
return false;
}
buffer = nullptr;
}
}
return true;
}
// Making sure the output data are correctly updated after execution.
bool RunTimePoolInfo::update() {
auto memType = hidlMemory.name();
if (memType == "ashmem") {
memory->commit();
return true;
} else if (memType == "mmap_fd") {
int prot = hidlMemory.handle()->data[1];
if (prot & PROT_WRITE) {
size_t size = hidlMemory.size();
return msync(buffer, size, MS_SYNC) == 0;
}
}
// No-op for other types of memory.
return true;
}
int sizeOfData(OperandType type, std::vector<uint32_t> dims) {
int size;
switch (type) {
case OperandType::FLOAT32:
size = 4;
break;
case OperandType::TENSOR_FLOAT32:
size = 4;
break;
case OperandType::TENSOR_INT32:
case OperandType::INT32:
size = 4;
break;
case OperandType::TENSOR_QUANT8_ASYMM:
case OperandType::TENSOR_QUANT8_SYMM:
case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
case OperandType::TENSOR_BOOL8:
size = 1;
break;
case OperandType::TENSOR_FLOAT16:
case OperandType::TENSOR_QUANT16_SYMM:
case OperandType::TENSOR_QUANT16_ASYMM:
case OperandType::FLOAT16:
size = 2;
break;
default:
size = 0;
}
for (auto d : dims) size *= d;
return size;
}
bool getGrpcSocketPath(char* socket_path) {
if (property_get("vendor.nn.hal.grpc_socket_path", socket_path, NULL) <= 0) {
ALOGV("%s : failed to read vendor.nn.hal.grpc_socket_path", __func__);
return false;
}
return true;
}
bool getGrpcIpPort(char* ip_port) {
if (property_get("vendor.nn.hal.grpc_ip_port", ip_port, NULL) <= 0) {
ALOGV("%s : failed to read vendor.nn.hal.grpc_ip_port", __func__);
return false;
}
return true;
}
} // namespace android::hardware::neuralnetworks::nnhal