blob: 34cabb5c2ee20945a9e06cc78d55905d8be68982 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/NEON/functions/NEFullyConnectedLayer.h"
25
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000026#include "arm_compute/core/Helpers.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010027#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Validate.h"
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arenaa855af12018-07-16 17:20:38 +010030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/NEON/NEScheduler.h"
32
33#include <algorithm>
34#include <cmath>
35
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000036using namespace arm_compute;
37using namespace arm_compute::misc::shape_calculator;
38
Giorgio Arenaa855af12018-07-16 17:20:38 +010039namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
Giorgio Arenaa855af12018-07-16 17:20:38 +010041Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042{
Giorgio Arenaa855af12018-07-16 17:20:38 +010043 if(is_data_type_quantized_asymmetric(input.data_type()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 {
Giorgio Arenaa855af12018-07-16 17:20:38 +010045 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
46 // Extract and negate input and weights offset
47 const QuantizationInfo input_quantization_info(input.quantization_info().scale, -input.quantization_info().offset);
48 const QuantizationInfo weights_quantization_info(weights.quantization_info().scale, -weights.quantization_info().offset);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
Giorgio Arenaa855af12018-07-16 17:20:38 +010050 // Validate gemmlowp function
51 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info),
52 &weights.clone()->set_quantization_info(weights_quantization_info),
53 &output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 }
55 else
56 {
Giorgio Arenaa855af12018-07-16 17:20:38 +010057 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(&input, &weights, nullptr, &output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */)));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000058 }
59
60 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061}
Giorgio Arenaa855af12018-07-16 17:20:38 +010062} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063
Giorgio Arenaa855af12018-07-16 17:20:38 +010064void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
Giorgio Arenaa855af12018-07-16 17:20:38 +010066 auto k = arm_compute::support::cpp14::make_unique<NETransposeKernel>();
67 k->configure(input, output);
68 _kernel = std::move(k);
69}
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010070
Giorgio Arenaa855af12018-07-16 17:20:38 +010071Status NEFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output)
72{
73 return NETransposeKernel::validate(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074}
75
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010076NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitasef776a82018-07-25 17:57:49 +010077 : _memory_group(std::move(memory_manager)), _im2col_kernel(), _convert_weights(), _reshape_weights_function(), _mm_gemm(), _mm_gemmlowp(), _gemmlowp_output_stage(), _accumulate_biases_kernel(),
78 _im2col_output(), _gemmlowp_output(), _converted_weights_output(), _reshape_weights_output(), _original_weights(nullptr), _are_weights_converted(true), _are_weights_reshaped(false),
79 _is_fc_after_conv(false), _accumulate_biases(false), _is_quantized(false), _is_prepared(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080{
81}
82
Giorgio Arenaa855af12018-07-16 17:20:38 +010083void NEFullyConnectedLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output)
84{
85 if(_is_quantized)
86 {
87 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
88 // Extract and negate input and weights offset
89 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
90 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
91
92 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
93 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
94
95 // Configure gemmlowp function
96 _mm_gemmlowp.configure(input, weights, output);
97
98 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
99 input->info()->set_quantization_info(input_quantization_info);
100 weights->info()->set_quantization_info(weights_quantization_info);
101 }
102 else
103 {
104 // Configure matrix multiply kernel
105 _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */));
106 }
107}
108
109void NEFullyConnectedLayer::configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output)
110{
111 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
112
113 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
114
115 // Initialize output tensor for im2col
116 TensorShape shape_im2col = compute_im2col_fc_shape(input->info());
117 _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
118
119 // Configure im2col kernel
120 _memory_group.manage(&_im2col_output);
121 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true);
122
123 // Configure matrix multiply kernel
124 configure_mm(&_im2col_output, weights, output);
125
126 // Allocate the output tensor for im2col once all the configure methods have been called
127 _im2col_output.allocator()->allocate();
128}
129
130void NEFullyConnectedLayer::configure_fc_fc(const ITensor *input, const ITensor *weights, ITensor *output)
131{
132 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
133
134 // Configure matrix multiply kernel
135 configure_mm(input, weights, output);
136}
137
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100138void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
139 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000141 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000143 // Perform validate step
144 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(),
145 weights->info(),
146 biases != nullptr ? biases->info() : nullptr,
147 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100148 fc_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149
Georgios Pinitasef776a82018-07-25 17:57:49 +0100150 _are_weights_converted = true;
151 _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
152 _is_fc_after_conv = true;
153 _accumulate_biases = false;
154 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
155 _original_weights = weights;
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100156
Giorgio Arenaa855af12018-07-16 17:20:38 +0100157 // Configure gemmlowp output
158 if(_is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100160 _gemmlowp_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100161 }
162
Giorgio Arenaa855af12018-07-16 17:20:38 +0100163 // Configure accumulate biases kernel for non quantized asymmetric types
164 if(biases != nullptr && !_is_quantized)
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100165 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100166 _accumulate_biases = true;
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100167
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100168 // Configure accumulate biases kernel
169 _accumulate_biases_kernel.configure(output, biases);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 }
171
Giorgio Arenaa855af12018-07-16 17:20:38 +0100172 // With the Fully Connected layer we can have 4 different cases:
173 // 1) Convolution layer -> Fully Connected layer without batches
174 // 2) Fully Connected layer -> Fully Connected layer without batches
175 // 3) Convolution layer -> Fully Connected layer with batches
176 // 4) Fully Connected layer -> Fully Connected layer with batches
177
178 const ITensor *weights_to_use = weights;
179
Giorgio Arenaa855af12018-07-16 17:20:38 +0100180 // Check if we have a fully connected layer with batches
181 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100182 if(is_batched_fc_layer)
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100183 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100184 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
185 input->info()->tensor_shape().cend(),
186 output->info()->tensor_shape().cbegin() + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100188 else
189 {
190 _is_fc_after_conv = input->info()->num_dimensions() > 1;
191 }
192
Georgios Pinitasef776a82018-07-25 17:57:49 +0100193 // Reshape weights if needed
194 if(!_are_weights_reshaped)
195 {
196 // Reshape the weights
197 _reshape_weights_function.configure(weights, &_reshape_weights_output);
198 weights_to_use = &_reshape_weights_output;
199 }
200
201 // Convert weights if needed
202 if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
203 {
204 // Convert weights
205 _convert_weights.configure(weights_to_use,
206 &_converted_weights_output,
207 input->info()->tensor_shape(),
208 fc_info.weights_trained_layout);
209
210 weights_to_use = &_converted_weights_output;
211 _are_weights_converted = false;
212 }
213
Giorgio Arenaa855af12018-07-16 17:20:38 +0100214 ITensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output;
215 if(_is_fc_after_conv)
216 {
217 // Fully Connected layer after a Convolution Layer without batches
218 configure_conv_fc(input, weights_to_use, tmp_output);
219 }
220 else
221 {
222 // Fully Connected layer after a Fully Connected Layer without batches
223 configure_fc_fc(input, weights_to_use, tmp_output);
224 }
225
226 // Configure output stage for asymmetric quantized types
227 if(_is_quantized)
228 {
229 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale;
230 int output_multiplier, output_shift;
231 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
232 _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset);
233 _gemmlowp_output.allocator()->allocate();
234 }
235
236 _are_weights_reshaped = _are_weights_reshaped || fc_info.retain_internal_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237}
238
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100239Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
240 FullyConnectedLayerInfo fc_info)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000241{
Giorgio Arenaa855af12018-07-16 17:20:38 +0100242 ARM_COMPUTE_UNUSED(fc_info.retain_internal_weights);
243 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
244 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000245 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000246 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2);
247
Giorgio Arenaa855af12018-07-16 17:20:38 +0100248 bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true;
249 bool is_fc_after_conv = true;
250 bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000251
Georgios Pinitasef776a82018-07-25 17:57:49 +0100252 const ITensorInfo &im2col_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_im2col_fc_shape(input)));
253 const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights)));
254 const ITensorInfo &converted_weights = TensorInfo(reshaped_weights.clone()->set_is_resizable(true).reset_padding());
255 const ITensorInfo &gemmlowp_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100256
257 // Configure accumulate biases kernel for non quantized asymmetric types
258 if(biases != nullptr && !is_quantized)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000259 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100260 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
261 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000262 }
263
Giorgio Arenaa855af12018-07-16 17:20:38 +0100264 // With the Fully Connected layer we can have 4 different cases:
265 // 1) Convolution layer -> Fully Connected layer without batches
266 // 2) Fully Connected layer -> Fully Connected layer without batches
267 // 3) Convolution layer -> Fully Connected layer with batches
268 // 4) Fully Connected layer -> Fully Connected layer with batches
269
270 const ITensorInfo *input_to_use = input;
271 const ITensorInfo *weights_to_use = weights;
272 const ITensorInfo *tmp_output = (is_quantized) ? &gemmlowp_output : output;
273
Giorgio Arenaa855af12018-07-16 17:20:38 +0100274 // Check if we have a fully connected layer with batches
275 const bool is_batched_fc_layer = output->dimension(1) > 1;
276
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000277 if(is_batched_fc_layer)
278 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100279 is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3,
280 input->tensor_shape().cend(),
281 output->tensor_shape().cbegin() + 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000282 }
283 else
284 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100285 is_fc_after_conv = input->num_dimensions() > 1;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000286 }
287
Georgios Pinitasef776a82018-07-25 17:57:49 +0100288 if(!weights_reshaped)
289 {
290 // Validate reshape weights kernel
291 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights));
292 weights_to_use = &reshaped_weights;
293 }
294
295 if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout))
296 {
297 // Validate convert weights kernel
298 ARM_COMPUTE_RETURN_ON_ERROR(NEConvertFullyConnectedWeights::validate(weights_to_use,
299 &converted_weights,
300 input->tensor_shape(),
301 fc_info.weights_trained_layout));
302 weights_to_use = &converted_weights;
303 }
304
Giorgio Arenaa855af12018-07-16 17:20:38 +0100305 if(is_fc_after_conv)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000306 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100307 // Fully Connected layer after a Convolution Layer without batches
308 ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2))));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000309
Giorgio Arenaa855af12018-07-16 17:20:38 +0100310 // Validate im2col kernel
311 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2col_input, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true));
312 input_to_use = &im2col_input;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000313 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100314 else
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000315 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100316 // Fully Connected layer after a Fully Connected Layer without batches
317 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000318 }
Giorgio Arenaa855af12018-07-16 17:20:38 +0100319 // Validate matrix multiply kernel
320 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, *tmp_output));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000321
Giorgio Arenaa855af12018-07-16 17:20:38 +0100322 // Validate output stage for asymmetric quantized types
323 if(is_quantized)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000324 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100325 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&gemmlowp_output, biases, output));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000326 }
327
328 return Status{};
329}
330
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331void NEFullyConnectedLayer::run()
332{
Georgios Pinitas72219332018-06-05 14:56:06 +0100333 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100335 _memory_group.acquire();
336
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100337 // Linearize input if it comes from a convolutional layer
Giorgio Arenaa855af12018-07-16 17:20:38 +0100338 if(_is_fc_after_conv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339 {
340 NEScheduler::get().schedule(&_im2col_kernel, Window::DimY);
341 }
342
Giorgio Arenaa855af12018-07-16 17:20:38 +0100343 // Run matrix multiply
344 if(_is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100346 _mm_gemmlowp.run();
347 }
348 else
349 {
350 _mm_gemm.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351 }
352
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100353 // Accumulate biases if provided
Giorgio Arenaa855af12018-07-16 17:20:38 +0100354 if(_is_quantized)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100356 _gemmlowp_output_stage.run();
357 }
358 else
359 {
360 if(_accumulate_biases)
361 {
362 NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY);
363 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100365
366 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367}
Georgios Pinitas72219332018-06-05 14:56:06 +0100368
369void NEFullyConnectedLayer::prepare()
370{
Georgios Pinitas72219332018-06-05 14:56:06 +0100371 if(!_is_prepared)
372 {
Georgios Pinitasef776a82018-07-25 17:57:49 +0100373 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
374
375 auto release_unused = [](Tensor * w)
376 {
377 if(!w->is_used())
378 {
379 w->allocator()->free();
380 }
381 };
382
383 // Pointer to current weights
384 const ITensor *cur_weights = _original_weights;
385
Giorgio Arenaa855af12018-07-16 17:20:38 +0100386 // Reshape of the weights (happens only once)
387 if(!_are_weights_reshaped)
388 {
Giorgio Arenaa855af12018-07-16 17:20:38 +0100389 // Run reshape weights kernel and mark weights as unused
390 _reshape_weights_output.allocator()->allocate();
391 _reshape_weights_function.run();
Giorgio Arenaa855af12018-07-16 17:20:38 +0100392
Georgios Pinitasef776a82018-07-25 17:57:49 +0100393 cur_weights->mark_as_unused();
394 cur_weights = &_reshape_weights_output;
Giorgio Arenaa855af12018-07-16 17:20:38 +0100395 _are_weights_reshaped = true;
396 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100397
Georgios Pinitasef776a82018-07-25 17:57:49 +0100398 // Convert weights if needed (happens only once)
399 if(!_are_weights_converted)
400 {
401 _converted_weights_output.allocator()->allocate();
402 _convert_weights.run();
403
404 cur_weights->mark_as_unused();
405 _are_weights_converted = true;
406 }
407
408 // Release reshaped weights if unused
409 release_unused(&_reshape_weights_output);
410
411 // Prepare GEMM prepare and release unused weights
412 if(!_is_quantized)
413 {
414 _mm_gemm.prepare();
415 }
416
417 // Release converted weights if unused
418 release_unused(&_reshape_weights_output);
419 release_unused(&_converted_weights_output);
420
Georgios Pinitas72219332018-06-05 14:56:06 +0100421 _is_prepared = true;
422 }
423}