Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / pal / common / PALConv2DCommon.h
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef LUCI_INTERPRETER_PAL_CONV2D_COMMON_H
19 #define LUCI_INTERPRETER_PAL_CONV2D_COMMON_H
20 #include "Params.h"
21 #include "PALUtils.h"
22
23 namespace luci_interpreter_pal
24 {
25 static inline void Conv(const ConvParams &params, const int32_t *input_shape,
26                         const float *input_data, const int32_t *filter_shape,
27                         const float *filter_data, const float *bias_data,
28                         const int32_t *output_shape, float *output_data)
29 {
30   const int stride_width = params.stride_width;
31   const int stride_height = params.stride_height;
32   const int dilation_width_factor = params.dilation_width_factor;
33   const int dilation_height_factor = params.dilation_height_factor;
34   const int pad_width = params.padding_values.width;
35   const int pad_height = params.padding_values.height;
36   const float output_activation_min = params.float_activation_min;
37   const float output_activation_max = params.float_activation_max;
38
39   const auto batches = input_shape[0];
40   const int input_height = input_shape[1];
41   const int input_width = input_shape[2];
42   const int input_depth = input_shape[3];
43   const int output_depth = filter_shape[0];
44   const int filter_height = filter_shape[1];
45   const int filter_width = filter_shape[2];
46   const int output_height = output_shape[1];
47   const int output_width = output_shape[2];
48   for (int batch = 0; batch < batches; ++batch)
49   {
50     for (int out_y = 0; out_y < output_height; ++out_y)
51     {
52       const int in_y_origin = (out_y * stride_height) - pad_height;
53       for (int out_x = 0; out_x < output_width; ++out_x)
54       {
55         const int in_x_origin = (out_x * stride_width) - pad_width;
56         for (int out_channel = 0; out_channel < output_depth; ++out_channel)
57         {
58           float total = 0.f;
59           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
60           {
61             const int in_y = in_y_origin + dilation_height_factor * filter_y;
62             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
63             {
64               const int in_x = in_x_origin + dilation_width_factor * filter_x;
65
66               // Zero padding by omitting the areas outside the image.
67               const bool is_point_inside_image =
68                 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
69
70               if (!is_point_inside_image)
71               {
72                 continue;
73               }
74
75               for (int in_channel = 0; in_channel < input_depth; ++in_channel)
76               {
77                 const int input_data_offset =
78                   ((batch * input_height + in_y) * input_width + in_x) * input_depth + in_channel;
79
80                 const int filter_data_offset =
81                   ((out_channel * filter_height + filter_y) * filter_width + filter_x) *
82                     input_depth +
83                   in_channel;
84
85                 const float input_value = input_data[input_data_offset];
86                 const float filter_value = filter_data[filter_data_offset];
87                 total += (input_value * filter_value);
88               }
89             }
90           }
91           // float bias_value = 0.0f;
92           if (bias_data)
93           {
94             total += bias_data[out_channel];
95           }
96
97           const int output_data_offset =
98             ((batch * output_height + out_y) * output_width + out_x) * output_depth + out_channel;
99
100           output_data[output_data_offset] =
101             std::min(std::max(total, output_activation_min), output_activation_max);
102         }
103       }
104     }
105   }
106 }
107
108 static inline void Conv(const ConvParams &params, const int32_t *input_shape,
109                         const uint8_t *input_data, const int32_t *filter_shape,
110                         const uint8_t *filter_data, const int32_t *bias_data,
111                         const int32_t *output_shape, uint8_t *output_data)
112 {
113   const int stride_width = params.stride_width;
114   const int stride_height = params.stride_height;
115   const int dilation_width_factor = params.dilation_width_factor;
116   const int dilation_height_factor = params.dilation_height_factor;
117   const int pad_width = params.padding_values.width;
118   const int pad_height = params.padding_values.height;
119   const int32_t input_offset = params.input_offset;
120   const int32_t filter_offset = params.weights_offset;
121   const int32_t output_offset = params.output_offset;
122   const int32_t output_multiplier = params.output_multiplier;
123   const int output_shift = params.output_shift;
124   const int32_t output_activation_min = params.quantized_activation_min;
125   const int32_t output_activation_max = params.quantized_activation_max;
126
127   const auto batches = input_shape[0];
128   const int input_height = input_shape[1];
129   const int input_width = input_shape[2];
130   const int input_depth = input_shape[3];
131   const int output_depth = filter_shape[0];
132   const int filter_height = filter_shape[1];
133   const int filter_width = filter_shape[2];
134   const int output_height = output_shape[1];
135   const int output_width = output_shape[2];
136
137   for (int batch = 0; batch < batches; ++batch)
138   {
139     for (int out_y = 0; out_y < output_height; ++out_y)
140     {
141       const int in_y_origin = (out_y * stride_height) - pad_height;
142       for (int out_x = 0; out_x < output_width; ++out_x)
143       {
144         const int in_x_origin = (out_x * stride_width) - pad_width;
145         for (int out_channel = 0; out_channel < output_depth; ++out_channel)
146         {
147           int32_t acc = 0;
148           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
149           {
150             const int in_y = in_y_origin + dilation_height_factor * filter_y;
151             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
152             {
153               const int in_x = in_x_origin + dilation_width_factor * filter_x;
154
155               // Zero padding by omitting the areas outside the image.
156               const bool is_point_inside_image =
157                 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
158
159               if (!is_point_inside_image)
160               {
161                 continue;
162               }
163
164               for (int in_channel = 0; in_channel < input_depth; ++in_channel)
165               {
166                 const int input_data_offset =
167                   ((batch * input_height + in_y) * input_width + in_x) * input_depth + in_channel;
168
169                 const int filter_data_offset =
170                   ((out_channel * filter_height + filter_y) * filter_width + filter_x) *
171                     input_depth +
172                   in_channel;
173
174                 const int32_t input_val = input_data[input_data_offset];
175                 const int32_t filter_val = filter_data[filter_data_offset];
176                 acc += (filter_val + filter_offset) * (input_val + input_offset);
177               }
178             }
179           }
180           if (bias_data)
181           {
182             acc += bias_data[out_channel];
183           }
184           acc = multiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
185           acc += output_offset;
186           acc = std::max(acc, output_activation_min);
187           acc = std::min(acc, output_activation_max);
188
189           const int output_data_offset =
190             ((batch * output_height + out_y) * output_width + out_x) * output_depth + out_channel;
191
192           output_data[output_data_offset] = static_cast<uint8_t>(acc);
193         }
194       }
195     }
196   }
197 }
198
199 } // namespace luci_interpreter_pal
200
201 #endif // LUCI_INTERPRETER_PAL_CONV2D_COMMON_H