Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [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/CLGEMMConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
| 27 | #include "arm_compute/core/Size2D.h" |
| 28 | #include "arm_compute/core/Utils.h" |
| 29 | #include "arm_compute/core/Validate.h" |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/utils/misc/Cast.h" |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 34 | |
| 35 | #include <cmath> |
| 36 | #include <memory> |
| 37 | #include <tuple> |
| 38 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 41 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 42 | using namespace arm_compute::utils::cast; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 43 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 44 | CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights() |
| 45 | : _weights_reshape_kernel() |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 49 | void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 50 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 51 | // Perform validation step |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayerReshapeWeights::validate(weights->info(), |
| 54 | (biases != nullptr) ? biases->info() : nullptr, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 55 | output->info(), |
| 56 | num_groups)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 57 | |
| 58 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
| 59 | const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 60 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 61 | _weights_reshape_kernel.configure(weights, biases_to_use, output, num_groups); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 62 | |
| 63 | output->info()->set_quantization_info(weights->info()->quantization_info()); |
| 64 | } |
| 65 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 66 | Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::F16, DataType::F32); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 71 | |
| 72 | if(biases != nullptr) |
| 73 | { |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 74 | const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(weights->data_type())); |
| 76 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 79 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 82 | if((output != nullptr) && (output->total_size() != 0)) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 83 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 84 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 85 | CLWeightsReshapeKernel::validate(weights, biases, output, num_groups); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 88 | return Status{}; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void CLConvolutionLayerReshapeWeights::run() |
| 92 | { |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 93 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 96 | CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager) |
| 97 | : _memory_group(memory_manager), _weights_manager(weights_manager), _reshape_weights(), _reshape_weights_managed(), _im2col_kernel(), _mm_gemm(memory_manager, weights_manager), |
| 98 | _mm_gemmlowp(memory_manager), _col2im_kernel(), _activationlayer_function(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _skip_im2col(false), |
| 99 | _skip_col2im(false), _is_quantized(false), _fuse_activation(true), _is_prepared(false) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 100 | { |
| 101 | } |
| 102 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 103 | void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage, |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 104 | int gemm_3d_depth, const ActivationLayerInfo &act_info) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 105 | { |
| 106 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), gemmlowp_output_stage, gemm_3d_depth, _skip_im2col, act_info)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 108 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 109 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 110 | false, // is_b_reshaped |
| 111 | true, // reshape_b_only_on_first_run |
| 112 | gemm_3d_depth, // depth_output_gemm3d |
| 113 | _skip_im2col, // reinterpret_input_as_3d |
| 114 | false, // retain_internal_weights |
| 115 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 116 | false, // fp_mixed_precision |
| 117 | true, // broadcast_bias |
| 118 | act_info); // activation_info |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 119 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 120 | if(_is_quantized) |
| 121 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 122 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 123 | // Extract and negate input and weights offset |
| 124 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 125 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 126 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 127 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 128 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 129 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 130 | _mm_gemmlowp.configure(input, weights, biases, output, gemm_info); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 131 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 132 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 133 | input->info()->set_quantization_info(input_quantization_info); |
| 134 | weights->info()->set_quantization_info(weights_quantization_info); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 138 | // Configure matrix multiply function |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 139 | _mm_gemm.configure(input, weights, biases, output, 1.0f, 1.0f, gemm_info); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 143 | Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 144 | const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 145 | { |
| 146 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 147 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 148 | const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped |
| 149 | false, // is_b_reshaped |
| 150 | true, // reshape_b_only_on_first_run |
| 151 | gemm_3d_depth, // depth_output_gemm3d |
| 152 | skip_im2col, // reinterpret_input_as_3d |
| 153 | false, // retain_internal_weights |
| 154 | gemmlowp_output_stage, // gemmlowp_output_stage |
| 155 | false, // fp_mixed_precision |
| 156 | true, // broadcast_bias |
| 157 | act_info); // activation_info |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 158 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 159 | if(is_quantized) |
| 160 | { |
| 161 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 162 | // Extract and negate input and weights offset |
| 163 | const QuantizationInfo input_quantization_info = input->quantization_info(); |
| 164 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 165 | |
| 166 | std::unique_ptr<ITensorInfo> input_qa = input->clone(); |
| 167 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 168 | input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset)); |
| 169 | weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 170 | |
| 171 | // Perform validation step on GEMMLowp |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 172 | return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, gemm_info); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
| 176 | // Perform validation step on Matrix multiply function |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 177 | return CLGEMM::validate(input, weights, biases, output, 1.0f, 1.0f, gemm_info); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 178 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 181 | void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 182 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 183 | { |
| 184 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 185 | |
| 186 | ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(), |
| 187 | weights->info(), |
| 188 | biases != nullptr ? biases->info() : nullptr, |
| 189 | output->info(), |
| 190 | conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 191 | weights_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 192 | dilation, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 193 | act_info, |
| 194 | num_groups)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 195 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 196 | const DataType data_type = input->info()->data_type(); |
| 197 | const DataLayout data_layout = input->info()->data_layout(); |
| 198 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 199 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 200 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 201 | |
| 202 | const unsigned int kernel_width = weights->info()->dimension(idx_width); |
| 203 | const unsigned int kernel_height = weights->info()->dimension(idx_height); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 204 | const unsigned int num_kernels = weights->info()->dimension(idx_kernels); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 205 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 206 | const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 207 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 208 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 209 | _is_prepared = weights_info.retain_internal_weights(); |
| 210 | _original_weights = weights; |
| 211 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 212 | _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1); |
| 213 | _skip_col2im = data_layout == DataLayout::NHWC; |
| 214 | |
| 215 | // Only for quantize there are few cases where we cannot fuse the activation function in GEMM |
| 216 | _fuse_activation = true; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 217 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 218 | // Set the GPU target for im2col and col2im |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 219 | _im2col_kernel.set_target(CLScheduler::get().target()); |
| 220 | _col2im_kernel.set_target(CLScheduler::get().target()); |
| 221 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 222 | const ICLTensor *gemm_input_to_use = input; |
| 223 | ICLTensor *gemm_output_to_use = output; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 224 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 225 | // Get parameters from conv_info |
| 226 | unsigned int stride_x = 0; |
| 227 | unsigned int stride_y = 0; |
| 228 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 229 | |
| 230 | // Get convolved dimensions |
| 231 | unsigned int conv_w = 0; |
| 232 | unsigned int conv_h = 0; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 233 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width), |
| 234 | input->info()->dimension(idx_height), |
| 235 | kernel_width, |
| 236 | kernel_height, |
| 237 | conv_info, |
| 238 | dilation); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 239 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 240 | unsigned int mat_weights_cols = num_kernels / num_groups; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 241 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 242 | const ICLTensor *biases_to_use = biases; |
| 243 | bool append_bias = false; |
| 244 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 245 | ICLTensor *weights_to_use = &_weights_reshaped; |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 246 | if(num_groups != 1 && biases != nullptr) |
| 247 | { |
| 248 | // num_groups != 1 can only be for NCHW |
| 249 | // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor |
| 250 | biases_to_use = nullptr; |
| 251 | append_bias = true; |
| 252 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 253 | if(_weights_manager && _weights_manager->are_weights_managed(weights)) |
| 254 | { |
| 255 | _reshape_weights_managed.configure(weights, biases, num_groups); |
| 256 | weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed)); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | _reshape_weights.configure(weights, biases, &_weights_reshaped, num_groups); |
| 261 | } |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 262 | } |
| 263 | else |
| 264 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 265 | if(_weights_manager && _weights_manager->are_weights_managed(weights)) |
| 266 | { |
| 267 | _reshape_weights_managed.configure(weights, nullptr, num_groups); |
| 268 | weights_to_use = utils::cast::polymorphic_downcast<ICLTensor *>(_weights_manager->acquire(weights, &_reshape_weights_managed)); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | _reshape_weights.configure(weights, nullptr, &_weights_reshaped, num_groups); |
| 273 | } |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 274 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 275 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 276 | // Create tensor to store im2col reshaped inputs |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 277 | if(!_skip_im2col) |
| 278 | { |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 279 | _memory_group.manage(&_im2col_output); |
| 280 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 281 | // Configure and tune im2col. im2col output shape is auto-initialized |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 282 | _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation, num_groups); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 283 | |
| 284 | // Set quantization info |
| 285 | _im2col_output.info()->set_quantization_info(input->info()->quantization_info()); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 286 | CLScheduler::get().tune_kernel_static(_im2col_kernel); |
| 287 | |
| 288 | // Update GEMM input |
| 289 | gemm_input_to_use = &_im2col_output; |
| 290 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 291 | |
| 292 | // Create GEMM output tensor |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 293 | if(!_skip_col2im) |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 294 | { |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 295 | TensorShape shape_gemm; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 296 | |
| 297 | // If we cannot skip col2im it means we run im2col as well |
| 298 | shape_gemm = _im2col_output.info()->tensor_shape(); |
| 299 | shape_gemm.set(0, mat_weights_cols); |
| 300 | shape_gemm.set(1, conv_w * conv_h); |
| 301 | |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 302 | // TODO(COMPMID-2078): input->clone() doesn't work with subtensors for grouped convolutions. |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 303 | TensorInfo info_gemm(shape_gemm, 1, data_type); |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 304 | info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout()); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 305 | _gemm_output.allocator()->init(info_gemm); |
| 306 | _memory_group.manage(&_gemm_output); |
| 307 | |
| 308 | // Update GEMM output |
| 309 | gemm_output_to_use = &_gemm_output; |
| 310 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 311 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 312 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 313 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 314 | gemmlowp_output_stage.gemmlowp_offset = 0; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 315 | |
| 316 | // Configure output stage for quantized case |
| 317 | if(_is_quantized) |
| 318 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 319 | const auto output_quant_info = (output->info()->total_size() == 0) ? iq_info : oq_info; |
| 320 | const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->info()->data_type()); |
| 321 | const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1; |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 322 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 323 | gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel; |
| 324 | |
| 325 | gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters); |
| 326 | gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters); |
| 327 | quantization::compute_quantized_multipliers_and_shifts(input->info(), |
| 328 | weights->info(), |
| 329 | output->info(), |
| 330 | idx_kernels, |
| 331 | gemmlowp_output_stage.gemmlowp_multipliers.data(), |
| 332 | gemmlowp_output_stage.gemmlowp_shifts.data()); |
| 333 | gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0]; |
| 334 | gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0]; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 335 | |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 336 | PixelValue min_val{}; |
| 337 | PixelValue max_val{}; |
| 338 | std::tie(min_val, max_val) = get_min_max(output->info()->data_type()); |
| 339 | |
| 340 | auto min_activation = min_val.get<int32_t>(); |
| 341 | auto max_activation = max_val.get<int32_t>(); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 342 | |
| 343 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 344 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 345 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 346 | }; |
| 347 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 348 | if(act_info.enabled()) |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 349 | { |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 350 | if(supported_acts.count(act_info.activation()) != 0) |
| 351 | { |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 352 | std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, output_quant_info); |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 353 | } |
| 354 | else |
| 355 | { |
| 356 | _fuse_activation = false; |
| 357 | } |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 358 | } |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 359 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 360 | // Set the GEMMLowp output stage info |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 361 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 362 | gemmlowp_output_stage.gemmlowp_min_bound = min_activation; |
| 363 | gemmlowp_output_stage.gemmlowp_max_bound = max_activation; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Configure and tune GEMM |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 367 | // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix |
| 368 | const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0; |
| 369 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 370 | configure_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, act_info); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 371 | |
| 372 | if(!_skip_im2col) |
| 373 | { |
| 374 | _im2col_output.allocator()->allocate(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 377 | if(!_skip_col2im) |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 378 | { |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 379 | // Configure and tune Col2Im |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 380 | _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups); |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 381 | CLScheduler::get().tune_kernel_static(_col2im_kernel); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 384 | if(!_skip_col2im) |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 385 | { |
| 386 | _gemm_output.allocator()->allocate(); |
| 387 | } |
| 388 | |
| 389 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h), |
| 390 | "Output shape does not match the expected one"); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 391 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 392 | if(!_fuse_activation) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 393 | { |
| 394 | _activationlayer_function.configure(output, nullptr, act_info); |
| 395 | } |
| 396 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 397 | ARM_COMPUTE_UNUSED(weights_info); |
| 398 | } |
| 399 | |
| 400 | Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 401 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 402 | { |
| 403 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 404 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 405 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 406 | const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type()); |
| 407 | |
| 408 | if(is_quantized_per_channel) |
| 409 | { |
| 410 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() != DataType::QASYMM8, "Input data type not compatible with Weights"); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 415 | } |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 416 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 417 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_layout() != DataLayout::NCHW), "Grouping (num_groups != 1) with NHWC data layout is not supported"); |
| 418 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8"); |
| 419 | ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 420 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 421 | const DataLayout data_layout = input->data_layout(); |
| 422 | const DataType data_type = input->data_type(); |
| 423 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 424 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 425 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 426 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 427 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 428 | const unsigned int kernel_width = weights->dimension(idx_width); |
| 429 | const unsigned int kernel_height = weights->dimension(idx_height); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 430 | const unsigned int num_kernels = weights->dimension(idx_kernels); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 431 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 432 | TensorInfo im2col_reshaped_info{}; |
| 433 | TensorInfo info_gemm{}; |
| 434 | TensorInfo weights_reshaped_info{}; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 435 | const ITensorInfo *gemm_input_to_use = input; |
| 436 | const ITensorInfo *gemm_output_to_use = output; |
| 437 | const ITensorInfo *weights_to_use = weights; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 438 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
| 439 | const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1); |
| 440 | const bool skip_col2im = data_layout == DataLayout::NHWC; |
| 441 | bool fuse_activation = true; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 442 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 443 | ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel)); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 444 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 445 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 446 | // Validate biases |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 447 | if(biases != nullptr) |
| 448 | { |
| 449 | if(is_quantized) |
| 450 | { |
| 451 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 456 | } |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 457 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 458 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 459 | } |
| 460 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 461 | if(act_info.enabled()) |
| 462 | { |
| 463 | ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a()); |
| 464 | } |
| 465 | |
| 466 | // Get convolved dimensions |
| 467 | unsigned int conv_w = 0; |
| 468 | unsigned int conv_h = 0; |
| 469 | |
| 470 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), |
| 471 | input->dimension(idx_height), |
| 472 | kernel_width, |
| 473 | kernel_height, |
| 474 | conv_info, |
| 475 | dilation); |
| 476 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 477 | unsigned int mat_weights_cols = num_kernels / num_groups; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 478 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 479 | const ITensorInfo *biases_to_use = biases; |
| 480 | bool append_bias = false; |
| 481 | |
| 482 | if(num_groups != 1 && biases != nullptr) |
| 483 | { |
| 484 | // num_groups != 1 can only be for NCHW |
| 485 | // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor |
| 486 | biases_to_use = nullptr; |
| 487 | append_bias = true; |
| 488 | |
| 489 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, biases, nullptr, num_groups)); |
| 490 | weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, true, num_groups), 1, data_type); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, nullptr, nullptr, num_groups)); |
| 495 | weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, false, num_groups), 1, data_type); |
| 496 | } |
| 497 | |
| 498 | weights_to_use = &weights_reshaped_info; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 499 | |
| 500 | if(!skip_im2col) |
| 501 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 502 | const Size2D kernel_dims(kernel_width, kernel_height); |
| 503 | |
| 504 | // Output tensor auto initialization if not yet initialized |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 505 | TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 506 | |
| 507 | auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape)); |
| 508 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 509 | ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups)); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 510 | gemm_input_to_use = &im2col_reshaped_info; |
| 511 | } |
| 512 | |
| 513 | // Create GEMM output tensor |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 514 | if(!skip_col2im) |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 515 | { |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 516 | TensorShape shape_gemm; |
| 517 | |
| 518 | shape_gemm = gemm_input_to_use->tensor_shape(); |
| 519 | shape_gemm.set(0, mat_weights_cols); |
| 520 | shape_gemm.set(1, conv_w * conv_h); |
| 521 | |
| 522 | info_gemm = TensorInfo(shape_gemm, 1, data_type); |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 523 | info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout()); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 524 | gemm_output_to_use = &info_gemm; |
| 525 | } |
| 526 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 527 | GEMMLowpOutputStageInfo gemmlowp_output_stage; |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 528 | gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 529 | gemmlowp_output_stage.gemmlowp_offset = 0; |
| 530 | gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 531 | |
| 532 | if(is_quantized) |
| 533 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 534 | const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); |
| 535 | const UniformQuantizationInfo oq_info = output->quantization_info().uniform(); |
| 536 | const auto output_quant_info = (output->total_size() == 0) ? iq_info : oq_info; |
| 537 | const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 538 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 539 | gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters); |
| 540 | gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters); |
| 541 | quantization::compute_quantized_multipliers_and_shifts(input, |
| 542 | weights, |
| 543 | output, |
| 544 | idx_kernels, |
| 545 | gemmlowp_output_stage.gemmlowp_multipliers.data(), |
| 546 | gemmlowp_output_stage.gemmlowp_shifts.data()); |
| 547 | gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0]; |
| 548 | gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0]; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 549 | |
| 550 | int min_activation = 0; |
| 551 | int max_activation = 0; |
| 552 | |
| 553 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 554 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 555 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 556 | }; |
| 557 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 558 | if(act_info.enabled()) |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 559 | { |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 560 | if(supported_acts.count(act_info.activation()) != 0) |
| 561 | { |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 562 | std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, output_quant_info); |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 563 | } |
| 564 | else |
| 565 | { |
| 566 | fuse_activation = false; |
| 567 | } |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 568 | } |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 569 | |
| 570 | // Set the GEMMLowp output stage info |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 571 | gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset; |
| 572 | gemmlowp_output_stage.gemmlowp_min_bound = min_activation; |
| 573 | gemmlowp_output_stage.gemmlowp_max_bound = max_activation; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 576 | // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix |
| 577 | const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0; |
| 578 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 579 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, skip_im2col, act_info)); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 580 | |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 581 | // Validate Col2Im |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 582 | if(!skip_col2im) |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 583 | { |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 584 | ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups)); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 587 | //Validate Activation Layer |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 588 | if(!fuse_activation) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 589 | { |
Vidhya Sudhan Loganathan | edf357c | 2018-04-27 14:25:30 +0100 | [diff] [blame] | 590 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 593 | return Status{}; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | void CLGEMMConvolutionLayer::run() |
| 597 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 598 | prepare(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 599 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 600 | MemoryGroupResourceScope scope_mg(_memory_group); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 601 | |
| 602 | // Run im2col |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 603 | if(!_skip_im2col) |
| 604 | { |
| 605 | CLScheduler::get().enqueue(_im2col_kernel); |
| 606 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 607 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 608 | // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions |
| 609 | if(_is_quantized) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 610 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 611 | // Run gemmlowp |
| 612 | _mm_gemmlowp.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 613 | } |
| 614 | else |
| 615 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 616 | // Run gemm |
| 617 | _mm_gemm.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Reshape output matrix |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 621 | if(!_skip_col2im) |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 622 | { |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 623 | CLScheduler::get().enqueue(_col2im_kernel, false); |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 624 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 625 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 626 | //Run Activation Layer if we cannot fuse in GEMM |
| 627 | if(!_fuse_activation) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 628 | { |
| 629 | _activationlayer_function.run(); |
| 630 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 631 | } |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 632 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 633 | void CLGEMMConvolutionLayer::prepare() |
| 634 | { |
| 635 | if(!_is_prepared) |
| 636 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 637 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 638 | if(_weights_manager && _weights_manager->are_weights_managed(_original_weights)) |
| 639 | { |
| 640 | _weights_manager->run(_original_weights, &_reshape_weights_managed); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | // Run weights reshaping and mark original weights tensor as unused |
| 645 | _weights_reshaped.allocator()->allocate(); |
| 646 | _reshape_weights.run(); |
| 647 | _original_weights->mark_as_unused(); |
| 648 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 649 | |
| 650 | // Prepare GEMM |
| 651 | _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare(); |
| 652 | if(!_weights_reshaped.is_used()) |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 653 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 654 | _weights_reshaped.allocator()->free(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | CLScheduler::get().queue().finish(); |
| 658 | _is_prepared = true; |
| 659 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 660 | } |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 661 | } // namespace arm_compute |