Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / reference / Conv.h
1 /*
2  * Copyright (c) 2020 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 __NNFW_CKER_REFERENCE_CONV_H__
19 #define __NNFW_CKER_REFERENCE_CONV_H__
20
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
23
24 #include <cmath>
25
26 namespace nnfw
27 {
28 namespace cker
29 {
30 namespace reference
31 {
32
33 inline void Conv(const ConvParams &params, const Shape &input_shape, const float *input_data,
34                  const Shape &filter_shape, const float *filter_data, const Shape &bias_shape,
35                  const float *bias_data, const Shape &output_shape, float *output_data)
36 {
37   const int stride_width = params.stride_width;
38   const int stride_height = params.stride_height;
39   const int dilation_width_factor = params.dilation_width_factor;
40   const int dilation_height_factor = params.dilation_height_factor;
41   const int pad_width = params.padding_values.width;
42   const int pad_height = params.padding_values.height;
43   const float output_activation_min = params.float_activation_min;
44   const float output_activation_max = params.float_activation_max;
45   assert(input_shape.DimensionsCount() == 4);
46   assert(filter_shape.DimensionsCount() == 4);
47   assert(output_shape.DimensionsCount() == 4);
48   UNUSED_RELEASE(bias_shape);
49
50   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
51   const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
52   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
53   if (bias_data)
54   {
55     assert(bias_shape.FlatSize() == output_depth);
56   }
57   const int input_height = input_shape.Dims(1);
58   const int input_width = input_shape.Dims(2);
59   const int filter_height = filter_shape.Dims(1);
60   const int filter_width = filter_shape.Dims(2);
61   const int output_height = output_shape.Dims(1);
62   const int output_width = output_shape.Dims(2);
63   for (int batch = 0; batch < batches; ++batch)
64   {
65     for (int out_y = 0; out_y < output_height; ++out_y)
66     {
67       for (int out_x = 0; out_x < output_width; ++out_x)
68       {
69         for (int out_channel = 0; out_channel < output_depth; ++out_channel)
70         {
71           const int in_x_origin = (out_x * stride_width) - pad_width;
72           const int in_y_origin = (out_y * stride_height) - pad_height;
73           float total = 0.f;
74           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
75           {
76             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
77             {
78               const int in_x = in_x_origin + dilation_width_factor * filter_x;
79               const int in_y = in_y_origin + dilation_height_factor * filter_y;
80               // If the location is outside the bounds of the input image,
81               // use zero as a default value.
82               if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
83               {
84                 const int in_offset = Offset(input_shape, batch, in_y, in_x, 0);
85                 const int filter_offset = Offset(filter_shape, out_channel, filter_y, filter_x, 0);
86                 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
87                 {
88                   float input_value = input_data[in_offset + in_channel];
89                   float filter_value = filter_data[filter_offset + in_channel];
90                   total += (input_value * filter_value);
91                 }
92               }
93             }
94           }
95           float bias_value = 0.0f;
96           if (bias_data)
97           {
98             bias_value = bias_data[out_channel];
99           }
100           output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
101             ActivationFunctionWithMinMax(total + bias_value, output_activation_min,
102                                          output_activation_max);
103         }
104       }
105     }
106   }
107 }
108
109 inline void Conv(const ConvParams &params, const Shape &input_shape, const uint8_t *input_data,
110                  const Shape &filter_shape, const uint8_t *filter_data, const Shape &bias_shape,
111                  const int32_t *bias_data, const Shape &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   assert(output_activation_min <= output_activation_max);
127
128   assert(input_shape.DimensionsCount() == 4);
129   assert(filter_shape.DimensionsCount() == 4);
130   assert(output_shape.DimensionsCount() == 4);
131   UNUSED_RELEASE(bias_shape);
132   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
133   const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
134   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
135   if (bias_data)
136   {
137     assert(bias_shape.FlatSize() == output_depth);
138   }
139   const int input_height = input_shape.Dims(1);
140   const int input_width = input_shape.Dims(2);
141   const int filter_height = filter_shape.Dims(1);
142   const int filter_width = filter_shape.Dims(2);
143   const int output_height = output_shape.Dims(1);
144   const int output_width = output_shape.Dims(2);
145   for (int batch = 0; batch < batches; ++batch)
146   {
147     for (int out_y = 0; out_y < output_height; ++out_y)
148     {
149       for (int out_x = 0; out_x < output_width; ++out_x)
150       {
151         for (int out_channel = 0; out_channel < output_depth; ++out_channel)
152         {
153           const int in_x_origin = (out_x * stride_width) - pad_width;
154           const int in_y_origin = (out_y * stride_height) - pad_height;
155           int32_t acc = 0;
156           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
157           {
158             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
159             {
160               const int in_x = in_x_origin + dilation_width_factor * filter_x;
161               const int in_y = in_y_origin + dilation_height_factor * filter_y;
162               // If the location is outside the bounds of the input image,
163               // use zero as a default value.
164               if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
165               {
166                 const int in_base = Offset(input_shape, batch, in_y, in_x, 0);
167                 const int filter_base = Offset(filter_shape, out_channel, filter_y, filter_x, 0);
168                 for (int in_channel = 0; in_channel < input_depth; in_channel++)
169                 {
170                   int32_t input_val = input_data[in_channel + in_base];
171                   int32_t filter_val = filter_data[in_channel + filter_base];
172                   acc += (filter_val + filter_offset) * (input_val + input_offset);
173                 }
174               }
175             }
176           }
177           if (bias_data)
178           {
179             acc += bias_data[out_channel];
180           }
181           acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
182           acc += output_offset;
183           acc = std::max(acc, output_activation_min);
184           acc = std::min(acc, output_activation_max);
185           output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
186             static_cast<uint8_t>(acc);
187         }
188       }
189     }
190   }
191 }
192
193 template <typename T, bool is_asymmetric>
194 inline void Conv(const ConvParams &params, const int32_t *output_multiplier,
195                  const int32_t *output_shift, const Shape &input_shape, const T *input_data,
196                  const Shape &filter_shape, const T *filter_data, const int32_t *filter_zeropoint,
197                  const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape,
198                  T *output_data)
199
200 {
201   UNUSED_RELEASE(bias_shape);
202   // Get parameters.
203   const int32_t input_offset = params.input_offset; // r = s(q - Z)
204   const int stride_width = params.stride_width;
205   const int stride_height = params.stride_height;
206   const int dilation_width_factor = params.dilation_width_factor;
207   const int dilation_height_factor = params.dilation_height_factor;
208   const int pad_width = params.padding_values.width;
209   const int pad_height = params.padding_values.height;
210   const int32_t output_offset = params.output_offset;
211
212   // Set min and max value of the output.
213   const int32_t output_activation_min = params.quantized_activation_min;
214   const int32_t output_activation_max = params.quantized_activation_max;
215
216   // Consistency check.
217   assert(output_activation_min < output_activation_max);
218   assert(input_shape.DimensionsCount() == 4);
219   assert(filter_shape.DimensionsCount() == 4);
220   assert(output_shape.DimensionsCount() == 4);
221   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
222   const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
223   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
224   if (bias_data)
225   {
226     assert(bias_shape.FlatSize() == output_depth);
227   }
228
229   // Check dimensions of the tensors.
230   const int input_height = input_shape.Dims(1);
231   const int input_width = input_shape.Dims(2);
232   const int filter_height = filter_shape.Dims(1);
233   const int filter_width = filter_shape.Dims(2);
234   const int output_height = output_shape.Dims(1);
235   const int output_width = output_shape.Dims(2);
236   for (int batch = 0; batch < batches; ++batch)
237   {
238     for (int out_y = 0; out_y < output_height; ++out_y)
239     {
240       const int in_y_origin = (out_y * stride_height) - pad_height;
241       for (int out_x = 0; out_x < output_width; ++out_x)
242       {
243         const int in_x_origin = (out_x * stride_width) - pad_width;
244         for (int out_channel = 0; out_channel < output_depth; ++out_channel)
245         {
246           int32_t acc = 0;
247           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
248           {
249             const int in_y = in_y_origin + dilation_height_factor * filter_y;
250             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
251             {
252               const int in_x = in_x_origin + dilation_width_factor * filter_x;
253
254               // Zero padding by omitting the areas outside the image.
255               const bool is_point_inside_image =
256                 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
257
258               if (!is_point_inside_image)
259               {
260                 continue;
261               }
262
263               for (int in_channel = 0; in_channel < input_depth; ++in_channel)
264               {
265                 const T input_val = input_data[Offset(input_shape, batch, in_y, in_x, in_channel)];
266                 const T filter_val =
267                   filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
268                 if (is_asymmetric)
269                 {
270                   const int32_t filter_offset = -filter_zeropoint[out_channel];
271                   acc += (filter_val + filter_offset) * (input_val + input_offset);
272                 }
273                 else
274                 {
275                   // Accumulate with 32 bits accumulator.
276                   // In the nudging process during model quantization, we force
277                   // real value of 0.0 be represented by a quantized value. This
278                   // guarantees that the input_offset is a int8_t, even though
279                   // it is represented using int32_t. int32_t += int8_t *
280                   // (int8_t - int8_t) so the highest value we can get from each
281                   // accumulation is [-127, 127] * ([-128, 127] -
282                   // [-128, 127]), which is [-32512, 32512]. log2(32512)
283                   // = 14.98, which means we can accumulate at least 2^16
284                   // multiplications without overflow. The accumulator is
285                   // applied to a filter so the accumulation logic will hold as
286                   // long as the filter size (filter_y * filter_x * in_channel)
287                   // does not exceed 2^16, which is the case in all the models
288                   // we have seen so far.
289                   // TODO(jianlijianli): Add a check to make sure the
290                   // accumulator depth is smaller than 2^16.
291                   acc += filter_val * (input_val + input_offset);
292                   UNUSED_RELEASE(filter_zeropoint);
293                 }
294               }
295             }
296           }
297
298           if (bias_data)
299           {
300             acc += bias_data[out_channel];
301           }
302           acc = MultiplyByQuantizedMultiplier(acc, output_multiplier[out_channel],
303                                               output_shift[out_channel]);
304           acc += output_offset;
305           acc = std::max(acc, output_activation_min);
306           acc = std::min(acc, output_activation_max);
307           output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = static_cast<T>(acc);
308         }
309       }
310     }
311   }
312 }
313
314 // Slightly modified from tflite 2.13.0 HybridConvPerChannel
315 // im2col and im2col_shape are removed since it is not used in reference kernel.
316 inline void HybridConvPerChannel(const ConvParams &params, float *scaling_factors_ptr,
317                                  const Shape &input_shape, const int8_t *input_data,
318                                  const Shape &filter_shape, const int8_t *filter_data,
319                                  const Shape &bias_shape, const float *bias_data,
320                                  const Shape &output_shape, float *output_data,
321                                  const float *per_channel_scale, const int32_t *input_offset)
322
323 {
324   const int stride_width = params.stride_width;
325   const int stride_height = params.stride_height;
326   const int dilation_width_factor = params.dilation_width_factor;
327   const int dilation_height_factor = params.dilation_height_factor;
328   const int pad_width = params.padding_values.width;
329   const int pad_height = params.padding_values.height;
330   const float output_activation_min = params.float_activation_min;
331   const float output_activation_max = params.float_activation_max;
332   assert(input_shape.DimensionsCount() == 4);
333   assert(filter_shape.DimensionsCount() == 4);
334   assert(output_shape.DimensionsCount() == 4);
335   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
336   const int input_depth = input_shape.Dims(3);
337   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
338   if (bias_data)
339   {
340     assert(bias_shape.FlatSize() == output_depth);
341     UNUSED_RELEASE(bias_shape);
342   }
343   const int input_height = input_shape.Dims(1);
344   const int input_width = input_shape.Dims(2);
345   const int filter_height = filter_shape.Dims(1);
346   const int filter_width = filter_shape.Dims(2);
347   const int filter_input_depth = filter_shape.Dims(3);
348   const int groups = input_depth / filter_input_depth;
349   assert(input_depth % filter_input_depth == 0);
350   const int filters_per_group = output_depth / groups;
351   const int output_height = output_shape.Dims(1);
352   const int output_width = output_shape.Dims(2);
353   for (int batch = 0; batch < batches; ++batch)
354   {
355     for (int out_y = 0; out_y < output_height; ++out_y)
356     {
357       for (int out_x = 0; out_x < output_width; ++out_x)
358       {
359         for (int out_channel = 0; out_channel < output_depth; ++out_channel)
360         {
361           auto group = out_channel / filters_per_group;
362           const int in_x_origin = (out_x * stride_width) - pad_width;
363           const int in_y_origin = (out_y * stride_height) - pad_height;
364           int32_t acc = 0;
365           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
366           {
367             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
368             {
369               for (int in_channel = 0; in_channel < filter_input_depth; ++in_channel)
370               {
371                 const int in_x = in_x_origin + dilation_width_factor * filter_x;
372                 const int in_y = in_y_origin + dilation_height_factor * filter_y;
373                 // If the location is outside the bounds of the input image,
374                 // use zero as a default value.
375                 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
376                 {
377                   int32_t input_val = input_data[Offset(input_shape, batch, in_y, in_x,
378                                                         in_channel + group * filter_input_depth)];
379                   int32_t filter_val =
380                     filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
381                   acc += filter_val * (input_val - input_offset[batch]);
382                 }
383               }
384             }
385           }
386           float acc_float = acc * per_channel_scale[out_channel] * scaling_factors_ptr[batch];
387           if (bias_data)
388           {
389             acc_float += bias_data[out_channel];
390           }
391           output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
392             ActivationFunctionWithMinMax(acc_float, output_activation_min, output_activation_max);
393         }
394       }
395     }
396   }
397 }
398
399 } // namespace reference
400 } // namespace cker
401 } // namespace nnfw
402
403 #endif // __NNFW_CKER_REFERENCE_CONV_H__