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