Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Equal.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   if (op_params.is_broadcast)
51   {
52     luci_interpreter_pal::BroadcastComparison4DSlowNoScaling<T>(
53       op_params, kernels::getTensorShape(x), x_data, kernels::getTensorShape(y), y_data,
54       kernels::getTensorShape(output), output_data, luci_interpreter_pal::EqualFn);
55   }
56   else
57   {
58     const int64_t flat_size = kernels::getTensorShape(x).flatSize();
59     luci_interpreter_pal::ComparisonNoScaling<T>(flat_size, x_data, y_data, output_data,
60                                                  luci_interpreter_pal::EqualFn);
61   }
62 }
63
64 } // namespace
65
66 void configure_kernel_CircleEqual(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
67 {
68   kernels::TISOKernel kernel(cur_op, runtime_graph);
69
70   LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
71                          Tensor::element_type(kernel.input2()));
72   LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.output()) == DataType::BOOL);
73 }
74
75 void execute_kernel_CircleEqual(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
76 {
77   kernels::TISOKernel kernel(cur_op, runtime_graph);
78
79   switch (Tensor::element_type(kernel.input1()))
80   {
81     case DataType::S64:
82       evalGeneric<int64_t>(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
83       break;
84     case DataType::S32:
85       evalGeneric<int32_t>(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
86       break;
87 #ifndef DIS_FLOAT
88     case DataType::FLOAT32:
89       evalGeneric<float>(kernel.input1(), kernel.input2(), kernel.output(), runtime_graph);
90       break;
91 #endif // DIS_FLOAT
92     default:
93       assert(false && "Unsupported type.");
94   }
95 }
96
97 } // namespace luci_interpreter