ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DepthwiseConv2D.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 "OMStatus.h"
18
19#include "core/OMUtils.h"
20#include "core/OMKernelData.h"
21
23#include "execute/OMUtils.h"
26
27#include "PALDepthwiseConv2D.h"
28
29using namespace onert_micro;
30using namespace onert_micro::core;
31using namespace onert_micro::execute;
32
33namespace
34{
35
36constexpr uint32_t inputTensorIdx = 0;
37constexpr uint32_t weightTensorIdx = 1;
38constexpr uint32_t biasTensorIdx = 2;
39
40constexpr uint32_t outputTensorIdx = 0;
41
42} // namespace
43
44namespace onert_micro
45{
46namespace execute
47{
48
49// NOTE: doesn't currently support dynamic shapes
51{
52 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
53 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
54 uint16_t op_index = execute_args.kernel_index;
55
56 const circle::Tensor *input;
57 const circle::Tensor *weight;
58 const circle::Tensor *output;
59
60 uint8_t *input_data;
61 uint8_t *weight_data;
62 uint8_t *bias_data;
63 uint8_t *output_data;
64
65 const circle::DepthwiseConv2DOptions *options;
66 // Read kernel
67 {
68 execute::OMRuntimeKernel runtime_kernel;
69 OMStatus status = runtime_kernel.readKernel(op_index, runtime_context);
70 if (status != Ok)
71 return status;
72
73 input = runtime_kernel.inputs[inputTensorIdx];
74 weight = runtime_kernel.inputs[weightTensorIdx];
75 output = runtime_kernel.outputs[outputTensorIdx];
76 assert(input != nullptr);
77 assert(weight != nullptr);
78 // Bias can be nullptr
79 assert(output != nullptr);
80
81 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
82 if (status != Ok)
83 return status;
84
85 input_data = runtime_kernel.inputs_data[inputTensorIdx];
86 weight_data = runtime_kernel.inputs_data[weightTensorIdx];
87 bias_data = runtime_kernel.inputs_data[biasTensorIdx];
88 output_data = runtime_kernel.outputs_data[outputTensorIdx];
89 assert(input_data != nullptr);
90 assert(weight_data != nullptr);
91 // Bias can be nullptr
92 assert(output_data != nullptr);
93
94 options = runtime_kernel.first_operator->builtin_options_as_DepthwiseConv2DOptions();
95 }
96
97 OMStatus status;
98
99 int32_t padding_h = 0;
100 int32_t padding_w = 0;
101
102 OMRuntimeShape weight_shape(weight);
103 OMRuntimeShape input_shape(input);
104
105 const int input_width = input_shape.dims(2);
106 const int input_height = input_shape.dims(1);
107 const int weight_width = weight_shape.dims(2);
108 const int weight_height = weight_shape.dims(1);
109 execute::computePaddingHeightWidth(options->stride_h(), options->stride_w(),
110 options->dilation_h_factor(), options->dilation_w_factor(),
111 input_height, input_width, weight_height, weight_width,
112 options->padding(), &padding_h, &padding_w);
113
114 const auto output_shape = OMRuntimeShape(output);
115
116 switch (input->type())
117 {
118#ifndef DIS_FLOAT
119 case circle::TensorType_FLOAT32:
120 {
121
122 FloatConv2D params{};
123 status = calculateActivationRange(options->fused_activation_function(),
124 &params.activation_min, &params.activation_max);
125 params.stride_w = options->stride_w();
126 params.stride_h = options->stride_h();
127 params.dilation_width_factor = options->dilation_w_factor();
128 params.dilation_height_factor = options->dilation_h_factor();
129 params.depth_multiplier = options->depth_multiplier();
130 params.pad_h = padding_h;
131 params.pad_w = padding_w;
132
133 if (status != Ok)
134 return status;
135
137 &params, input_shape, core::utils::castInputData<float>(input_data), weight_shape,
138 core::utils::castInputData<float>(weight_data),
139 core::utils::castInputData<float>(bias_data), output_shape,
140 core::utils::castOutputData<float>(output_data));
141 assert(status == Ok);
142 }
143 break;
144#endif // DIS_FLOAT
145#ifndef DIS_QUANT
146 case circle::TensorType_INT8:
147 {
148 ConvQuant params{};
149 params.pad_h = padding_h;
150 params.pad_w = padding_w;
151 params.depth_multiplier = options->depth_multiplier();
152
153 const auto padding = options->padding();
154 const auto stride_height = options->stride_h();
155 const auto stride_width = options->stride_w();
156 const auto dilation_height_factor = options->dilation_h_factor();
157 const auto dilation_width_factor = options->dilation_h_factor();
158
159 params.stride_height = stride_height;
160 params.stride_width = stride_width;
161 params.dilation_height_factor = dilation_height_factor;
162 params.dilation_width_factor = dilation_width_factor;
163
164 status =
165 createConvParams(params, input, weight, output, options->fused_activation_function());
166 assert(status == Ok);
167 if (status != Ok)
168 return status;
169
171 params, input_shape, core::utils::castInputData<int8_t>(input_data), weight_shape,
172 core::utils::castInputData<int8_t>(weight_data),
173 core::utils::castInputData<int32_t>(bias_data), output_shape,
174 core::utils::castOutputData<int8_t>(output_data));
175 }
176 break;
177#endif // DIS_QUANT
178 default:
179 {
180 status = UnsupportedActivation;
181 assert(false && "Unsupported type.");
182 }
183 }
184
185 return status;
186}
187
188} // namespace execute
189} // namespace onert_micro
uint8_t * outputs_data[maxOutputSize]
const circle::Operator * first_operator
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t outputTensorIdx
OMStatus DepthwiseConv2D< float >(const core::FloatConv2D *params, const core::OMRuntimeShape &input_shape, const float *input_data, const core::OMRuntimeShape &filter_shape, const float *filter_data, const float *bias_data, const core::OMRuntimeShape &output_shape, float *output_data)
OMStatus DepthwiseConvPerChannel(const core::ConvQuant &params, const core::OMRuntimeShape &input_shape, const int8_t *input_data, const core::OMRuntimeShape &filter_shape, const int8_t *filter_data, const int32_t *bias_data, const core::OMRuntimeShape &output_shape, int8_t *output_data)
OMStatus execute_kernel_CircleDepthwiseConv2D(const OMExecuteArgs &execute_args)
OMStatus calculateActivationRange(circle::ActivationFunctionType activation, T *activation_min, T *activation_max)
Definition OMUtils.h:36
OMStatus createConvParams(core::ConvQuant &params, const circle::Tensor *input, const circle::Tensor *filter, const circle::Tensor *output, circle::ActivationFunctionType act_type)
void computePaddingHeightWidth(int32_t stride_height, int32_t stride_width, int32_t dilation_rate_height, int32_t dilation_rate_width, int32_t in_height, int32_t in_width, int32_t filter_height, int32_t filter_width, circle::Padding padding, int32_t *padding_h, int32_t *padding_w)
Definition OMUtils.h:141
@ UnsupportedActivation
Definition OMStatus.h:28
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage