ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 int64_t num_elements = src[i]->shape().large_num_elements();
39 const std::size_t element_size = getDataTypeSize(src[i]->element_type());
40
41 // Check for integer overflow in size calculation
42 if (num_elements < 0 || static_cast<uint64_t>(num_elements) > SIZE_MAX / element_size)
43 {
44 throw std::runtime_error("Integer overflow in size calculation");
45 }
46
47 const int64_t total_size = num_elements * element_size;
48 std::memcpy(dst[i]->data<void>(), src[i]->data<void>(), static_cast<size_t>(total_size));
49 }
50}
51
52void copy(const std::vector<Tensor *> &src, const std::vector<Tensor *> &dst)
53{
54 std::vector<const Tensor *> const_src;
55 for (const auto &t : src)
56 const_src.push_back(t);
57 copy(const_src, dst);
58}
59
60// TODO: Think about how allocate memory for output in main graph
61void configureTensorsAllocations(const std::vector<Tensor *> &tensors, RuntimeGraph *run_graph)
62{
63 for (auto tensor : tensors)
64 run_graph->configureAllocations(tensor);
65}
66
67} // namespace
68
69While::While(std::vector<const Tensor *> inputs, std::vector<Tensor *> outputs,
70 RuntimeGraph *cond_graph, RuntimeGraph *body_graph)
71 : Kernel(std::move(inputs), std::move(outputs)), _cond_graph(cond_graph), _body_graph(body_graph)
72{
73}
74
76{
77 LUCI_INTERPRETER_CHECK(_body_graph->getInputTensors().size() == getInputTensors().size());
79 LUCI_INTERPRETER_CHECK(_body_graph->getOutputTensors().size() == getInputTensors().size());
80
81 LUCI_INTERPRETER_CHECK(_cond_graph->getInputTensors().size() == getInputTensors().size());
82
83 const auto &cond_outputs = _cond_graph->getOutputTensors();
85 LUCI_INTERPRETER_CHECK(cond_outputs[0]->element_type() == DataType::BOOL);
86}
87
91void While::execute() const
92{
93 const auto &cond_inputs = _cond_graph->getInputTensors();
94 const auto &cond_outputs = _cond_graph->getOutputTensors();
95
97
99
100 const auto &body_inputs = _body_graph->getInputTensors();
101 const auto &body_outputs = _body_graph->getOutputTensors();
102
104
105 while (true)
106 {
107 _cond_graph->execute();
108
109 bool cond_value = cond_outputs[0]->data<bool>()[0];
110 if (!cond_value)
111 break;
112
114
115 _body_graph->execute();
116
118 }
119
121}
122
123} // namespace kernels
124} // 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:91
While(std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs, RuntimeGraph *cond_graph, RuntimeGraph *body_graph)
Definition While.cpp:69
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
T must_cast(loco::Node *node)
int32_t size[5]
Definition Slice.cpp:35