Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Softmax.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "kernels/Softmax.h"
18
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/reference/softmax.h>
22 #include "PALSoftmax.h"
23
24 #include <stdexcept>
25
26 namespace luci_interpreter
27 {
28
29 namespace kernels
30 {
31
32 Softmax::Softmax(const Tensor *input, Tensor *output, const SoftmaxParams &params)
33   : KernelWithParams<SoftmaxParams>({input}, {output}, params)
34 {
35 }
36
37 void Softmax::configure()
38 {
39   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40   LUCI_INTERPRETER_CHECK(input()->shape().num_dims() >= 1);
41   if (input()->element_type() == DataType::U8 || input()->element_type() == DataType::S8)
42   {
43     LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::S8 || output()->zero_point() == 0);
44     LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::U8 ||
45                            output()->zero_point() == std::numeric_limits<int8_t>::min());
46     tflite::SoftmaxParams op_params{};
47     op_params.table = _table;
48     luci_interpreter_pal::PopulateSoftmaxLookupTable(&op_params, input()->scale(), params().beta);
49   }
50   output()->resize(input()->shape());
51 }
52
53 void Softmax::execute() const
54 {
55   switch (input()->element_type())
56   {
57     case DataType::FLOAT32:
58       evalFloat();
59       break;
60     case DataType::S8:
61       evalQuantized<int8_t>();
62       break;
63     case DataType::U8:
64       evalQuantized<uint8_t>();
65       break;
66     default:
67       throw std::runtime_error("Unsupported type.");
68   }
69 }
70
71 void Softmax::evalFloat() const
72 {
73   tflite::SoftmaxParams op_params{};
74   op_params.beta = params().beta;
75
76   tflite::reference_ops::Softmax(op_params, getTensorShape(input()), getTensorData<float>(input()),
77                                  getTensorShape(output()), getTensorData<float>(output()));
78 }
79
80 template <typename T> void Softmax::evalQuantized() const
81 {
82   tflite::SoftmaxParams op_params{};
83   op_params.table = const_cast<float *>(_table);
84   op_params.zero_point = output()->zero_point();
85   op_params.scale = output()->scale();
86   luci_interpreter_pal::InitializeParams(&op_params, input()->scale(), params().beta);
87   luci_interpreter_pal::Softmax(op_params, getTensorShape(input()), getTensorData<T>(input()),
88                                 getTensorShape(output()), getTensorData<T>(output()));
89 }
90
91 } // namespace kernels
92 } // namespace luci_interpreter