ONE - On-device Neural Engine
Loading...
Searching...
No Matches
FullyConnectedLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "FullyConnectedLayer.h"
18
19#include "ir/Padding.h"
20
21namespace onert
22{
23namespace backend
24{
25namespace xnnpack
26{
27namespace ops
28{
29
30FullyConnectedLayer::FullyConnectedLayer(const std::shared_ptr<ExternalContext> external_context)
31 : Layer(external_context), _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
32 _activation(ir::Activation::NONE)
33{
34 // DO NOTHING
35}
36
38 const IPortableTensor *bias, ir::Activation activation,
39 IPortableTensor *output)
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}
50
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}
73
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}
112
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}
138
139} // namespace ops
140} // namespace xnnpack
141} // namespace backend
142} // namespace onert
A tensor class that is portable for other backends.
ir::DataType data_type() const override final
ir::Shape getShape() const override final
Get ir::Shape of tensor.
virtual uint8_t * buffer() const =0
FullyConnectedLayer(const std::shared_ptr< ExternalContext > external_context)
void configure(const IPortableTensor *input, const IPortableTensor *_kernel, const IPortableTensor *bias, ir::Activation activation, IPortableTensor *output)
const std::shared_ptr< ExternalContext > _external_context
Definition Layer.h:73
const luci_interpreter::RuntimeShape output_shape