Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/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 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 46 | void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output) |
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, |
| 52 | output->info())); |
| 53 | |
| 54 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
| 55 | const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 56 | |
| 57 | _weights_reshape_kernel.configure(weights, biases_to_use, output); |
| 58 | |
| 59 | output->info()->set_quantization_info(weights->info()->quantization_info()); |
| 60 | } |
| 61 | |
| 62 | Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output) |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 67 | |
| 68 | if(biases != nullptr) |
| 69 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type())); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 76 | if((output != nullptr) && (output->total_size() != 0)) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 77 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 80 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 81 | CLWeightsReshapeKernel::validate(weights, biases, output); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 84 | return Status{}; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void CLConvolutionLayerReshapeWeights::run() |
| 88 | { |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 89 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 93 | : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(), |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 94 | _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false), |
| 95 | _retain_internal_weights(false) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 99 | void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 100 | { |
| 101 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 102 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info())); |
| 103 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 104 | if(_is_quantized) |
| 105 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 106 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 107 | // Extract and negate input and weights offset |
| 108 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 109 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 110 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 111 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 112 | 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] | 113 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 114 | _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 115 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 116 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 117 | input->info()->set_quantization_info(input_quantization_info); |
| 118 | weights->info()->set_quantization_info(weights_quantization_info); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 122 | // Configure matrix multiply function |
| 123 | _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 127 | Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output) |
| 128 | { |
| 129 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 130 | |
| 131 | const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */); |
| 132 | if(is_quantized) |
| 133 | { |
| 134 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 135 | // Extract and negate input and weights offset |
| 136 | const QuantizationInfo input_quantization_info = input->quantization_info(); |
| 137 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 138 | |
| 139 | std::unique_ptr<ITensorInfo> input_qa = input->clone(); |
| 140 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
| 141 | input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 142 | weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 143 | |
| 144 | // Perform validation step on GEMMLowp |
| 145 | CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | // Perform validation step on Matrix multiply function |
| 150 | CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info); |
| 151 | } |
| 152 | return Status{}; |
| 153 | } |
| 154 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 155 | void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 156 | const Size2D &dilation, const ActivationLayerInfo &act_info) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 157 | { |
| 158 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 159 | |
| 160 | ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(), |
| 161 | weights->info(), |
| 162 | biases != nullptr ? biases->info() : nullptr, |
| 163 | output->info(), |
| 164 | conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 165 | weights_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 166 | dilation, |
| 167 | act_info)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 168 | |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 169 | _is_prepared = false; |
| 170 | _original_weights = weights; |
| 171 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 172 | _retain_internal_weights = weights_info.retain_internal_weights(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 173 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 174 | const DataType dt = input->info()->data_type(); |
| 175 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 176 | // Set the GPU target for im2col and col2im |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 177 | _im2col_kernel.set_target(CLScheduler::get().target()); |
| 178 | _col2im_kernel.set_target(CLScheduler::get().target()); |
| 179 | |
| 180 | const bool append_bias = (biases != nullptr) && (!_is_quantized); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 181 | |
| 182 | const unsigned bias_element = (append_bias) ? 1 : 0; |
| 183 | const ICLTensor *biases_to_use = (append_bias) ? biases : nullptr; |
| 184 | |
| 185 | // Get parameters from conv_info |
| 186 | unsigned int stride_x = 0; |
| 187 | unsigned int stride_y = 0; |
| 188 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 189 | |
| 190 | // Get convolved dimensions |
| 191 | unsigned int conv_w = 0; |
| 192 | unsigned int conv_h = 0; |
| 193 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 194 | const unsigned int kernel_width = weights->info()->dimension(0); |
| 195 | const unsigned int kernel_height = weights->info()->dimension(1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 196 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 197 | conv_info, dilation); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 198 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 199 | unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 200 | unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
| 201 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 202 | // _weights_reshaped will be auto configured in the kernel. |
| 203 | // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM |
| 204 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 205 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 206 | weights = &_weights_reshaped; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 207 | |
| 208 | // Create tensor to store im2col reshaped inputs |
| 209 | const unsigned int mat_input_cols = mat_weights_rows; |
| 210 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 211 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 212 | shape_im2col.set(0, mat_input_cols); |
| 213 | shape_im2col.set(1, mat_input_rows); |
| 214 | shape_im2col.set(2, 1); |
| 215 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 216 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position()); |
| 217 | im2col_reshaped_info.set_quantization_info(input->info()->quantization_info()); |
| 218 | _im2col_output.allocator()->init(im2col_reshaped_info); |
| 219 | _memory_group.manage(&_im2col_output); |
| 220 | |
| 221 | // Create GEMM output tensor |
| 222 | TensorShape shape_gemm = _im2col_output.info()->tensor_shape(); |
| 223 | shape_gemm.set(0, mat_weights_cols); |
| 224 | shape_gemm.set(1, mat_input_rows); |
| 225 | const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt; |
| 226 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 227 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 228 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position()); |
| 229 | info_gemm.set_quantization_info(output->info()->quantization_info()); |
| 230 | _gemm_output.allocator()->init(info_gemm); |
| 231 | _memory_group.manage(&_gemm_output); |
| 232 | |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 233 | // Configure and tune im2col |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 234 | _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation); |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 235 | CLScheduler::get().tune_kernel_static(_im2col_kernel); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 236 | |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 237 | // Configure and tune GEMM |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 238 | configure_mm(&_im2col_output, weights, &_gemm_output); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 239 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 240 | _im2col_output.allocator()->allocate(); |
| 241 | |
| 242 | // Configure output stage for quantized case |
| 243 | if(_is_quantized) |
| 244 | { |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 245 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 246 | |
| 247 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 248 | int output_multiplier, output_shift; |
| 249 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 250 | _memory_group.manage(&_tmp_output); |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 251 | _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 254 | // Configure and tune Col2Im |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 255 | _col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, std::make_pair(conv_w, conv_h)); |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 256 | CLScheduler::get().tune_kernel_static(_col2im_kernel); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 257 | if(_is_quantized) |
| 258 | { |
| 259 | _tmp_output.allocator()->allocate(); |
| 260 | } |
| 261 | _gemm_output.allocator()->allocate(); |
| 262 | |
| 263 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 264 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 265 | //Configure Activation Layer |
| 266 | _is_activationlayer_enabled = act_info.enabled(); |
| 267 | |
| 268 | if(_is_activationlayer_enabled) |
| 269 | { |
| 270 | _activationlayer_function.configure(output, nullptr, act_info); |
| 271 | } |
| 272 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 273 | ARM_COMPUTE_UNUSED(weights_info); |
| 274 | } |
| 275 | |
| 276 | Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 277 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 278 | { |
| 279 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 280 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
| 281 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 282 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 283 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights); |
| 284 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2)); |
| 285 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 286 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 287 | if(act_info.enabled()) |
| 288 | { |
| 289 | ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a()); |
| 290 | } |
| 291 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 292 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 293 | const bool append_bias = (biases != nullptr) && (!is_quantized); |
| 294 | const unsigned bias_element = (append_bias) ? 1 : 0; |
| 295 | const DataType dt = input->data_type(); |
| 296 | |
| 297 | // Get convolved dimensions |
| 298 | unsigned int conv_w = 0; |
| 299 | unsigned int conv_h = 0; |
| 300 | |
| 301 | const unsigned int kernel_width = weights->dimension(0); |
| 302 | const unsigned int kernel_height = weights->dimension(1); |
| 303 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 304 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, conv_info, dilation); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 305 | |
| 306 | unsigned int mat_weights_cols = weights->dimension(3); |
| 307 | unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + bias_element; |
| 308 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 309 | ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 310 | |
| 311 | // Create tensor info for im2col reshaped inputs |
| 312 | const unsigned int mat_input_cols = mat_weights_rows; |
| 313 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 314 | TensorShape shape_im2col = input->tensor_shape(); |
| 315 | shape_im2col.set(0, mat_input_cols); |
| 316 | shape_im2col.set(1, mat_input_rows); |
| 317 | shape_im2col.set(2, 1); |
| 318 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->fixed_point_position()); |
| 319 | im2col_reshaped_info.set_quantization_info(input->quantization_info()); |
Vidhya Sudhan Loganathan | edf357c | 2018-04-27 14:25:30 +0100 | [diff] [blame] | 320 | ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation)); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 321 | |
| 322 | // Create GEMM output tensor |
| 323 | TensorShape shape_gemm = im2col_reshaped_info.tensor_shape(); |
| 324 | shape_gemm.set(0, mat_weights_cols); |
| 325 | shape_gemm.set(1, mat_input_rows); |
| 326 | const DataType gemm_data_type = is_quantized ? DataType::S32 : dt; |
| 327 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 328 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->fixed_point_position()); |
| 329 | info_gemm.set_quantization_info(output->quantization_info()); |
| 330 | |
Vidhya Sudhan Loganathan | edf357c | 2018-04-27 14:25:30 +0100 | [diff] [blame] | 331 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(&im2col_reshaped_info, weights, &info_gemm)); |
| 332 | TensorInfo tmp_info(shape_gemm, 1, DataType::QASYMM8, input->fixed_point_position()); |
| 333 | tmp_info.set_quantization_info(output->quantization_info()); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 334 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 335 | if(is_quantized) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 336 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 337 | float multiplier = input->quantization_info().scale * weights->quantization_info().scale / output->quantization_info().scale; |
| 338 | int output_multiplier, output_shift; |
| 339 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 340 | // Validate output stage for quantized case |
| 341 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&info_gemm, biases, &tmp_info, output->quantization_info().offset); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 342 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 343 | |
| 344 | // Validate Col2Im |
Vidhya Sudhan Loganathan | edf357c | 2018-04-27 14:25:30 +0100 | [diff] [blame] | 345 | ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(is_quantized ? &tmp_info : &info_gemm, output, std::make_pair(conv_w, conv_h))); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 346 | |
| 347 | if(biases != nullptr) |
| 348 | { |
| 349 | if(is_quantized) |
| 350 | { |
| 351 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 356 | } |
| 357 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 358 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 359 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 360 | } |
| 361 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 362 | //Validate Activation Layer |
| 363 | if(act_info.enabled()) |
| 364 | { |
Vidhya Sudhan Loganathan | edf357c | 2018-04-27 14:25:30 +0100 | [diff] [blame] | 365 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 368 | return Status{}; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void CLGEMMConvolutionLayer::run() |
| 372 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 373 | prepare(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 374 | |
| 375 | _memory_group.acquire(); |
| 376 | |
| 377 | // Run im2col |
| 378 | CLScheduler::get().enqueue(_im2col_kernel); |
| 379 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 380 | // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions |
| 381 | if(_is_quantized) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 382 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 383 | // Run gemmlowp |
| 384 | _mm_gemmlowp.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 385 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 386 | // Run output stage |
| 387 | _gemmlowp_output_stage.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 388 | } |
| 389 | else |
| 390 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 391 | // Run gemm |
| 392 | _mm_gemm.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Reshape output matrix |
| 396 | CLScheduler::get().enqueue(_col2im_kernel, false); |
| 397 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 398 | //Run Activation Layer if enabled |
| 399 | if(_is_activationlayer_enabled) |
| 400 | { |
| 401 | _activationlayer_function.run(); |
| 402 | } |
| 403 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 404 | _memory_group.release(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 405 | } |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 406 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 407 | void CLGEMMConvolutionLayer::prepare() |
| 408 | { |
| 409 | if(!_is_prepared) |
| 410 | { |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 411 | if(!_retain_internal_weights) |
| 412 | { |
| 413 | // Run weights reshaping and mark as unused |
| 414 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 415 | _weights_reshaped.allocator()->allocate(); |
| 416 | _reshape_weights.run(); |
| 417 | _original_weights->mark_as_unused(); |
| 418 | } |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 419 | |
| 420 | // Run GEMM prepare |
| 421 | if(!_is_quantized) |
| 422 | { |
| 423 | _mm_gemm.prepare(); |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 424 | if(!_weights_reshaped.is_used() && !_retain_internal_weights) |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 425 | { |
| 426 | _weights_reshaped.allocator()->free(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | CLScheduler::get().queue().finish(); |
| 431 | _is_prepared = true; |
| 432 | } |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 433 | } |