2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "kernels/Conv2D.h"
20 #include <luci/Plan/CircleNodeExecutionPlan.h>
22 namespace luci_interpreter
25 std::unique_ptr<Kernel> build_kernel_CircleConv2D(const luci::CircleNode *circle_node,
26 KernelBuilderHelper &helper)
28 const auto *node = dynamic_cast<const luci::CircleConv2D *>(circle_node);
30 throw std::runtime_error("wrong builder for operation");
31 assert(node->arity() == 3);
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);
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))
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));
54 Tensor *tmp = helper.getRuntimeGraph(node->graph())->addTensor(std::move(im2col));
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();
64 return std::make_unique<kernels::Conv2D>(input, filter, bias, output, tmp, params);
67 } // namespace luci_interpreter