blob: 5f4807ad48e5adffc682618fb0a8ec89c252f990 [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
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
28#include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h"
29#include "support/ToolchainSupport.h"
30#include "utils/TypePrinter.h"
31
32using namespace arm_compute::graph;
33
34namespace
35{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010036TensorShape calculate_fullyconnected_layer_output_shape(const TensorShape &input_shape, unsigned int output_neurons)
37{
38 // Note: Only 1D batch space is supported at the moment
39 unsigned int batches = input_shape[1];
40 if(input_shape.num_dimensions() > 2)
41 {
42 batches = input_shape[3];
43 }
44 return TensorShape(output_neurons, batches);
45}
Georgios Pinitasff421f22017-10-04 16:53:58 +010046template <typename FullyConnectedType, typename TensorType, TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010047std::unique_ptr<arm_compute::IFunction> instantiate_function(arm_compute::ITensor *input, Tensor &weights, Tensor &biases, arm_compute::ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010048{
49 bool weights_are_loaded = weights.tensor() != nullptr;
50 bool biases_are_loaded = biases.tensor() != nullptr;
51
52 auto conv = arm_compute::support::cpp14::make_unique<FullyConnectedType>();
53 conv->configure(
54 dynamic_cast<TensorType *>(input),
Georgios Pinitasff421f22017-10-04 16:53:58 +010055 dynamic_cast<TensorType *>(weights.set_target(target_hint)),
56 dynamic_cast<TensorType *>(biases.set_target(target_hint)),
Anthony Barbier2a07e182017-08-04 18:20:27 +010057 dynamic_cast<TensorType *>(output));
58 if(!weights_are_loaded)
59 {
60 weights.allocate_and_fill_if_needed();
61 }
62 if(!biases_are_loaded)
63 {
64 biases.allocate_and_fill_if_needed();
65 }
66
67 return std::move(conv);
68}
69
Georgios Pinitasff421f22017-10-04 16:53:58 +010070template <TargetHint target_hint>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010071std::unique_ptr<arm_compute::IFunction> instantiate(arm_compute::ITensor *input, Tensor &weights, Tensor &biases, arm_compute::ITensor *output);
Anthony Barbier2a07e182017-08-04 18:20:27 +010072
73template <>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010074std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(arm_compute::ITensor *input, Tensor &weights, Tensor &biases, arm_compute::ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010075{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010076 return instantiate_function<arm_compute::CLFullyConnectedLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, weights, biases, output);
Anthony Barbier2a07e182017-08-04 18:20:27 +010077}
78
79template <>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010080std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(arm_compute::ITensor *input, Tensor &weights, Tensor &biases, arm_compute::ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010081{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010082 return instantiate_function<arm_compute::NEFullyConnectedLayer, arm_compute::ITensor, TargetHint::NEON>(input, weights, biases, output);
Anthony Barbier2a07e182017-08-04 18:20:27 +010083}
84} // namespace
85
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010086std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010087{
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010088 ARM_COMPUTE_ERROR_ON(input == nullptr || input->tensor() == nullptr);
89 ARM_COMPUTE_ERROR_ON(output == nullptr || output->tensor() == nullptr);
90
91 arm_compute::ITensor *in = input->tensor();
92 arm_compute::ITensor *out = output->tensor();
93
Anthony Barbier2a07e182017-08-04 18:20:27 +010094 if(_weights.tensor() == nullptr)
95 {
96 unsigned int num_weights = 1;
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010097 unsigned int num_dimensions = in->info()->num_dimensions();
Anthony Barbier2a07e182017-08-04 18:20:27 +010098 // Ignore the batch dimension if there is one:
99 if(num_dimensions == 2 || num_dimensions == 4)
100 {
101 num_dimensions--;
102 }
103 for(unsigned int i = 0; i < num_dimensions; i++)
104 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100105 num_weights *= in->info()->dimension(i);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100106 }
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100107 _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 +0100108 }
109 if(_biases.tensor() == nullptr)
110 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100111 _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 +0100112 }
113
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100114 // Auto configure output
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100115 arm_compute::auto_init_if_empty(*out->info(),
116 calculate_fullyconnected_layer_output_shape(in->info()->tensor_shape(), _num_neurons),
117 in->info()->num_channels(), in->info()->data_type(), in->info()->fixed_point_position());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100118
119 std::unique_ptr<arm_compute::IFunction> func;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100120 _target_hint = ctx.hints().target_hint();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121
Georgios Pinitasff421f22017-10-04 16:53:58 +0100122 if(_target_hint == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100123 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100124 func = instantiate<TargetHint::OPENCL>(in, _weights, _biases, out);
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100125 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLFullyConnectedLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +0100126 }
127 else
128 {
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100129 func = instantiate<TargetHint::NEON>(in, _weights, _biases, out);
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100130 ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEFullyConnectedLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +0100131 }
132
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100133 ARM_COMPUTE_LOG_GRAPH_INFO(" Type: " << in->info()->data_type()
134 << " Input Shape: " << in->info()->tensor_shape()
135 << " Weights shape: " << _weights.info().tensor_shape()
136 << " Biases Shape: " << _biases.info().tensor_shape()
137 << " Output Shape: " << out->info()->tensor_shape()
138 << std::endl);
Michalis Spyroue4720822017-10-02 17:44:52 +0100139
Anthony Barbier2a07e182017-08-04 18:20:27 +0100140 return func;
141}