ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Conv2D.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 "PALConv2D.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
44// NOTE: doesn't currently support dynamic shapes
45OMStatus onert_micro::execute::execute_kernel_CircleConv2D(const OMExecuteArgs &execute_args)
46{
47 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
48 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
49 uint16_t op_index = execute_args.kernel_index;
50
51 const circle::Tensor *input;
52 const circle::Tensor *weight;
53 const circle::Tensor *output;
54
55 uint8_t *input_data;
56 uint8_t *weight_data;
57 uint8_t *bias_data;
58 uint8_t *output_data;
59
60 const circle::Conv2DOptions *options;
61 // Read kernel
62 {
63 execute::OMRuntimeKernel runtime_kernel;
64 OMStatus status = runtime_kernel.readKernel(op_index, runtime_context);
65 if (status != Ok)
66 return status;
67
68 input = runtime_kernel.inputs[inputTensorIdx];
69 weight = runtime_kernel.inputs[weightTensorIdx];
70 output = runtime_kernel.outputs[outputTensorIdx];
71 assert(input != nullptr);
72 assert(weight != nullptr);
73 // Bias can be nullptr
74 assert(output != nullptr);
75
76 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
77 if (status != Ok)
78 return status;
79
80 input_data = runtime_kernel.inputs_data[inputTensorIdx];
81 weight_data = runtime_kernel.inputs_data[weightTensorIdx];
82 bias_data = runtime_kernel.inputs_data[biasTensorIdx];
84 assert(input_data != nullptr);
85 assert(weight_data != nullptr);
86 // Bias can be nullptr
87 assert(output_data != nullptr);
88
89 options = runtime_kernel.first_operator->builtin_options_as_Conv2DOptions();
90 }
91
92 OMStatus status;
93
94 int32_t padding_h = 0;
95 int32_t padding_w = 0;
96
97 OMRuntimeShape weight_shape(weight);
98 OMRuntimeShape input_shape(input);
100
101 const int input_width = input_shape.dims(2);
102 const int input_height = input_shape.dims(1);
103 const int weight_width = weight_shape.dims(2);
104 const int weight_height = weight_shape.dims(1);
105 execute::computePaddingHeightWidth(options->stride_h(), options->stride_w(),
106 options->dilation_h_factor(), options->dilation_w_factor(),
107 input_height, input_width, weight_height, weight_width,
108 options->padding(), &padding_h, &padding_w);
109
110 switch (input->type())
111 {
112#ifndef DIS_FLOAT
113 case circle::TensorType_FLOAT32:
114 {
115 FloatConv2D params{};
116 status = calculateActivationRange(options->fused_activation_function(),
117 &params.activation_min, &params.activation_max);
118 params.stride_w = options->stride_w();
119 params.stride_h = options->stride_h();
120 params.dilation_width_factor = options->dilation_w_factor();
121 params.dilation_height_factor = options->dilation_h_factor();
122 params.pad_h = padding_h;
123 params.pad_w = padding_w;
124
125 if (status != Ok)
126 return status;
127
128 status = pal::ConvFloat(&params, input_shape, core::utils::castInputData<float>(input_data),
129 weight_shape, core::utils::castInputData<float>(weight_data),
130 core::utils::castInputData<float>(bias_data), output_shape,
131 core::utils::castOutputData<float>(output_data));
132 assert(status == Ok);
133 }
134 break;
135#endif // DIS_FLOAT
136#ifndef DIS_QUANT
137 case circle::TensorType_INT8:
138 {
139 ConvQuant params{};
140 params.pad_h = padding_h;
141 params.pad_w = padding_w;
142
143 const auto padding = options->padding();
144 const auto stride_height = options->stride_h();
145 const auto stride_width = options->stride_w();
146 const auto dilation_height_factor = options->dilation_h_factor();
147 const auto dilation_width_factor = options->dilation_h_factor();
148
149 params.stride_height = stride_height;
150 params.stride_width = stride_width;
151 params.dilation_height_factor = dilation_height_factor;
152 params.dilation_width_factor = dilation_width_factor;
153
154 status =
155 createConvParams(params, input, weight, output, options->fused_activation_function());
156 assert(status == Ok);
157 if (status != Ok)
158 return status;
159
160 status =
161 pal::ConvPerChannel(params, input_shape, core::utils::castInputData<int8_t>(input_data),
162 weight_shape, core::utils::castInputData<int8_t>(weight_data),
163 core::utils::castInputData<int32_t>(bias_data), output_shape,
164 core::utils::castOutputData<int8_t>(output_data));
165 }
166 break;
167#endif // DIS_QUANT
168 default:
169 {
170 status = UnsupportedActivation;
171 assert(false && "Unsupported type.");
172 }
173 }
174
175 return status;
176}
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
list input_data
Definition infer.py:29
OMStatus ConvPerChannel(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)
Definition PALConv2D.h:36
OMStatus ConvFloat(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 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