-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuton.c
838 lines (633 loc) · 17.5 KB
/
neuton.c
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
#include "neuton.h"
#include <stdlib.h>
#include "preprocessing/blocks/normalize/normalize.h"
#if (NEUTON_MODEL_FLOAT_SUPPORT == 1)
#include <math.h>
#endif
// '__attribute__ ((aligned))' is used only in gcc compiler for MC purposes
#ifdef _MSC_VER
#define ATTRIBUTE_ALIGNED_ARRAY
#else
#define ATTRIBUTE_ALIGNED_ARRAY __attribute__ ((aligned))
#endif
#define N_ELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
#define MAX_INPUT_FLOAT 0.9999999f
#if (NEUTON_MODEL_HEADER_VERSION < 3)
#define NEUTON_INPUTS_IS_INTEGER 0
#endif
static float modelOutput[NEUTON_MODEL_OUTPUTS_COUNT];
static coeff_t modelAccumulators[NEUTON_MODEL_NEURONS_COUNT];
static uint8_t modelIsReadyForInference = 0;
#if (NEUTON_MODEL_QLEVEL < 32)
static const coeff_t ctMax = (UINT32_C(1) << NEUTON_MODEL_QLEVEL) - 1;
#endif
#define NEUTON_MODEL_INPUT_TYPE_SIZE ((sizeof(input_t) > sizeof(coeff_t)) ? sizeof(input_t) : sizeof(coeff_t))
#if (NEUTON_PREPROCESSING_ENABLED == 0)
static uint8_t modelInputBuffer[NEUTON_MODEL_INPUTS_COUNT * NEUTON_MODEL_INPUT_TYPE_SIZE] ATTRIBUTE_ALIGNED_ARRAY;
#else
#include "fe/statistical/DSP.h"
#include "model/dsp_config.h"
static uint8_t modelInputBuffer[NEUTON_MODEL_USED_ORIGINAL_INPUTS_COUNT * NEUTON_MODEL_WINDOW_SIZE * NEUTON_MODEL_INPUT_TYPE_SIZE] ATTRIBUTE_ALIGNED_ARRAY;
static uint16_t modelWindowFill = 0;
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
#define NEUTON_MODEL_EXTRACTED_FEATURES_TYPE_SIZE ((sizeof(extracted_feature_t) > sizeof(coeff_t)) ? sizeof(extracted_feature_t) : sizeof(coeff_t))
static uint8_t extractedFeaturesBuffer[NEUTON_MODEL_EXTRACTED_FEATURES_COUNT * NEUTON_MODEL_EXTRACTED_FEATURES_TYPE_SIZE] ATTRIBUTE_ALIGNED_ARRAY;
#endif
#endif
#if NEUTON_MODEL_QLEVEL==8
typedef uint16_t double_qu_t;
typedef int16_t double_qs_t;
#elif NEUTON_MODEL_QLEVEL==16
typedef uint32_t double_qu_t;
typedef int32_t double_qs_t;
#else
typedef double double_qu_t;
typedef double double_qs_t;
#endif
extern inline uint8_t neuton_model_quantization_level()
{
return NEUTON_MODEL_QLEVEL;
}
extern inline uint8_t neuton_model_float_calculations()
{
return NEUTON_MODEL_FLOAT_SUPPORT;
}
extern inline TaskType neuton_model_task_type()
{
return (TaskType) NEUTON_MODEL_TASK_TYPE;
}
extern inline uint16_t neuton_model_outputs_count()
{
return NEUTON_MODEL_OUTPUTS_COUNT;
}
extern inline uint16_t neuton_model_neurons_count()
{
return NEUTON_MODEL_NEURONS_COUNT;
}
extern inline uint32_t neuton_model_weights_count()
{
return NEUTON_MODEL_WEIGHTS_COUNT;
}
extern inline uint16_t neuton_model_inputs_limits_count()
{
return NEUTON_MODEL_INPUT_LIMITS_COUNT;
}
extern inline uint16_t neuton_model_inputs_count()
{
#if (NEUTON_PREPROCESSING_ENABLED == 0)
return NEUTON_MODEL_INPUTS_COUNT;
#else
return NEUTON_MODEL_INPUTS_COUNT_ORIGINAL;
#endif
}
extern inline uint16_t neuton_model_window_size()
{
#if (NEUTON_PREPROCESSING_ENABLED == 1)
return NEUTON_MODEL_WINDOW_SIZE;
#else
return 1;
#endif
}
uint32_t neuton_model_ram_usage()
{
return sizeof(modelOutput) + sizeof(modelAccumulators) + sizeof(modelIsReadyForInference)
+ sizeof(modelInputBuffer)
#if (NEUTON_PREPROCESSING_ENABLED == 1)
+ sizeof(modelWindowFill)
#endif
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
+ sizeof(extractedFeaturesBuffer)
#endif
#if (NEUTON_MODEL_QLEVEL < 32)
+ sizeof(ctMax)
#endif
;
}
uint32_t neuton_model_size()
{
return sizeof(modelWeights) + sizeof(modelLinks) + sizeof(modelFuncCoeffs)
+ sizeof(modelIntLinksBoundaries) + sizeof(modelExtLinksBoundaries)
+ sizeof(modelOutputNeurons)
#if (NEUTON_MODEL_HEADER_VERSION > 1)
+ sizeof(modelFuncTypes)
#endif
;
}
uint32_t neuton_model_size_with_meta()
{
return neuton_model_size()
#if (NEUTON_PREPROCESSING_ENABLED == 0)
+ sizeof(modelInputMin) + sizeof(modelInputMax)
#endif
#if (NEUTON_PREPROCESSING_ENABLED == 1)
#if (NEUTON_DROP_ORIGINAL_FEATURES != 1)
+ sizeof(modelInputScaleMin) + sizeof(modelInputScaleMax)
#endif
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
+ sizeof(extractedFeaturesScaleMin) + sizeof(extractedFeaturesScaleMax)
#endif
#endif
#if (NEUTON_BITMASK_ENABLED == 1)
+ sizeof(modelUsedInputsMask)
#endif
#if (NEUTON_MODEL_TASK_TYPE == 2)
+ sizeof(modelOutputMin) + sizeof(modelOutputMax)
#endif
#if (NEUTON_MODEL_LOG_SCALE_OUTPUTS == 1)
+ sizeof(modelOutputLogFlag) + sizeof(modelOutputLogScale)
#endif
#if (NEUTON_PREPROCESSING_ENABLED == 1)
+ sizeof(modelOriginalFeatureUsed)
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
+ sizeof(modelExtractedFeaturesStartIdxForAxle)
+ sizeof(modelExtractedFeaturesCountForAxle)
+ sizeof(modelExtractedFeaturesParamsOffset)
+ sizeof(modelExtractedFeatures)
+ sizeof(modelExtractedFeaturesParams)
#endif
#endif
;
}
static void denormalize_outputs()
{
#if (NEUTON_MODEL_TASK_TYPE == 0) || (NEUTON_MODEL_TASK_TYPE == 1)
float sum = 0;
for (uint16_t i = 0; i < NEUTON_MODEL_OUTPUTS_COUNT; ++i)
sum += modelOutput[i];
for (uint16_t i = 0; i < NEUTON_MODEL_OUTPUTS_COUNT; ++i)
modelOutput[i] = (sum != 0) ? modelOutput[i] / sum: 0;
#endif
#if (NEUTON_MODEL_TASK_TYPE == 2)
for (uint16_t i = 0; i < NEUTON_MODEL_OUTPUTS_COUNT; ++i)
{
modelOutput[i] = modelOutput[i] * (modelOutputMax[i] - modelOutputMin[i]) + modelOutputMin[i];
#if (NEUTON_MODEL_LOG_SCALE_OUTPUTS == 1)
if (modelOutputLogFlag[i])
modelOutput[i] = exp(modelOutput[i]) - modelOutputLogScale[i];
#endif
}
#endif
}
void neuton_model_reset_inputs()
{
#if (NEUTON_PREPROCESSING_ENABLED != 0)
modelWindowFill = 0;
#endif
modelIsReadyForInference = 0;
}
int8_t neuton_model_set_inputs(input_t *inputs)
{
if (!inputs)
return -1;
input_t* buffer = (input_t*)modelInputBuffer;
#if (NEUTON_PREPROCESSING_ENABLED == 0)
for (uint16_t i = 0; i < neuton_model_inputs_count(); ++i)
buffer[i] = inputs[i];
modelIsReadyForInference = 1;
return 0;
#else
uint16_t column = 0;
for (uint16_t i = 0; i < neuton_model_inputs_count(); ++i)
{
#if (NEUTON_MODEL_USED_ORIGINAL_INPUTS_COUNT != NEUTON_MODEL_INPUTS_COUNT_ORIGINAL)
if (modelOriginalFeatureUsed[i >> 3] & (UINT8_C(1) << (i % 8)))
#endif
{
buffer[column * NEUTON_MODEL_WINDOW_SIZE + modelWindowFill] = inputs[i];
++column;
}
}
if (++modelWindowFill >= NEUTON_MODEL_WINDOW_SIZE)
{
modelWindowFill = 0;
modelIsReadyForInference = 1;
return 0;
}
return 1;
#endif
}
#if (NEUTON_MODEL_QLEVEL == 32)
static inline coeff_t neuton_activation_fn(neurons_size_t neuronIndex, acc_signed_t summ)
{
return 1.0f / (1.0f + exp((acc_signed_t) ((acc_signed_t) -modelFuncCoeffs[neuronIndex]) * summ));
}
#else // (NEUTON_MODEL_QLEVEL == 32)
#if (NEUTON_MODEL_FLOAT_SUPPORT == 0)
static coeff_t accurate_fast_sigmoid(acc_signed_t arg)
{
coeff_t qResult = 0;
coeff_t secondPointY = 0;
coeff_t firstPointY = 0;
static const uint8_t QLVL = NEUTON_MODEL_QLEVEL;
static const uint8_t QLVLM1 = QLVL - 1;
const coeff_t intPart = abs(arg) / (UINT32_C(1) << QLVL);
const coeff_t realPart = abs(arg) - (intPart << QLVL);
if (intPart == 0 && realPart == 0)
{
return UINT32_C(1) << QLVLM1;
}
uint8_t s = arg < 0;
uint8_t odd = 1;
if (realPart == 0)
{
for (uint8_t i = 0; i < QLVL; i++)
{
const uint8_t bit = ((i / intPart + s) & odd);
qResult = qResult | (bit << (QLVLM1 - i));
}
return qResult;
}
const coeff_t secondPointX = intPart + 1;
if (intPart == 0)
{
firstPointY = UINT32_C(1) << QLVLM1;
for (uint8_t i = 0; i < QLVL; i++)
{
const uint8_t bit = ((i / secondPointX) & odd);
secondPointY = secondPointY | (bit << (QLVLM1 - i));
}
}
else
{
if (secondPointX == 0)
{
for (uint8_t i = 0; i < QLVL; i++)
{
const uint8_t bit = ((i / intPart) & odd);
firstPointY = firstPointY | (bit << (QLVLM1 - i));
}
secondPointY = UINT32_C(1) << QLVLM1;
}
else
{
for (uint8_t i = 0; i < QLVL; i++)
{
uint8_t bit = ((i / intPart) & odd);
firstPointY = firstPointY | (bit << (QLVLM1 - i));
bit = ((i / secondPointX) & odd);
secondPointY = secondPointY | (bit << (QLVLM1 - i));
}
}
}
const coeff_t res = firstPointY + ((realPart * (secondPointY - firstPointY)) >> QLVL);
if (s)
return res == 0 ? ctMax : ctMax + 1 - res;
return res;
}
#endif // (NEUTON_MODEL_FLOAT_SUPPORT == 0)
static inline float neuton_deqantize_value(coeff_t value)
{
return (float) value / (float) (UINT32_C(1) << NEUTON_MODEL_QLEVEL);
}
#if (NEUTON_MODEL_QLEVEL == 8)
#define KSHIFT 2
#endif
#if (NEUTON_MODEL_QLEVEL == 16)
#define KSHIFT 10
#endif
static coeff_t neuton_activation_fn(neurons_size_t neuronIndex, acc_signed_t summ)
{
#if (NEUTON_MODEL_FLOAT_SUPPORT == 1)
const float qs = (float) (((acc_signed_t) modelFuncCoeffs[neuronIndex] * summ)
>> (NEUTON_MODEL_QLEVEL + KSHIFT - 1)) / (float) (UINT32_C(1) << (NEUTON_MODEL_QLEVEL));
const float tmpValue = 1.0f / (1.0f + expf(-qs));
return (tmpValue > MAX_INPUT_FLOAT ? MAX_INPUT_FLOAT : tmpValue) * (float) (UINT32_C(1) << NEUTON_MODEL_QLEVEL);
#else // (NEUTON_MODEL_FLOAT_SUPPORT == 1)
return accurate_fast_sigmoid(
-(((acc_signed_t) modelFuncCoeffs[neuronIndex] * summ) >> (NEUTON_MODEL_QLEVEL + KSHIFT - 1))
);
#endif // (NEUTON_MODEL_FLOAT_SUPPORT == 1)
}
#endif // (NEUTON_MODEL_QLEVEL == 32)
#if (NEUTON_BITMASK_ENABLED == 1)
static inline uint8_t is_input_used(uint32_t pos)
{
return (modelUsedInputsMask[pos >> 3] & (UINT8_C(1) << (pos % 8)));
}
#endif
#if (NEUTON_DROP_ORIGINAL_FEATURES != 1)
static coeff_t prepare_model_input(input_t value, uint16_t index)
{
input_t min, max;
#if (NEUTON_PREPROCESSING_ENABLED == 1)
min = modelInputScaleMin[index / NEUTON_MODEL_WINDOW_SIZE];
max = modelInputScaleMax[index / NEUTON_MODEL_WINDOW_SIZE];
#else
#if (NEUTON_MODEL_INPUT_LIMITS_COUNT == 1)
min = modelInputMin[0];
max = modelInputMax[0];
#else
min = modelInputMin[index];
max = modelInputMax[index];
#endif
#endif
#if (NEUTON_MODEL_QLEVEL == 32)
coeff_t ct = value;
neuton_preprocessing_block_normalize(&ct, &ct, 1, min, max);
return ct;
#else
if (value < min)
value = min;
if (value > max)
value = max;
#if (NEUTON_INPUTS_IS_INTEGER == 1)
uint64_t tmp = value - min;
if ((max - min) == 0)
{
return tmp * ctMax;
}
else
{
return tmp * ctMax / (max - min);
}
#else
if ((max - min) == 0)
{
return (value - min) * ctMax;
}
else
{
return (value - min) * ctMax / (max - min);
}
#endif
#endif
}
static void prepare_model_inputs()
{
input_t* src = (input_t*)modelInputBuffer;
coeff_t* dst = (coeff_t*)modelInputBuffer;
static const uint16_t count = sizeof(modelInputBuffer) / NEUTON_MODEL_INPUT_TYPE_SIZE;
if (sizeof(coeff_t) > sizeof(input_t))
{
for (uint16_t i = count-1; ; --i)
{
#if (NEUTON_BITMASK_ENABLED == 1)
if (is_input_used(i))
#endif
dst[i] = prepare_model_input(src[i], i);
if (i == 0) break;
}
}
else
{
for (uint16_t i = 0; i < count; ++i)
{
#if (NEUTON_BITMASK_ENABLED == 1)
if (is_input_used(i))
#endif
dst[i] = prepare_model_input(src[i], i);
}
}
}
#endif // (NEUTON_DROP_ORIGINAL_FEATURES != 1)
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
static void extract_features()
{
input_t* src = (input_t*)modelInputBuffer;
extracted_feature_t* dst = (extracted_feature_t*)extractedFeaturesBuffer;
#if (NEUTON_DROP_ORIGINAL_FEATURES == 1)
uint32_t shift = 0;
#else
uint32_t shift = sizeof(modelInputBuffer) / NEUTON_MODEL_INPUT_TYPE_SIZE;
#endif
dsp_init_lib();
for (uint16_t column = 0; column < NEUTON_MODEL_USED_ORIGINAL_INPUTS_COUNT; ++column)
{
const uint16_t efCountForCol = modelExtractedFeaturesCountForAxle[column];
if (efCountForCol > 0)
{
const uint32_t efStartIdx = modelExtractedFeaturesStartIdxForAxle[column];
SaPrecalcStatData(
modelExtractedFeatures + efStartIdx,
efCountForCol,
column,
src,
NEUTON_MODEL_WINDOW_SIZE);
// enum features for chosen column
const uint32_t featureEndIdx = efStartIdx + efCountForCol;
for (uint32_t featureIdx = efStartIdx, feature = 0; featureIdx < featureEndIdx; ++featureIdx)
{
#if (NEUTON_BITMASK_ENABLED == 1)
if (is_input_used(shift + feature))
#endif
switch (modelExtractedFeatures[featureIdx])
{
case EF_STAT_MIN:
dst[feature] = SaMin();
break;
case EF_STAT_MAX:
dst[feature] = SaMax();
break;
case EF_STAT_MEAN:
dst[feature] = SaMean();
break;
case EF_STAT_RMS:
dst[feature] = SaRootMeanSquare();
break;
case EF_STAT_MEAN_CROSSING:
dst[feature] = SaMeanCrossing();
break;
case EF_STAT_NEGATIVE_MEAN_CROSSING:
dst[feature] = SaNegMeanCrossing();
break;
case EF_STAT_POSITIVE_MEAN_CROSSING:
dst[feature] = SaPosMeanCrossing();
break;
case EF_STAT_VARIANCE:
dst[feature] = SaVariance();
break;
case EF_STAT_PFD:
dst[feature] = SaPetrosianFractalDimension();
break;
case EF_STAT_SKEWNESS:
dst[feature] = SaSkewness();
break;
case EF_STAT_KURTOSIS:
dst[feature] = SaKurtosis();
break;
case EF_AMP_HIGH_FREQUENCY_P2P:
dst[feature] = SaAmplitudeGlobalP2pHighFrequency();
break;
case EF_AMP_LOW_FREQUENCY_P2P:
dst[feature] = SaAmplitudeGlobalP2pLowFrequency();
break;
case EF_STAT_UNUSED:
default:
break;
}
++feature;
}
} // efCountForCol > 0
// move buffers to next column
src += NEUTON_MODEL_WINDOW_SIZE;
dst += efCountForCol;
shift += efCountForCol;
} // for column
}
static coeff_t prepare_extracted_feature(extracted_feature_t value, uint16_t index)
{
extracted_feature_t min = extractedFeaturesScaleMin[index];
extracted_feature_t max = extractedFeaturesScaleMax[index];
#if (NEUTON_MODEL_QLEVEL == 32)
coeff_t ct = value;
neuton_preprocessing_block_normalize(&ct, &ct, 1, min, max);
return ct;
#else
if (value < min)
value = min;
if (value > max)
value = max;
#if (NEUTON_INPUTS_IS_INTEGER == 1)
uint64_t tmp = value - min;
if ((max - min) == 0)
{
return tmp * ctMax;
}
else
{
return tmp * ctMax / (max - min);
}
#else
if ((max - min) == 0)
{
return (value - min) * ctMax;
}
else
{
return (value - min) * ctMax / (max - min);
}
#endif
#endif
}
static void prepare_extracted_features()
{
extracted_feature_t* src = (extracted_feature_t*)extractedFeaturesBuffer;
coeff_t* dst = (coeff_t*)extractedFeaturesBuffer;
static const uint16_t count = sizeof(extractedFeaturesBuffer) / NEUTON_MODEL_EXTRACTED_FEATURES_TYPE_SIZE;
#if (NEUTON_BITMASK_ENABLED == 1)
#if (NEUTON_DROP_ORIGINAL_FEATURES == 1)
uint32_t shift = 0;
#else
uint32_t shift = sizeof(modelInputBuffer) / NEUTON_MODEL_INPUT_TYPE_SIZE;
#endif
#endif
if (sizeof(coeff_t) > sizeof(input_t))
{
for (uint16_t i = count-1; ; --i)
{
#if (NEUTON_BITMASK_ENABLED == 1)
if (is_input_used(i + shift))
#endif
dst[i] = prepare_extracted_feature(src[i], i);
if (i == 0) break;
}
}
else
{
for (uint16_t i = 0; i < count; ++i)
{
#if (NEUTON_BITMASK_ENABLED == 1)
if (is_input_used(i + shift))
#endif
dst[i] = prepare_extracted_feature(src[i], i);
}
}
}
#endif // (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
static inline coeff_t get_model_input(uint16_t index)
{
#if (NEUTON_PREPROCESSING_ENABLED == 0)
static const uint16_t inputsCount = NEUTON_MODEL_INPUTS_COUNT;
#else
static const uint16_t inputsCount = sizeof(modelInputBuffer) / NEUTON_MODEL_INPUT_TYPE_SIZE;
#endif
coeff_t* coeff;
#if (NEUTON_DROP_ORIGINAL_FEATURES == 1)
index += inputsCount;
#else
if (index < inputsCount)
{
coeff = (coeff_t*)modelInputBuffer;
return coeff[index];
}
#endif
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
index -= inputsCount;
if (index < NEUTON_MODEL_EXTRACTED_FEATURES_COUNT)
{
coeff = (coeff_t*)extractedFeaturesBuffer;
return coeff[index];
}
#endif
#if (NEUTON_MODEL_QLEVEL == 32)
return 1.0f;
#else
return ctMax;
#endif
}
int8_t neuton_model_run_inference(uint16_t *index, float **outputs)
{
if (!modelIsReadyForInference)
return 1;
#if (NEUTON_MODEL_EXTRACTED_FEATURES_COUNT > 0)
extract_features();
prepare_extracted_features();
#endif
#if (NEUTON_DROP_ORIGINAL_FEATURES != 1)
prepare_model_inputs();
#endif
weights_size_t weightIndex = 0;
for (neurons_size_t neuronIndex = 0; neuronIndex < NEUTON_MODEL_NEURONS_COUNT; ++neuronIndex)
{
acc_signed_t sum = 0;
double_qs_t mul;
weights_size_t boundary = modelIntLinksBoundaries[neuronIndex];
while (weightIndex < boundary)
{
const double_qs_t firstValue = modelWeights[weightIndex];
const double_qs_t secondValue = modelAccumulators[modelLinks[weightIndex]];
mul = firstValue * secondValue;
sum += mul;
++weightIndex;
}
boundary = modelExtLinksBoundaries[neuronIndex];
while (weightIndex < boundary)
{
const double_qs_t firstValue = modelWeights[weightIndex];
const double_qs_t secondValue = get_model_input(modelLinks[weightIndex]);
mul = firstValue * secondValue;
sum += mul;
++weightIndex;
}
modelAccumulators[neuronIndex] = neuton_activation_fn(neuronIndex, sum);
}
for (neurons_size_t i = 0; i < NEUTON_MODEL_OUTPUTS_COUNT; ++i)
{
#if (NEUTON_MODEL_QLEVEL == 32)
modelOutput[i] = modelAccumulators[modelOutputNeurons[i]];
#else
modelOutput[i] = neuton_deqantize_value(modelAccumulators[modelOutputNeurons[i]]);
#endif
}
denormalize_outputs();
#if (NEUTON_MODEL_OUTPUTS_COUNT == 1)
if (index)
*index = 0;
#else
if (index)
{
uint16_t target = 0;
float max = 0.0;
for (uint16_t i = 0; i < NEUTON_MODEL_OUTPUTS_COUNT; ++i)
if (max < modelOutput[i])
{
max = modelOutput[i];
target = i;
}
*index = target;
}
#endif
if (outputs)
*outputs = modelOutput;
return 0;
}