143{
144 if (!fused_bias)
145 {
146 throw std::runtime_error{"Quantized Conv2D cannot be executed without fused bias"};
147 }
148
149 const auto &input_type =
input.getType();
150 const auto &kernel_type = kernel.getType();
151 const auto &bias_type = fused_bias->getType();
153 (void)bias_type;
154
155 assert(input_type.isQuantized());
156 assert(kernel_type.isQuantized());
157 assert(bias_type.isQuantized());
159 assert(input_type.getElementType() == DataType::UINT8);
160 assert(kernel_type.getElementType() == DataType::UINT8);
161 assert(bias_type.getElementType() == DataType::INT32);
162 assert(
output_type.getElementType() == DataType::UINT8);
163
164 int32_t input_offset = -input_type.getQuantization().getZeroPoint();
165 int32_t kernel_offset = -kernel_type.getQuantization().getZeroPoint();
166 int32_t output_offset =
output_type.getQuantization().getZeroPoint();
167
168 double input_scale = input_type.getQuantization().getScale();
169 double kernel_scale = kernel_type.getQuantization().getScale();
170 double output_scale =
output_type.getQuantization().getScale();
171
172 double real_multiplier = input_scale * kernel_scale / output_scale;
173 int32_t output_multiplier = 0;
174 int output_shift = 0;
176
178 const Shape &kernel_shape = kernel.getShape();
180 const auto &strides = attributes.strides;
181 const std::vector<int32_t> &pads = attributes.padding_before;
182 assert(attributes.num_groups == 1);
183 assert(attributes.data_format == DataFormat::NHWC);
184
185 assert(in_shape.rank() == 4);
186 assert(kernel_shape.rank() == 4);
187 assert(kernel_shape.dim(3) == in_shape.dim(3));
188 assert(kernel_shape.dim(0) == out_shape.dim(3));
189 assert(strides.size() == 2);
190 assert(pads.size() == 2);
191
192 int32_t stride_height = strides[0];
193 int32_t stride_width = strides[1];
194
195 int32_t pad_height = pads[0];
196 int32_t pad_width = pads[1];
197
198 int32_t input_height = in_shape.dim(1);
199 int32_t input_width = in_shape.dim(2);
200
205
206 int32_t output_min = std::numeric_limits<uint8_t>::min();
207 int32_t output_max = std::numeric_limits<uint8_t>::max();
208
209 for (int batch = 0; batch < out_shape.dim(0); ++batch)
210 {
211 for (int out_y = 0; out_y < out_shape.dim(1); ++out_y)
212 {
213 for (int out_x = 0; out_x < out_shape.dim(2); ++out_x)
214 {
215 for (int out_channel = 0; out_channel < out_shape.dim(3); ++out_channel)
216 {
217 const int in_x_origin = (out_x * stride_width) - pad_width;
218 const int in_y_origin = (out_y * stride_height) - pad_height;
219 int32_t acc = 0;
220 for (int filter_y = 0; filter_y < kernel_shape.dim(1); ++filter_y)
221 {
222 for (int filter_x = 0; filter_x < kernel_shape.dim(2); ++filter_x)
223 {
224 for (int in_channel = 0; in_channel < kernel_shape.dim(3); ++in_channel)
225 {
226 const int in_x = in_x_origin + filter_x;
227 const int in_y = in_y_origin + filter_y;
228
229
230 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
231 {
232 Index in_index{batch, in_y, in_x, in_channel};
233 Index ker_index{out_channel, filter_y, filter_x, in_channel};
234 int32_t input_val = input_accessor.at(in_index);
235 int32_t kernel_val = kernel_accessor.at(ker_index);
236 acc += (kernel_val + kernel_offset) * (input_val + input_offset);
237 }
238 }
239 }
240 }
241 acc += bias_accessor.at(Index{out_channel});
243 acc += output_offset;
244 acc = std::max(acc, output_min);
245 acc = std::min(acc, output_max);
246 Index out_index{batch, out_y, out_x, out_channel};
247 res_accessor.at(out_index) = static_cast<uint8_t>(acc);
248 }
249 }
250 }
251 }
252}
int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)