Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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), |
| 53 | &output)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | } |
| 55 | else |
| 56 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 57 | 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] | 58 | } |
| 59 | |
| 60 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 62 | } // namespace |
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 | void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output) |
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 | auto k = arm_compute::support::cpp14::make_unique<NETransposeKernel>(); |
| 67 | k->configure(input, output); |
| 68 | _kernel = std::move(k); |
| 69 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 70 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 71 | Status NEFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 72 | { |
| 73 | return NETransposeKernel::validate(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 76 | NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 77 | : _memory_group(std::move(memory_manager)), _im2col_kernel(), _reshape_weights_function(), _mm_gemm(), _mm_gemmlowp(), _gemmlowp_output_stage(), _accumulate_biases_kernel(), _im2col_output(), |
| 78 | _gemmlowp_output(), _reshape_weights_output(), _original_weights(nullptr), _are_weights_reshaped(false), _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] | 79 | { |
| 80 | } |
| 81 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 82 | void NEFullyConnectedLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output) |
| 83 | { |
| 84 | if(_is_quantized) |
| 85 | { |
| 86 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 87 | // Extract and negate input and weights offset |
| 88 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 89 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 90 | |
| 91 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 92 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 93 | |
| 94 | // Configure gemmlowp function |
| 95 | _mm_gemmlowp.configure(input, weights, output); |
| 96 | |
| 97 | // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers |
| 98 | input->info()->set_quantization_info(input_quantization_info); |
| 99 | weights->info()->set_quantization_info(weights_quantization_info); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | // Configure matrix multiply kernel |
| 104 | _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void NEFullyConnectedLayer::configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output) |
| 109 | { |
| 110 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
| 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 = compute_im2col_fc_shape(input->info()); |
| 116 | _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 117 | |
| 118 | // Configure im2col kernel |
| 119 | _memory_group.manage(&_im2col_output); |
| 120 | _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true); |
| 121 | |
| 122 | // Configure matrix multiply kernel |
| 123 | configure_mm(&_im2col_output, weights, output); |
| 124 | |
| 125 | // Allocate the output tensor for im2col once all the configure methods have been called |
| 126 | _im2col_output.allocator()->allocate(); |
| 127 | } |
| 128 | |
| 129 | void NEFullyConnectedLayer::configure_fc_fc(const ITensor *input, const ITensor *weights, ITensor *output) |
| 130 | { |
| 131 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 132 | |
| 133 | // Configure matrix multiply kernel |
| 134 | configure_mm(input, weights, output); |
| 135 | } |
| 136 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 137 | void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, |
| 138 | FullyConnectedLayerInfo fc_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
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 | // Perform validate step |
| 143 | ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(), |
| 144 | weights->info(), |
| 145 | biases != nullptr ? biases->info() : nullptr, |
| 146 | output->info(), |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 147 | fc_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 149 | _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
| 150 | _is_fc_after_conv = true; |
| 151 | _accumulate_biases = false; |
| 152 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 153 | _original_weights = weights; |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 154 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 155 | // Configure gemmlowp output |
| 156 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 158 | _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] | 159 | } |
| 160 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 161 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 162 | if(biases != nullptr && !_is_quantized) |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 163 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 164 | _accumulate_biases = true; |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 165 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 166 | // Configure accumulate biases kernel |
| 167 | _accumulate_biases_kernel.configure(output, biases); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 170 | // With the Fully Connected layer we can have 4 different cases: |
| 171 | // 1) Convolution layer -> Fully Connected layer without batches |
| 172 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 173 | // 3) Convolution layer -> Fully Connected layer with batches |
| 174 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 175 | |
| 176 | const ITensor *weights_to_use = weights; |
| 177 | |
| 178 | if(!_are_weights_reshaped) |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 179 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 180 | weights_to_use = &_reshape_weights_output; |
| 181 | |
| 182 | // Reshape the weights |
| 183 | _reshape_weights_function.configure(weights, &_reshape_weights_output); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 186 | // Check if we have a fully connected layer with batches |
| 187 | const bool is_batched_fc_layer = output->info()->dimension(1) > 1; |
| 188 | |
| 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 | |
| 200 | ITensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output; |
| 201 | if(_is_fc_after_conv) |
| 202 | { |
| 203 | // Fully Connected layer after a Convolution Layer without batches |
| 204 | configure_conv_fc(input, weights_to_use, tmp_output); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | // Fully Connected layer after a Fully Connected Layer without batches |
| 209 | configure_fc_fc(input, weights_to_use, tmp_output); |
| 210 | } |
| 211 | |
| 212 | // Configure output stage for asymmetric quantized types |
| 213 | if(_is_quantized) |
| 214 | { |
| 215 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 216 | int output_multiplier, output_shift; |
| 217 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 218 | _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
| 219 | _gemmlowp_output.allocator()->allocate(); |
| 220 | } |
| 221 | |
| 222 | _are_weights_reshaped = _are_weights_reshaped || fc_info.retain_internal_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 225 | Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 226 | FullyConnectedLayerInfo fc_info) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 227 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 228 | ARM_COMPUTE_UNUSED(fc_info.retain_internal_weights); |
| 229 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 230 | 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] | 231 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 232 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2); |
| 233 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 234 | bool weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
| 235 | bool is_fc_after_conv = true; |
| 236 | bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 237 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 238 | const ITensorInfo &im2col_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_im2col_fc_shape(input))); |
| 239 | const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights))); |
| 240 | const ITensorInfo &gemmlowp_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
| 241 | |
| 242 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 243 | if(biases != nullptr && !is_quantized) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 244 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 245 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 246 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 249 | // With the Fully Connected layer we can have 4 different cases: |
| 250 | // 1) Convolution layer -> Fully Connected layer without batches |
| 251 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 252 | // 3) Convolution layer -> Fully Connected layer with batches |
| 253 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 254 | |
| 255 | const ITensorInfo *input_to_use = input; |
| 256 | const ITensorInfo *weights_to_use = weights; |
| 257 | const ITensorInfo *tmp_output = (is_quantized) ? &gemmlowp_output : output; |
| 258 | |
| 259 | if(!weights_reshaped) |
| 260 | { |
| 261 | // Validate reshape weights kernel |
| 262 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights)); |
| 263 | weights_to_use = &reshaped_weights; |
| 264 | } |
| 265 | |
| 266 | // Check if we have a fully connected layer with batches |
| 267 | const bool is_batched_fc_layer = output->dimension(1) > 1; |
| 268 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 269 | if(is_batched_fc_layer) |
| 270 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 271 | is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->tensor_shape().cbegin() + 3, |
| 272 | input->tensor_shape().cend(), |
| 273 | output->tensor_shape().cbegin() + 1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 274 | } |
| 275 | else |
| 276 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 277 | is_fc_after_conv = input->num_dimensions() > 1; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 280 | if(is_fc_after_conv) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 281 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 282 | // Fully Connected layer after a Convolution Layer without batches |
| 283 | 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] | 284 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 285 | // Validate im2col kernel |
| 286 | ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2col_input, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true)); |
| 287 | input_to_use = &im2col_input; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 288 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 289 | else |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 290 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 291 | // Fully Connected layer after a Fully Connected Layer without batches |
| 292 | 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] | 293 | } |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 294 | // Validate matrix multiply kernel |
| 295 | 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] | 296 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 297 | // Validate output stage for asymmetric quantized types |
| 298 | if(is_quantized) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 299 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 300 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&gemmlowp_output, biases, output)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return Status{}; |
| 304 | } |
| 305 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 306 | void NEFullyConnectedLayer::run() |
| 307 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 308 | prepare(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 309 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 310 | _memory_group.acquire(); |
| 311 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 312 | // Linearize input if it comes from a convolutional layer |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 313 | if(_is_fc_after_conv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 314 | { |
| 315 | NEScheduler::get().schedule(&_im2col_kernel, Window::DimY); |
| 316 | } |
| 317 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 318 | // Run matrix multiply |
| 319 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 320 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 321 | _mm_gemmlowp.run(); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | _mm_gemm.run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 328 | // Accumulate biases if provided |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 329 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 330 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 331 | _gemmlowp_output_stage.run(); |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | if(_accumulate_biases) |
| 336 | { |
| 337 | NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY); |
| 338 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 340 | |
| 341 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 342 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 343 | |
| 344 | void NEFullyConnectedLayer::prepare() |
| 345 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 346 | if(!_is_prepared) |
| 347 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 348 | // Reshape of the weights (happens only once) |
| 349 | if(!_are_weights_reshaped) |
| 350 | { |
| 351 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 352 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame^] | 353 | // Run reshape weights kernel and mark weights as unused |
| 354 | _reshape_weights_output.allocator()->allocate(); |
| 355 | _reshape_weights_function.run(); |
| 356 | _original_weights->mark_as_unused(); |
| 357 | |
| 358 | // Prepare GEMM prepare and release unused weights |
| 359 | if(!_is_quantized) |
| 360 | { |
| 361 | _mm_gemm.prepare(); |
| 362 | if(!_reshape_weights_output.is_used()) |
| 363 | { |
| 364 | _reshape_weights_output.allocator()->free(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | _are_weights_reshaped = true; |
| 369 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 370 | |
| 371 | _is_prepared = true; |
| 372 | } |
| 373 | } |