ONE - On-device Neural Engine
Loading...
Searching...
No Matches
KernelGenerator.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "KernelGenerator.h"
18
19#include "kernel/CallLayer.h"
20#include "kernel/IfLayer.h"
21#include "kernel/PermuteLayer.h"
22#include "kernel/WhileLayer.h"
23
25
27{
28
30 const std::shared_ptr<TensorRegistry> &tensor_reg,
31 const std::shared_ptr<ExternalContext> &external_context)
32 : basic::KernelGeneratorBase{graph}, _dyn_tensor_manager{dyn_tensor_manager},
33 _tensor_reg{tensor_reg}, _tensor_registries{}, _executors{nullptr}, _model_index{},
34 _external_context{external_context}
35{
36 // DO NOTHING
37}
38
39std::unique_ptr<exec::FunctionSequence> KernelGenerator::generate(ir::OperationIndex ind)
40{
41 assert(_dyn_tensor_manager);
42 assert(_tensor_reg);
43
44 auto ret = std::make_unique<exec::FunctionSequence>();
45
46 // Prepare to handle dynamic tensors later
47 auto dyn_ctx = std::make_shared<exec::FunctionSequence::DynamicTensorCtx>();
48 {
49 dyn_ctx->op = &_graph.operations().at(ind);
50 dyn_ctx->dynamic_shape_inferer = std::make_unique<exec::DynamicShapeInferer>(_tensor_reg);
51 }
52 ret->dynamic_tensor_ctx(dyn_ctx);
53
54 auto &op = _graph.operations().at(ind);
55 op.accept(*this);
56 assert(_return_fn); // _return_fn must have been generated
57 ret->append(std::move(_return_fn));
58
59 return ret;
60}
61
62void KernelGenerator::visit(const ir::operation::Call &node)
63{
64 const auto callee_subg_index = node.param().callee_subg_index;
65
66 std::vector<backend::IPortableTensor *> input_tensors;
67 for (const auto &input_index : node.getInputs())
68 {
69 auto input_tensor = getPortableTensor(input_index);
70 input_tensors.emplace_back(input_tensor);
71 }
72
73 std::vector<backend::IPortableTensor *> output_tensors;
74 for (const auto &output_index : node.getOutputs())
75 {
76 auto output_tensor = getPortableTensor(output_index);
77 output_tensors.emplace_back(output_tensor);
78 }
79
80 auto fn = std::make_unique<::onert::backend::builtin::kernel::CallLayer>(
81 input_tensors, output_tensors, callee_subg_index, _executors, _model_index, _external_context);
82
83 _return_fn = std::move(fn);
84}
85
86void KernelGenerator::visit(const ir::operation::If &node)
87{
88 const auto then_subg_index = node.param().then_subg_index;
89 const auto else_subg_index = node.param().else_subg_index;
90
91 std::vector<backend::IPortableTensor *> input_tensors;
92 for (const auto &input_index : node.getInputs())
93 {
94 auto input_tensor = getPortableTensor(input_index);
95 input_tensors.emplace_back(input_tensor);
96 }
97
98 std::vector<backend::IPortableTensor *> output_tensors;
99 for (const auto &output_index : node.getOutputs())
100 {
101 auto output_tensor = getPortableTensor(output_index);
102 output_tensors.emplace_back(output_tensor);
103 }
104
105 // IfLayer just set Executors instead of then and else executor to avoid complexity of
106 // creating executor recusively
107 const auto cond_tensor = input_tensors.front();
108 input_tensors.erase(input_tensors.begin());
109 auto fn = std::make_unique<::onert::backend::builtin::kernel::IfLayer>(
110 cond_tensor, input_tensors, output_tensors, then_subg_index, else_subg_index, _executors,
111 _model_index, _external_context);
112
113 _return_fn = std::move(fn);
114}
115
116void KernelGenerator::visit(const ir::operation::Permute &node)
117{
118 const auto output_index{node.getOutputs().at(0)};
119 const auto input_index{node.getInputs().at(0)};
120
121 // Add PermuteLayer
122 std::vector<ITensor *> output_tensors{getTensor(output_index)};
123 std::vector<ITensor *> input_tensors{getTensor(input_index)};
124 std::vector<ir::PermuteType> permute_types{node.getPermuteType()};
125
126 auto fn = std::make_unique<kernel::PermuteLayer>(input_tensors, output_tensors, permute_types,
127 _external_context);
128 _return_fn = std::move(fn);
129}
130
131void KernelGenerator::visit(const ir::operation::While &node)
132{
133 const auto cond_subg_index = node.param().cond_subg_index;
134 const auto body_subg_index = node.param().body_subg_index;
135
136 // This op does not support input as a constant, because builtin backend does not have
137 // TensorBuilder
138 std::vector<backend::IPortableTensor *> input_tensors;
139 for (const auto &input_index : node.getInputs())
140 {
141 auto input_tensor = getPortableTensor(input_index);
142 input_tensors.emplace_back(input_tensor);
143 }
144
145 std::vector<backend::IPortableTensor *> output_tensors;
146 for (const auto &output_index : node.getOutputs())
147 {
148 auto output_tensor = getPortableTensor(output_index);
149 output_tensors.emplace_back(output_tensor);
150 }
151
152 // WhileLayer just set Executors instead of cond and body executor to avoid complexity of
153 // creating executor recusively
154 auto fn = std::make_unique<::onert::backend::builtin::kernel::WhileLayer>(
155 input_tensors, output_tensors, cond_subg_index, body_subg_index, _executors, _model_index,
156 _dyn_tensor_manager->dynamic_mem_mgr().get(), _external_context);
157
158 _return_fn = std::move(fn);
159}
160
161backend::ITensor *KernelGenerator::getTensor(const ir::OperandIndex &index)
162{
163 // get Tensor from all tensor registries (for Permute op)
164 auto ret = _tensor_registries.getITensor(index);
165 assert(ret != nullptr);
166 return ret;
167}
168
169backend::IPortableTensor *KernelGenerator::getPortableTensor(const ir::OperandIndex &index)
170{
171 auto ret = _tensor_reg->getPortableTensor(index);
172 assert(ret != nullptr);
173 return ret;
174}
175
176} // namespace onert::backend::builtin
Class to manage dynamic tensor and its memory.
std::shared_ptr< DynamicMemoryManager > dynamic_mem_mgr()
std::unique_ptr< exec::IFunction > _return_fn
KernelGenerator(const ir::Graph &graph, DynamicTensorManager *dyn_tensor_manager, const std::shared_ptr< TensorRegistry > &tensor_reg, const std::shared_ptr< ExternalContext > &external_context)
std::unique_ptr< exec::FunctionSequence > generate(ir::OperationIndex ind) override
backend::ITensor * getITensor(ir::OperandIndex ind) const
const Operations & operations() const override
Definition Graph.h:105
const Param & param() const
Definition Call.h:42
const Object & at(const Index &index) const
Get the object that is associated with the given index.
::onert::util::Index< uint32_t, OperandIndexTag > OperandIndex
Definition Index.h:33
SubgraphIndex callee_subg_index
Definition Call.h:31