blob: abb41e9f70f777e7fd0e884d50e425bef7631844 [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
26#include "arm_compute/core/Validate.h"
27#include "arm_compute/runtime/NEON/NEScheduler.h"
28
29#include <algorithm>
30#include <cmath>
31
32using namespace arm_compute;
33
34NEFullyConnectedLayerReshapeWeights::NEFullyConnectedLayerReshapeWeights()
35 : _transpose_kernel(), _transpose1xW_kernel(), _transpose_output(), _transpose_weights(false), _is_batched_fc_layer(false)
36{
37}
38
39void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output, bool transpose_weights, bool is_batched_fc_layer)
40{
41 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F32);
42 ARM_COMPUTE_ERROR_ON(output == nullptr);
43 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != 2);
44 ARM_COMPUTE_ERROR_ON((transpose_weights == false) && (is_batched_fc_layer == false));
45
46 const DataType dt = input->info()->data_type();
47 const int fixed_point_position = input->info()->fixed_point_position();
48
49 _transpose_weights = transpose_weights;
50 _is_batched_fc_layer = is_batched_fc_layer;
51
52 // Check if we need to transpose the weights
53 if(_transpose_weights)
54 {
55 if(_is_batched_fc_layer)
56 {
57 // Initialize the output tensor for transpose
58 TensorShape shape_transposed(input->info()->dimension(1), input->info()->dimension(0));
59 _transpose_output.allocator()->init(TensorInfo(shape_transposed, 1, dt, fixed_point_position));
60 _transpose_kernel.configure(input, &_transpose_output);
61
62 // Configure transpose 1xW kernel
63 _transpose1xW_kernel.configure(&_transpose_output, output);
64
65 // Allocate temporary tensor used for transposing the weights
66 _transpose_output.allocator()->allocate();
67 }
68 else
69 {
70 _transpose_kernel.configure(input, output);
71 }
72 }
73 else
74 {
75 if(_is_batched_fc_layer)
76 {
77 // Configure transpose 1xW kernel
78 _transpose1xW_kernel.configure(input, output);
79 }
80 else
81 {
82 ARM_COMPUTE_ERROR("Configuration transpose_weights=false & is_batched_fc_layer=false not supported");
83 }
84 }
85}
86
87void NEFullyConnectedLayerReshapeWeights::run()
88{
89 if(_transpose_weights)
90 {
91 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
92 }
93 if(_is_batched_fc_layer)
94 {
95 NEScheduler::get().schedule(&_transpose1xW_kernel, Window::DimY);
96 }
97}
98
99NEFullyConnectedLayer::NEFullyConnectedLayer()
100 : _im2col_kernel(), _reshape_weights_kernel(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), _interleave4x4_output(), _reshape_weights_output(),
101 _are_weights_reshaped(false), _is_fc_after_conv(false), _is_batched_fc_layer(false), _accumulate_biases(false)
102{
103}
104
105void NEFullyConnectedLayer::configure_conv_fc_wb(const ITensor *input, const ITensor *weights, ITensor *output)
106{
107 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2) * (16 / weights->info()->element_size())));
108
109 const DataType dt = input->info()->data_type();
110 const int fixed_point_position = input->info()->fixed_point_position();
111
112 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
113
114 // Initialize output tensor for im2col
115 TensorShape shape_im2col;
116 shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2));
117 shape_im2col.set(1, input->info()->dimension(3));
118 shape_im2col.set(2, input->info()->dimension(4));
119 shape_im2col.set(3, input->info()->dimension(5));
120 _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
121
122 // Initialize output tensor for interleave 4x4
123 TensorShape shape_interleaved = _im2col_output.info()->tensor_shape();
124 shape_interleaved.set(0, shape_interleaved.x() * 4);
125 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4));
126 _interleave4x4_output.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
127
128 // Configure im2col kernel
129 _im2col_kernel.configure(input, &_im2col_output, std::make_pair(1, 1), PadStrideInfo(1, 1, 0, 0), false);
130
131 // Configure interleave4x4 kernel
132 _interleave4x4_kernel.configure(&_im2col_output, &_interleave4x4_output);
133
134 // Configure matrix multiply kernel
135 _mm_kernel.configure(&_interleave4x4_output, weights, output, 1.0f);
136
137 // Allocate the tensors once all the configure methods have been called
138 _im2col_output.allocator()->allocate();
139 _interleave4x4_output.allocator()->allocate();
140}
141
142void NEFullyConnectedLayer::configure_fc_fc_wb(const ITensor *input, const ITensor *weights, ITensor *output)
143{
144 const DataType dt = input->info()->data_type();
145 const int fixed_point_position = input->info()->fixed_point_position();
146
147 // Initialize output tensor for interleave 4x4
148 TensorShape shape_interleaved = input->info()->tensor_shape();
149 shape_interleaved.set(0, shape_interleaved.x() * 4);
150 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4));
151 _interleave4x4_output.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
152
153 // Configure interleave4x4 kernel
154 _interleave4x4_kernel.configure(input, &_interleave4x4_output);
155
156 // Configure matrix multiply kernel
157 _mm_kernel.configure(&_interleave4x4_output, weights, output, 1.0f);
158
159 // Allocate the tensors once all the configure methods have been called
160 _interleave4x4_output.allocator()->allocate();
161}
162
163void NEFullyConnectedLayer::configure_conv_fc_nb(const ITensor *input, const ITensor *weights, ITensor *output)
164{
165 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
166
167 const DataType dt = input->info()->data_type();
168 const int fixed_point_position = input->info()->fixed_point_position();
169
170 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
171
172 // Initialize output tensor for im2col
173 TensorShape shape_im2col;
174 shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2));
175 shape_im2col.set(1, 1);
176 _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
177
178 // Configure im2col kernel
179 _im2col_kernel.configure(input, &_im2col_output, std::make_pair(1, 1), PadStrideInfo(1, 1, 0, 0), false);
180
181 // Configure matrix multiply kernel
182 _mm_kernel.configure(&_im2col_output, weights, output, 1.0f);
183
184 // Allocate the output tensor for im2col once all the configure methods have been called
185 _im2col_output.allocator()->allocate();
186}
187
188void NEFullyConnectedLayer::configure_fc_fc_nb(const ITensor *input, const ITensor *weights, ITensor *output)
189{
190 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
191
192 // Configure matrix multiply kernel
193 _mm_kernel.configure(input, weights, output, 1.0f);
194}
195
196void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose_weights, bool are_weights_reshaped)
197{
198 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::F32);
199 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::F32);
200 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::F32);
201 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
202 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() != 2);
203
204 const DataType dt = input->info()->data_type();
205 const int fixed_point_position = input->info()->fixed_point_position();
206
207 _are_weights_reshaped = are_weights_reshaped;
208 _is_fc_after_conv = true;
209 _is_batched_fc_layer = false;
210 _accumulate_biases = false;
211
212 if(biases != nullptr)
213 {
214 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
215
216 _accumulate_biases = true;
217
218 // Configure accumulate biases kernel
219 _accumulate_biases_kernel.configure(output, biases);
220 }
221
222 // With the Fully Connected layer we can have 4 different cases:
223 // 1) Convolution layer -> Fully Connected layer without batches
224 // 2) Fully Connected layer -> Fully Connected layer without batches
225 // 3) Convolution layer -> Fully Connected layer with batches
226 // 4) Fully Connected layer -> Fully Connected layer with batches
227
228 // Check if we have a fully connected layer with batches
229 _is_batched_fc_layer = (output->info()->dimension(1) > 1);
230
231 const ITensor *weights_to_use = weights;
232
233 if(!are_weights_reshaped)
234 {
235 if((transpose_weights || _is_batched_fc_layer))
236 {
237 weights_to_use = &_reshape_weights_output;
238
239 if(transpose_weights)
240 {
241 if(_is_batched_fc_layer)
242 {
243 const float transpose_width = 16.0f / input->info()->element_size();
244 TensorShape shape_wt(weights->info()->dimension(0) * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(weights->info()->dimension(1) / transpose_width)));
245 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
246 _reshape_weights_output.allocator()->init(info_wt);
247 }
248 else
249 {
250 TensorShape shape_wt(weights->info()->dimension(1), weights->info()->dimension(0));
251 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
252 _reshape_weights_output.allocator()->init(info_wt);
253 }
254 }
255 else
256 {
257 ARM_COMPUTE_ERROR_ON(!_is_batched_fc_layer);
258
259 const float transpose_width = 16.0f / input->info()->element_size();
260 TensorShape shape_wt(weights->info()->dimension(1) * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(weights->info()->dimension(0) / transpose_width)));
261 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
262 _reshape_weights_output.allocator()->init(info_wt);
263 }
264
265 // Reshape the weights
266 _reshape_weights_kernel.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer);
267 }
268 }
269
270 if(_is_batched_fc_layer)
271 {
272 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
273 input->info()->tensor_shape().cend(),
274 output->info()->tensor_shape().cbegin() + 1));
275
276 if(_is_fc_after_conv)
277 {
278 // Fully Connected layer after a Convolution Layer with batches
279 configure_conv_fc_wb(input, weights_to_use, output);
280 }
281 else
282 {
283 // Fully Connected layer after a Fully Connected Layer with batches
284 configure_fc_fc_wb(input, weights_to_use, output);
285 }
286 }
287 else
288 {
289 // In case of not batched fully connected layer, the weights will not be reshaped using transposed1xW
290 _is_fc_after_conv = ((weights_to_use->info()->dimension(1)) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)));
291
292 if(_is_fc_after_conv)
293 {
294 // Fully Connected layer after a Convolution Layer without batches
295 configure_conv_fc_nb(input, weights_to_use, output);
296 }
297 else
298 {
299 // Fully Connected layer after a Fully Connected Layer without batches
300 configure_fc_fc_nb(input, weights_to_use, output);
301 }
302 }
303
304 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
305 if(!are_weights_reshaped)
306 {
307 if(transpose_weights || _is_batched_fc_layer)
308 {
309 // Allocate the tensor for the weights reshaped
310 _reshape_weights_output.allocator()->allocate();
311 }
312 }
313}
314
315void NEFullyConnectedLayer::run()
316{
317 // Reshape of the weights (happens only once)
318 if(!_are_weights_reshaped)
319 {
320 _are_weights_reshaped = true;
321 _reshape_weights_kernel.run();
322 }
323
324 // Linearize input if comes from a convolutional layer
325 if(_is_fc_after_conv)
326 {
327 NEScheduler::get().schedule(&_im2col_kernel, Window::DimY);
328 }
329
330 // Interleave input
331 if(_is_batched_fc_layer)
332 {
333 NEScheduler::get().schedule(&_interleave4x4_kernel, Window::DimY);
334 }
335
336 // Run matrix multiply
337 NEScheduler::get().schedule(&_mm_kernel, _is_batched_fc_layer ? Window::DimY : Window::DimX);
338
339 // Accumulate biases if provided
340 if(_accumulate_biases)
341 {
342 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
343 }
344}