Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +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/CL/functions/CLFullyConnectedLayer.h" |
| 25 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 31 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | #include <algorithm> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 35 | using namespace arm_compute; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 36 | using namespace arm_compute::misc::shape_calculator; |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | Status validate_mm(const ITensorInfo &input, const ITensorInfo &weights, const ITensorInfo &output, bool is_interleaved_transposed) |
| 41 | { |
| 42 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 43 | |
| 44 | if(is_data_type_quantized_asymmetric(input.data_type())) |
| 45 | { |
| 46 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 47 | // Extract and negate input and weights offset |
| 48 | const QuantizationInfo input_quantization_info(input.quantization_info().scale, -input.quantization_info().offset); |
| 49 | const QuantizationInfo weights_quantization_info(weights.quantization_info().scale, -weights.quantization_info().offset); |
| 50 | |
| 51 | // Validate gemmlowp function |
| 52 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input.clone()->set_quantization_info(input_quantization_info), |
| 53 | &weights.clone()->set_quantization_info(weights_quantization_info), |
| 54 | &output)); |
| 55 | } |
| 56 | else |
| 57 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixMultiplyKernel::validate(&input, &weights, &output, 1.f, is_interleaved_transposed, GEMMReshapeInfo(), gpu_target)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return Status{}; |
| 62 | } |
| 63 | } // namespace |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 64 | |
| 65 | void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 66 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 67 | auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>(); |
| 68 | k->configure(input, output); |
| 69 | _kernel = std::move(k); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 72 | Status CLFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 73 | { |
| 74 | return CLTransposeKernel::validate(input, output); |
| 75 | } |
| 76 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 77 | CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 78 | : _memory_group(memory_manager), _im2col_kernel(), _reshape_weights_kernel(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _accumulate_biases_kernel(), _im2col_output(), |
| 79 | _gemmlowp_output(), _reshape_weights_output(), _are_weights_reshaped(true), _is_fc_after_conv(true), _accumulate_biases(false), _is_quantized(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 83 | void CLFullyConnectedLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed) |
| 84 | { |
| 85 | if(_is_quantized) |
| 86 | { |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 87 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 88 | // Extract and negate input and weights offset |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 89 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 90 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 91 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 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)); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 94 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 95 | // Configure gemmlowp function |
| 96 | _mm_gemmlowp.configure(input, weights, output); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 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); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
| 104 | // Configure matrix multiply kernel |
| 105 | _mm_kernel.set_target(CLScheduler::get().target()); |
| 106 | _mm_kernel.configure(input, weights, output, 1.f, is_interleaved_transposed); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void CLFullyConnectedLayer::configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
| 113 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 114 | // If the fully connected layer is called after a convolution layer, the input tensor must be linearized |
| 115 | |
| 116 | // Initialize output tensor for im2col |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame^] | 117 | TensorShape shape_im2col = compute_im2col_shape(input->info()); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 118 | _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 119 | |
| 120 | // Configure im2col kernel |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 121 | _memory_group.manage(&_im2col_output); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 122 | _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false); |
| 123 | |
| 124 | // Configure matrix multiply kernel |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 125 | configure_mm(&_im2col_output, weights, output, false); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 126 | |
| 127 | // Allocate the output tensor for im2col once all the configure methods have been called |
| 128 | _im2col_output.allocator()->allocate(); |
| 129 | } |
| 130 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 131 | void CLFullyConnectedLayer::configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 132 | { |
| 133 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 134 | |
| 135 | // Configure matrix multiply kernel |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 136 | configure_mm(input, weights, output, false); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights, bool are_weights_reshaped) |
| 140 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 141 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 142 | |
| 143 | // Perform validate step |
| 144 | ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(), |
| 145 | weights->info(), |
| 146 | biases != nullptr ? biases->info() : nullptr, |
| 147 | output->info(), |
| 148 | transpose_weights, |
| 149 | are_weights_reshaped)); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 150 | |
| 151 | _are_weights_reshaped = transpose_weights ? are_weights_reshaped : true; |
| 152 | _is_fc_after_conv = true; |
| 153 | _accumulate_biases = false; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 154 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 155 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 156 | // Configure gemmlowp output |
| 157 | if(_is_quantized) |
| 158 | { |
| 159 | _gemmlowp_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
| 160 | } |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 161 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 162 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 163 | if(biases != nullptr && !_is_quantized) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 164 | { |
| 165 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 166 | |
| 167 | _accumulate_biases = true; |
| 168 | |
| 169 | // Configure accumulate biases kernel |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 170 | _accumulate_biases_kernel.set_target(CLScheduler::get().target()); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 171 | _accumulate_biases_kernel.configure(output, biases); |
| 172 | } |
| 173 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | // With the Fully Connected layer we can have 4 different cases: |
| 175 | // 1) Convolution layer -> Fully Connected layer without batches |
| 176 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 177 | // 3) Convolution layer -> Fully Connected layer with batches |
| 178 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 179 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 180 | const ICLTensor *weights_to_use = weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 182 | if(!_are_weights_reshaped) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | { |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 184 | weights_to_use = &_reshape_weights_output; |
| 185 | |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 186 | // Reshape the weights |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 187 | _reshape_weights_kernel.configure(weights, &_reshape_weights_output); |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 190 | // Check if we have a fully connected layer with batches |
| 191 | const bool is_batched_fc_layer = output->info()->dimension(1) > 1; |
| 192 | |
| 193 | if(is_batched_fc_layer) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 194 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 195 | _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 196 | input->info()->tensor_shape().cend(), |
| 197 | output->info()->tensor_shape().cbegin() + 1)); |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 201 | _is_fc_after_conv = input->info()->num_dimensions() > 1; |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 204 | ICLTensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output; |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 205 | if(_is_fc_after_conv) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 206 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 207 | // Fully Connected layer after a Convolution Layer without batches |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 208 | configure_conv_fc(input, weights_to_use, tmp_output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 210 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 212 | // Fully Connected layer after a Fully Connected Layer without batches |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 213 | configure_fc_fc(input, weights_to_use, tmp_output); |
| 214 | } |
| 215 | |
| 216 | // Configure output stage for asymmetric quantized types |
| 217 | if(_is_quantized) |
| 218 | { |
| 219 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 220 | int output_multiplier, output_shift; |
| 221 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 222 | _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 223 | _gemmlowp_output.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 227 | if(!_are_weights_reshaped) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | { |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 229 | // Allocate the tensor for the weights reshaped |
| 230 | _reshape_weights_output.allocator()->allocate(); |
| 231 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 234 | Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose_weights, bool are_weights_reshaped) |
| 235 | { |
| 236 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 237 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 238 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
| 239 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2); |
| 240 | |
| 241 | bool weights_reshaped = transpose_weights ? are_weights_reshaped : true; |
| 242 | bool is_fc_after_conv = true; |
| 243 | bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 244 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 245 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame^] | 246 | const ITensorInfo &im2col_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_im2col_shape(input))); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 247 | const ITensorInfo &reshaped_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*weights))); |
| 248 | const ITensorInfo &gemmlowp_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
| 249 | |
| 250 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 251 | if(biases != nullptr && !is_quantized) |
| 252 | { |
| 253 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 254 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixAccumulateBiasesKernel::validate(output, biases, gpu_target)); |
| 255 | } |
| 256 | |
| 257 | // With the Fully Connected layer we can have 4 different cases: |
| 258 | // 1) Convolution layer -> Fully Connected layer without batches |
| 259 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 260 | // 3) Convolution layer -> Fully Connected layer with batches |
| 261 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 262 | |
| 263 | const ITensorInfo *input_to_use = input; |
| 264 | const ITensorInfo *weights_to_use = weights; |
| 265 | const ITensorInfo *tmp_output = (is_quantized) ? &gemmlowp_output : output; |
| 266 | |
| 267 | if(!weights_reshaped) |
| 268 | { |
| 269 | // Validate reshape weights kernel |
| 270 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayerReshapeWeights::validate(weights, &reshaped_weights)); |
| 271 | weights_to_use = &reshaped_weights; |
| 272 | } |
| 273 | |
| 274 | // Check if we have a fully connected layer with batches |
| 275 | const bool is_batched_fc_layer = output->dimension(1) > 1; |
| 276 | |
| 277 | if(is_batched_fc_layer) |
| 278 | { |
| 279 | 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)); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | is_fc_after_conv = input->num_dimensions() > 1; |
| 286 | } |
| 287 | |
| 288 | if(is_fc_after_conv) |
| 289 | { |
| 290 | // Fully Connected layer after a Convolution Layer without batches |
| 291 | ARM_COMPUTE_RETURN_ERROR_ON((weights_to_use->dimension(1) != (input->dimension(0) * input->dimension(1) * input->dimension(2)))); |
| 292 | |
| 293 | // Validate im2col kernel |
| 294 | ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_input, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false)); |
| 295 | input_to_use = &im2col_input; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | // Fully Connected layer after a Fully Connected Layer without batches |
| 300 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_to_use->dimension(1)); |
| 301 | } |
| 302 | // Validate matrix multiply kernel |
| 303 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(*input_to_use, *weights_to_use, *tmp_output, false)); |
| 304 | |
| 305 | // Validate output stage for asymmetric quantized types |
| 306 | if(is_quantized) |
| 307 | { |
| 308 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&gemmlowp_output, biases, output)); |
| 309 | } |
| 310 | |
| 311 | return Status{}; |
| 312 | } |
| 313 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 314 | void CLFullyConnectedLayer::run() |
| 315 | { |
| 316 | // Reshape of the weights (happens only once) |
| 317 | if(!_are_weights_reshaped) |
| 318 | { |
| 319 | _are_weights_reshaped = true; |
| 320 | _reshape_weights_kernel.run(); |
| 321 | } |
| 322 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 323 | _memory_group.acquire(); |
| 324 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 325 | // Linearize input if it comes from a convolutional layer |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 326 | if(_is_fc_after_conv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 327 | { |
| 328 | CLScheduler::get().enqueue(_im2col_kernel, false); |
| 329 | } |
| 330 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 331 | // Run matrix multiply |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 332 | if(_is_quantized) |
| 333 | { |
| 334 | _mm_gemmlowp.run(); |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | CLScheduler::get().enqueue(_mm_kernel, !_accumulate_biases); |
| 339 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 340 | |
| 341 | // Accumulate biases if provided |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 342 | if(_is_quantized) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 343 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 344 | _gemmlowp_output_stage.run(); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | if(_accumulate_biases) |
| 349 | { |
| 350 | CLScheduler::get().enqueue(_accumulate_biases_kernel); |
| 351 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 353 | |
| 354 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 355 | } |