blob: 8d244cb5153c0804db01c41681fe90c2b7fed98b [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +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{
36template <typename FullyConnectedType, typename TensorType, Hint hint>
37std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output)
38{
39 bool weights_are_loaded = weights.tensor() != nullptr;
40 bool biases_are_loaded = biases.tensor() != nullptr;
41
42 auto conv = arm_compute::support::cpp14::make_unique<FullyConnectedType>();
43 conv->configure(
44 dynamic_cast<TensorType *>(input),
45 dynamic_cast<TensorType *>(weights.set_target(hint)),
46 dynamic_cast<TensorType *>(biases.set_target(hint)),
47 dynamic_cast<TensorType *>(output));
48 if(!weights_are_loaded)
49 {
50 weights.allocate_and_fill_if_needed();
51 }
52 if(!biases_are_loaded)
53 {
54 biases.allocate_and_fill_if_needed();
55 }
56
57 return std::move(conv);
58}
59
60template <Hint hint>
61std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output);
62
63template <>
64std::unique_ptr<arm_compute::IFunction> instantiate<Hint::OPENCL>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output)
65{
66 return instantiate_function<arm_compute::CLFullyConnectedLayer, arm_compute::CLTensor, Hint::OPENCL>(input, weights, biases, output);
67}
68
69template <>
70std::unique_ptr<arm_compute::IFunction> instantiate<Hint::NEON>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output)
71{
72 return instantiate_function<arm_compute::NEFullyConnectedLayer, arm_compute::Tensor, Hint::NEON>(input, weights, biases, output);
73}
74} // namespace
75
76std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(Hint hint, ITensor *input, ITensor *output)
77{
78 if(_weights.tensor() == nullptr)
79 {
80 unsigned int num_weights = 1;
81 unsigned int num_dimensions = input->info()->num_dimensions();
82 // Ignore the batch dimension if there is one:
83 if(num_dimensions == 2 || num_dimensions == 4)
84 {
85 num_dimensions--;
86 }
87 for(unsigned int i = 0; i < num_dimensions; i++)
88 {
89 num_weights *= input->info()->dimension(i);
90 }
91 _weights.set_info(TensorInfo(TensorShape(num_weights, _num_neurons), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
92 }
93 if(_biases.tensor() == nullptr)
94 {
95 _biases.set_info(TensorInfo(TensorShape(_num_neurons), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
96 }
97
98 arm_compute::auto_init_if_empty(*output->info(), TensorShape(_num_neurons, input->info()->dimension(1)), input->info()->num_channels(), input->info()->data_type(),
99 input->info()->fixed_point_position());
100
101 std::unique_ptr<arm_compute::IFunction> func;
102 _hint = hint;
103 _input = input;
104 _output = output;
105
106 if(_hint == Hint::OPENCL)
107 {
108 func = instantiate<Hint::OPENCL>(input, _weights, _biases, output);
109 }
110 else
111 {
112 func = instantiate<Hint::NEON>(input, _weights, _biases, output);
113 }
114
115 return func;
116}
117
118void FullyConnectedLayer::print_info()
119{
120 if(_hint == Hint::OPENCL)
121 {
122 std::cout << "Instantiating CLFullyConnectedLayer";
123 }
124 else
125 {
126 std::cout << "Instantiating NEFullyConnectedLayer";
127 }
128 std::cout << " Type: " << _input->info()->data_type() << " Input Shape: " << _input->info()->tensor_shape() << " Weights shape: " << _weights.info().tensor_shape() << " Biases Shape: " <<
129 _biases.info().tensor_shape() << " Output Shape: " << _output->info()->tensor_shape() << std::endl;
130}