ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mir_interpreter::Conv2DImpl< uint8_t > Struct Reference

Static Public Member Functions

static void run (const TensorVariant &input, const TensorVariant &kernel, const Conv2DOpAttributes &attributes, TensorVariant &result, const TensorVariant *fused_bias)
 

Detailed Description

Definition at line 133 of file Conv2D.cpp.

Member Function Documentation

◆ run()

void mir_interpreter::Conv2DImpl< uint8_t >::run ( const TensorVariant input,
const TensorVariant kernel,
const Conv2DOpAttributes attributes,
TensorVariant result,
const TensorVariant fused_bias 
)
static

Definition at line 140 of file Conv2D.cpp.

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();
152 const auto &output_type = result.getType();
153 (void)bias_type;
154
155 assert(input_type.isQuantized());
156 assert(kernel_type.isQuantized());
157 assert(bias_type.isQuantized());
158 assert(output_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;
175 QuantizeMultiplier(real_multiplier, &output_multiplier, &output_shift);
176
177 const Shape &in_shape = input.getShape();
178 const Shape &kernel_shape = kernel.getShape();
179 const Shape &out_shape = result.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
201 Tensor<uint8_t> input_accessor(input);
202 Tensor<uint8_t> kernel_accessor(kernel);
203 Tensor<int32_t> bias_accessor(*fused_bias);
204 Tensor<uint8_t> res_accessor(result);
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 // If the location is outside the bounds of the input image,
229 // use zero as a default value.
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});
242 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
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}
result
Definition infer.py:103
int32_t MultiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
Definition Shape.h:28

References mir::Tensor< T >::at(), mir::Conv2DOpAttributes::data_format, mir::Shape::dim(), mir::TensorVariant::getShape(), mir::TensorVariant::getType(), mir_interpreter::MultiplyByQuantizedMultiplier(), mir::Conv2DOpAttributes::num_groups, mir::Conv2DOpAttributes::padding_before, mir_interpreter::QuantizeMultiplier(), mir::Shape::rank(), and mir::Conv2DOpAttributes::strides.

Referenced by package.infer.session::inference().


The documentation for this struct was generated from the following file: