blob: 39ed8276312192863c2ece301c92c56d4e0df01c [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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
Georgios Pinitas407c3e62017-10-25 18:26:46 +010026#include "arm_compute/graph/NodeContext.h"
27#include "arm_compute/graph/OperationRegistry.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010028#include "support/ToolchainSupport.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010029
30using namespace arm_compute::graph;
31
32namespace
33{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010034TensorShape calculate_fullyconnected_layer_output_shape(const TensorShape &input_shape, unsigned int output_neurons)
35{
36 // Note: Only 1D batch space is supported at the moment
37 unsigned int batches = input_shape[1];
38 if(input_shape.num_dimensions() > 2)
39 {
40 batches = input_shape[3];
41 }
42 return TensorShape(output_neurons, batches);
43}
Anthony Barbier2a07e182017-08-04 18:20:27 +010044} // namespace
45
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010046std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010047{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010048 ARM_COMPUTE_ERROR_ON(input == nullptr || input->tensor() == nullptr);
49 ARM_COMPUTE_ERROR_ON(output == nullptr || output->tensor() == nullptr);
50
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
91 // Fill biases
92 if(!weights_are_loaded)
Anthony Barbier2a07e182017-08-04 18:20:27 +010093 {
Georgios Pinitas407c3e62017-10-25 18:26:46 +010094 _weights.allocate_and_fill_if_needed();
Anthony Barbier2a07e182017-08-04 18:20:27 +010095 }
Georgios Pinitas407c3e62017-10-25 18:26:46 +010096 if(!biases_are_loaded)
Anthony Barbier2a07e182017-08-04 18:20:27 +010097 {
Georgios Pinitas407c3e62017-10-25 18:26:46 +010098 _biases.allocate_and_fill_if_needed();
Anthony Barbier2a07e182017-08-04 18:20:27 +010099 }
100
Georgios Pinitas407c3e62017-10-25 18:26:46 +0100101 // Get function
102 return OperationRegistry::get().find_operation(OperationType::FullyConnectedLayer, _target_hint)->configure(node_ctx);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100103}