Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Greater.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 "TISOKernel.h"
20
21 #include "PALComparisons.h"
22
23 namespace luci_interpreter
24 {
25
26 namespace
27 {
28 // TODO: reduce code duplication with less
29 template <typename T>
30 void evalGeneric(const circle::Tensor *x, const circle::Tensor *y, const circle::Tensor *output,
31                  BaseRuntimeGraph *runtime_graph)
32 {
33   auto x_data = kernels::getTensorData<T>(runtime_graph->getDataByTensor(x));
34   if (x_data == nullptr)
35     x_data = kernels::getTensorData<T>(runtime_graph->getConstDataByTensor(x));
36
37   assert(x_data != nullptr);
38
39   auto y_data = kernels::getTensorData<T>(runtime_graph->getDataByTensor(y));
40   if (y_data == nullptr)
41     y_data = kernels::getTensorData<T>(runtime_graph->getConstDataByTensor(y));
42
43   assert(y_data != nullptr);
44
45   auto output_data = kernels::getTensorData<bool>(runtime_graph->getDataByTensor(output));
46
47   luci_interpreter_pal::ComparisonParams op_params;
48   op_params.is_broadcast = Tensor::num_elements(x) != Tensor::num_elements(y);
49
50   const int64_t flat_size = kernels::getTensorShape(x).flatSize();
51   luci_interpreter_pal::ComparisonNoScaling<T>(flat_size, x_data, y_data, output_data,
52                                                luci_interpreter_pal::GreaterFn);
53 }
54
55 } // namespace
56
57 void configure_kernel_CircleGreater(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
58 {
59   kernels::TISOKernel kernel(cur_op, runtime_graph);
60
61   LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
62                          Tensor::element_type(kernel.input2()));
63   LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.output()) == DataType::BOOL);
64 }
65
66 void execute_kernel_CircleGreater(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
67 {
68   kernels::TISOKernel kernel(cur_op, runtime_graph);
69
70   switch (Tensor::element_type(kernel.input1()))
71   {
72     case DataType::S64:
73       evalGeneric<int64_t>(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
74       break;
75     case DataType::S32:
76       evalGeneric<int32_t>(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
77       break;
78 #ifndef DIS_FLOAT
79     case DataType::FLOAT32:
80       evalGeneric<float>(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
81       break;
82 #endif // DIS_FLOAT
83     default:
84       assert(false && "Unsupported type.");
85   }
86 }
87
88 } // namespace luci_interpreter