blob: 3742150d37f76e6ed5339f6fa63c37a713b76c80 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +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
Anthony Barbier8140e1e2017-12-14 23:48:46 +000026#include "arm_compute/graph/Error.h"
27#include "arm_compute/graph/NodeContext.h"
28#include "arm_compute/graph/OperationRegistry.h"
Kaizen8938bd32017-09-28 14:38:23 +010029#include "support/ToolchainSupport.h"
Kaizen8938bd32017-09-28 14:38:23 +010030
31using namespace arm_compute::graph;
32
33namespace
34{
Kaizenbf8b01d2017-10-12 14:26:51 +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}
Kaizen8938bd32017-09-28 14:38:23 +010045} // namespace
46
Anthony Barbier8140e1e2017-12-14 23:48:46 +000047std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Kaizen8938bd32017-09-28 14:38:23 +010048{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049 ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
50
51 arm_compute::ITensor *in = input->tensor();
52 arm_compute::ITensor *out = output->tensor();
53 _target_hint = ctx.hints().target_hint();
54
Kaizen8938bd32017-09-28 14:38:23 +010055 if(_weights.tensor() == nullptr)
56 {
57 unsigned int num_weights = 1;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000058 unsigned int num_dimensions = in->info()->num_dimensions();
Kaizen8938bd32017-09-28 14:38:23 +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 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +000066 num_weights *= in->info()->dimension(i);
Kaizen8938bd32017-09-28 14:38:23 +010067 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +000068 _weights.set_info(TensorInfo(TensorShape(num_weights, _num_neurons), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
Kaizen8938bd32017-09-28 14:38:23 +010069 }
70 if(_biases.tensor() == nullptr)
71 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +000072 _biases.set_info(TensorInfo(TensorShape(_num_neurons), in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position()));
Kaizen8938bd32017-09-28 14:38:23 +010073 }
74
Kaizenbf8b01d2017-10-12 14:26:51 +010075 // Auto configure output
Anthony Barbier8140e1e2017-12-14 23:48:46 +000076 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());
Kaizen8938bd32017-09-28 14:38:23 +010079
Anthony Barbier8140e1e2017-12-14 23:48:46 +000080 bool weights_are_loaded = _weights.tensor() != nullptr;
81 bool biases_are_loaded = _biases.tensor() != nullptr;
Kaizen8938bd32017-09-28 14:38:23 +010082
Anthony Barbier8140e1e2017-12-14 23:48:46 +000083 // 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 // Configure operation
92 auto func = OperationRegistry::get().find_operation(OperationType::FullyConnectedLayer, _target_hint)->configure(node_ctx);
93
94 // Fill biases
95 if(!weights_are_loaded)
Kaizen8938bd32017-09-28 14:38:23 +010096 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +000097 _weights.allocate_and_fill_if_needed();
Kaizen8938bd32017-09-28 14:38:23 +010098 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +000099 if(!biases_are_loaded)
Kaizen8938bd32017-09-28 14:38:23 +0100100 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000101 _biases.allocate_and_fill_if_needed();
Kaizen8938bd32017-09-28 14:38:23 +0100102 }
103
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104 // Get function
Kaizen8938bd32017-09-28 14:38:23 +0100105 return func;
106}