ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::backend::cpu::ops::DepthwiseConvolutionLayer Class Reference

#include <DepthwiseConvolutionLayer.h>

Collaboration diagram for onert::backend::cpu::ops::DepthwiseConvolutionLayer:

Public Member Functions

 DepthwiseConvolutionLayer ()=default
 
void convFloat32 ()
 
void convQ8uPerTensor ()
 
void convQ8uPerChannel ()
 
void convQ8i ()
 
void convQ8iHybridPerChannel ()
 
void configure (const IPortableTensor *input, const IPortableTensor *kernel, const IPortableTensor *bias, const uint32_t paddingLeft, const uint32_t paddingRight, const uint32_t paddingTop, const uint32_t paddingBottom, const uint32_t strideW, const uint32_t strideH, const uint32_t multiplier, const uint32_t dilationWidth, const uint32_t dilationHeight, const ir::Activation activation, IPortableTensor *output, const std::shared_ptr< ExternalContext > &external_context)
 
void run () override
 
- Public Member Functions inherited from onert::exec::IFunction
virtual ~IFunction ()=default
 
virtual void prepare ()
 

Protected Attributes

const IPortableTensor_input {nullptr}
 
const IPortableTensor_kernel {nullptr}
 
const IPortableTensor_bias {nullptr}
 
IPortableTensor_output {nullptr}
 
uint32_t _paddingLeft {0}
 
uint32_t _paddingTop {0}
 
uint32_t _paddingRight {0}
 
uint32_t _paddingBottom {0}
 
uint32_t _strideWidth {0}
 
uint32_t _strideHeight {0}
 
uint32_t _multiplier {0}
 
uint32_t _dilationWidth {1}
 
uint32_t _dilationHeight {1}
 
ir::Activation _activation {ir::Activation::NONE}
 
bool _use_padded_filter {false}
 
std::unique_ptr< Tensor_padded_filter {nullptr}
 
std::unique_ptr< Tensor_filter_buffers {nullptr}
 

Detailed Description

Definition at line 36 of file DepthwiseConvolutionLayer.h.

Constructor & Destructor Documentation

◆ DepthwiseConvolutionLayer()

onert::backend::cpu::ops::DepthwiseConvolutionLayer::DepthwiseConvolutionLayer ( )
default

Member Function Documentation

◆ configure()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::configure ( const IPortableTensor input,
const IPortableTensor kernel,
const IPortableTensor bias,
const uint32_t  paddingLeft,
const uint32_t  paddingRight,
const uint32_t  paddingTop,
const uint32_t  paddingBottom,
const uint32_t  strideW,
const uint32_t  strideH,
const uint32_t  multiplier,
const uint32_t  dilationWidth,
const uint32_t  dilationHeight,
const ir::Activation  activation,
IPortableTensor output,
const std::shared_ptr< ExternalContext > &  external_context 
)

Definition at line 286 of file DepthwiseConvolutionLayer.cc.

293{
294 _input = input;
295 _kernel = kernel;
296 _bias = bias;
297 _paddingLeft = paddingLeft;
298 _paddingRight = paddingRight;
299 _paddingTop = paddingTop;
300 _paddingBottom = paddingBottom;
301 _strideWidth = strideWidth;
302 _strideHeight = strideHeight;
303 _multiplier = multiplier;
304 _dilationWidth = dilationWidth;
305 _dilationHeight = dilationHeight;
306 _activation = activation;
307 _output = output;
308 _external_context = external_context;
309 _is_hybrid = _input->data_type() == OperandType::FLOAT32 &&
310 _kernel->data_type() == OperandType::QUANT_INT8_SYMM;
311
312 if (_is_hybrid)
313 {
314 ensureQ8iHybridPerChannel();
315 prepareQ8iHybridPerChannel();
316 _prepared = true;
317 }
318 else if (_input->data_type() == OperandType::FLOAT32)
319 {
320 prepareF32();
321 }
322 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
323 {
325 {
326 prepareQ8i();
327 _prepared = true;
328 }
329 }
330 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM && _kernel->is_constant() &&
332 {
333 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
334 if (per_channel_quantized)
335 {
336 prepareQ8uPerChannel();
337 _prepared = true;
338 }
339 }
340}
const std::vector< float > & data_scales() const override final
ir::DataType data_type() const override final
bool is_dynamic() const override final
Return true if the tensor needs dynamic allocation, meaning that during compile-time the outpus shape...
bool is_constant() const override final
Return true if the tensor is constant.

References _activation, _bias, _dilationHeight, _dilationWidth, _input, _kernel, _multiplier, _output, _paddingBottom, _paddingLeft, _paddingRight, _paddingTop, _strideHeight, _strideWidth, onert::backend::IPortableTensor::data_scales(), onert::backend::IPortableTensor::data_type(), onert::backend::IPortableTensor::is_constant(), and onert::backend::IPortableTensor::is_dynamic().

◆ convFloat32()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::convFloat32 ( )

Definition at line 68 of file DepthwiseConvolutionLayer.cc.

69{
70 float output_activation_min = 0, output_activation_max = 0;
71 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
72
74 op_params.stride_width = _strideWidth;
75 op_params.stride_height = _strideHeight;
80 op_params.depth_multiplier = _multiplier;
81 op_params.float_activation_min = output_activation_min;
82 op_params.float_activation_max = output_activation_max;
83
84 // Since DepthwiseConvOp does not support dilation and different W/H stride yet,
85 // it uses the existing kernel in this case.
87 {
88 nnfw::cker::DepthwiseConvOp(op_params, getShape(_input), getBuffer<float>(_input),
89 getShape(_kernel), getBuffer<float>(_kernel), getShape(_bias),
90 getBuffer<float>(_bias), getBuffer<float>(_padded_filter.get()),
91 _use_padded_filter, getBuffer<float>(_filter_buffers.get()),
92 getShape(_output), getBuffer<float>(_output));
93 }
94 else
95 {
96 nnfw::cker::DepthwiseConv<float, float>(
97 op_params, getShape(_input), getBuffer<float>(_input), getShape(_kernel),
98 getBuffer<float>(_kernel), getShape(_bias), getBuffer<float>(_bias), getShape(_output),
99 getBuffer<float>(_output), _external_context->ruy_context());
100 }
101}
void DepthwiseConvOp(const DepthwiseConvParams &params, const Shape &input_shape, const float *input_data, const Shape &filter_shape, const float *filter_data, const Shape &bias_shape, const float *bias_data, float *padded_filter_data, bool pad_filter, float *filter_buffers_data, const Shape &output_shape, float *output_data)
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
void CalculateActivationRange(ir::Activation activation, T *activation_min, T *activation_max)
PaddingValues padding_values
Definition Types.h:234

References _activation, _bias, _dilationHeight, _dilationWidth, _filter_buffers, _input, _kernel, _multiplier, _output, _padded_filter, _paddingLeft, _paddingTop, _strideHeight, _strideWidth, _use_padded_filter, onert::util::CalculateActivationRange(), nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::DepthwiseConvOp(), nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, nnfw::cker::DepthwiseConvParams::float_activation_max, nnfw::cker::DepthwiseConvParams::float_activation_min, onert::backend::cpu::ops::getShape(), nnfw::cker::PaddingValues::height, nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, and nnfw::cker::PaddingValues::width.

Referenced by run().

◆ convQ8i()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::convQ8i ( )

Definition at line 166 of file DepthwiseConvolutionLayer.cc.

167{
168 if (!_prepared)
169 {
170 prepareQ8i();
171 _prepared = true;
172 }
173
174 int32_t output_activation_min = 0;
175 int32_t output_activation_max = 0;
177 &output_activation_max);
178
183 op_params.depth_multiplier = _multiplier;
184 op_params.stride_width = _strideWidth;
185 op_params.stride_height = _strideHeight;
188 op_params.input_offset = -_input->data_zero_point();
189 op_params.weights_offset = 0;
191 op_params.quantized_activation_min = output_activation_min;
192 op_params.quantized_activation_max = output_activation_max;
193
195 op_params, _per_channel_output_multiplier.data(), _per_channel_output_shift.data(),
196 getShape(_input), getBuffer<int8_t>(_input), getShape(_kernel), getBuffer<int8_t>(_kernel),
197 getShape(_bias), getBuffer<int32_t>(_bias), getShape(_output), getBuffer<int8_t>(_output),
198 _external_context->ruy_context());
199}
int32_t data_zero_point() const override final
void DepthwiseConvPerChannel(const DepthwiseConvParams &params, const int32_t *output_multiplier, const int32_t *output_shift, const Shape &input_shape, const int8_t *input_data, const Shape &filter_shape, const int8_t *filter_data, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, int8_t *output_data, ruy::Context *ruy_context)
void CalculateActivationRangeQuantized(ir::Activation activation, const IPortableTensor *output, int32_t *act_min, int32_t *act_max)

References _activation, _bias, _dilationHeight, _dilationWidth, _input, _kernel, _multiplier, _output, _paddingLeft, _paddingTop, _strideHeight, _strideWidth, onert::backend::cpu::ops::CalculateActivationRangeQuantized(), onert::backend::IPortableTensor::data_zero_point(), nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::optimized_integer_ops::DepthwiseConvPerChannel(), nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, onert::backend::cpu::ops::getShape(), nnfw::cker::PaddingValues::height, nnfw::cker::DepthwiseConvParams::input_offset, nnfw::cker::kSame, nnfw::cker::DepthwiseConvParams::output_offset, nnfw::cker::DepthwiseConvParams::padding_type, nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::DepthwiseConvParams::quantized_activation_max, nnfw::cker::DepthwiseConvParams::quantized_activation_min, nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, nnfw::cker::DepthwiseConvParams::weights_offset, and nnfw::cker::PaddingValues::width.

Referenced by run().

◆ convQ8iHybridPerChannel()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::convQ8iHybridPerChannel ( )

Definition at line 201 of file DepthwiseConvolutionLayer.cc.

202{
203 if (!_prepared)
204 {
205 prepareQ8iHybridPerChannel();
206 _prepared = true;
207 }
208
209 float output_activation_min = 0, output_activation_max = 0;
210 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
211
212 auto input_shape = getShape(_input);
213 const int batch_size = input_shape.Dims(0);
214 const int input_size = input_shape.FlatSize() / batch_size;
215
216 auto scaling_factors_ptr = _input_scaling_factors.data();
217 auto input_offsets_ptr = _input_offsets.data();
218
219 for (int b = 0; b < batch_size; ++b)
220 {
221 const int offset = b * input_size;
222 nnfw::cker::PortableAsymmetricQuantizeFloats(getBuffer<float>(_input) + offset, input_size,
223 _input_quantized.data() + offset,
224 &scaling_factors_ptr[b], &input_offsets_ptr[b]);
225 }
226
230 op_params.depth_multiplier = _multiplier;
231 op_params.stride_width = _strideWidth;
232 op_params.stride_height = _strideHeight;
235 op_params.float_activation_min = output_activation_min;
236 op_params.float_activation_max = output_activation_max;
237
239 op_params, _input_scaling_factors.data(), getShape(_input), _input_quantized.data(),
240 getShape(_kernel), getBuffer<int8_t>(_kernel), getShape(_bias), getBuffer<float>(_bias),
241 getShape(_output), getBuffer<float>(_output), _kernel->data_scales().data(),
242 _input_offsets.data());
243}
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
void DepthwiseConvHybridPerChannel(const DepthwiseConvParams &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, int32_t *input_offset)
void PortableAsymmetricQuantizeFloats(const float *values, const int size, int8_t *quantized_values, float *scaling_factor, int32_t *offset)

References _activation, _bias, _dilationHeight, _dilationWidth, _input, _kernel, _multiplier, _output, _paddingLeft, _paddingTop, _strideHeight, _strideWidth, onert::util::CalculateActivationRange(), onert::backend::IPortableTensor::data_scales(), nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::reference_integer_ops::DepthwiseConvHybridPerChannel(), nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, nnfw::cker::DepthwiseConvParams::float_activation_max, nnfw::cker::DepthwiseConvParams::float_activation_min, onert::backend::cpu::ops::getShape(), nnfw::cker::PaddingValues::height, offset(), nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::PortableAsymmetricQuantizeFloats(), nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, and nnfw::cker::PaddingValues::width.

Referenced by run().

◆ convQ8uPerChannel()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::convQ8uPerChannel ( )

Definition at line 138 of file DepthwiseConvolutionLayer.cc.

139{
143 op_params.stride_width = _strideWidth;
144 op_params.stride_height = _strideHeight;
147 op_params.depth_multiplier = _multiplier;
148 op_params.input_offset = -_input->data_zero_point();
150 int32_t output_activation_min = 0;
151 int32_t output_activation_max = 0;
153 &output_activation_max);
154 op_params.quantized_activation_min = output_activation_min;
155 op_params.quantized_activation_max = output_activation_max;
156 // NOTE: The following fields of ConvParams are not used:
157 // padding_type, weights_offset, output_{multiplier,shift}, float_activation_{min,max}
158
160 op_params, _per_channel_output_multiplier.data(), _per_channel_output_shift.data(),
161 getShape(_input), getBuffer<uint8_t>(_input), getShape(_kernel), getBuffer<uint8_t>(_kernel),
162 _kernel->data_zero_points().data(), getShape(_bias), getBuffer<int32_t>(_bias),
163 getShape(_output), getBuffer<uint8_t>(_output));
164}
const std::vector< int32_t > & data_zero_points() const override
void DepthwiseConvPerChannel(const DepthwiseConvParams &params, const int32_t *output_multiplier, const int32_t *output_shift, const Shape &input_shape, const uint8_t *input_data, const Shape &filter_shape, const uint8_t *filter_data, const int32_t *filter_zeropoint, const Shape &bias_shape, const int32_t *bias_data, const Shape &output_shape, uint8_t *output_data)

References _activation, _bias, _dilationHeight, _dilationWidth, _input, _kernel, _multiplier, _output, _paddingLeft, _paddingTop, _strideHeight, _strideWidth, onert::backend::cpu::ops::CalculateActivationRangeQuantized(), onert::backend::IPortableTensor::data_zero_point(), onert::backend::IPortableTensor::data_zero_points(), nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::reference_integer_ops::DepthwiseConvPerChannel(), nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, onert::backend::cpu::ops::getShape(), nnfw::cker::PaddingValues::height, nnfw::cker::DepthwiseConvParams::input_offset, nnfw::cker::DepthwiseConvParams::output_offset, nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::DepthwiseConvParams::quantized_activation_max, nnfw::cker::DepthwiseConvParams::quantized_activation_min, nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, and nnfw::cker::PaddingValues::width.

Referenced by run().

◆ convQ8uPerTensor()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::convQ8uPerTensor ( )

Definition at line 103 of file DepthwiseConvolutionLayer.cc.

104{
105 int32_t output_activation_min = 0;
106 int32_t output_activation_max = 0;
108 &output_activation_max);
109
110 double real_multiplier = 0.0;
111 int32_t output_multiplier = 0;
112 int32_t output_shift = 0;
114 QuantizeMultiplier(real_multiplier, &output_multiplier, &output_shift);
115
117 op_params.stride_width = _strideWidth;
118 op_params.stride_height = _strideHeight;
123 op_params.depth_multiplier = _multiplier;
124 op_params.input_offset = -_input->data_zero_point();
125 op_params.weights_offset = -_kernel->data_zero_point();
127 op_params.output_multiplier = output_multiplier;
128 op_params.output_shift = output_shift;
129 op_params.quantized_activation_min = output_activation_min;
130 op_params.quantized_activation_max = output_activation_max;
131
132 nnfw::cker::DepthwiseConv<uint8_t, int32_t>(
133 op_params, getShape(_input), getBuffer<uint8_t>(_input), getShape(_kernel),
134 getBuffer<uint8_t>(_kernel), getShape(_bias), getBuffer<int32_t>(_bias), getShape(_output),
135 getBuffer<uint8_t>(_output), _external_context->ruy_context());
136}
void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
void GetQuantizedConvolutionMultiplier(const IPortableTensor *input, const IPortableTensor *filter, const IPortableTensor *bias, const IPortableTensor *output, double *multiplier)

References _activation, _bias, _dilationHeight, _dilationWidth, _input, _kernel, _multiplier, _output, _paddingLeft, _paddingTop, _strideHeight, _strideWidth, onert::backend::cpu::ops::CalculateActivationRangeQuantized(), onert::backend::IPortableTensor::data_zero_point(), nnfw::cker::DepthwiseConvParams::depth_multiplier, nnfw::cker::DepthwiseConvParams::dilation_height_factor, nnfw::cker::DepthwiseConvParams::dilation_width_factor, onert::backend::cpu::ops::GetQuantizedConvolutionMultiplier(), onert::backend::cpu::ops::getShape(), nnfw::cker::PaddingValues::height, nnfw::cker::DepthwiseConvParams::input_offset, nnfw::cker::DepthwiseConvParams::output_multiplier, nnfw::cker::DepthwiseConvParams::output_offset, nnfw::cker::DepthwiseConvParams::output_shift, nnfw::cker::DepthwiseConvParams::padding_values, nnfw::cker::DepthwiseConvParams::quantized_activation_max, nnfw::cker::DepthwiseConvParams::quantized_activation_min, onert::backend::cpu::ops::QuantizeMultiplier(), nnfw::cker::DepthwiseConvParams::stride_height, nnfw::cker::DepthwiseConvParams::stride_width, nnfw::cker::DepthwiseConvParams::weights_offset, and nnfw::cker::PaddingValues::width.

Referenced by run().

◆ run()

void onert::backend::cpu::ops::DepthwiseConvolutionLayer::run ( )
overridevirtual

Implements onert::exec::IFunction.

Definition at line 342 of file DepthwiseConvolutionLayer.cc.

343{
344 if (_is_hybrid)
345 {
347 }
348 else if (_input->data_type() == OperandType::FLOAT32)
349 {
350 convFloat32();
351 }
352 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
353 {
354 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
355 if (per_channel_quantized)
357 else
359 }
360 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
361 {
362 convQ8i();
363 }
364 else
365 {
366 throw std::runtime_error{"DepthwiseConv: unsupported data type"};
367 }
368}

References _input, _kernel, convFloat32(), convQ8i(), convQ8iHybridPerChannel(), convQ8uPerChannel(), convQ8uPerTensor(), onert::backend::IPortableTensor::data_scales(), and onert::backend::IPortableTensor::data_type().

Referenced by onert::backend::train::ops::DepthwiseConvolutionLayer::forward(), and package.infer.session::inference().

Field Documentation

◆ _activation

ir::Activation onert::backend::cpu::ops::DepthwiseConvolutionLayer::_activation {ir::Activation::NONE}
protected

◆ _bias

const IPortableTensor* onert::backend::cpu::ops::DepthwiseConvolutionLayer::_bias {nullptr}
protected

◆ _dilationHeight

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_dilationHeight {1}
protected

◆ _dilationWidth

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_dilationWidth {1}
protected

◆ _filter_buffers

std::unique_ptr<Tensor> onert::backend::cpu::ops::DepthwiseConvolutionLayer::_filter_buffers {nullptr}
protected

Definition at line 90 of file DepthwiseConvolutionLayer.h.

90{nullptr};

Referenced by convFloat32().

◆ _input

const IPortableTensor* onert::backend::cpu::ops::DepthwiseConvolutionLayer::_input {nullptr}
protected

◆ _kernel

const IPortableTensor* onert::backend::cpu::ops::DepthwiseConvolutionLayer::_kernel {nullptr}
protected

◆ _multiplier

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_multiplier {0}
protected

◆ _output

IPortableTensor* onert::backend::cpu::ops::DepthwiseConvolutionLayer::_output {nullptr}
protected

◆ _padded_filter

std::unique_ptr<Tensor> onert::backend::cpu::ops::DepthwiseConvolutionLayer::_padded_filter {nullptr}
protected

Definition at line 89 of file DepthwiseConvolutionLayer.h.

89{nullptr};

Referenced by convFloat32().

◆ _paddingBottom

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_paddingBottom {0}
protected

Definition at line 76 of file DepthwiseConvolutionLayer.h.

76{0};

Referenced by configure().

◆ _paddingLeft

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_paddingLeft {0}
protected

◆ _paddingRight

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_paddingRight {0}
protected

Definition at line 75 of file DepthwiseConvolutionLayer.h.

75{0};

Referenced by configure().

◆ _paddingTop

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_paddingTop {0}
protected

◆ _strideHeight

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_strideHeight {0}
protected

◆ _strideWidth

uint32_t onert::backend::cpu::ops::DepthwiseConvolutionLayer::_strideWidth {0}
protected

◆ _use_padded_filter

bool onert::backend::cpu::ops::DepthwiseConvolutionLayer::_use_padded_filter {false}
protected

Definition at line 88 of file DepthwiseConvolutionLayer.h.

88{false};

Referenced by convFloat32().


The documentation for this class was generated from the following files: