Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / interp / operations / DepthwiseConv2D.cc
1 /*
2  * Copyright (c) 2019 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 <cker/operation/DepthwiseConv.h>
18 #include <misc/polymorphic_downcast.h>
19
20 #include "OperationUtil.h"
21
22 #include "interp/Registration.h"
23 #include "ir/operation/DepthwiseConv2D.h"
24 #include "util/Utils.h"
25 #include "util/ShapeInference.h"
26
27 namespace onert
28 {
29 namespace interp
30 {
31
32 namespace
33 {
34
35 void prepareDepthwiseConv(ExecEnv *env, const ir::Operation &node)
36 {
37   const auto in_index = node.getInputs().at(ir::operation::DepthwiseConv2D::INPUT);
38   const auto kernel_index = node.getInputs().at(ir::operation::DepthwiseConv2D::KERNEL);
39   const auto bias_index = node.getInputs().at(ir::operation::DepthwiseConv2D::BIAS);
40   const auto out_index = node.getOutputs().at(0);
41
42   const auto in_tensor = env->tensorAt(in_index);
43   const auto kernel_tensor = env->tensorAt(kernel_index);
44   const auto bias_tensor = env->tensorAt(bias_index);
45
46   assert(in_tensor->num_dimensions() == 4);
47   assert(kernel_tensor->num_dimensions() == 4);
48   assert(bias_tensor->num_dimensions() == 1);
49
50   UNUSED_RELEASE(in_tensor);
51   UNUSED_RELEASE(kernel_tensor);
52   UNUSED_RELEASE(bias_tensor);
53
54   // TODO handle unspecified output shape:
55   //      calculate output shape using ifm shape, kernel shape, padding, stride
56   const auto output_info = env->graph().operands().at(out_index).info();
57   if (output_info.total_size() == 0)
58   {
59     // Handle unspecified output shape
60     const auto &depth_conv_node =
61         nnfw::misc::polymorphic_downcast<const ir::operation::DepthwiseConv2D &>(node);
62     const auto infered_output_shape = shape_inference::inferDepthwiseConv2DShape(
63         in_tensor->tensorInfo().shape(), kernel_tensor->tensorInfo().shape(),
64         depth_conv_node.param());
65     env->allocateIfNeeded(
66         out_index, ir::OperandInfo::createStaticInfo(infered_output_shape, output_info.typeInfo()));
67   }
68   else
69   {
70     env->allocateIfNeeded(out_index, output_info);
71   }
72
73   auto out_tensor = env->tensorAt(out_index);
74   UNUSED_RELEASE(out_tensor);
75
76   // Handle same ifm & ofm data type only
77   assert(in_tensor->data_type() == out_tensor->data_type());
78   assert(out_tensor->num_dimensions() == 4);
79 }
80
81 void invoke(const ITensor *ifm_tensor, const ITensor *ker_tensor, const ITensor *bias_tensor,
82             const ITensor *ofm_tensor, const ir::operation::DepthwiseConv2D::Param &param)
83 {
84   // TODO Support NCHW frontend
85   const auto ifm_shape = ifm_tensor->tensorInfo().shape().asFeature(ir::Layout::NHWC);
86   const auto ofm_shape = ofm_tensor->tensorInfo().shape().asFeature(ir::Layout::NHWC);
87   // Kernel format is [1, kernel_height, kernel_width, depth_out].
88   const auto &ker_shape = ker_tensor->tensorInfo().shape();
89   const auto ker_height = ker_shape.dim(1);
90   const auto ker_width = ker_shape.dim(2);
91   const auto padding = ir::calculatePadding(param.padding, ifm_shape, ofm_shape, param.stride,
92                                             ker_width, ker_height);
93
94   // Calculate
95   float activation_min, activation_max;
96   calculateActivationRange(param.activation, &activation_min, &activation_max);
97
98   nnfw::cker::DepthwiseConvParams cker_param;
99   cker_param.padding_values.width = padding.left;
100   cker_param.padding_values.height = padding.top;
101   cker_param.depth_multiplier = param.multiplier;
102   cker_param.stride_width = param.stride.horizontal;
103   cker_param.stride_height = param.stride.vertical;
104   cker_param.dilation_width_factor = 1;
105   cker_param.dilation_height_factor = 1;
106   cker_param.float_activation_min = activation_min;
107   cker_param.float_activation_max = activation_max;
108
109   const auto cker_ifm_shape = convertShape(ifm_tensor->tensorInfo().shape());
110   const auto cker_ker_shape = convertShape(ker_tensor->tensorInfo().shape());
111   const auto cker_bias_shape = convertShape(bias_tensor->tensorInfo().shape());
112   const auto cker_ofm_shape = convertShape(ofm_tensor->tensorInfo().shape());
113   const float *ifm_ptr = reinterpret_cast<const float *>(ifm_tensor->bufferRO());
114   const float *ker_ptr = reinterpret_cast<const float *>(ker_tensor->bufferRO());
115   const float *bias_ptr = reinterpret_cast<const float *>(bias_tensor->bufferRO());
116   float *ofm_ptr = reinterpret_cast<float *>(ofm_tensor->buffer());
117
118   nnfw::cker::DepthwiseConv(cker_param, cker_ifm_shape, ifm_ptr, cker_ker_shape, ker_ptr,
119                             cker_bias_shape, bias_ptr, cker_ofm_shape, ofm_ptr, nullptr);
120 }
121
122 void invokeDepthwiseConv(const ExecEnv *env, const ir::Operation &node)
123 {
124   const auto &conv_node = static_cast<const ir::operation::DepthwiseConv2D &>(node);
125
126   const auto ifm_index = node.getInputs().at(ir::operation::DepthwiseConv2D::INPUT);
127   const auto ker_index = node.getInputs().at(ir::operation::DepthwiseConv2D::KERNEL);
128   const auto bias_index = node.getInputs().at(ir::operation::DepthwiseConv2D::BIAS);
129   const auto ofm_index = node.getOutputs().at(0);
130
131   const auto ifm_tensor = env->tensorAt(ifm_index);
132   const auto ker_tensor = env->tensorAt(ker_index);
133   const auto bias_tensor = env->tensorAt(bias_index);
134   const auto ofm_tensor = env->tensorAt(ofm_index);
135
136   const auto data_type = ifm_tensor->data_type();
137   if (data_type == ir::DataType::FLOAT32)
138   {
139     invoke(ifm_tensor, ker_tensor, bias_tensor, ofm_tensor, conv_node.param());
140   }
141   else
142   {
143     throw std::runtime_error{"NYI: Support float32 only"};
144   }
145 }
146
147 } // namespace
148
149 OpKernel *getDepthwiseConv2D()
150 {
151   static OpKernel kernel = {prepareDepthwiseConv, invokeDepthwiseConv};
152   return &kernel;
153 }
154
155 } // namespace interp
156 } // namespace onert