Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / 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 "Builders.h"
18 #include "kernels/Utils.h"
19 #include "SISOKernel.h"
20
21 #include "PALSoftmax.h"
22
23 namespace luci_interpreter
24 {
25
26 namespace
27 {
28
29 #ifndef DIS_FLOAT
30 void evalFloat(const circle::Tensor *input, const circle::Tensor *output,
31                const circle::SoftmaxOptions *options, BaseRuntimeGraph *runtime_graph)
32 {
33   const auto *input_data = runtime_graph->getDataByTensor(input);
34   auto *output_data = runtime_graph->getDataByTensor(output);
35
36   luci_interpreter_pal::Softmax(options->beta(), kernels::getTensorShape(input),
37                                 kernels::getTensorData<float>(input_data),
38                                 kernels::getTensorData<float>(output_data));
39 }
40 #endif // DIS_FLOAT
41
42 } // namespace
43
44 void configure_kernel_CircleSoftmax(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
45 {
46   kernels::SISOKernel kernel(cur_op, runtime_graph);
47
48   LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input()) ==
49                          Tensor::element_type(kernel.output()));
50   LUCI_INTERPRETER_CHECK(Tensor::num_dims(kernel.input()) >= 1);
51
52 #ifndef DIS_QUANT
53   if (Tensor::element_type(kernel.input()) == DataType::U8 ||
54       Tensor::element_type(kernel.input()) == DataType::S8)
55   {
56     LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input()) == DataType::S8 ||
57                            Tensor::zero_point(kernel.output()) == 0);
58     LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input()) == DataType::U8 ||
59                            Tensor::zero_point(kernel.output()) ==
60                              std::numeric_limits<int8_t>::min());
61   }
62 #endif
63 }
64
65 void execute_kernel_CircleSoftmax(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
66 {
67   kernels::SISOKernel kernel(cur_op, runtime_graph);
68
69   const auto *options = cur_op->builtin_options_as_SoftmaxOptions();
70
71   switch (Tensor::element_type(kernel.input()))
72   {
73 #ifndef DIS_FLOAT
74     case DataType::FLOAT32:
75       evalFloat(kernel.input(), kernel.output(), options, runtime_graph);
76       break;
77 #endif // DIS_FLOAT
78     default:
79       assert(false && "Unsupported type.");
80   }
81 }
82
83 } // namespace luci_interpreter