blob: 57d57d517f1932e81863b99861724ae3073e6a51 [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/CL/functions/CLFullyConnectedLayer.h"
25
26#include "arm_compute/core/Validate.h"
27#include "arm_compute/runtime/CL/CLScheduler.h"
28
29#include <algorithm>
30#include <cmath>
31
32using namespace arm_compute;
33
34CLFullyConnectedLayerReshapeWeights::CLFullyConnectedLayerReshapeWeights()
35 : _transpose_kernel(), _transpose1xW_kernel(), _transpose_output(), _transpose_weights(false), _is_batched_fc_layer(false)
36{
37}
38
39void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *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 CLFullyConnectedLayerReshapeWeights::run()
88{
89 if(_transpose_weights)
90 {
91 CLScheduler::get().enqueue(_transpose_kernel, _is_batched_fc_layer);
92 }
93 if(_is_batched_fc_layer)
94 {
95 CLScheduler::get().enqueue(_transpose1xW_kernel);
96 }
97}
98
99CLFullyConnectedLayer::CLFullyConnectedLayer()
100 : _im2col_kernel(), _reshape_weights_kernel(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), _interleave4x4_output(), _reshape_weights_output(),
101 _are_weights_reshaped(true), _is_fc_after_conv(true), _is_batched_fc_layer(false), _accumulate_biases(false)
102{
103}
104
105void CLFullyConnectedLayer::configure_conv_fc_wb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *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 CLFullyConnectedLayer::configure_fc_fc_wb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *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 CLFullyConnectedLayer::configure_conv_fc_nb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *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 CLFullyConnectedLayer::configure_fc_fc_nb(const ICLTensor *input, const ICLTensor *weights, ICLTensor *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 CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights, bool are_weights_reshaped)
197{
198 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
199 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32);
200 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
201 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() != 2);
202
203 const DataType dt = input->info()->data_type();
204 const int fixed_point_position = input->info()->fixed_point_position();
205
206 _are_weights_reshaped = are_weights_reshaped;
207 _is_fc_after_conv = true;
208 _is_batched_fc_layer = false;
209 _accumulate_biases = false;
210
211 if(biases != nullptr)
212 {
213 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
214
215 _accumulate_biases = true;
216
217 // Configure accumulate biases kernel
218 _accumulate_biases_kernel.configure(output, biases);
219 }
220
221 // With the Fully Connected layer we can have 4 different cases:
222 // 1) Convolution layer -> Fully Connected layer without batches
223 // 2) Fully Connected layer -> Fully Connected layer without batches
224 // 3) Convolution layer -> Fully Connected layer with batches
225 // 4) Fully Connected layer -> Fully Connected layer with batches
226
227 // Check if we have a fully connected layer with batches
228 _is_batched_fc_layer = (output->info()->dimension(1) > 1);
229
230 const ICLTensor *weights_to_use = weights;
231
232 if(!are_weights_reshaped)
233 {
234 if((transpose_weights || _is_batched_fc_layer))
235 {
236 weights_to_use = &_reshape_weights_output;
237
238 if(transpose_weights)
239 {
240 if(_is_batched_fc_layer)
241 {
242 const float transpose_width = 16.0f / input->info()->element_size();
243 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)));
244 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
245 _reshape_weights_output.allocator()->init(info_wt);
246 }
247 else
248 {
249 TensorShape shape_wt(weights->info()->dimension(1), weights->info()->dimension(0));
250 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
251 _reshape_weights_output.allocator()->init(info_wt);
252 }
253 }
254 else
255 {
256 ARM_COMPUTE_ERROR_ON(!_is_batched_fc_layer);
257
258 const float transpose_width = 16.0f / input->info()->element_size();
259 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)));
260 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
261 _reshape_weights_output.allocator()->init(info_wt);
262 }
263
264 // Reshape the weights
265 _reshape_weights_kernel.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer);
266 }
267 }
268
269 if(_is_batched_fc_layer)
270 {
271 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
272 input->info()->tensor_shape().cend(),
273 output->info()->tensor_shape().cbegin() + 1));
274
275 if(_is_fc_after_conv)
276 {
277 // Fully Connected layer after a Convolution Layer with batches
278 configure_conv_fc_wb(input, weights_to_use, output);
279 }
280 else
281 {
282 // Fully Connected layer after a Fully Connected Layer with batches
283 configure_fc_fc_wb(input, weights_to_use, output);
284 }
285 }
286 else
287 {
288 // In case of not batched fully connected layer, the weights will not be reshaped using transposed1xW
289 _is_fc_after_conv = ((weights_to_use->info()->dimension(1)) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)));
290
291 if(_is_fc_after_conv)
292 {
293 // Fully Connected layer after a Convolution Layer without batches
294 configure_conv_fc_nb(input, weights_to_use, output);
295 }
296 else
297 {
298 // Fully Connected layer after a Fully Connected Layer without batches
299 configure_fc_fc_nb(input, weights_to_use, output);
300 }
301 }
302
303 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
304 if(!are_weights_reshaped)
305 {
306 if(transpose_weights || _is_batched_fc_layer)
307 {
308 // Allocate the tensor for the weights reshaped
309 _reshape_weights_output.allocator()->allocate();
310 }
311 }
312}
313
314void CLFullyConnectedLayer::run()
315{
316 // Reshape of the weights (happens only once)
317 if(!_are_weights_reshaped)
318 {
319 _are_weights_reshaped = true;
320 _reshape_weights_kernel.run();
321 }
322
323 // Linearize input if it comes from a convolutional layer
324 if(_is_fc_after_conv)
325 {
326 CLScheduler::get().enqueue(_im2col_kernel, false);
327 }
328
329 // Interleave input
330 if(_is_batched_fc_layer)
331 {
332 CLScheduler::get().enqueue(_interleave4x4_kernel, false);
333 }
334
335 // Run matrix multiply
336 CLScheduler::get().enqueue(_mm_kernel, !_accumulate_biases);
337
338 // Accumulate biases if provided
339 if(_accumulate_biases)
340 {
341 CLScheduler::get().enqueue(_accumulate_biases_kernel);
342 }
343}