1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/micro/kernels/softmax.h"
17 
18 #include "CMSIS/NN/Include/arm_nnfunctions.h"
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/kernels/internal/common.h"
21 #include "tensorflow/lite/kernels/internal/quantization_util.h"
22 #include "tensorflow/lite/kernels/internal/reference/softmax.h"
23 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
24 #include "tensorflow/lite/kernels/kernel_util.h"
25 #include "tensorflow/lite/kernels/op_macros.h"
26 #include "tensorflow/lite/micro/kernels/kernel_util.h"
27 
28 namespace tflite {
29 namespace {
30 
SoftmaxQuantized(const TfLiteEvalTensor * input,TfLiteEvalTensor * output,const SoftmaxParams & op_data)31 void SoftmaxQuantized(const TfLiteEvalTensor* input, TfLiteEvalTensor* output,
32                       const SoftmaxParams& op_data) {
33   if (input->type == kTfLiteInt8) {
34     if (output->type == kTfLiteInt16) {
35       tflite::reference_ops::Softmax(
36           op_data, tflite::micro::GetTensorShape(input),
37           tflite::micro::GetTensorData<int8_t>(input),
38           tflite::micro::GetTensorShape(output),
39           tflite::micro::GetTensorData<int16_t>(output));
40     } else {
41       const auto input_shape = tflite::micro::GetTensorShape(input);
42       const auto output_shape = tflite::micro::GetTensorShape(output);
43       const int trailing_dim = input_shape.DimensionsCount() - 1;
44       const int outer_size =
45           MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
46       const int depth =
47           MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
48 
49       arm_softmax_s8(tflite::micro::GetTensorData<int8_t>(input), outer_size,
50                      depth, op_data.input_multiplier, op_data.input_left_shift,
51                      op_data.diff_min,
52                      tflite::micro::GetTensorData<int8_t>(output));
53     }
54   } else {
55     tflite::reference_ops::SoftmaxInt16(
56         op_data, tflite::micro::GetTensorShape(input),
57         tflite::micro::GetTensorData<int16_t>(input),
58         tflite::micro::GetTensorShape(output),
59         tflite::micro::GetTensorData<int16_t>(output));
60   }
61 }
62 
SoftmaxEval(TfLiteContext * context,TfLiteNode * node)63 TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) {
64   const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0);
65   TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0);
66 
67   TFLITE_DCHECK(node->user_data != nullptr);
68   const SoftmaxParams data =
69       *static_cast<const SoftmaxParams*>(node->user_data);
70 
71   switch (input->type) {
72     case kTfLiteFloat32: {
73       tflite::reference_ops::Softmax(
74           data, tflite::micro::GetTensorShape(input),
75           tflite::micro::GetTensorData<float>(input),
76           tflite::micro::GetTensorShape(output),
77           tflite::micro::GetTensorData<float>(output));
78       return kTfLiteOk;
79     }
80     case kTfLiteInt8:
81     case kTfLiteInt16: {
82       SoftmaxQuantized(input, output, data);
83       return kTfLiteOk;
84     }
85     default:
86       TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
87                          TfLiteTypeGetName(input->type), input->type);
88       return kTfLiteError;
89   }
90 }
91 
92 }  // namespace
93 
Register_SOFTMAX()94 TfLiteRegistration Register_SOFTMAX() {
95   return {/*init=*/SoftmaxInit,
96           /*free=*/nullptr,
97           /*prepare=*/SoftmaxPrepare,
98           /*invoke=*/SoftmaxEval,
99           /*profiling_string=*/nullptr,
100           /*builtin_code=*/0,
101           /*custom_name=*/nullptr,
102           /*version=*/0};
103 }
104 
105 }  // namespace tflite
106