2 * Copyright (c) 2021 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/While.h"
19 #include "kernels/Utils.h"
23 namespace luci_interpreter
31 void copy(const std::vector<const Tensor *> &src, const std::vector<Tensor *> &dst)
33 for (size_t i = 0; i < src.size(); ++i)
35 LUCI_INTERPRETER_CHECK(dst[i]->element_type() == src[i]->element_type());
36 dst[i]->resize(src[i]->shape());
38 const int32_t num_elements = src[i]->shape().num_elements();
39 const std::size_t element_size = getDataTypeSize(src[i]->element_type());
40 std::memcpy(dst[i]->data<void>(), src[i]->data<void>(), num_elements * element_size);
44 void copy(const std::vector<Tensor *> &src, const std::vector<Tensor *> &dst)
46 std::vector<const Tensor *> const_src;
47 for (const auto &t : src)
48 const_src.push_back(t);
52 // TODO: Think about how allocate memory for output in main graph
53 void configureTensorsAllocations(const std::vector<Tensor *> &tensors, RuntimeGraph *run_graph)
55 for (auto tensor : tensors)
56 run_graph->configureAllocations(tensor);
61 While::While(std::vector<const Tensor *> inputs, std::vector<Tensor *> outputs,
62 RuntimeGraph *cond_graph, RuntimeGraph *body_graph)
63 : Kernel(std::move(inputs), std::move(outputs)), _cond_graph(cond_graph), _body_graph(body_graph)
67 void While::configure()
69 LUCI_INTERPRETER_CHECK(_body_graph->getInputTensors().size() == getInputTensors().size());
70 LUCI_INTERPRETER_CHECK(_body_graph->getOutputTensors().size() == getOutputTensors().size());
71 LUCI_INTERPRETER_CHECK(_body_graph->getOutputTensors().size() == getInputTensors().size());
73 LUCI_INTERPRETER_CHECK(_cond_graph->getInputTensors().size() == getInputTensors().size());
75 const auto &cond_outputs = _cond_graph->getOutputTensors();
76 LUCI_INTERPRETER_CHECK(cond_outputs.size() == 1)
77 LUCI_INTERPRETER_CHECK(cond_outputs[0]->element_type() == DataType::BOOL);
81 * @note Dynamic shape such as {1, 0, 8} may fail in tensor->data()
83 void While::execute() const
85 const auto &cond_inputs = _cond_graph->getInputTensors();
86 const auto &cond_outputs = _cond_graph->getOutputTensors();
88 configureTensorsAllocations(cond_inputs, _cond_graph);
90 copy(getInputTensors(), cond_inputs);
92 const auto &body_inputs = _body_graph->getInputTensors();
93 const auto &body_outputs = _body_graph->getOutputTensors();
95 configureTensorsAllocations(body_inputs, _body_graph);
99 _cond_graph->execute();
101 bool cond_value = cond_outputs[0]->data<bool>()[0];
105 copy(cond_inputs, body_inputs);
107 _body_graph->execute();
109 copy(body_outputs, cond_inputs);
112 copy(cond_inputs, getOutputTensors());
115 } // namespace kernels
116 } // namespace luci_interpreter