blob: 6b21810a36c260084ac109ee56c3c62f20dfbaab [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"
Michalis Spyroue4720822017-10-02 17:44:52 +010027#include "arm_compute/core/Logger.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010028#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
29#include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h"
30#include "support/ToolchainSupport.h"
31#include "utils/TypePrinter.h"
32
33using namespace arm_compute::graph;
34
35namespace
36{
Georgios Pinitas6f669f02017-09-26 12:32:57 +010037TensorShape calculate_fullyconnected_layer_output_shape(const TensorShape &input_shape, unsigned int output_neurons)
38{
39 // Note: Only 1D batch space is supported at the moment
40 unsigned int batches = input_shape[1];
41 if(input_shape.num_dimensions() > 2)
42 {
43 batches = input_shape[3];
44 }
45 return TensorShape(output_neurons, batches);
46}
Georgios Pinitasff421f22017-10-04 16:53:58 +010047template <typename FullyConnectedType, typename TensorType, TargetHint target_hint>
Anthony Barbier2a07e182017-08-04 18:20:27 +010048std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output)
49{
50 bool weights_are_loaded = weights.tensor() != nullptr;
51 bool biases_are_loaded = biases.tensor() != nullptr;
52
53 auto conv = arm_compute::support::cpp14::make_unique<FullyConnectedType>();
54 conv->configure(
55 dynamic_cast<TensorType *>(input),
Georgios Pinitasff421f22017-10-04 16:53:58 +010056 dynamic_cast<TensorType *>(weights.set_target(target_hint)),
57 dynamic_cast<TensorType *>(biases.set_target(target_hint)),
Anthony Barbier2a07e182017-08-04 18:20:27 +010058 dynamic_cast<TensorType *>(output));
59 if(!weights_are_loaded)
60 {
61 weights.allocate_and_fill_if_needed();
62 }
63 if(!biases_are_loaded)
64 {
65 biases.allocate_and_fill_if_needed();
66 }
67
68 return std::move(conv);
69}
70
Georgios Pinitasff421f22017-10-04 16:53:58 +010071template <TargetHint target_hint>
Anthony Barbier2a07e182017-08-04 18:20:27 +010072std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output);
73
74template <>
Georgios Pinitasff421f22017-10-04 16:53:58 +010075std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010076{
Georgios Pinitasff421f22017-10-04 16:53:58 +010077 return instantiate_function<arm_compute::CLFullyConnectedLayer, arm_compute::CLTensor, TargetHint::OPENCL>(input, weights, biases, output);
Anthony Barbier2a07e182017-08-04 18:20:27 +010078}
79
80template <>
Georgios Pinitasff421f22017-10-04 16:53:58 +010081std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010082{
Georgios Pinitasff421f22017-10-04 16:53:58 +010083 return instantiate_function<arm_compute::NEFullyConnectedLayer, arm_compute::Tensor, TargetHint::NEON>(input, weights, biases, output);
Anthony Barbier2a07e182017-08-04 18:20:27 +010084}
85} // namespace
86
Georgios Pinitasff421f22017-10-04 16:53:58 +010087std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(GraphContext &ctx, ITensor *input, ITensor *output)
Anthony Barbier2a07e182017-08-04 18:20:27 +010088{
89 if(_weights.tensor() == nullptr)
90 {
91 unsigned int num_weights = 1;
92 unsigned int num_dimensions = input->info()->num_dimensions();
93 // Ignore the batch dimension if there is one:
94 if(num_dimensions == 2 || num_dimensions == 4)
95 {
96 num_dimensions--;
97 }
98 for(unsigned int i = 0; i < num_dimensions; i++)
99 {
100 num_weights *= input->info()->dimension(i);
101 }
102 _weights.set_info(TensorInfo(TensorShape(num_weights, _num_neurons), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
103 }
104 if(_biases.tensor() == nullptr)
105 {
106 _biases.set_info(TensorInfo(TensorShape(_num_neurons), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
107 }
108
Georgios Pinitas6f669f02017-09-26 12:32:57 +0100109 // Auto configure output
110 arm_compute::auto_init_if_empty(*output->info(),
111 calculate_fullyconnected_layer_output_shape(input->info()->tensor_shape(), _num_neurons),
112 input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100113
114 std::unique_ptr<arm_compute::IFunction> func;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100115 _target_hint = ctx.hints().target_hint();
Anthony Barbier2a07e182017-08-04 18:20:27 +0100116
Georgios Pinitasff421f22017-10-04 16:53:58 +0100117 if(_target_hint == TargetHint::OPENCL)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100118 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100119 func = instantiate<TargetHint::OPENCL>(input, _weights, _biases, output);
Michalis Spyroue4720822017-10-02 17:44:52 +0100120 ARM_COMPUTE_LOG("Instantiating CLFullyConnectedLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121 }
122 else
123 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100124 func = instantiate<TargetHint::NEON>(input, _weights, _biases, output);
Michalis Spyroue4720822017-10-02 17:44:52 +0100125 ARM_COMPUTE_LOG("Instantiating NEFullyConnectedLayer");
Anthony Barbier2a07e182017-08-04 18:20:27 +0100126 }
127
Michalis Spyroue4720822017-10-02 17:44:52 +0100128 ARM_COMPUTE_LOG(" Type: " << input->info()->data_type()
129 << " Input Shape: " << input->info()->tensor_shape()
130 << " Weights shape: " << _weights.info().tensor_shape()
131 << " Biases Shape: " << _biases.info().tensor_shape()
132 << " Output Shape: " << output->info()->tensor_shape()
133 << std::endl);
134
Anthony Barbier2a07e182017-08-04 18:20:27 +0100135 return func;
136}