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 "../Tensor.h"
21#include <ruy/TensorUtils.h>
22
23namespace onert
24{
25namespace backend
26{
27namespace ruy
28{
29namespace ops
30{
31
33 : _input(nullptr), _weights(nullptr), _bias(nullptr), _output(nullptr),
34 _activation(ir::Activation::NONE), _external_context(nullptr)
35{
36 // DO NOTHING
37}
38
40
42{
43 float output_activation_min = 0, output_activation_max = 0;
44 CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
46
47 op_params.float_activation_min = output_activation_min;
48 op_params.float_activation_max = output_activation_max;
49 op_params.activation = convertActivationType(_activation);
50 op_params.lhs_cacheable = _weights->is_constant();
51 op_params.rhs_cacheable = _input->is_constant();
52
54 op_params, getTensorShape(_input), reinterpret_cast<const float *>(_input->buffer()),
55 getTensorShape(_weights), reinterpret_cast<const float *>(_weights->buffer()),
56 getTensorShape(_bias), reinterpret_cast<const float *>(_bias ? _bias->buffer() : nullptr),
57 getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()),
58 _external_context->ruy_context());
59}
60
62 const IPortableTensor *bias, ir::Activation activation,
63 IPortableTensor *output,
64 const std::shared_ptr<ExternalContext> &external_context)
65{
66 _input = input;
67 _weights = weights;
68 _bias = bias;
69 _activation = activation;
70 _output = output;
71 _external_context = external_context;
72}
73
75{
76 if (_input->data_type() == OperandType::FLOAT32)
77 {
79 }
80 else
81 {
82 throw std::runtime_error{"FullyConnected: unsupported data type"};
83 }
84}
85
87{
88 if (_bias && _bias->is_constant())
89 {
90 const int bias_size = getTensorShape(_bias).FlatSize();
91 if (nnfw::ruy::IsZeroVector(reinterpret_cast<float *>(_bias->buffer()), bias_size))
92 {
93 _bias = nullptr;
94 }
95 }
96}
97
98} // namespace ops
99} // namespace ruy
100} // namespace backend
101} // namespace onert
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