Imported Upstream version 1.19.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / loader / nodes / Conv2D.cpp
1 /*
2  * Copyright (c) 2021 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 "Builders.h"
18
19 #include "kernels/Conv2D.h"
20 #include <luci/Plan/CircleNodeExecutionPlan.h>
21
22 namespace luci_interpreter
23 {
24
25 std::unique_ptr<Kernel> build_kernel_CircleConv2D(const luci::CircleNode *circle_node,
26                                                   KernelBuilderHelper &helper)
27 {
28   const auto *node = dynamic_cast<const luci::CircleConv2D *>(circle_node);
29   if (node == nullptr)
30     throw std::runtime_error("wrong builder for operation");
31   assert(node->arity() == 3);
32
33   const Tensor *input = helper.getInputTensor(node->input());
34   const Tensor *filter = helper.getInputTensor(node->filter());
35   const Tensor *bias = helper.getOptionalInputTensor(node->bias());
36   Tensor *output = helper.getOutputTensor(node);
37
38   auto im2col =
39     std::make_unique<Tensor>(input->element_type(), Shape({}), AffineQuantization{}, "");
40   im2col->set_observable(false);
41   im2col->set_data_buffer(nullptr);
42   // If node has execution plan then read memory offsets for im2col temporary tensor
43   // from the beginning of shared memory buffer.
44   // Used in Static Memory Manager.
45   // TODO move tensors offset initialization to one place
46   if (luci::has_execution_plan(node))
47   {
48     const auto execution_plan = luci::get_execution_plan(node);
49     // Check whether the offset for the current CircleConv2D temporary was found.
50     if (execution_plan.offsets().size() > 1)
51       // If this is true, then we keep this offset in im2col.
52       im2col->set_offset(execution_plan.offsets().at(1));
53   }
54   Tensor *tmp = helper.getRuntimeGraph(node->graph())->addTensor(std::move(im2col));
55
56   Conv2DParams params{};
57   params.padding = node->padding();
58   params.stride_height = node->stride()->h();
59   params.stride_width = node->stride()->w();
60   params.dilation_height_factor = node->dilation()->h();
61   params.dilation_width_factor = node->dilation()->w();
62   params.activation = node->fusedActivationFunction();
63
64   return std::make_unique<kernels::Conv2D>(input, filter, bias, output, tmp, params);
65 }
66
67 } // namespace luci_interpreter