b472323acaee916985dcd30e78697db51baa9c77
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / If.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 "kernels/If.h"
19 #include "kernels/Utils.h"
20
21 #include <cstring>
22
23 namespace luci_interpreter
24 {
25 namespace kernels
26 {
27
28 static std::vector<const Tensor *> joinInputs(const Tensor *cond,
29                                               const std::vector<const Tensor *> &inputs)
30 {
31   std::vector<const Tensor *> result{cond};
32   result.insert(result.cend(), inputs.cbegin(), inputs.cend());
33   return result;
34 }
35
36 If::If(const Tensor *cond, const std::vector<const Tensor *> &inputs, std::vector<Tensor *> outputs,
37        RuntimeGraph *then_graph, RuntimeGraph *else_graph)
38   : Kernel(joinInputs(cond, inputs), std::move(outputs)), _then_graph(then_graph),
39     _else_graph(else_graph)
40 {
41 }
42
43 void If::configure()
44 {
45   LUCI_INTERPRETER_CHECK(cond()->element_type() == DataType::BOOL);
46   LUCI_INTERPRETER_CHECK(cond()->shape().num_elements() == 1);
47
48   for (RuntimeGraph *graph : {_then_graph, _else_graph})
49   {
50     (void)graph;
51     LUCI_INTERPRETER_CHECK(graph->getInputTensors().size() == getInputTensors().size() - 1);
52     LUCI_INTERPRETER_CHECK(graph->getOutputTensors().size() == getOutputTensors().size());
53   }
54 }
55
56 void If::execute() const
57 {
58   const bool cond_value = cond()->data<bool>()[0];
59
60   RuntimeGraph *active_graph = cond_value ? _then_graph : _else_graph;
61   const auto &graph_inputs = active_graph->getInputTensors();
62   const auto &graph_outputs = active_graph->getOutputTensors();
63
64   // Copy kernel inputs to active graph inputs.
65   for (size_t i = 0; i < getInputTensors().size() - 1; ++i)
66   {
67     LUCI_INTERPRETER_CHECK(graph_inputs[i]->element_type() == input(i)->element_type());
68     graph_inputs[i]->resize(input(i)->shape());
69
70     const int32_t num_elements = input(i)->shape().num_elements();
71     const std::size_t element_size = getDataTypeSize(input(i)->element_type());
72     // TODO: Think about how allocate memory for output in main graph
73     active_graph->configureAllocations(graph_inputs[i]);
74     std::memcpy(graph_inputs[i]->data<void>(), input(i)->data<void>(), num_elements * element_size);
75   }
76
77   active_graph->execute();
78
79   // Copy graph outputs to kernel outputs.
80   for (size_t i = 0; i < getOutputTensors().size(); ++i)
81   {
82     LUCI_INTERPRETER_CHECK(graph_outputs[i]->element_type() == output(i)->element_type());
83     output(i)->resize(graph_outputs[i]->shape());
84     // TODO: Think about how allocate memory for output in main graph
85     active_graph->configureAllocations(output(i));
86
87     const int32_t num_elements = output(i)->shape().num_elements();
88     const std::size_t element_size = getDataTypeSize(output(i)->element_type());
89     std::memcpy(output(i)->data<void>(), graph_outputs[i]->data<void>(),
90                 num_elements * element_size);
91   }
92 }
93
94 } // namespace kernels
95 } // namespace luci_interpreter