Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 28 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 30 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 31 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 32 | #include <algorithm> |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 33 | |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 34 | using namespace arm_compute; |
| 35 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 36 | void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output) |
| 37 | { |
| 38 | auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>(); |
| 39 | k->configure(input, output); |
| 40 | _kernel = std::move(k); |
| 41 | } |
| 42 | |
| 43 | CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 44 | : _memory_group(memory_manager), _im2col_kernel(), _reshape_weights_kernel(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _accumulate_biases_kernel(), _im2col_output(), |
| 45 | _gemmlowp_output(), _reshape_weights_output(), _are_weights_reshaped(true), _is_fc_after_conv(true), _accumulate_biases(false), _is_quantized(false) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 49 | void CLFullyConnectedLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed) |
| 50 | { |
| 51 | if(_is_quantized) |
| 52 | { |
| 53 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 54 | // Extract and negate input and weights offset |
| 55 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 56 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 57 | |
| 58 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 59 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 60 | |
| 61 | // Configure gemmlowp function |
| 62 | _mm_gemmlowp.configure(input, weights, output); |
| 63 | |
| 64 | // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers |
| 65 | input->info()->set_quantization_info(input_quantization_info); |
| 66 | weights->info()->set_quantization_info(weights_quantization_info); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Configure matrix multiply kernel |
| 71 | _mm_kernel.set_target(CLScheduler::get().target()); |
| 72 | _mm_kernel.configure(input, weights, output, 1.f, is_interleaved_transposed); |
| 73 | } |
| 74 | } |
| 75 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 76 | void CLFullyConnectedLayer::configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 77 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 79 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 80 | // If the fully connected layer is called after a convolution layer, the input tensor must be linearized |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 81 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 82 | // Initialize output tensor for im2col |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 83 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 84 | shape_im2col.collapse(3); |
| 85 | _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 86 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 87 | // Configure im2col kernel |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 88 | _memory_group.manage(&_im2col_output); |
| 89 | _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 90 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 91 | // Configure matrix multiply kernel |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 92 | configure_mm(&_im2col_output, weights, output, false); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 93 | |
| 94 | // Allocate the output tensor for im2col once all the configure methods have been called |
| 95 | _im2col_output.allocator()->allocate(); |
| 96 | } |
| 97 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 98 | void CLFullyConnectedLayer::configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 101 | |
| 102 | // Configure matrix multiply kernel |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 103 | configure_mm(input, weights, output, false); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 106 | void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights, bool are_weights_reshaped) |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 107 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 108 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 109 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 110 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 2); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 111 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 112 | _are_weights_reshaped = transpose_weights ? are_weights_reshaped : true; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 113 | _is_fc_after_conv = true; |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 114 | _accumulate_biases = false; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 115 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 116 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 117 | // Configure gemmlowp output |
| 118 | if(_is_quantized) |
| 119 | { |
| 120 | _gemmlowp_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
| 121 | } |
| 122 | |
| 123 | // Configure accumulate biases kernel for non quantized asymmetric types |
| 124 | if(biases != nullptr && !_is_quantized) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 125 | { |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 127 | |
| 128 | _accumulate_biases = true; |
| 129 | |
| 130 | // Configure accumulate biases kernel |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 131 | _accumulate_biases_kernel.set_target(CLScheduler::get().target()); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 132 | _accumulate_biases_kernel.configure(output, biases); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 135 | // With the Fully Connected layer we can have 4 different cases: |
| 136 | // 1) Convolution layer -> Fully Connected layer without batches |
| 137 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 138 | // 3) Convolution layer -> Fully Connected layer with batches |
| 139 | // 4) Fully Connected layer -> Fully Connected layer with batches |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 140 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 141 | const ICLTensor *weights_to_use = weights; |
| 142 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 143 | if(!_are_weights_reshaped) |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 144 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 145 | weights_to_use = &_reshape_weights_output; |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 146 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 147 | // Reshape the weights |
| 148 | _reshape_weights_kernel.configure(weights, &_reshape_weights_output); |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 151 | // Check if we have a fully connected layer with batches |
| 152 | const bool is_batched_fc_layer = output->info()->dimension(1) > 1; |
| 153 | |
| 154 | if(is_batched_fc_layer) |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 155 | { |
| 156 | _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 157 | input->info()->tensor_shape().cend(), |
| 158 | output->info()->tensor_shape().cbegin() + 1)); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 159 | } |
| 160 | else |
| 161 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 162 | _is_fc_after_conv = input->info()->num_dimensions() > 1; |
| 163 | } |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 164 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 165 | ICLTensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output; |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 166 | if(_is_fc_after_conv) |
| 167 | { |
| 168 | // Fully Connected layer after a Convolution Layer without batches |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 169 | configure_conv_fc(input, weights_to_use, tmp_output); |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
| 173 | // Fully Connected layer after a Fully Connected Layer without batches |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 174 | configure_fc_fc(input, weights_to_use, tmp_output); |
| 175 | } |
| 176 | |
| 177 | // Configure output stage for asymmetric quantized types |
| 178 | if(_is_quantized) |
| 179 | { |
| 180 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 181 | int output_multiplier, output_shift; |
| 182 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 183 | _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
| 184 | _gemmlowp_output.allocator()->allocate(); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 187 | // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 188 | if(!_are_weights_reshaped) |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 189 | { |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 190 | // Allocate the tensor for the weights reshaped |
| 191 | _reshape_weights_output.allocator()->allocate(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void CLFullyConnectedLayer::run() |
| 196 | { |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 197 | // Reshape of the weights (happens only once) |
| 198 | if(!_are_weights_reshaped) |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 199 | { |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 200 | _are_weights_reshaped = true; |
| 201 | _reshape_weights_kernel.run(); |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 204 | _memory_group.acquire(); |
| 205 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 206 | // Linearize input if it comes from a convolutional layer |
Anthony Barbier | dbdab85 | 2017-06-23 15:42:00 +0100 | [diff] [blame] | 207 | if(_is_fc_after_conv) |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 208 | { |
| 209 | CLScheduler::get().enqueue(_im2col_kernel, false); |
| 210 | } |
| 211 | |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 212 | // Run matrix multiply |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 213 | if(_is_quantized) |
| 214 | { |
| 215 | _mm_gemmlowp.run(); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | CLScheduler::get().enqueue(_mm_kernel, !_accumulate_biases); |
| 220 | } |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 221 | |
| 222 | // Accumulate biases if provided |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 223 | if(_is_quantized) |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 224 | { |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame^] | 225 | _gemmlowp_output_stage.run(); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | if(_accumulate_biases) |
| 230 | { |
| 231 | CLScheduler::get().enqueue(_accumulate_biases_kernel); |
| 232 | } |
Anthony Barbier | a437638 | 2017-04-12 15:12:46 +0100 | [diff] [blame] | 233 | } |
Kaizen | 8938bd3 | 2017-09-28 14:38:23 +0100 | [diff] [blame] | 234 | |
| 235 | _memory_group.release(); |
Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [diff] [blame] | 236 | } |