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

#include <FullyConnectedLayer.h>

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

Public Member Functions

 FullyConnectedLayer (const std::shared_ptr< ExternalContext > external_context)
 
void configure (const IPortableTensor *input, const IPortableTensor *_kernel, const IPortableTensor *bias, 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 FullyConnectedLayer.h.

Constructor & Destructor Documentation

◆ FullyConnectedLayer()

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

Definition at line 30 of file FullyConnectedLayer.cc.

31 : Layer(external_context), _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
32 _activation(ir::Activation::NONE)
33{
34 // DO NOTHING
35}
Layer(const std::shared_ptr< ExternalContext > external_context)
Definition Layer.h:43

Member Function Documentation

◆ configure()

void onert::backend::xnnpack::ops::FullyConnectedLayer::configure ( const IPortableTensor input,
const IPortableTensor _kernel,
const IPortableTensor bias,
ir::Activation  activation,
IPortableTensor output 
)

Definition at line 37 of file FullyConnectedLayer.cc.

40{
41 _input = input;
42 _kernel = weights;
43 _bias = bias;
44 _activation = activation;
45 _output = output;
46
47 assert(_activation == ir::Activation::NONE || _activation == ir::Activation::RELU ||
48 _activation == ir::Activation::RELU1 || _activation == ir::Activation::RELU6);
49}

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

◆ create()

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

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

Definition at line 74 of file FullyConnectedLayer.cc.

75{
76 float output_activation_min = 0.f, output_activation_max = 0.f;
77 CalculateActivationRange<float>(_activation, &output_activation_min, &output_activation_max);
78
79 const auto &kernel_shape = _kernel->getShape();
80 assert(kernel_shape.rank() == 2);
81 uint32_t output_channels = kernel_shape.dim(0);
82 uint32_t input_channels = kernel_shape.dim(1);
83
84 const auto &input_shape = _input->getShape();
85 const auto &output_shape = _output->getShape();
86 uint32_t flag = 0;
87 if (input_shape.rank() != output_shape.rank())
88 {
89 flag |= XNN_FLAG_TENSORFLOW_RESHAPE_2D;
90 assert(input_shape.num_elements() % input_channels == 0);
91 }
92 else
93 {
94 assert(static_cast<uint32_t>(input_shape.dim(input_shape.rank() - 1)) == input_channels);
95 }
96
97 assert(_kernel && _kernel->buffer());
98 const float *kernel_buffer = reinterpret_cast<const float *>(_kernel->buffer());
99 const float *bias_buffer = (_bias) ? reinterpret_cast<const float *>(_bias->buffer()) : nullptr;
100
101 enum xnn_status status = xnn_create_fully_connected_nc_f32(
102 input_channels, output_channels, input_channels /* input stride */,
103 output_channels /* output stride */, kernel_buffer, bias_buffer, output_activation_min,
104 output_activation_max, flag, nullptr, nullptr, &_kernel_op);
105 if (status != xnn_status_success)
106 {
107 throw std::runtime_error{"failed to create FP32 FullyConnected operator"};
108 }
109 assert(_kernel_op != nullptr);
110 return true;
111}
ir::Shape getShape() const override final
Get ir::Shape of tensor.
virtual uint8_t * buffer() const =0
const luci_interpreter::RuntimeShape output_shape

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

◆ run()

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

Implements onert::exec::IFunction.

Definition at line 51 of file FullyConnectedLayer.cc.

52{
53 assert(_external_context && _external_context->getThreadPool());
54 if (!_setup)
55 {
56 _setup = setup();
57 assert(_setup);
58 }
59
60 if (_input->data_type() == OperandType::FLOAT32)
61 {
62 enum xnn_status status = xnn_run_operator(_kernel_op, _external_context->getThreadPool());
63 if (status != xnn_status_success)
64 {
65 throw std::runtime_error{"failed to run FP32 FullyConnected operator"};
66 }
67 }
68 else
69 {
70 throw std::runtime_error{"XNNPACK FC: unsupported data type"};
71 }
72}
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::FullyConnectedLayer::setup ( )
overridevirtual

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

Definition at line 113 of file FullyConnectedLayer.cc.

114{
115 if (_input->buffer() == nullptr || _output->buffer() == nullptr)
116 {
117 // it could be models's input or output
118 return false;
119 }
120
121 uint32_t batch_size = _input->getShape().num_elements() / _kernel->getShape().dim(1);
122 enum xnn_status status =
123 xnn_reshape_fully_connected_nc_f32(_kernel_op, batch_size, _external_context->getThreadPool());
124 if (status != xnn_status_success)
125 {
126 throw std::runtime_error{"failed to create FP32 FullyConnected operator"};
127 }
128
129 status =
130 xnn_setup_fully_connected_nc_f32(_kernel_op, reinterpret_cast<const float *>(_input->buffer()),
131 reinterpret_cast<float *>(_output->buffer()));
132 if (status != xnn_status_success)
133 {
134 throw std::runtime_error{"failed to create FP32 FullyConnected operator"};
135 }
136 return true;
137}

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: