ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "../Tensor.h"
21#include <ruy/TensorUtils.h>
22
24{
25
27 : _input(nullptr), _weights(nullptr), _bias(nullptr), _output(nullptr),
28 _activation(ir::Activation::NONE), _external_context(nullptr)
29{
30 // DO NOTHING
31}
32
34
36{
37 float output_activation_min = 0, output_activation_max = 0;
38 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
40
41 op_params.float_activation_min = output_activation_min;
42 op_params.float_activation_max = output_activation_max;
43 op_params.activation = convertActivationType(_activation);
44 op_params.lhs_cacheable = _weights->is_constant();
45 op_params.rhs_cacheable = _input->is_constant();
46
48 op_params, getTensorShape(_input), reinterpret_cast<const float *>(_input->buffer()),
49 getTensorShape(_weights), reinterpret_cast<const float *>(_weights->buffer()),
50 getTensorShape(_bias), reinterpret_cast<const float *>(_bias ? _bias->buffer() : nullptr),
51 getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()),
52 _external_context->ruy_context());
53}
54
56 const IPortableTensor *bias, ir::Activation activation,
57 IPortableTensor *output,
58 const std::shared_ptr<ExternalContext> &external_context)
59{
60 _input = input;
61 _weights = weights;
62 _bias = bias;
63 _activation = activation;
64 _output = output;
65 _external_context = external_context;
66}
67
69{
70 if (_input->data_type() == OperandType::FLOAT32)
71 {
73 }
74 else
75 {
76 throw std::runtime_error{"FullyConnected: unsupported data type"};
77 }
78}
79
81{
82 if (_bias && _bias->is_constant())
83 {
84 const int bias_size = getTensorShape(_bias).FlatSize();
85 if (nnfw::ruy::IsZeroVector(reinterpret_cast<float *>(_bias->buffer()), bias_size))
86 {
87 _bias = nullptr;
88 }
89 }
90}
91
92} // 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
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)
FusedActivationFunctionType activation
Definition Types.h:83