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 "ops/ConvolutionLayer.h"
20#include "ops/DepthwiseConvolutionLayer.h"
21#include "ops/FullyConnectedLayer.h"
22
23#include <backend/Backend.h>
24#include <backend/IConfig.h>
25#include <memory>
26#include <util/Utils.h>
27#include <util/logging.h>
29
30#include <stdexcept>
31
32namespace onert
33{
34namespace backend
35{
36namespace xnnpack
37{
38
40 const ir::Graph &graph, const std::shared_ptr<TensorBuilder> &tensor_builder,
41 const std::shared_ptr<basic::TensorRegistry> &tensor_reg,
42 const std::shared_ptr<backend::custom::IKernelBuilder> &kernel_builder,
43 const std::shared_ptr<ExternalContext> &external_context)
44 : basic::KernelGeneratorBase{graph}, _ctx(graph.operands()), _operations_ctx{graph.operations()},
45 _tensor_builder(tensor_builder), _tensor_reg{tensor_reg}, _kernel_builder(kernel_builder),
46 _external_context(external_context)
47{
48 // DO NOTHING
49}
50
51std::unique_ptr<exec::FunctionSequence> KernelGenerator::generate(ir::OperationIndex ind)
52{
53 auto ret = std::make_unique<exec::FunctionSequence>();
54
55 assert(_tensor_builder->dynamicTensorManager());
56 assert(_tensor_reg);
57
58 // Prepare to handle dynamic tensors later
59 auto dyn_ctx = std::make_shared<exec::FunctionSequence::DynamicTensorCtx>();
60 {
61 dyn_ctx->op = &_operations_ctx.at(ind);
62 dyn_ctx->dynamic_shape_inferer = std::make_shared<exec::DynamicShapeInferer>(_tensor_reg);
63 }
64 ret->dynamic_tensor_ctx(dyn_ctx);
65
66 auto &op = _graph.operations().at(ind);
67 op.accept(*this);
68 assert(_return_fn); // _return_fn must have been generated
69 ret->append(std::move(_return_fn));
70
71 for (auto &&ind : (op.getInputs() | ir::Remove::UNDEFINED) + op.getOutputs())
72 {
73 auto tensor = _tensor_reg->getNativeTensor(ind);
74 if (tensor)
75 {
76 tensor->increase_ref();
77 }
78 }
79 return ret;
80}
81
82void KernelGenerator::visit(const ir::operation::Conv2D &node)
83{
85
86 const auto ofm_index{node.getOutputs().at(0)};
87 const auto ifm_index{node.getInputs().at(Conv2D::Input::INPUT)};
88 const auto ker_index{node.getInputs().at(Conv2D::Input::KERNEL)};
89 const auto bias_index{node.getInputs().at(Conv2D::Input::BIAS)};
90
91 auto ofm_tensor = _tensor_reg->getPortableTensor(ofm_index);
92 auto ifm_tensor = _tensor_reg->getPortableTensor(ifm_index);
93 auto ker_tensor = _tensor_reg->getPortableTensor(ker_index);
94 auto bias_tensor = _tensor_reg->getPortableTensor(bias_index);
95
96 const auto stride = node.param().stride;
97 const auto activation = node.param().activation;
98 const auto &param_padding = node.param().padding;
99 const auto dilation = node.param().dilation;
100 auto fn = std::make_unique<ops::ConvolutionLayer>(_external_context);
101
102 const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature();
103 const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature();
104 // Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
105 const auto &ker_shape = _ctx.at(ker_index).shape();
106 const auto ker_height = ker_shape.dim(1);
107 const auto ker_width = ker_shape.dim(2);
108
109 const auto padding =
110 ir::calculatePadding(param_padding, ifm_shape, ofm_shape, stride, ker_width, ker_height,
111 dilation.width_factor, dilation.height_factor);
112
113 fn->configure(ifm_tensor, ker_tensor, bias_tensor, param_padding.type, padding.left,
114 padding.right, padding.top, padding.bottom, stride.horizontal, stride.vertical,
115 dilation.width_factor, dilation.height_factor, activation, ofm_tensor);
116
117 _return_fn = std::move(fn);
118}
119
120void KernelGenerator::visit(const ir::operation::DepthwiseConv2D &node)
121{
122 using ir::operation::DepthwiseConv2D;
123
124 const auto ofm_index{node.getOutputs().at(0)};
125 const auto ifm_index{node.getInputs().at(DepthwiseConv2D::Input::INPUT)};
126 const auto ker_index{node.getInputs().at(DepthwiseConv2D::Input::KERNEL)};
127 const auto bias_index{node.getInputs().at(DepthwiseConv2D::Input::BIAS)};
128
129 const auto stride = node.param().stride;
130 const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature();
131 const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature();
132 // Kernel format is [1, kernel_height, kernel_width, depth_out].
133 const auto &ker_shape = _ctx.at(ker_index).shape();
134 const auto ker_height = ker_shape.dim(1);
135 const auto ker_width = ker_shape.dim(2);
136 const auto dilation_width = node.param().dilation.width_factor;
137 const auto dilation_height = node.param().dilation.height_factor;
138 const auto &param_padding = node.param().padding;
139 const auto padding = ir::calculatePadding(param_padding, ifm_shape, ofm_shape, stride, ker_width,
140 ker_height, dilation_width, dilation_height);
141 const auto multiplier = node.param().multiplier;
142 const auto activation = node.param().activation;
143
144 auto ofm_tensor = _tensor_reg->getPortableTensor(ofm_index);
145 auto ifm_tensor = _tensor_reg->getPortableTensor(ifm_index);
146 auto ker_tensor = _tensor_reg->getPortableTensor(ker_index);
147 auto bias_tensor = _tensor_reg->getPortableTensor(bias_index);
148
149 auto fn = std::make_unique<ops::DepthwiseConvolutionLayer>(_external_context);
150
151 fn->configure(ifm_tensor, ker_tensor, bias_tensor, param_padding.type, padding.left,
152 padding.right, padding.top, padding.bottom, stride.horizontal, stride.vertical,
153 multiplier, dilation_width, dilation_height, activation, ofm_tensor);
154
155 _return_fn = std::move(fn);
156}
157
158void KernelGenerator::visit(const ir::operation::FullyConnected &node)
159{
160 using ir::operation::FullyConnected;
161
162 const auto output_index{node.getOutputs().at(0)};
163 const auto input_index{node.getInputs().at(FullyConnected::Input::INPUT)};
164 const auto weight_index{node.getInputs().at(FullyConnected::Input::WEIGHT)};
165 const auto bias_index{node.getInputs().at(FullyConnected::Input::BIAS)};
166 const auto activation = node.param().activation;
167
168 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
169 auto input_tensor = _tensor_reg->getPortableTensor(input_index);
170 auto weight_tensor = _tensor_reg->getPortableTensor(weight_index);
171 auto bias_tensor = bias_index.undefined() ? nullptr : _tensor_reg->getPortableTensor(bias_index);
172
173 auto fn = std::make_unique<ops::FullyConnectedLayer>(_external_context);
174
175 fn->configure(input_tensor, weight_tensor, bias_tensor, activation, output_tensor);
176
177 _return_fn = std::move(fn);
178}
179
180} // namespace xnnpack
181} // namespace backend
182} // namespace onert
KernelGenerator(const ir::Graph &graph, const std::shared_ptr< TensorBuilder > &tensor_builder, const std::shared_ptr< basic::TensorRegistry > &tensor_reg, const std::shared_ptr< custom::IKernelBuilder > &kernel_builder, const std::shared_ptr< ExternalContext > &external_context)
const OperandIndex & at(IOIndex set_index) const
const OperandIndexSequence & getOutputs() const override
Definition Operation.h:55
OperandIndexSequence & getInputs()
Definition Operation.h:53
const Param & param() const
Definition Conv2D.h:60
This file contains utility macro.