ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Conv.h
Go to the documentation of this file.
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
26namespace nnfw
27{
28namespace cker
29{
30namespace reference
31{
32
33inline void Conv(const ConvParams &params, const Shape &input_shape, const float *input_data,
34 const Shape &filter_shape, const float *filter_data,
35 [[maybe_unused]] const Shape &bias_shape, const float *bias_data,
36 const Shape &output_shape, float *output_data)
37{
38 const int stride_width = params.stride_width;
39 const int stride_height = params.stride_height;
40 const int dilation_width_factor = params.dilation_width_factor;
41 const int dilation_height_factor = params.dilation_height_factor;
42 const int pad_width = params.padding_values.width;
43 const int pad_height = params.padding_values.height;
44 const float output_activation_min = params.float_activation_min;
45 const float output_activation_max = params.float_activation_max;
46 assert(input_shape.DimensionsCount() == 4);
47 assert(filter_shape.DimensionsCount() == 4);
48 assert(output_shape.DimensionsCount() == 4);
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
109inline void Conv(const ConvParams &params, const Shape &input_shape, const uint8_t *input_data,
110 const Shape &filter_shape, const uint8_t *filter_data,
111 [[maybe_unused]] const Shape &bias_shape, const int32_t *bias_data,
112 const Shape &output_shape, uint8_t *output_data)
113{
114 const int stride_width = params.stride_width;
115 const int stride_height = params.stride_height;
116 const int dilation_width_factor = params.dilation_width_factor;
117 const int dilation_height_factor = params.dilation_height_factor;
118 const int pad_width = params.padding_values.width;
119 const int pad_height = params.padding_values.height;
120 const int32_t input_offset = params.input_offset;
121 const int32_t filter_offset = params.weights_offset;
122 const int32_t output_offset = params.output_offset;
123 const int32_t output_multiplier = params.output_multiplier;
124 const int output_shift = params.output_shift;
125 const int32_t output_activation_min = params.quantized_activation_min;
126 const int32_t output_activation_max = params.quantized_activation_max;
127 assert(output_activation_min <= output_activation_max);
128
129 assert(input_shape.DimensionsCount() == 4);
130 assert(filter_shape.DimensionsCount() == 4);
131 assert(output_shape.DimensionsCount() == 4);
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
193template <typename T, bool is_asymmetric>
194inline void
195Conv(const ConvParams &params, const int32_t *output_multiplier, const int32_t *output_shift,
196 const Shape &input_shape, const T *input_data, const Shape &filter_shape, const T *filter_data,
197 [[maybe_unused]] const int32_t *filter_zeropoint, [[maybe_unused]] const Shape &bias_shape,
198 const int32_t *bias_data, const Shape &output_shape, T *output_data)
199
200{
201 // Get parameters.
202 const int32_t input_offset = params.input_offset; // r = s(q - Z)
203 const int stride_width = params.stride_width;
204 const int stride_height = params.stride_height;
205 const int dilation_width_factor = params.dilation_width_factor;
206 const int dilation_height_factor = params.dilation_height_factor;
207 const int pad_width = params.padding_values.width;
208 const int pad_height = params.padding_values.height;
209 const int32_t output_offset = params.output_offset;
210
211 // Set min and max value of the output.
212 const int32_t output_activation_min = params.quantized_activation_min;
213 const int32_t output_activation_max = params.quantized_activation_max;
214
215 // Consistency check.
216 assert(output_activation_min < output_activation_max);
217 assert(input_shape.DimensionsCount() == 4);
218 assert(filter_shape.DimensionsCount() == 4);
219 assert(output_shape.DimensionsCount() == 4);
220 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
221 const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
222 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
223 if (bias_data)
224 {
225 assert(bias_shape.FlatSize() == output_depth);
226 }
227
228 // Check dimensions of the tensors.
229 const int input_height = input_shape.Dims(1);
230 const int input_width = input_shape.Dims(2);
231 const int filter_height = filter_shape.Dims(1);
232 const int filter_width = filter_shape.Dims(2);
233 const int output_height = output_shape.Dims(1);
234 const int output_width = output_shape.Dims(2);
235 for (int batch = 0; batch < batches; ++batch)
236 {
237 for (int out_y = 0; out_y < output_height; ++out_y)
238 {
239 const int in_y_origin = (out_y * stride_height) - pad_height;
240 for (int out_x = 0; out_x < output_width; ++out_x)
241 {
242 const int in_x_origin = (out_x * stride_width) - pad_width;
243 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
244 {
245 int32_t acc = 0;
246 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
247 {
248 const int in_y = in_y_origin + dilation_height_factor * filter_y;
249 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
250 {
251 const int in_x = in_x_origin + dilation_width_factor * filter_x;
252
253 // Zero padding by omitting the areas outside the image.
254 const bool is_point_inside_image =
255 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
256
257 if (!is_point_inside_image)
258 {
259 continue;
260 }
261
262 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
263 {
264 const T input_val = input_data[Offset(input_shape, batch, in_y, in_x, in_channel)];
265 const T filter_val =
266 filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
267 if (is_asymmetric)
268 {
269 const int32_t filter_offset = -filter_zeropoint[out_channel];
270 acc += (filter_val + filter_offset) * (input_val + input_offset);
271 }
272 else
273 {
274 // Accumulate with 32 bits accumulator.
275 // In the nudging process during model quantization, we force
276 // real value of 0.0 be represented by a quantized value. This
277 // guarantees that the input_offset is a int8_t, even though
278 // it is represented using int32_t. int32_t += int8_t *
279 // (int8_t - int8_t) so the highest value we can get from each
280 // accumulation is [-127, 127] * ([-128, 127] -
281 // [-128, 127]), which is [-32512, 32512]. log2(32512)
282 // = 14.98, which means we can accumulate at least 2^16
283 // multiplications without overflow. The accumulator is
284 // applied to a filter so the accumulation logic will hold as
285 // long as the filter size (filter_y * filter_x * in_channel)
286 // does not exceed 2^16, which is the case in all the models
287 // we have seen so far.
288 // TODO(jianlijianli): Add a check to make sure the
289 // accumulator depth is smaller than 2^16.
290 acc += filter_val * (input_val + input_offset);
291 }
292 }
293 }
294 }
295
296 if (bias_data)
297 {
298 acc += bias_data[out_channel];
299 }
300 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier[out_channel],
301 output_shift[out_channel]);
302 acc += output_offset;
303 acc = std::max(acc, output_activation_min);
304 acc = std::min(acc, output_activation_max);
305 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = static_cast<T>(acc);
306 }
307 }
308 }
309 }
310}
311
312// Slightly modified from tflite 2.13.0 HybridConvPerChannel
313// im2col and im2col_shape are removed since it is not used in reference kernel.
314inline void HybridConvPerChannel(const ConvParams &params, float *scaling_factors_ptr,
315 const Shape &input_shape, const int8_t *input_data,
316 const Shape &filter_shape, const int8_t *filter_data,
317 [[maybe_unused]] const Shape &bias_shape, const float *bias_data,
318 const Shape &output_shape, float *output_data,
319 const float *per_channel_scale, const int32_t *input_offset)
320
321{
322 const int stride_width = params.stride_width;
323 const int stride_height = params.stride_height;
324 const int dilation_width_factor = params.dilation_width_factor;
325 const int dilation_height_factor = params.dilation_height_factor;
326 const int pad_width = params.padding_values.width;
327 const int pad_height = params.padding_values.height;
328 const float output_activation_min = params.float_activation_min;
329 const float output_activation_max = params.float_activation_max;
330 assert(input_shape.DimensionsCount() == 4);
331 assert(filter_shape.DimensionsCount() == 4);
332 assert(output_shape.DimensionsCount() == 4);
333 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
334 const int input_depth = input_shape.Dims(3);
335 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
336 if (bias_data)
337 {
338 assert(bias_shape.FlatSize() == output_depth);
339 }
340 const int input_height = input_shape.Dims(1);
341 const int input_width = input_shape.Dims(2);
342 const int filter_height = filter_shape.Dims(1);
343 const int filter_width = filter_shape.Dims(2);
344 const int filter_input_depth = filter_shape.Dims(3);
345 const int groups = input_depth / filter_input_depth;
346 assert(input_depth % filter_input_depth == 0);
347 const int filters_per_group = output_depth / groups;
348 const int output_height = output_shape.Dims(1);
349 const int output_width = output_shape.Dims(2);
350 for (int batch = 0; batch < batches; ++batch)
351 {
352 for (int out_y = 0; out_y < output_height; ++out_y)
353 {
354 for (int out_x = 0; out_x < output_width; ++out_x)
355 {
356 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
357 {
358 auto group = out_channel / filters_per_group;
359 const int in_x_origin = (out_x * stride_width) - pad_width;
360 const int in_y_origin = (out_y * stride_height) - pad_height;
361 int32_t acc = 0;
362 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
363 {
364 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
365 {
366 for (int in_channel = 0; in_channel < filter_input_depth; ++in_channel)
367 {
368 const int in_x = in_x_origin + dilation_width_factor * filter_x;
369 const int in_y = in_y_origin + dilation_height_factor * filter_y;
370 // If the location is outside the bounds of the input image,
371 // use zero as a default value.
372 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
373 {
374 int32_t input_val = input_data[Offset(input_shape, batch, in_y, in_x,
375 in_channel + group * filter_input_depth)];
376 int32_t filter_val =
377 filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
378 acc += filter_val * (input_val - input_offset[batch]);
379 }
380 }
381 }
382 }
383 float acc_float = acc * per_channel_scale[out_channel] * scaling_factors_ptr[batch];
384 if (bias_data)
385 {
386 acc_float += bias_data[out_channel];
387 }
388 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
389 ActivationFunctionWithMinMax(acc_float, output_activation_min, output_activation_max);
390 }
391 }
392 }
393 }
394}
395
396} // namespace reference
397} // namespace cker
398} // namespace nnfw
399
400#endif // __NNFW_CKER_REFERENCE_CONV_H__
int32_t DimensionsCount() const
Definition Shape.h:91
int32_t Dims(int i) const
Definition Shape.h:92
const luci_interpreter::RuntimeShape output_shape
void HybridConvPerChannel(const ConvParams &params, float *scaling_factors_ptr, const Shape &input_shape, const int8_t *input_data, const Shape &filter_shape, const int8_t *filter_data, const Shape &bias_shape, const float *bias_data, const Shape &output_shape, float *output_data, const float *per_channel_scale, const int32_t *input_offset)
Definition Conv.h:314
int MatchingDim(const Shape &shape1, int index1, const Shape &shape2, int index2)
Definition Shape.h:220
int Offset(const Shape &shape, int i0, int i1, int i2, int i3)
Definition Shape.h:237
T ActivationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
Definition Utils.h:43
int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
Definition Utils.h:96
Definition topk_v2.h:30
int16_t stride_height
Definition Types.h:146
PaddingValues padding_values
Definition Types.h:143
float float_activation_max
Definition Types.h:161
int32_t output_multiplier
Definition Types.h:154
int32_t weights_offset
Definition Types.h:152
int32_t output_offset
Definition Types.h:153
int16_t dilation_width_factor
Definition Types.h:147
float float_activation_min
Definition Types.h:160
int32_t quantized_activation_max
Definition Types.h:158
int16_t dilation_height_factor
Definition Types.h:148
int32_t quantized_activation_min
Definition Types.h:157