Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / AddN.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "Builders.h"
19 #include "kernels/Utils.h"
20
21 #include "PALAddN.h"
22
23 namespace luci_interpreter
24 {
25
26 namespace
27 {
28
29 template <typename T>
30 void evalGeneric(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
31 {
32   const auto output_index = cur_op->outputs()->operator[](0);
33   assert(output_index != -1);
34   const auto output = runtime_graph->getCircleTensorByIndex(output_index);
35
36   const auto input_sizes = cur_op->inputs()->size();
37
38   auto input1_index = cur_op->inputs()->operator[](0);
39   const auto *tensor1 = runtime_graph->getCircleTensorByIndex(input1_index);
40
41   const int flat_size = Tensor::num_elements(tensor1);
42
43   std::vector<const T *> all_input_data;
44   for (int32_t i = 0; i < input_sizes; ++i)
45   {
46     auto input_index = cur_op->inputs()->operator[](i);
47     const auto *tensor = runtime_graph->getCircleTensorByIndex(input_index);
48
49     const auto *tensor_data = runtime_graph->getDataByTensor(tensor);
50     if (tensor_data == nullptr)
51       tensor_data = runtime_graph->getConstDataByTensor(tensor);
52
53     auto *data = reinterpret_cast<const T *>(tensor_data);
54     all_input_data.push_back(data);
55   }
56
57   auto *output_data = reinterpret_cast<T *>(runtime_graph->getDataByTensor(output));
58
59   luci_interpreter_pal::AddN(flat_size, input_sizes, all_input_data.data(), output_data);
60 }
61
62 } // namespace
63
64 void configure_kernel_CircleAddN(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
65 {
66   const int num_inputs = cur_op->inputs()->size();
67
68   LUCI_INTERPRETER_CHECK(num_inputs >= 2);
69   LUCI_INTERPRETER_CHECK(cur_op->outputs()->size() == 1);
70
71   const auto input1_index = cur_op->inputs()->operator[](0);
72   assert(input1_index != -1);
73
74   const auto input1_tensor = runtime_graph->getCircleTensorByIndex(input1_index);
75   assert(input1_tensor != nullptr);
76
77   for (int i = 1; i < num_inputs; ++i)
78   {
79     const auto input_index = cur_op->inputs()->operator[](i);
80     assert(input_index != -1);
81
82     const auto input_tensor = runtime_graph->getCircleTensorByIndex(input_index);
83     assert(input_tensor != nullptr);
84
85     LUCI_INTERPRETER_CHECK(Tensor::element_type(input1_tensor) ==
86                            Tensor::element_type(input_tensor));
87     LUCI_INTERPRETER_CHECK(Tensor::num_dims(input1_tensor) == Tensor::num_dims(input_tensor));
88     LUCI_INTERPRETER_CHECK(Tensor::num_elements(input1_tensor) ==
89                            Tensor::num_elements(input_tensor));
90   }
91 }
92
93 void execute_kernel_CircleAddN(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
94 {
95   const auto output_index = cur_op->outputs()->operator[](0);
96   assert(output_index != -1);
97   const auto output = runtime_graph->getCircleTensorByIndex(output_index);
98
99   switch (Tensor::element_type(output))
100   {
101 #ifndef DIS_FLOAT
102     case DataType::FLOAT32:
103     {
104       evalGeneric<float>(cur_op, runtime_graph);
105     }
106     break;
107 #endif // DIS_FLOAT
108     default:
109       assert(false && "Unsupported type.");
110   }
111 }
112
113 } // namespace luci_interpreter