Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Validate.h" |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cmath> |
| 35 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 36 | using namespace arm_compute; |
| 37 | using namespace arm_compute::misc::shape_calculator; |
| 38 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 39 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 41 | Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 43 | if(is_data_type_quantized_asymmetric(input.data_type())) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 45 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 50 | // 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), |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 53 | nullptr, |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 54 | &output)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | } |
| 56 | else |
| 57 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 58 | 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 Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 63 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 65 | void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 67 | auto k = arm_compute::support::cpp14::make_unique<NETransposeKernel>(); |
| 68 | k->configure(input, output); |
| 69 | _kernel = std::move(k); |
| 70 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 71 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 72 | Status NEFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 73 | { |
| 74 | return NETransposeKernel::validate(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 77 | NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 78 | : _memory_group(std::move(memory_manager)), _flatten_kernel(), _convert_weights(), _reshape_weights_function(), _mm_gemm(), _mm_gemmlowp(), _gemmlowp_output_stage(), _accumulate_biases_kernel(), |
| 79 | _flatten_output(), _gemmlowp_output(), _converted_weights_output(), _reshape_weights_output(), _original_weights(nullptr), _are_weights_converted(true), _are_weights_reshaped(false), |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 80 | _is_fc_after_conv(false), _accumulate_biases(false), _is_quantized(false), _is_prepared(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | { |
| 82 | } |
| 83 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 84 | void NEFullyConnectedLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output) |
| 85 | { |
| 86 | if(_is_quantized) |
| 87 | { |
| 88 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 89 | // Extract and negate input and weights offset |
| 90 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 91 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 92 | |
| 93 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 94 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 95 | |
| 96 | // Configure gemmlowp function |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 97 | _mm_gemmlowp.configure(input, weights, nullptr, output); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 98 | |
| 99 | // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers |
| 100 | input->info()->set_quantization_info(input_quantization_info); |
| 101 | weights->info()->set_quantization_info(weights_quantization_info); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | // Configure matrix multiply kernel |
| 106 | _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void NEFullyConnectedLayer::configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
| 113 | |
| 114 | // If the fully connected layer is called after a convolution layer, the input tensor must be linearized |
| 115 | |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 116 | // Initialize output tensor for flatten |
| 117 | TensorShape shape_flatten = compute_flatten_shape(input->info()); |
| 118 | _flatten_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten)); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 119 | |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 120 | // Configure flatten kernel |
| 121 | _memory_group.manage(&_flatten_output); |
| 122 | _flatten_kernel.configure(input, &_flatten_output); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 123 | |
| 124 | // Configure matrix multiply kernel |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 125 | configure_mm(&_flatten_output, weights, output); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 126 | |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 127 | // Allocate the output tensor for flatten once all the configure methods have been called |
| 128 | _flatten_output.allocator()->allocate(); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void NEFullyConnectedLayer::configure_fc_fc(const ITensor *input, const ITensor *weights, ITensor *output) |
| 132 | { |
| 133 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 134 | |
| 135 | // Configure matrix multiply kernel |
| 136 | configure_mm(input, weights, output); |
| 137 | } |
| 138 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 139 | void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, |
| 140 | FullyConnectedLayerInfo fc_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 144 | // Perform validate step |
| 145 | ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(), |
| 146 | weights->info(), |
| 147 | biases != nullptr ? biases->info() : nullptr, |
| 148 | output->info(), |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 149 | fc_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 151 | _are_weights_converted = true; |
| 152 | _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
| 153 | _is_fc_after_conv = true; |
| 154 | _accumulate_biases = false; |
| 155 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 156 | _original_weights = weights; |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 157 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 158 | // Configure gemmlowp output |
| 159 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 161 | _gemmlowp_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 164 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 165 | if(biases != nullptr && !_is_quantized) |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 166 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 167 | _accumulate_biases = true; |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 168 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 169 | // Configure accumulate biases kernel |
| 170 | _accumulate_biases_kernel.configure(output, biases); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 173 | // With the Fully Connected layer we can have 4 different cases: |
| 174 | // 1) Convolution layer -> Fully Connected layer without batches |
| 175 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 176 | // 3) Convolution layer -> Fully Connected layer with batches |
| 177 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 178 | |
| 179 | const ITensor *weights_to_use = weights; |
| 180 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 181 | // Check if we have a fully connected layer with batches |
| 182 | const bool is_batched_fc_layer = output->info()->dimension(1) > 1; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 183 | if(is_batched_fc_layer) |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 184 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 185 | _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 186 | input->info()->tensor_shape().cend(), |
| 187 | output->info()->tensor_shape().cbegin() + 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 189 | else |
| 190 | { |
| 191 | _is_fc_after_conv = input->info()->num_dimensions() > 1; |
| 192 | } |
| 193 | |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 194 | // Reshape weights if needed |
| 195 | if(!_are_weights_reshaped) |
| 196 | { |
| 197 | // Reshape the weights |
| 198 | _reshape_weights_function.configure(weights, &_reshape_weights_output); |
| 199 | weights_to_use = &_reshape_weights_output; |
| 200 | } |
| 201 | |
| 202 | // Convert weights if needed |
| 203 | if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout)) |
| 204 | { |
| 205 | // Convert weights |
| 206 | _convert_weights.configure(weights_to_use, |
| 207 | &_converted_weights_output, |
| 208 | input->info()->tensor_shape(), |
| 209 | fc_info.weights_trained_layout); |
| 210 | |
| 211 | weights_to_use = &_converted_weights_output; |
| 212 | _are_weights_converted = false; |
| 213 | } |
| 214 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 215 | ITensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output; |
| 216 | if(_is_fc_after_conv) |
| 217 | { |
| 218 | // Fully Connected layer after a Convolution Layer without batches |
| 219 | configure_conv_fc(input, weights_to_use, tmp_output); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | // Fully Connected layer after a Fully Connected Layer without batches |
| 224 | configure_fc_fc(input, weights_to_use, tmp_output); |
| 225 | } |
| 226 | |
| 227 | // Configure output stage for asymmetric quantized types |
| 228 | if(_is_quantized) |
| 229 | { |
| 230 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 231 | int output_multiplier, output_shift; |
| 232 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 233 | _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
| 234 | _gemmlowp_output.allocator()->allocate(); |
| 235 | } |
| 236 | |
| 237 | _are_weights_reshaped = _are_weights_reshaped || fc_info.retain_internal_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 240 | Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 241 | FullyConnectedLayerInfo fc_info) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 242 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 243 | ARM_COMPUTE_UNUSED(fc_info.retain_internal_weights); |
| 244 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 245 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 246 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 247 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2); |
| 248 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 249 | bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
| 250 | bool is_fc_after_conv = true; |
| 251 | bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 252 | |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 253 | const ITensorInfo &flatten_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_flatten_shape(input))); |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 254 | const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights))); |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 255 | const ITensorInfo &converted_weights = weights_reshaped ? TensorInfo(weights->clone()->set_is_resizable(true).reset_padding()) : TensorInfo(*reshaped_weights.clone()); |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 256 | const ITensorInfo &gemmlowp_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 257 | |
| 258 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 259 | if(biases != nullptr && !is_quantized) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 260 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 261 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 262 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 265 | // With the Fully Connected layer we can have 4 different cases: |
| 266 | // 1) Convolution layer -> Fully Connected layer without batches |
| 267 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 268 | // 3) Convolution layer -> Fully Connected layer with batches |
| 269 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 270 | |
| 271 | const ITensorInfo *input_to_use = input; |
| 272 | const ITensorInfo *weights_to_use = weights; |
| 273 | const ITensorInfo *tmp_output = (is_quantized) ? &gemmlowp_output : output; |
| 274 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 275 | // Check if we have a fully connected layer with batches |
| 276 | const bool is_batched_fc_layer = output->dimension(1) > 1; |
| 277 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 278 | if(is_batched_fc_layer) |
| 279 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 280 | is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3, |
| 281 | input->tensor_shape().cend(), |
| 282 | output->tensor_shape().cbegin() + 1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 283 | } |
| 284 | else |
| 285 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 286 | is_fc_after_conv = input->num_dimensions() > 1; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 289 | if(!weights_reshaped) |
| 290 | { |
| 291 | // Validate reshape weights kernel |
| 292 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights)); |
| 293 | weights_to_use = &reshaped_weights; |
| 294 | } |
| 295 | |
| 296 | if(is_fc_after_conv && (input->data_layout() != fc_info.weights_trained_layout)) |
| 297 | { |
| 298 | // Validate convert weights kernel |
| 299 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvertFullyConnectedWeights::validate(weights_to_use, |
| 300 | &converted_weights, |
| 301 | input->tensor_shape(), |
| 302 | fc_info.weights_trained_layout)); |
| 303 | weights_to_use = &converted_weights; |
| 304 | } |
| 305 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 306 | if(is_fc_after_conv) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 307 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 308 | // Fully Connected layer after a Convolution Layer without batches |
| 309 | ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2)))); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 310 | |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 311 | // Validate flatten kernel |
| 312 | ARM_COMPUTE_RETURN_ON_ERROR(NEFlattenLayerKernel::validate(input, &flatten_input)); |
| 313 | input_to_use = &flatten_input; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 314 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 315 | else |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 316 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 317 | // Fully Connected layer after a Fully Connected Layer without batches |
| 318 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 319 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 320 | // Validate matrix multiply kernel |
| 321 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, *tmp_output)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 322 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 323 | // Validate output stage for asymmetric quantized types |
| 324 | if(is_quantized) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 325 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 326 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&gemmlowp_output, biases, output)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return Status{}; |
| 330 | } |
| 331 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 332 | void NEFullyConnectedLayer::run() |
| 333 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 334 | prepare(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 335 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame^] | 336 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 337 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 338 | // Linearize input if it comes from a convolutional layer |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 339 | if(_is_fc_after_conv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 340 | { |
Giorgio Arena | 368e635 | 2018-08-20 15:06:07 +0100 | [diff] [blame] | 341 | NEScheduler::get().schedule(&_flatten_kernel, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 344 | // Run matrix multiply |
| 345 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 346 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 347 | _mm_gemmlowp.run(); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | _mm_gemm.run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 354 | // Accumulate biases if provided |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 355 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 356 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 357 | _gemmlowp_output_stage.run(); |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | if(_accumulate_biases) |
| 362 | { |
| 363 | NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY); |
| 364 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 365 | } |
| 366 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 367 | |
| 368 | void NEFullyConnectedLayer::prepare() |
| 369 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 370 | if(!_is_prepared) |
| 371 | { |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 372 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 373 | |
| 374 | auto release_unused = [](Tensor * w) |
| 375 | { |
| 376 | if(!w->is_used()) |
| 377 | { |
| 378 | w->allocator()->free(); |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | // Pointer to current weights |
| 383 | const ITensor *cur_weights = _original_weights; |
| 384 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 385 | // Reshape of the weights (happens only once) |
| 386 | if(!_are_weights_reshaped) |
| 387 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 388 | // Run reshape weights kernel and mark weights as unused |
| 389 | _reshape_weights_output.allocator()->allocate(); |
| 390 | _reshape_weights_function.run(); |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 391 | |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 392 | cur_weights->mark_as_unused(); |
| 393 | cur_weights = &_reshape_weights_output; |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 394 | _are_weights_reshaped = true; |
| 395 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 396 | |
Georgios Pinitas | ef776a8 | 2018-07-25 17:57:49 +0100 | [diff] [blame] | 397 | // Convert weights if needed (happens only once) |
| 398 | if(!_are_weights_converted) |
| 399 | { |
| 400 | _converted_weights_output.allocator()->allocate(); |
| 401 | _convert_weights.run(); |
| 402 | |
| 403 | cur_weights->mark_as_unused(); |
| 404 | _are_weights_converted = true; |
| 405 | } |
| 406 | |
| 407 | // Release reshaped weights if unused |
| 408 | release_unused(&_reshape_weights_output); |
| 409 | |
| 410 | // Prepare GEMM prepare and release unused weights |
| 411 | if(!_is_quantized) |
| 412 | { |
| 413 | _mm_gemm.prepare(); |
| 414 | } |
| 415 | |
| 416 | // Release converted weights if unused |
| 417 | release_unused(&_reshape_weights_output); |
| 418 | release_unused(&_converted_weights_output); |
| 419 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 420 | _is_prepared = true; |
| 421 | } |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 422 | } |