18#ifndef LUCI_INTERPRETER_PAL_CONV2D_COMMON_H
19#define LUCI_INTERPRETER_PAL_CONV2D_COMMON_H
25static inline void Conv(
const ConvParams ¶ms,
const int32_t *input_shape,
26 const float *input_data,
const int32_t *filter_shape,
27 const float *filter_data,
const float *bias_data,
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;
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];
48 for (
int batch = 0; batch < batches; ++batch)
50 for (
int out_y = 0; out_y < output_height; ++out_y)
52 const int in_y_origin = (out_y * stride_height) - pad_height;
53 for (
int out_x = 0; out_x < output_width; ++out_x)
55 const int in_x_origin = (out_x * stride_width) - pad_width;
56 for (
int out_channel = 0; out_channel < output_depth; ++out_channel)
59 for (
int filter_y = 0; filter_y < filter_height; ++filter_y)
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)
64 const int in_x = in_x_origin + dilation_width_factor * filter_x;
67 const bool is_point_inside_image =
68 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
70 if (!is_point_inside_image)
75 for (
int in_channel = 0; in_channel < input_depth; ++in_channel)
77 const int input_data_offset =
78 ((batch * input_height + in_y) * input_width + in_x) * input_depth + in_channel;
80 const int filter_data_offset =
81 ((out_channel * filter_height + filter_y) * filter_width + filter_x) *
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);
94 total += bias_data[out_channel];
97 const int output_data_offset =
98 ((batch * output_height + out_y) * output_width + out_x) * output_depth + out_channel;
101 std::min(std::max(total, output_activation_min), output_activation_max);
108static inline void Conv(
const ConvParams ¶ms,
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,
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;
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];
137 for (
int batch = 0; batch < batches; ++batch)
139 for (
int out_y = 0; out_y < output_height; ++out_y)
141 const int in_y_origin = (out_y * stride_height) - pad_height;
142 for (
int out_x = 0; out_x < output_width; ++out_x)
144 const int in_x_origin = (out_x * stride_width) - pad_width;
145 for (
int out_channel = 0; out_channel < output_depth; ++out_channel)
148 for (
int filter_y = 0; filter_y < filter_height; ++filter_y)
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)
153 const int in_x = in_x_origin + dilation_width_factor * filter_x;
156 const bool is_point_inside_image =
157 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
159 if (!is_point_inside_image)
164 for (
int in_channel = 0; in_channel < input_depth; ++in_channel)
166 const int input_data_offset =
167 ((batch * input_height + in_y) * input_width + in_x) * input_depth + in_channel;
169 const int filter_data_offset =
170 ((out_channel * filter_height + filter_y) * filter_width + filter_x) *
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);
182 acc += bias_data[out_channel];
185 acc += output_offset;
186 acc = std::max(acc, output_activation_min);
187 acc = std::min(acc, output_activation_max);
189 const int output_data_offset =
190 ((batch * output_height + out_y) * output_width + out_x) * output_depth + out_channel;
192 output_data[output_data_offset] =
static_cast<uint8_t
>(acc);
const luci_interpreter::RuntimeShape output_shape
int32_t multiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
void Conv(const ConvParams ¶ms, const Shape &input_shape, const uint8_t *input_data, const Shape &filter_shape, const uint8_t *filter_data, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, uint8_t *output_data, const Shape &im2col_shape, uint8_t *im2col_data)