ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
While.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 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/While.h"
19#include "kernels/Utils.h"
20
21#include <cstring>
22
23namespace luci_interpreter
24{
25namespace kernels
26{
27
28namespace
29{
30
31void copy(const std::vector<const Tensor *> &src, const std::vector<Tensor *> &dst)
32{
33 for (size_t i = 0; i < src.size(); ++i)
34 {
35 LUCI_INTERPRETER_CHECK(dst[i]->element_type() == src[i]->element_type());
36 dst[i]->resize(src[i]->shape());
37
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);
41 }
42}
43
44void copy(const std::vector<Tensor *> &src, const std::vector<Tensor *> &dst)
45{
46 std::vector<const Tensor *> const_src;
47 for (const auto &t : src)
48 const_src.push_back(t);
49 copy(const_src, dst);
50}
51
52// TODO: Think about how allocate memory for output in main graph
53void configureTensorsAllocations(const std::vector<Tensor *> &tensors, RuntimeGraph *run_graph)
54{
55 for (auto tensor : tensors)
56 run_graph->configureAllocations(tensor);
57}
58
59} // namespace
60
61While::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)
64{
65}
66
68{
69 LUCI_INTERPRETER_CHECK(_body_graph->getInputTensors().size() == getInputTensors().size());
71 LUCI_INTERPRETER_CHECK(_body_graph->getOutputTensors().size() == getInputTensors().size());
72
73 LUCI_INTERPRETER_CHECK(_cond_graph->getInputTensors().size() == getInputTensors().size());
74
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);
78}
79
83void While::execute() const
84{
85 const auto &cond_inputs = _cond_graph->getInputTensors();
86 const auto &cond_outputs = _cond_graph->getOutputTensors();
87
88 configureTensorsAllocations(cond_inputs, _cond_graph);
89
90 copy(getInputTensors(), cond_inputs);
91
92 const auto &body_inputs = _body_graph->getInputTensors();
93 const auto &body_outputs = _body_graph->getOutputTensors();
94
95 configureTensorsAllocations(body_inputs, _body_graph);
96
97 while (true)
98 {
99 _cond_graph->execute();
100
101 bool cond_value = cond_outputs[0]->data<bool>()[0];
102 if (!cond_value)
103 break;
104
105 copy(cond_inputs, body_inputs);
106
107 _body_graph->execute();
108
109 copy(body_outputs, cond_inputs);
110 }
111
112 copy(cond_inputs, getOutputTensors());
113}
114
115} // namespace kernels
116} // namespace luci_interpreter
const std::vector< Tensor * > & getOutputTensors() const
Definition Kernel.h:40
const std::vector< const Tensor * > & getInputTensors() const
Definition Kernel.h:39
const std::vector< Tensor * > & getOutputTensors() const
const std::vector< Tensor * > & getInputTensors() const
void execute() const override
Definition While.cpp:83
While(std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs, RuntimeGraph *cond_graph, RuntimeGraph *body_graph)
Definition While.cpp:61
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
int32_t size[5]
Definition Slice.cpp:35