blob: 2e8d10598d803439a8f8906ce1204d7f6e5c1fe8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44: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/runtime/NEON/functions/NEFullyConnectedLayer.h"
25
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010026#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Validate.h"
28#include "arm_compute/runtime/NEON/NEScheduler.h"
29
30#include <algorithm>
31#include <cmath>
32
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010033namespace arm_compute
34{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010035NEFullyConnectedLayerReshapeWeights::NEFullyConnectedLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
36 : _memory_group(std::move(memory_manager)), _transpose_kernel(), _transpose1xW_kernel(), _transpose_output(), _transpose_weights(false), _is_batched_fc_layer(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037{
38}
39
40void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output, bool transpose_weights, bool is_batched_fc_layer)
41{
Pablo Tellodcdc85e2017-06-28 10:05:29 +010042 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010043 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() > 2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 ARM_COMPUTE_ERROR_ON(output == nullptr);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010045 ARM_COMPUTE_ERROR_ON(!transpose_weights && !is_batched_fc_layer);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010047 const DataType data_type = input->info()->data_type();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048 const int fixed_point_position = input->info()->fixed_point_position();
49
50 _transpose_weights = transpose_weights;
51 _is_batched_fc_layer = is_batched_fc_layer;
52
53 // Check if we need to transpose the weights
54 if(_transpose_weights)
55 {
56 if(_is_batched_fc_layer)
57 {
58 // Initialize the output tensor for transpose
59 TensorShape shape_transposed(input->info()->dimension(1), input->info()->dimension(0));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010060 _transpose_output.allocator()->init(TensorInfo(shape_transposed, 1, data_type, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010061 _memory_group.manage(&_transpose_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 _transpose_kernel.configure(input, &_transpose_output);
63
64 // Configure transpose 1xW kernel
65 _transpose1xW_kernel.configure(&_transpose_output, output);
66
67 // Allocate temporary tensor used for transposing the weights
68 _transpose_output.allocator()->allocate();
69 }
70 else
71 {
72 _transpose_kernel.configure(input, output);
73 }
74 }
75 else
76 {
77 if(_is_batched_fc_layer)
78 {
79 // Configure transpose 1xW kernel
80 _transpose1xW_kernel.configure(input, output);
81 }
82 else
83 {
84 ARM_COMPUTE_ERROR("Configuration transpose_weights=false & is_batched_fc_layer=false not supported");
85 }
86 }
87}
88
89void NEFullyConnectedLayerReshapeWeights::run()
90{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010091 _memory_group.acquire();
92
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 if(_transpose_weights)
94 {
95 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
96 }
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010097
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 if(_is_batched_fc_layer)
99 {
100 NEScheduler::get().schedule(&_transpose1xW_kernel, Window::DimY);
101 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100102
103 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104}
105
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100106NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
107 : _memory_group(std::move(memory_manager)), _im2col_kernel(), _reshape_weights_kernel(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), _interleave4x4_output(),
108 _reshape_weights_output(), _are_weights_reshaped(false), _is_batched_fc_layer(false), _linearize_input(false), _accumulate_biases(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109{
110}
111
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose_weights, bool are_weights_reshaped)
113{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 // With the Fully Connected layer we can have 4 different cases:
115 // 1) Convolution layer -> Fully Connected layer without batches
116 // 2) Fully Connected layer -> Fully Connected layer without batches
117 // 3) Convolution layer -> Fully Connected layer with batches
118 // 4) Fully Connected layer -> Fully Connected layer with batches
119
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100120 // Expected shape before transpose and reshaping
121 // Input: In x B (In and B can be multi-dimensional)
122 // Weights: flat(In) x Out
123 // Biases: Out
124 // Output: Out x B (B can be multi-dimensional)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100126 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
127 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
128 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100130 const DataType data_type = input->info()->data_type();
131 const int fixed_point_position = input->info()->fixed_point_position();
132 const int num_batch_dimensions = std::max(0, static_cast<int>(output->info()->tensor_shape().num_dimensions()) - 1);
133 const int num_input_dimensions = input->info()->tensor_shape().num_dimensions() - num_batch_dimensions;
134 const size_t linear_input_size = input->info()->tensor_shape().total_size_lower(num_input_dimensions);
135
136 _linearize_input = input->info()->tensor_shape().x() != linear_input_size;
137 _are_weights_reshaped = are_weights_reshaped;
138 _accumulate_biases = biases != nullptr;
139 _is_batched_fc_layer = num_batch_dimensions > 0;
140
141 // Check if number of batches match
142 ARM_COMPUTE_ERROR_ON(input->info()->tensor_shape().total_size_upper(num_input_dimensions) != output->info()->tensor_shape().total_size_upper(1));
143 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 2);
144
145 const size_t interleave_width = 16 / input->info()->element_size();
146 const ITensor *weights_to_use = weights;
147
148 if(!are_weights_reshaped && (transpose_weights || _is_batched_fc_layer))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100150 weights_to_use = &_reshape_weights_output;
151
152 TensorShape reshaped_weights_shape(weights->info()->tensor_shape());
153
154 // Transpose weights if the user hasn't done it
155 if(transpose_weights)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100157 const size_t shape_x = reshaped_weights_shape.x();
158 reshaped_weights_shape.set(0, reshaped_weights_shape.y());
159 reshaped_weights_shape.set(1, shape_x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 }
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100161
162 // If the we run multiple batches we need 1xW transpose, too.
163 if(_is_batched_fc_layer)
164 {
165 const float shape_x = reshaped_weights_shape.x();
166 reshaped_weights_shape.set(0, reshaped_weights_shape.y() * interleave_width);
167 reshaped_weights_shape.set(1, static_cast<unsigned int>(std::ceil(shape_x / interleave_width)));
168 }
169
170 _reshape_weights_output.allocator()->init(TensorInfo(reshaped_weights_shape, 1, data_type, fixed_point_position));
171
172 // Reshape the weights
173 _reshape_weights_kernel.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer);
174 }
175
176 // Check correct shape of weights
177 if(_is_batched_fc_layer)
178 {
179 // Transpose + Transpose1xW
180 ARM_COMPUTE_ERROR_ON(weights_to_use->info()->tensor_shape().x() != linear_input_size * interleave_width);
181 ARM_COMPUTE_ERROR_ON(weights_to_use->info()->tensor_shape().y() != static_cast<unsigned int>(std::ceil(static_cast<float>(output->info()->tensor_shape().x()) / interleave_width)));
182 }
183 else
184 {
185 // Transpose
186 ARM_COMPUTE_ERROR_ON(weights_to_use->info()->tensor_shape().x() != output->info()->tensor_shape().x());
187 ARM_COMPUTE_ERROR_ON(weights_to_use->info()->tensor_shape().y() != linear_input_size);
188 }
189
190 const ITensor *multiply_input = input;
191
192 if(_linearize_input)
193 {
194 TensorShape shape_im2col(input->info()->tensor_shape());
195 shape_im2col.collapse(num_input_dimensions);
196 _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, data_type, fixed_point_position));
197
198 // Configure im2col kernel
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100199 _memory_group.manage(&_im2col_output);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100200 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false);
201
202 multiply_input = &_im2col_output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 }
204
205 if(_is_batched_fc_layer)
206 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100207 TensorShape shape_interleaved(multiply_input->info()->tensor_shape());
208 shape_interleaved.set(0, shape_interleaved.x() * 4);
209 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
210 _interleave4x4_output.allocator()->init(TensorInfo(shape_interleaved, 1, data_type, fixed_point_position));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100212 // Configure interleave4x4 kernel
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100213 _memory_group.manage(&_interleave4x4_output);
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100214 _interleave4x4_kernel.configure(multiply_input, &_interleave4x4_output);
215
216 multiply_input = &_interleave4x4_output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100219 // Configure matrix multiply kernel
220 _mm_kernel.configure(multiply_input, weights_to_use, output, 1.0f);
221
222 if(_accumulate_biases)
223 {
224 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
225 ARM_COMPUTE_ERROR_ON(biases->info()->tensor_shape().x() != output->info()->tensor_shape().x());
226
227 // Configure accumulate biases kernel
228 _accumulate_biases_kernel.configure(output, biases);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 }
230
231 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100232 if(!are_weights_reshaped && (transpose_weights || _is_batched_fc_layer))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100234 // Allocate the tensor for the weights reshaped
235 _reshape_weights_output.allocator()->allocate();
236 }
237
238 if(_linearize_input)
239 {
240 _im2col_output.allocator()->allocate();
241 }
242
243 if(_is_batched_fc_layer)
244 {
245 _interleave4x4_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246 }
247}
248
249void NEFullyConnectedLayer::run()
250{
251 // Reshape of the weights (happens only once)
252 if(!_are_weights_reshaped)
253 {
254 _are_weights_reshaped = true;
255 _reshape_weights_kernel.run();
256 }
257
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100258 _memory_group.acquire();
259
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100260 // Linearize input if it comes from a convolutional layer
261 if(_linearize_input)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 {
263 NEScheduler::get().schedule(&_im2col_kernel, Window::DimY);
264 }
265
266 // Interleave input
267 if(_is_batched_fc_layer)
268 {
269 NEScheduler::get().schedule(&_interleave4x4_kernel, Window::DimY);
270 }
271
272 // Run matrix multiply
273 NEScheduler::get().schedule(&_mm_kernel, _is_batched_fc_layer ? Window::DimY : Window::DimX);
274
275 // Accumulate biases if provided
276 if(_accumulate_biases)
277 {
278 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
279 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100280
281 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282}
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100283} // namespace arm_compute