ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 37 of file ConvolutionLayer.h.

Constructor & Destructor Documentation

◆ ConvolutionLayer()

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

Definition at line 27 of file ConvolutionLayer.cc.

28 : _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
33 _is_hybrid(false)
34{
35 // DO NOTHING
36}
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 191 of file ConvolutionLayer.cc.

200{
201 _input = input;
202 _kernel = kernel;
203 _bias = bias;
204 _paddingType = paddingType;
205 _paddingLeft = paddingLeft;
206 _paddingRight = paddingRight;
207 _paddingTop = paddingTop;
208 _paddingBottom = paddingBottom;
209 _strideWidth = strideWidth;
210 _strideHeight = strideHeight;
211 _dilationWidthFactor = dilationWidthFactor;
212 _dilationHeightFactor = dilationHeightFactor;
213 _activation = activation;
214 _output = output;
215 _is_cachable_weights = is_cachable_weights;
216 _is_hybrid = _input->data_type() == OperandType::FLOAT32 &&
217 _kernel->data_type() == OperandType::QUANT_INT8_SYMM;
218}
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 278 of file ConvolutionLayer.cc.

279{
280 if (_prepare)
281 return;
282
283 if (_is_hybrid)
284 {
285 // ensure weight is per-channel quantized.
286 int32_t kernel_output_channel = getShape(_kernel).Dims(0);
287 // zero_points comes from flatbuffer vector. Its size is within uint32_t range.
288 size_t kernel_zerop_cnt = _kernel->data_scales().size();
289 // promote to int64_t to compare int32_t and uint32_t
290 if ((int64_t)kernel_output_channel != (int64_t)kernel_zerop_cnt)
291 throw std::runtime_error{"Conv2D hybrid supports only per-channel quantized weight."};
292
293 // allocate memory for activation quantization.
294 // - quantized values (int8_t type and same shape of original input)
295 // - quantization params (= scale/zeropoint for each input)
296 auto input_shape = getShape(_input);
297 const int batch_size = input_shape.Dims(0);
298 const int input_size = input_shape.FlatSize() / batch_size;
299 _hybrid_arena = std::make_unique<nnfw::cker::ConvHybridTempArena>(batch_size, input_size);
300 _prepare = true;
301 return;
302 }
303
305 if (_input->data_type() == OperandType::FLOAT32 && _is_cachable_weights)
306 {
307 bool is_transposed = false;
308 kernel.prepareF32(getShape(_kernel), getBuffer<float>(_kernel), getPaddingType(_paddingType),
310
311 // Decrease reference of _kernel(weights) only when _kernel is constant
312 if (is_transposed)
313 {
314 auto kernel_tensor = dynamic_cast<const Tensor *>(_kernel);
315 if (kernel_tensor)
316 // TODO Remove const_cast
317 const_cast<Tensor *>(kernel_tensor)->decrease_ref();
318 }
319 }
320 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM && _is_cachable_weights &&
322 {
323 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
324 if (per_channel_quantized)
325 {
328 _kernel->data_scales().size(), getShape(_kernel).Dims(0),
330 }
331 else
332 {
336 }
337 }
338 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
339 {
341 {
344 _kernel->data_scales().size(), getShape(_kernel).Dims(0),
346 }
347 else
348 {
349 throw std::runtime_error{"Conv2D: Int8 dynamic weight is not supported"};
350 }
351 }
352 _prepare = true;
353}
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:110
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 220 of file ConvolutionLayer.cc.

221{
222 prepare();
223 if (_input->is_dynamic() || _kernel->is_dynamic())
224 {
225 const auto ifm_shape = _input->getShape().asFeature();
226 const auto ofm_shape = _output->getShape().asFeature();
227 // Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
228 const auto ker_shape = _kernel->getShape();
229 const auto ker_height = ker_shape.dim(1);
230 const auto ker_width = ker_shape.dim(2);
231
232 ir::Stride stride;
233 stride.vertical = _strideWidth;
234 stride.horizontal = _strideWidth;
235
236 ir::Padding param_padding;
237 param_padding.type = _paddingType;
238 param_padding.param.left = _paddingLeft;
239 param_padding.param.right = _paddingRight;
240 param_padding.param.top = _paddingTop;
241 param_padding.param.bottom = _paddingBottom;
242
243 const auto padding =
244 ir::calculatePadding(param_padding, ifm_shape, ofm_shape, stride, ker_width, ker_height,
246
247 _paddingLeft = padding.left;
248 _paddingRight = padding.right;
249 _paddingTop = padding.top;
250 _paddingBottom = padding.bottom;
251 }
252 if (_is_hybrid)
253 {
254 convQ8iHybridPerChannel();
255 }
256 else if (_input->data_type() == OperandType::FLOAT32)
257 {
258 convFloat32();
259 }
260 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
261 {
262 const bool per_channel_quantized = _kernel->data_scales().size() > 1;
263 if (per_channel_quantized)
264 convQ8uPerChannel();
265 else
266 convQ8uPerTensor();
267 }
268 else if (_input->data_type() == OperandType::QUANT_INT8_ASYMM)
269 {
270 convQ8i();
271 }
272 else
273 {
274 throw std::runtime_error{"Conv: unsupported data type"};
275 }
276}
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:131

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().

Field Documentation

◆ _activation

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

Definition at line 78 of file ConvolutionLayer.h.

Referenced by configure().

◆ _bias

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

Definition at line 64 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 80 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 81 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 84 of file ConvolutionLayer.h.

Referenced by configure(), and prepare().

◆ _is_hybrid

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

Definition at line 85 of file ConvolutionLayer.h.

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

◆ _kernel

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

Definition at line 63 of file ConvolutionLayer.h.

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

◆ _output

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

Definition at line 65 of file ConvolutionLayer.h.

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

◆ _paddingBottom

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

Definition at line 71 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingLeft

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

Definition at line 68 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingRight

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

Definition at line 70 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingTop

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

Definition at line 69 of file ConvolutionLayer.h.

Referenced by configure(), and run().

◆ _paddingType

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

Definition at line 67 of file ConvolutionLayer.h.

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

◆ _prepare

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

Definition at line 83 of file ConvolutionLayer.h.

Referenced by prepare().

◆ _strideHeight

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

Definition at line 74 of file ConvolutionLayer.h.

Referenced by configure(), and prepare().

◆ _strideWidth

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

Definition at line 73 of file ConvolutionLayer.h.

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


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