2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/If.h"
19 #include "kernels/Utils.h"
23 namespace luci_interpreter
28 static std::vector<const Tensor *> joinInputs(const Tensor *cond,
29 const std::vector<const Tensor *> &inputs)
31 std::vector<const Tensor *> result{cond};
32 result.insert(result.cend(), inputs.cbegin(), inputs.cend());
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)
45 LUCI_INTERPRETER_CHECK(cond()->element_type() == DataType::BOOL);
46 LUCI_INTERPRETER_CHECK(cond()->shape().num_elements() == 1);
48 for (RuntimeGraph *graph : {_then_graph, _else_graph})
51 LUCI_INTERPRETER_CHECK(graph->getInputTensors().size() == getInputTensors().size() - 1);
52 LUCI_INTERPRETER_CHECK(graph->getOutputTensors().size() == getOutputTensors().size());
56 void If::execute() const
58 const bool cond_value = cond()->data<bool>()[0];
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();
64 // Copy kernel inputs to active graph inputs.
65 for (size_t i = 0; i < getInputTensors().size() - 1; ++i)
67 LUCI_INTERPRETER_CHECK(graph_inputs[i]->element_type() == input(i)->element_type());
68 graph_inputs[i]->resize(input(i)->shape());
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);
77 active_graph->execute();
79 // Copy graph outputs to kernel outputs.
80 for (size_t i = 0; i < getOutputTensors().size(); ++i)
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));
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);
94 } // namespace kernels
95 } // namespace luci_interpreter