blob: 3742150d37f76e6ed5339f6fa63c37a713b76c80 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/graph/nodes/FullyConnectedLayer.h"
25
Michalis Spyroued194b12017-10-31 15:04:34 +000026#include "arm_compute/graph/Error.h"
Georgios Pinitas407c3e62017-10-25 18:26:46 +010027#include "arm_compute/graph/NodeContext.h"
28#include "arm_compute/graph/OperationRegistry.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010029#include "support/ToolchainSupport.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010030
31using namespace arm_compute::graph;
32
33namespace
34{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010035TensorShape calculate_fullyconnected_layer_output_shape(const TensorShape &input_shape, unsigned int output_neurons)
36{
37 // Note: Only 1D batch space is supported at the moment
38 unsigned int batches = input_shape[1];
39 if(input_shape.num_dimensions() > 2)
40 {
41 batches = input_shape[3];
42 }
43 return TensorShape(output_neurons, batches);
44}
Anthony Barbier2a07e182017-08-04 18:20:27 +010045} // namespace
46
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010047std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010048{
Michalis Spyroued194b12017-10-31 15:04:34 +000049 ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010050
51 arm_compute::ITensor *in = input->tensor();
52 arm_compute::ITensor *out = output->tensor();
Georgios Pinitas407c3e62017-10-25 18:26:46 +010053 _target_hint = ctx.hints().target_hint();
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010054
Anthony Barbier2a07e182017-08-04 18:20:27 +010055 if(_weights.tensor() == nullptr)
56 {
57 unsigned int num_weights = 1;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010058 unsigned int num_dimensions = in->info()->num_dimensions();
Anthony Barbier2a07e182017-08-04 18:20:27 +010059 // Ignore the batch dimension if there is one:
60 if(num_dimensions == 2 || num_dimensions == 4)
61 {
62 num_dimensions--;
63 }
64 for(unsigned int i = 0; i < num_dimensions; i++)
65 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010066 num_weights *= in->info()->dimension(i);
Anthony Barbier2a07e182017-08-04 18:20:27 +010067 }
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010068 _weights.set_info(TensorInfo(TensorShape(num_weights, _num_neurons), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
Anthony Barbier2a07e182017-08-04 18:20:27 +010069 }
70 if(_biases.tensor() == nullptr)
71 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010072 _biases.set_info(TensorInfo(TensorShape(_num_neurons), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
Anthony Barbier2a07e182017-08-04 18:20:27 +010073 }
74
Georgios Pinitas6f669f02017-09-26 12:32:57 +010075 // Auto configure output
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010076 arm_compute::auto_init_if_empty(*out->info(),
77 calculate_fullyconnected_layer_output_shape(in->info()->tensor_shape(), _num_neurons),
78 in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position());
Anthony Barbier2a07e182017-08-04 18:20:27 +010079
Georgios Pinitas407c3e62017-10-25 18:26:46 +010080 bool weights_are_loaded = _weights.tensor() != nullptr;
81 bool biases_are_loaded = _biases.tensor() != nullptr;
Anthony Barbier2a07e182017-08-04 18:20:27 +010082
Georgios Pinitas407c3e62017-10-25 18:26:46 +010083 // Create node context
84 NodeContext node_ctx(OperationType::FullyConnectedLayer);
85 node_ctx.set_target(_target_hint);
86 node_ctx.add_input(in);
87 node_ctx.add_input(_weights.set_target(_target_hint));
88 node_ctx.add_input(_biases.set_target(_target_hint));
89 node_ctx.add_output(out);
90
Georgios Pinitas284c2042017-12-11 17:49:18 +000091 // Configure operation
92 auto func = OperationRegistry::get().find_operation(OperationType::FullyConnectedLayer, _target_hint)->configure(node_ctx);
93
Georgios Pinitas407c3e62017-10-25 18:26:46 +010094 // Fill biases
95 if(!weights_are_loaded)
Anthony Barbier2a07e182017-08-04 18:20:27 +010096 {
Georgios Pinitas407c3e62017-10-25 18:26:46 +010097 _weights.allocate_and_fill_if_needed();
Anthony Barbier2a07e182017-08-04 18:20:27 +010098 }
Georgios Pinitas407c3e62017-10-25 18:26:46 +010099 if(!biases_are_loaded)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100100 {
Georgios Pinitas407c3e62017-10-25 18:26:46 +0100101 _biases.allocate_and_fill_if_needed();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100102 }
103
Georgios Pinitas407c3e62017-10-25 18:26:46 +0100104 // Get function
Georgios Pinitas284c2042017-12-11 17:49:18 +0000105 return func;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100106}