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

#include <ConvolutionLayer.h>

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

Public Member Functions

 ConvolutionLayer ()
 
 ~ConvolutionLayer ()
 
void configure (const IPortableTensor *input, const IPortableTensor *kernel, const IPortableTensor *bias, ir::PaddingType _paddingType, const uint32_t paddingLeft, const uint32_t paddingRight, const uint32_t paddingTop, const uint32_t paddingBottom, const uint32_t strideWidth, const uint32_t strideHeight, const uint32_t dilationWidthFactor, const uint32_t dilationHeightFactor, const ir::Activation activation, IPortableTensor *output, bool is_cachable_weights)
 
void prepare () override
 
void run () override
 
- Public Member Functions inherited from onert::exec::IFunction
virtual ~IFunction ()=default
 

Protected Attributes

const IPortableTensor_input
 
const IPortableTensor_kernel
 
const IPortableTensor_bias
 
IPortableTensor_output
 
ir::PaddingType _paddingType
 
uint32_t _paddingLeft
 
uint32_t _paddingTop
 
uint32_t _paddingRight
 
uint32_t _paddingBottom
 
uint32_t _strideWidth
 
uint32_t _strideHeight
 
uint32_t _dilationWidthFactor
 
uint32_t _dilationHeightFactor
 
ir::Activation _activation
 
std::unique_ptr< nnfw::cker::Conv_conv_kernel
 
std::unique_ptr< nnfw::cker::ConvHybridTempArena_hybrid_arena
 
bool _prepare
 
bool _is_cachable_weights
 
bool _is_hybrid
 

Detailed Description

Definition at line 46 of file ConvolutionLayer.h.

Constructor & Destructor Documentation

◆ ConvolutionLayer()

onert::backend::cpu::ops::ConvolutionLayer::ConvolutionLayer ( )

Definition at line 33 of file ConvolutionLayer.cc.

34 : _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
39 _is_hybrid(false)
40{
41 // DO NOTHING
42}
std::unique_ptr< nnfw::cker::Conv > _conv_kernel

◆ ~ConvolutionLayer()

onert::backend::cpu::ops::ConvolutionLayer::~ConvolutionLayer ( )
default

Member Function Documentation

◆ configure()

void onert::backend::cpu::ops::ConvolutionLayer::configure ( const IPortableTensor input,
const IPortableTensor kernel,
const IPortableTensor bias,
ir::PaddingType  _paddingType,
const uint32_t  paddingLeft,
const uint32_t  paddingRight,
const uint32_t  paddingTop,
const uint32_t  paddingBottom,
const uint32_t  strideWidth,
const uint32_t  strideHeight,
const uint32_t  dilationWidthFactor,
const uint32_t  dilationHeightFactor,
const ir::Activation  activation,
IPortableTensor output,
bool  is_cachable_weights 
)

Definition at line 197 of file ConvolutionLayer.cc.

206{
207 _input = input;
208 _kernel = kernel;
209 _bias = bias;
210 _paddingType = paddingType;
211 _paddingLeft = paddingLeft;
212 _paddingRight = paddingRight;
213 _paddingTop = paddingTop;
214 _paddingBottom = paddingBottom;
215 _strideWidth = strideWidth;
216 _strideHeight = strideHeight;
217 _dilationWidthFactor = dilationWidthFactor;
218 _dilationHeightFactor = dilationHeightFactor;
219 _activation = activation;
220 _output = output;
221 _is_cachable_weights = is_cachable_weights;
222 _is_hybrid = _input->data_type() == OperandType::FLOAT32 &&
223 _kernel->data_type() == OperandType::QUANT_INT8_SYMM;
224}
ir::DataType data_type() const override final

References _activation, _bias, _dilationHeightFactor, _dilationWidthFactor, _input, _is_cachable_weights, _is_hybrid, _kernel, _output, _paddingBottom, _paddingLeft, _paddingRight, _paddingTop, _paddingType, _strideHeight, _strideWidth, and onert::backend::IPortableTensor::data_type().

◆ prepare()

void onert::backend::cpu::ops::ConvolutionLayer::prepare ( )
overridevirtual

Reimplemented from onert::exec::IFunction.

Definition at line 284 of file ConvolutionLayer.cc.

285{
286 if (_prepare)
287 return;
288
289 if (_is_hybrid)
290 {
291 // ensure weight is per-channel quantized.
292 int32_t kernel_output_channel = getShape(_kernel).Dims(0);
293 // zero_points comes from flatbuffer vector. Its size is within uint32_t range.
294 size_t kernel_zerop_cnt = _kernel->data_scales().size();
295 // promote to int64_t to compare int32_t and uint32_t
296 if ((int64_t)kernel_output_channel != (int64_t)kernel_zerop_cnt)
297 throw std::runtime_error{"Conv2D hybrid supports only per-channel quantized weight."};
298
299 // allocate memory for activation quantization.
300 // - quantized values (int8_t type and same shape of original input)
301 // - quantization params (= scale/zeropoint for each input)
302 auto input_shape = getShape(_input);
303 const int batch_size = input_shape.Dims(0);
304 const int input_size = input_shape.FlatSize() / batch_size;
305 _hybrid_arena = std::make_unique<nnfw::cker::ConvHybridTempArena>(batch_size, input_size);
306 _prepare = true;
307 return;
308 }
309
311 if (_input->data_type() == OperandType::FLOAT32 && _is_cachable_weights)
312 {
313 bool is_transposed = false;
314 kernel.prepareF32(getShape(_kernel), getBuffer<float>(_kernel), getPaddingType(_paddingType),
316
317 // Decrease reference of _kernel(weights) only when _kernel is constant
318 if (is_transposed)
319 {
320 auto kernel_tensor = dynamic_cast<const Tensor *>(_kernel);
321 if (kernel_tensor)
322 // TODO Remove const_cast
323 const_cast<Tensor *>(kernel_tensor)->decrease_ref();
324 }
325 }
326 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM && _is_cachable_weights &&
328 {
329 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
330 if (per_channel_quantized)
331 {
334 _kernel->data_scales().size(), getShape(_kernel).Dims(0),
336 }
337 else
338 {
342 }
343 }
344 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
345 {
347 {
350 _kernel->data_scales().size(), getShape(_kernel).Dims(0),
352 }
353 else
354 {
355 throw std::runtime_error{"Conv2D: Int8 dynamic weight is not supported"};
356 }
357 }
358 _prepare = true;
359}
std::vector< int > & per_channel_output_shift()
Definition Conv.h:151
void prepareQ8uPerTensor(const Shape &input_shape, const Shape &kernel_shape, const Shape &output_shape, uint32_t stride_width, uint32_t stride_height, uint32_t dilation_width_factor, uint32_t dilation_height_factor)
Definition Conv.h:74
void prepareF32(const Shape &filter_shape, const float *filter_data, PaddingType padding_type, bool &is_replaced_weights, uint32_t dilationWidthFactor, uint32_t dilationHeightFactor)
Definition Conv.h:60
std::vector< int32_t > & per_channel_output_multiplier()
Definition Conv.h:150
int32_t Dims(int i) const
Definition Shape.h:92
const std::vector< float > & data_scales() const override final
float data_scale() 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...
std::unique_ptr< nnfw::cker::ConvHybridTempArena > _hybrid_arena
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
void GetQuantizedConvolutionMultipliersAndShifts(float input_scale, float output_scale, const float *filter_scales, size_t filter_scales_size, int num_channels, std::vector< int32_t > &per_channel_output_multiplier, std::vector< int > &per_channel_output_shift)
nnfw::cker::PaddingType getPaddingType(ir::PaddingType ir_padding_type)
Definition Dims.h:26

References _conv_kernel, _dilationHeightFactor, _dilationWidthFactor, _hybrid_arena, _input, _is_cachable_weights, _is_hybrid, _kernel, _output, _paddingType, _prepare, _strideHeight, _strideWidth, onert::backend::IPortableTensor::data_scale(), onert::backend::IPortableTensor::data_scales(), onert::backend::IPortableTensor::data_type(), nnfw::cker::Shape::Dims(), onert::backend::cpu::ops::getPaddingType(), onert::backend::cpu::ops::GetQuantizedConvolutionMultipliersAndShifts(), onert::backend::cpu::ops::getShape(), onert::backend::IPortableTensor::is_dynamic(), nnfw::cker::Conv::per_channel_output_multiplier(), nnfw::cker::Conv::per_channel_output_shift(), nnfw::cker::Conv::prepareF32(), and nnfw::cker::Conv::prepareQ8uPerTensor().

Referenced by run().

◆ run()

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

Implements onert::exec::IFunction.

Definition at line 226 of file ConvolutionLayer.cc.

227{
228 prepare();
229 if (_input->is_dynamic() || _kernel->is_dynamic())
230 {
231 const auto ifm_shape = _input->getShape().asFeature();
232 const auto ofm_shape = _output->getShape().asFeature();
233 // Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
234 const auto ker_shape = _kernel->getShape();
235 const auto ker_height = ker_shape.dim(1);
236 const auto ker_width = ker_shape.dim(2);
237
238 ir::Stride stride;
239 stride.vertical = _strideWidth;
240 stride.horizontal = _strideWidth;
241
242 ir::Padding param_padding;
243 param_padding.type = _paddingType;
244 param_padding.param.left = _paddingLeft;
245 param_padding.param.right = _paddingRight;
246 param_padding.param.top = _paddingTop;
247 param_padding.param.bottom = _paddingBottom;
248
249 const auto padding =
250 ir::calculatePadding(param_padding, ifm_shape, ofm_shape, stride, ker_width, ker_height,
252
253 _paddingLeft = padding.left;
254 _paddingRight = padding.right;
255 _paddingTop = padding.top;
256 _paddingBottom = padding.bottom;
257 }
258 if (_is_hybrid)
259 {
260 convQ8iHybridPerChannel();
261 }
262 else if (_input->data_type() == OperandType::FLOAT32)
263 {
264 convFloat32();
265 }
266 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
267 {
268 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
269 if (per_channel_quantized)
270 convQ8uPerChannel();
271 else
272 convQ8uPerTensor();
273 }
274 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
275 {
276 convQ8i();
277 }
278 else
279 {
280 throw std::runtime_error{"Conv: unsupported data type"};
281 }
282}
ir::Shape getShape() const override final
Get ir::Shape of tensor.
const ExplicitPadding calculatePadding(const Padding &padding, const FeatureShape &ifm_shape, const FeatureShape &ofm_shape, const Stride &stride, uint32_t kw, uint32_t kh, uint32_t dwf=1, uint32_t dhf=1)
Definition Padding.cc:133

References _dilationHeightFactor, _dilationWidthFactor, _input, _is_hybrid, _kernel, _output, _paddingBottom, _paddingLeft, _paddingRight, _paddingTop, _paddingType, _strideWidth, onert::ir::ExplicitPadding::bottom, onert::ir::calculatePadding(), onert::backend::IPortableTensor::data_scales(), onert::backend::IPortableTensor::data_type(), onert::backend::IPortableTensor::getShape(), onert::ir::Stride::horizontal, onert::backend::IPortableTensor::is_dynamic(), onert::ir::ExplicitPadding::left, onert::ir::Padding::param, prepare(), onert::ir::ExplicitPadding::right, onert::ir::ExplicitPadding::top, onert::ir::Padding::type, and onert::ir::Stride::vertical.

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

Field Documentation

◆ _activation

ir::Activation onert::backend::cpu::ops::ConvolutionLayer::_activation
protected

Definition at line 87 of file ConvolutionLayer.h.

Referenced by configure().

◆ _bias

const IPortableTensor* onert::backend::cpu::ops::ConvolutionLayer::_bias
protected

Definition at line 73 of file ConvolutionLayer.h.

Referenced by configure().

◆ _conv_kernel

std::unique_ptr<nnfw::cker::Conv> onert::backend::cpu::ops::ConvolutionLayer::_conv_kernel
protected

Definition at line 89 of file ConvolutionLayer.h.

Referenced by prepare().

◆ _dilationHeightFactor

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_dilationHeightFactor
protected

◆ _dilationWidthFactor

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_dilationWidthFactor
protected

◆ _hybrid_arena

std::unique_ptr<nnfw::cker::ConvHybridTempArena> onert::backend::cpu::ops::ConvolutionLayer::_hybrid_arena
protected

Definition at line 90 of file ConvolutionLayer.h.

Referenced by prepare().

◆ _input

const IPortableTensor* onert::backend::cpu::ops::ConvolutionLayer::_input
protected

◆ _is_cachable_weights

bool onert::backend::cpu::ops::ConvolutionLayer::_is_cachable_weights
protected

Definition at line 93 of file ConvolutionLayer.h.

Referenced by configure(), and prepare().

◆ _is_hybrid

bool onert::backend::cpu::ops::ConvolutionLayer::_is_hybrid
protected

Definition at line 94 of file ConvolutionLayer.h.

Referenced by configure(), prepare(), and run().

◆ _kernel

const IPortableTensor* onert::backend::cpu::ops::ConvolutionLayer::_kernel
protected

Definition at line 72 of file ConvolutionLayer.h.

Referenced by configure(), prepare(), and run().

◆ _output

IPortableTensor* onert::backend::cpu::ops::ConvolutionLayer::_output
protected

Definition at line 74 of file ConvolutionLayer.h.

Referenced by configure(), prepare(), and run().

◆ _paddingBottom

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_paddingBottom
protected

Definition at line 80 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingLeft

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_paddingLeft
protected

Definition at line 77 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingRight

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_paddingRight
protected

Definition at line 79 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingTop

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_paddingTop
protected

Definition at line 78 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingType

ir::PaddingType onert::backend::cpu::ops::ConvolutionLayer::_paddingType
protected

Definition at line 76 of file ConvolutionLayer.h.

Referenced by configure(), prepare(), and run().

◆ _prepare

bool onert::backend::cpu::ops::ConvolutionLayer::_prepare
protected

Definition at line 92 of file ConvolutionLayer.h.

Referenced by prepare().

◆ _strideHeight

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_strideHeight
protected

Definition at line 83 of file ConvolutionLayer.h.

Referenced by configure(), and prepare().

◆ _strideWidth

uint32_t onert::backend::cpu::ops::ConvolutionLayer::_strideWidth
protected

Definition at line 82 of file ConvolutionLayer.h.

Referenced by configure(), prepare(), and run().


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