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

#include <ConvolutionLayer.h>

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

Public Member Functions

 ConvolutionLayer (const std::shared_ptr< ExternalContext > external_context)
 
void configure (const IPortableTensor *input, const IPortableTensor *kernel, const IPortableTensor *bias, ir::PaddingType padding_type, const uint32_t padding_left, const uint32_t padding_right, const uint32_t padding_top, const uint32_t padding_bottom, const uint32_t stride_width, const uint32_t stride_height, const uint32_t dilation_width_factor, const uint32_t dilation_height_factor, const ir::Activation activation, IPortableTensor *output)
 
void run () override
 
bool create () override
 
bool setup () override
 
- Public Member Functions inherited from onert::backend::xnnpack::ops::Layer
 Layer (const std::shared_ptr< ExternalContext > external_context)
 
 ~Layer ()
 
void prepare () override
 
- Public Member Functions inherited from onert::exec::IFunction
virtual ~IFunction ()=default
 

Additional Inherited Members

- Protected Attributes inherited from onert::backend::xnnpack::ops::Layer
xnn_operator_t _kernel_op
 
bool _create
 
bool _setup
 
const std::shared_ptr< ExternalContext_external_context
 

Detailed Description

Definition at line 33 of file ConvolutionLayer.h.

Constructor & Destructor Documentation

◆ ConvolutionLayer()

onert::backend::xnnpack::ops::ConvolutionLayer::ConvolutionLayer ( const std::shared_ptr< ExternalContext external_context)

Definition at line 29 of file ConvolutionLayer.cc.

30 : Layer(external_context), _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
31 _padding_type(ir::PaddingType::EXPLICIT), _padding_left(0), _padding_top(0), _padding_right(0),
32 _padding_bottom(0), _stride_width(0), _stride_height(0), _dilation_width_factor(1),
33 _dilation_height_factor(1), _activation(ir::Activation::NONE)
34{
35 // DO NOTHING
36}
Layer(const std::shared_ptr< ExternalContext > external_context)
Definition Layer.h:43

Member Function Documentation

◆ configure()

void onert::backend::xnnpack::ops::ConvolutionLayer::configure ( const IPortableTensor input,
const IPortableTensor kernel,
const IPortableTensor bias,
ir::PaddingType  padding_type,
const uint32_t  padding_left,
const uint32_t  padding_right,
const uint32_t  padding_top,
const uint32_t  padding_bottom,
const uint32_t  stride_width,
const uint32_t  stride_height,
const uint32_t  dilation_width_factor,
const uint32_t  dilation_height_factor,
const ir::Activation  activation,
IPortableTensor output 
)

Definition at line 38 of file ConvolutionLayer.cc.

46{
47 _input = input;
48 _kernel = kernel;
49 _bias = bias;
50 _padding_type = padding_type;
51 _padding_left = padding_left;
52 _padding_right = padding_right;
53 _padding_top = padding_top;
54 _padding_bottom = padding_bottom;
55 _stride_width = stride_width;
56 _stride_height = stride_height;
57 _dilation_width_factor = dilation_width_factor;
58 _dilation_height_factor = dilation_height_factor;
59 _activation = activation;
60 _output = output;
61
62 assert(_activation == ir::Activation::NONE || _activation == ir::Activation::RELU ||
63 _activation == ir::Activation::RELU1 || _activation == ir::Activation::RELU6);
64}

References onert::ir::NONE, onert::ir::RELU, onert::ir::RELU1, and onert::ir::RELU6.

◆ create()

bool onert::backend::xnnpack::ops::ConvolutionLayer::create ( )
overridevirtual

Implements onert::backend::xnnpack::ops::Layer.

Definition at line 89 of file ConvolutionLayer.cc.

90{
91 float output_activation_min = 0.f, output_activation_max = 0.f;
92 CalculateActivationRange<float>(_activation, &output_activation_min, &output_activation_max);
93
94 // NHWC
95 // Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
96 const auto &kernel_shape = _kernel->getShape();
97 uint32_t kernel_height = kernel_shape.dim(1);
98 uint32_t kernel_width = kernel_shape.dim(2);
99 uint32_t output_channels = kernel_shape.dim(0);
100 uint32_t input_channels = kernel_shape.dim(3);
101 assert(static_cast<uint32_t>(_input->getShape().dim(3)) == input_channels);
102 assert(static_cast<uint32_t>(_output->getShape().dim(3)) == output_channels);
103
104 enum xnn_status status = xnn_create_convolution2d_nhwc_f32(
105 _padding_top, _padding_right, _padding_bottom, _padding_left, kernel_height, kernel_width,
106 _stride_height, _stride_width, _dilation_height_factor, _dilation_width_factor, 1 /* groups */,
107 input_channels /* group_input_channels */, output_channels /* group_output_channels */,
108 input_channels /* input_channel_stride */, output_channels /* output_channel_stride */,
109 reinterpret_cast<const float *>(_kernel->buffer()),
110 reinterpret_cast<const float *>(_bias->buffer()), output_activation_min, output_activation_max,
111 0, nullptr, nullptr, &_kernel_op);
112 if (status != xnn_status_success)
113 {
114 throw std::runtime_error{"failed to create FP32 Convolution operator"};
115 }
116 assert(_kernel_op != nullptr);
117 return true;
118}
ir::Shape getShape() const override final
Get ir::Shape of tensor.
virtual uint8_t * buffer() const =0

References onert::backend::xnnpack::ops::Layer::_kernel_op, onert::backend::ITensor::buffer(), and onert::backend::IPortableTensor::getShape().

◆ run()

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

Implements onert::exec::IFunction.

Definition at line 66 of file ConvolutionLayer.cc.

67{
68 assert(_external_context && _external_context->getThreadPool());
69 if (!_setup)
70 {
71 _setup = setup();
72 assert(_setup);
73 }
74
75 if (_input->data_type() == OperandType::FLOAT32)
76 {
77 enum xnn_status status = xnn_run_operator(_kernel_op, _external_context->getThreadPool());
78 if (status != xnn_status_success)
79 {
80 throw std::runtime_error{"failed to run FP32 Convolution operator"};
81 }
82 }
83 else
84 {
85 throw std::runtime_error{"XNNPACK Conv: unsupported data type"};
86 }
87}
ir::DataType data_type() const override final
const std::shared_ptr< ExternalContext > _external_context
Definition Layer.h:73

References onert::backend::xnnpack::ops::Layer::_external_context, onert::backend::xnnpack::ops::Layer::_kernel_op, onert::backend::xnnpack::ops::Layer::_setup, onert::backend::IPortableTensor::data_type(), and setup().

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

◆ setup()

bool onert::backend::xnnpack::ops::ConvolutionLayer::setup ( )
overridevirtual

Implements onert::backend::xnnpack::ops::Layer.

Definition at line 120 of file ConvolutionLayer.cc.

121{
122 if (_input->buffer() == nullptr || _output->buffer() == nullptr)
123 {
124 // it could be models's input or output
125 return false;
126 }
127
128 uint32_t input_width = _input->getShape().dim(2);
129 uint32_t input_height = _input->getShape().dim(1);
130 uint32_t batch_size = _input->getShape().dim(0);
131 size_t workspace_size = 0;
132 size_t workspace_alignment = 0;
133 enum xnn_status status = xnn_reshape_convolution2d_nhwc_f32(
134 _kernel_op, batch_size, input_height, input_width, &workspace_size, &workspace_alignment,
135 nullptr, nullptr, _external_context->getThreadPool());
136 if (status != xnn_status_success)
137 {
138 throw std::runtime_error{"failed to create FP32 DepthwiseConvolution operator"};
139 }
140
141 std::vector<uint8_t> workspace(workspace_size);
142 status = xnn_setup_convolution2d_nhwc_f32(_kernel_op, workspace.data(),
143 reinterpret_cast<const float *>(_input->buffer()),
144 reinterpret_cast<float *>(_output->buffer()));
145 if (status != xnn_status_success)
146 {
147 throw std::runtime_error{"failed to create FP32 Convolution operator"};
148 }
149 return true;
150}

References onert::backend::xnnpack::ops::Layer::_external_context, onert::backend::xnnpack::ops::Layer::_kernel_op, onert::backend::ITensor::buffer(), and onert::backend::IPortableTensor::getShape().

Referenced by run().


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