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 "../KernelGenerator.h"
20#include "../Tensor.h"
21#include "../Validator.h"
22
24#include <ruy/TensorUtils.h>
25
26namespace onert::backend::ruy
27{
28
29void Validator::visit(const ir::operation::FullyConnected &) { _supported = true; }
30
31void KernelGenerator::visit(const ir::operation::FullyConnected &node)
32{
33 using ir::operation::FullyConnected;
34
35 const auto output_index{node.getOutputs().at(0)};
36 const auto input_index{node.getInputs().at(FullyConnected::Input::INPUT)};
37 const auto weight_index{node.getInputs().at(FullyConnected::Input::WEIGHT)};
38 const auto bias_index{node.getInputs().at(FullyConnected::Input::BIAS)};
39 const auto activation = node.param().activation;
40 const auto weights_format = node.param().weights_format;
41 if (weights_format != ir::FullyConnectedWeightsFormat::Default)
42 throw std::runtime_error("Unsupported FullyConnected Weights Format");
43
44 auto output_tensor = _tensor_reg->getPortableTensor(output_index);
45 auto input_tensor = _tensor_reg->getPortableTensor(input_index);
46 auto weight_tensor = _tensor_reg->getPortableTensor(weight_index);
47 auto bias_tensor = bias_index.undefined() ? nullptr : _tensor_reg->getPortableTensor(bias_index);
48
49 auto fn = std::make_unique<ops::FullyConnectedLayer>();
50
51 fn->configure(input_tensor, weight_tensor, bias_tensor, activation, output_tensor,
52 _external_context);
53
54 _return_fn = std::move(fn);
55}
56
57} // namespace onert::backend::ruy
58
60{
61
63 : _input(nullptr), _weights(nullptr), _bias(nullptr), _output(nullptr),
64 _activation(ir::Activation::NONE), _external_context(nullptr)
65{
66 // DO NOTHING
67}
68
70
72{
73 float output_activation_min = 0, output_activation_max = 0;
74 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
76
77 op_params.float_activation_min = output_activation_min;
78 op_params.float_activation_max = output_activation_max;
79 op_params.activation = convertActivationType(_activation);
80 op_params.lhs_cacheable = _weights->is_constant();
81 op_params.rhs_cacheable = _input->is_constant();
82
84 op_params, getTensorShape(_input), reinterpret_cast<const float *>(_input->buffer()),
85 getTensorShape(_weights), reinterpret_cast<const float *>(_weights->buffer()),
86 getTensorShape(_bias), reinterpret_cast<const float *>(_bias ? _bias->buffer() : nullptr),
87 getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()),
88 _external_context->ruy_context());
89}
90
92 const IPortableTensor *bias, ir::Activation activation,
93 IPortableTensor *output,
94 const std::shared_ptr<ExternalContext> &external_context)
95{
96 _input = input;
97 _weights = weights;
98 _bias = bias;
99 _activation = activation;
100 _output = output;
101 _external_context = external_context;
102}
103
105{
106 if (_input->data_type() == OperandType::FLOAT32)
107 {
109 }
110 else
111 {
112 throw std::runtime_error{"FullyConnected: unsupported data type"};
113 }
114}
115
117{
118 if (_bias && _bias->is_constant())
119 {
120 const int bias_size = getTensorShape(_bias).FlatSize();
121 if (nnfw::ruy::IsZeroVector(reinterpret_cast<float *>(_bias->buffer()), bias_size))
122 {
123 _bias = nullptr;
124 }
125 }
126}
127
128} // namespace onert::backend::ruy::ops
int FlatSize() const
Definition Shape.h:181
A tensor class that is portable for other backends.
ir::DataType data_type() const override final
bool is_constant() const override final
Return true if the tensor is constant.
virtual uint8_t * buffer() const =0
std::unique_ptr< exec::IFunction > _return_fn
void configure(const IPortableTensor *input, const IPortableTensor *weights, const IPortableTensor *bias, ir::Activation activation, IPortableTensor *output, const std::shared_ptr< ExternalContext > &external_context)
void FullyConnected(const FullyConnectedParams &params, const Shape &input_shape, const float *input_data, const Shape &weights_shape, const float *weights_data, const Shape &, const float *optional_bias_data, const Shape &output_shape, float *output_data, ::ruy::Context *ruy_context)
bool IsZeroVector(const float *vector, int v_size)
Definition TensorUtils.h:29
nnfw::ruy::Shape getTensorShape(const IPortableTensor *tensor)
nnfw::ruy::FusedActivationFunctionType convertActivationType(const ir::Activation activation)
void CalculateActivationRange(ir::Activation activation, T *activation_min, T *activation_max)
CLTensor bias_tensor
FusedActivationFunctionType activation
Definition Types.h:83