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