blob: 299a027b42a970e41b7093582fd1a59483de997b [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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/runtime/GLES_COMPUTE/functions/GCFullyConnectedLayer.h"
25
26#include "arm_compute/core/Validate.h"
27#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010028
29#include <algorithm>
30
31using namespace arm_compute;
32
33void GCFullyConnectedLayerReshapeWeights::configure(const IGCTensor *input, IGCTensor *output)
34{
Georgios Pinitas40f51a62020-11-21 03:04:18 +000035 auto k = std::make_unique<GCTransposeKernel>();
Anthony Barbier7068f992017-10-26 15:23:08 +010036 k->configure(input, output);
37 _kernel = std::move(k);
38}
39
Michalis Spyrou1a569a32019-09-10 17:20:34 +010040GCFullyConnectedLayer::GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Michalis Spyrou6bff1952019-10-02 17:22:11 +010041 : _memory_group(std::move(memory_manager)), _weights_manager(std::move(weights_manager)), _im2col_kernel(), _reshape_weights_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(),
42 _reshape_weights_output(), _original_weights(nullptr), _are_weights_reshaped(true), _is_fc_after_conv(true), _accumulate_biases(false)
Anthony Barbier7068f992017-10-26 15:23:08 +010043{
44}
45
46void GCFullyConnectedLayer::configure_conv_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output)
47{
48 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
49
50 const DataType dt = input->info()->data_type();
51
52 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
53
54 // Initialize output tensor for im2col
55 TensorShape shape_im2col;
56 shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2));
57 shape_im2col.set(1, input->info()->dimension(3));
58 shape_im2col.set(2, input->info()->dimension(4));
59 shape_im2col.set(3, input->info()->dimension(5));
60 _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt));
61
62 // Configure im2col kernel
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000063 _memory_group.manage(&_im2col_output);
Stephen Lie855c232018-01-04 14:13:22 +080064 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false);
Anthony Barbier7068f992017-10-26 15:23:08 +010065
66 // Configure matrix multiply kernel
67 _mm_kernel.configure(&_im2col_output, weights, output, 1.0f, false);
68
69 // Allocate the output tensor for im2col once all the configure methods have been called
70 _im2col_output.allocator()->allocate();
71}
72
73void GCFullyConnectedLayer::configure_fc_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output)
74{
75 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
76
77 // Configure matrix multiply kernel
78 _mm_kernel.configure(input, weights, output, 1.0f, false);
79}
80
Georgios Pinitasceff0f92018-03-19 19:57:01 +000081void GCFullyConnectedLayer::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010082 FullyConnectedLayerInfo fc_info)
Anthony Barbier7068f992017-10-26 15:23:08 +010083{
84 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
85 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
86 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 2);
87
Georgios Pinitas72219332018-06-05 14:56:06 +010088 _original_weights = weights;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010089 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
Anthony Barbier7068f992017-10-26 15:23:08 +010090 _is_fc_after_conv = true;
91 _accumulate_biases = false;
92
93 if(biases != nullptr)
94 {
95 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
96
97 _accumulate_biases = true;
98
99 // Configure accumulate biases kernel
100 _accumulate_biases_kernel.configure(output, biases);
101 }
102
103 // With the Fully Connected layer we can have 4 different cases:
104 // 1) Convolution layer -> Fully Connected layer without batches
105 // 2) Fully Connected layer -> Fully Connected layer without batches
106 // 3) Convolution layer -> Fully Connected layer with batches
107 // 4) Fully Connected layer -> Fully Connected layer with batches
108
109 const IGCTensor *weights_to_use = weights;
110
111 if(!_are_weights_reshaped)
112 {
113 weights_to_use = &_reshape_weights_output;
114
115 // Reshape the weights
116 _reshape_weights_kernel.configure(weights, &_reshape_weights_output);
117 }
118
119 // Check if we have a fully connected layer with batches
120 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
121
122 if(is_batched_fc_layer)
123 {
124 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
125 input->info()->tensor_shape().cend(),
126 output->info()->tensor_shape().cbegin() + 1));
127 }
128 else
129 {
130 _is_fc_after_conv = input->info()->num_dimensions() > 1;
131 }
132
133 if(_is_fc_after_conv)
134 {
135 // Fully Connected layer after a Convolution Layer without batches
136 configure_conv_fc(input, weights_to_use, output);
137 }
138 else
139 {
140 // Fully Connected layer after a Fully Connected Layer without batches
141 configure_fc_fc(input, weights_to_use, output);
142 }
143
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100144 ARM_COMPUTE_ERROR_ON(fc_info.retain_internal_weights && _reshape_weights_output.gc_buffer() == 0);
145 _are_weights_reshaped = _are_weights_reshaped || fc_info.retain_internal_weights;
Anthony Barbier7068f992017-10-26 15:23:08 +0100146}
147
148void GCFullyConnectedLayer::run()
149{
Georgios Pinitas72219332018-06-05 14:56:06 +0100150 prepare();
Anthony Barbier7068f992017-10-26 15:23:08 +0100151
Georgios Pinitasda953f22019-04-02 17:27:03 +0100152 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitasceff0f92018-03-19 19:57:01 +0000153
Anthony Barbier7068f992017-10-26 15:23:08 +0100154 // Linearize input if it comes from a convolutional layer
155 if(_is_fc_after_conv)
156 {
Joel Liangeb8f71e2017-12-27 13:16:00 +0800157 GCScheduler::get().dispatch(_im2col_kernel, false);
Anthony Barbier7068f992017-10-26 15:23:08 +0100158 }
159
Joel Liangeb8f71e2017-12-27 13:16:00 +0800160 if(!_are_weights_reshaped || _is_fc_after_conv)
161 {
162 GCScheduler::get().memory_barrier();
163 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100164
165 // Run matrix multiply
Joel Liangeb8f71e2017-12-27 13:16:00 +0800166 GCScheduler::get().dispatch(_mm_kernel, !_accumulate_biases);
Anthony Barbier7068f992017-10-26 15:23:08 +0100167
168 // Accumulate biases if provided
169 if(_accumulate_biases)
170 {
Joel Liangeb8f71e2017-12-27 13:16:00 +0800171 GCScheduler::get().memory_barrier();
Anthony Barbier7068f992017-10-26 15:23:08 +0100172
Joel Liangeb8f71e2017-12-27 13:16:00 +0800173 GCScheduler::get().dispatch(_accumulate_biases_kernel);
Anthony Barbier7068f992017-10-26 15:23:08 +0100174 }
Anthony Barbier7068f992017-10-26 15:23:08 +0100175}
Georgios Pinitas72219332018-06-05 14:56:06 +0100176
177void GCFullyConnectedLayer::prepare()
178{
179 // Reshape of the weights (happens only once)
180 if(!_are_weights_reshaped)
181 {
182 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
183
184 // Run reshape weights kernel and mark weights as unused
185 _reshape_weights_output.allocator()->allocate();
186 _reshape_weights_kernel.run();
187
188 // Mark original weights tensor as unused
189 _original_weights->mark_as_unused();
190
191 _are_weights_reshaped = true;
192 }
Matthew Bentham92046462020-03-07 22:15:55 +0000193}