forked from tensorflow/tflite-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softmax_int8_int16.cc
154 lines (133 loc) · 6.16 KB
/
softmax_int8_int16.cc
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
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
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 "tensorflow/lite/c/builtin_op_data.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/reference/softmax.h"
#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
#include "tensorflow/lite/kernels/kernel_util.h"
#include "tensorflow/lite/kernels/op_macros.h"
#include "tensorflow/lite/micro/kernels/kernel_util.h"
#include "tensorflow/lite/micro/kernels/softmax.h"
#include "tensorflow/lite/micro/kernels/xtensa/xtensa.h"
#include "tensorflow/lite/micro/kernels/xtensa/xtensa_softmax.h"
#include "tensorflow/lite/micro/micro_log.h"
namespace tflite {
namespace {
#if defined(HIFI4) || defined(HIFI5)
TfLiteStatus PrepareHifi(TfLiteContext* context, TfLiteNode* node) {
TF_LITE_ENSURE_OK(context, SoftmaxPrepare(context, node));
MicroContext* micro_context = GetMicroContext(context);
// Calculate scratch memory requirements and request scratch buffer
TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0);
TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0);
const RuntimeShape& input_shape = GetTensorShape(input);
const RuntimeShape& output_shape = GetTensorShape(output);
const int trailing_dim = input_shape.DimensionsCount() - 1;
const int depth =
MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
if (input->type == kTfLiteInt8) {
int required_scratch =
get_softmax_scratch_size(PREC_ASYM8S, PREC_ASYM8S, depth);
TF_LITE_ENSURE(context, required_scratch > 0);
auto* data = static_cast<XtensaSoftmaxOpData*>(node->user_data);
TF_LITE_ENSURE_OK(
context, context->RequestScratchBufferInArena(
context, required_scratch, &(data->scratch_tensor_index)));
}
micro_context->DeallocateTempTfLiteTensor(input);
micro_context->DeallocateTempTfLiteTensor(output);
return kTfLiteOk;
}
TfLiteStatus EvalHifi(const XtensaSoftmaxOpData* op_data,
const TfLiteEvalTensor* input, TfLiteEvalTensor* output,
TfLiteContext* context) {
const RuntimeShape& input_shape = tflite::micro::GetTensorShape(input);
const int8_t* input_data = tflite::micro::GetTensorData<int8_t>(input);
const RuntimeShape& output_shape = tflite::micro::GetTensorShape(output);
int16_t* output_data = tflite::micro::GetTensorData<int16_t>(output);
const int trailing_dim = input_shape.DimensionsCount() - 1;
const int outer_size =
MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
const int depth =
MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
void* p_scratch = static_cast<void*>(
context->GetScratchBuffer(context, op_data->scratch_tensor_index));
for (int i = 0; i < outer_size; ++i) {
int err = xa_nn_vec_softmax_asym8s_16(
&output_data[i * depth], &input_data[i * depth],
op_data->params.diff_min, op_data->params.input_left_shift,
op_data->params.input_multiplier, depth, p_scratch);
TF_LITE_ENSURE(context, err == 0);
}
return kTfLiteOk;
}
#endif // defined(HIFI4) || defined(HIFI5)
} // namespace
void* XtensaInitSoftmax(TfLiteContext* context, const char* buffer,
size_t length) {
#if defined(HIFI4) || defined(HIFI5)
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
return context->AllocatePersistentBuffer(context,
sizeof(XtensaSoftmaxOpData));
#elif defined(VISION_P6)
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
if (InitXtensaContext()) {
return nullptr;
}
return context->AllocatePersistentBuffer(context,
sizeof(XtensaSoftmaxOpData));
#else
return SoftmaxInit(context, buffer, length);
#endif // defined(HIFI4) || defined(HIFI5)
}
TfLiteStatus XtensaPrepareSoftmax(TfLiteContext* context, TfLiteNode* node) {
#if defined(HIFI4) || defined(HIFI5)
return PrepareHifi(context, node);
#else
TF_LITE_ENSURE_OK(context, SoftmaxPrepare(context, node));
#if defined(VISION_P6)
TF_LITE_ENSURE_OK(context, SoftmaxPrepareVision(context, node));
#endif
return kTfLiteOk;
#endif // defined(HIFI4) || defined(HIFI5)
}
TfLiteStatus XtensaEvalSoftmaxInt8Int16(TfLiteContext* context,
TfLiteNode* node) {
const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0);
TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0);
TFLITE_DCHECK(node->user_data != nullptr);
if (input->type == kTfLiteInt8 && output->type == kTfLiteInt16) {
#if defined(HIFI4) || defined(HIFI5)
return EvalHifi(static_cast<XtensaSoftmaxOpData*>(node->user_data), input,
output, context);
#else
SoftmaxParams op_data = *static_cast<SoftmaxParams*>(node->user_data);
tflite::reference_ops::Softmax(
op_data, tflite::micro::GetTensorShape(input),
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output));
return kTfLiteOk;
#endif // defined(HIFI4) || defined(HIFI5)
} else {
MicroPrintf("Type %s (%d) not supported.", TfLiteTypeGetName(input->type),
input->type);
return kTfLiteError;
}
}
TFLMRegistration Register_SOFTMAX_INT8_INT16() {
return tflite::micro::RegisterOp(XtensaInitSoftmax, XtensaPrepareSoftmax,
XtensaEvalSoftmaxInt8Int16);
}
} // namespace tflite