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 | |
| 41 | CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 42 | : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped() |
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 | { |
| 89 | _memory_group.acquire(); |
| 90 | |
| 91 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 92 | |
| 93 | _memory_group.release(); |
| 94 | } |
| 95 | |
| 96 | CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 97 | : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _im2col_output(), |
| 98 | _interleave_output(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _is_quantized(false) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 99 | { |
| 100 | } |
| 101 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 102 | void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 103 | { |
| 104 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 105 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info())); |
| 106 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 107 | if(_is_quantized) |
| 108 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 109 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 110 | // Extract and negate input and weights offset |
| 111 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 112 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
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 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 115 | 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] | 116 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 117 | _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] | 118 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 119 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 120 | input->info()->set_quantization_info(input_quantization_info); |
| 121 | weights->info()->set_quantization_info(weights_quantization_info); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 125 | // Configure matrix multiply function |
| 126 | _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] | 127 | } |
| 128 | } |
| 129 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 130 | Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output) |
| 131 | { |
| 132 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 133 | |
| 134 | const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */); |
| 135 | if(is_quantized) |
| 136 | { |
| 137 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 138 | // Extract and negate input and weights offset |
| 139 | const QuantizationInfo input_quantization_info = input->quantization_info(); |
| 140 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 141 | |
| 142 | std::unique_ptr<ITensorInfo> input_qa = input->clone(); |
| 143 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
| 144 | input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 145 | weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 146 | |
| 147 | // Perform validation step on GEMMLowp |
| 148 | CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | // Perform validation step on Matrix multiply function |
| 153 | CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info); |
| 154 | } |
| 155 | return Status{}; |
| 156 | } |
| 157 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 158 | void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
| 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 161 | |
| 162 | ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(), |
| 163 | weights->info(), |
| 164 | biases != nullptr ? biases->info() : nullptr, |
| 165 | output->info(), |
| 166 | conv_info, |
| 167 | weights_info)); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 168 | |
| 169 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 170 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 171 | const DataType dt = input->info()->data_type(); |
| 172 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 173 | // Set the GPU target for im2col and col2im |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 174 | _im2col_kernel.set_target(CLScheduler::get().target()); |
| 175 | _col2im_kernel.set_target(CLScheduler::get().target()); |
| 176 | |
| 177 | const bool append_bias = (biases != nullptr) && (!_is_quantized); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 178 | |
| 179 | const unsigned bias_element = (append_bias) ? 1 : 0; |
| 180 | const ICLTensor *biases_to_use = (append_bias) ? biases : nullptr; |
| 181 | |
| 182 | // Get parameters from conv_info |
| 183 | unsigned int stride_x = 0; |
| 184 | unsigned int stride_y = 0; |
| 185 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 186 | |
| 187 | // Get convolved dimensions |
| 188 | unsigned int conv_w = 0; |
| 189 | unsigned int conv_h = 0; |
| 190 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 191 | const unsigned int kernel_width = weights->info()->dimension(0); |
| 192 | const unsigned int kernel_height = weights->info()->dimension(1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 193 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height, |
| 194 | conv_info); |
| 195 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 196 | unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 197 | unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element; |
| 198 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 199 | // _weights_reshaped will be auto configured in the kernel. |
| 200 | // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM |
| 201 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 202 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 203 | weights = &_weights_reshaped; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 204 | |
| 205 | // Create tensor to store im2col reshaped inputs |
| 206 | const unsigned int mat_input_cols = mat_weights_rows; |
| 207 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 208 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 209 | shape_im2col.set(0, mat_input_cols); |
| 210 | shape_im2col.set(1, mat_input_rows); |
| 211 | shape_im2col.set(2, 1); |
| 212 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 213 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position()); |
| 214 | im2col_reshaped_info.set_quantization_info(input->info()->quantization_info()); |
| 215 | _im2col_output.allocator()->init(im2col_reshaped_info); |
| 216 | _memory_group.manage(&_im2col_output); |
| 217 | |
| 218 | // Create GEMM output tensor |
| 219 | TensorShape shape_gemm = _im2col_output.info()->tensor_shape(); |
| 220 | shape_gemm.set(0, mat_weights_cols); |
| 221 | shape_gemm.set(1, mat_input_rows); |
| 222 | const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt; |
| 223 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 224 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 225 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position()); |
| 226 | info_gemm.set_quantization_info(output->info()->quantization_info()); |
| 227 | _gemm_output.allocator()->init(info_gemm); |
| 228 | _memory_group.manage(&_gemm_output); |
| 229 | |
| 230 | // Configure im2col |
| 231 | _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias); |
| 232 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 233 | // Configure GEMM |
| 234 | configure_mm(&_im2col_output, weights, &_gemm_output); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 235 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 236 | _im2col_output.allocator()->allocate(); |
| 237 | |
| 238 | // Configure output stage for quantized case |
| 239 | if(_is_quantized) |
| 240 | { |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 241 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 242 | |
| 243 | 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] | 244 | int output_multiplier, output_shift; |
| 245 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 246 | _memory_group.manage(&_tmp_output); |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 247 | _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] | 248 | } |
| 249 | |
| 250 | // Configure Col2Im |
| 251 | _col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, std::make_pair(conv_w, conv_h)); |
| 252 | if(_is_quantized) |
| 253 | { |
| 254 | _tmp_output.allocator()->allocate(); |
| 255 | } |
| 256 | _gemm_output.allocator()->allocate(); |
| 257 | |
| 258 | 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"); |
| 259 | |
| 260 | // Allocate intermediate tensor |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 261 | _weights_reshaped.allocator()->allocate(); |
| 262 | |
| 263 | ARM_COMPUTE_UNUSED(weights_info); |
| 264 | } |
| 265 | |
| 266 | Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 267 | const WeightsInfo &weights_info) |
| 268 | { |
| 269 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 270 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
| 271 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 272 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 273 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights); |
| 274 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2)); |
| 275 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 276 | |
| 277 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 278 | const bool append_bias = (biases != nullptr) && (!is_quantized); |
| 279 | const unsigned bias_element = (append_bias) ? 1 : 0; |
| 280 | const DataType dt = input->data_type(); |
| 281 | |
| 282 | // Get convolved dimensions |
| 283 | unsigned int conv_w = 0; |
| 284 | unsigned int conv_h = 0; |
| 285 | |
| 286 | const unsigned int kernel_width = weights->dimension(0); |
| 287 | const unsigned int kernel_height = weights->dimension(1); |
| 288 | |
| 289 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, conv_info); |
| 290 | |
| 291 | unsigned int mat_weights_cols = weights->dimension(3); |
| 292 | unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + bias_element; |
| 293 | |
| 294 | CLConvolutionLayerReshapeWeights::validate(weights, biases, nullptr); |
| 295 | |
| 296 | // Create tensor info for im2col reshaped inputs |
| 297 | const unsigned int mat_input_cols = mat_weights_rows; |
| 298 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 299 | TensorShape shape_im2col = input->tensor_shape(); |
| 300 | shape_im2col.set(0, mat_input_cols); |
| 301 | shape_im2col.set(1, mat_input_rows); |
| 302 | shape_im2col.set(2, 1); |
| 303 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->fixed_point_position()); |
| 304 | im2col_reshaped_info.set_quantization_info(input->quantization_info()); |
| 305 | CLIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias); |
| 306 | |
| 307 | // Create GEMM output tensor |
| 308 | TensorShape shape_gemm = im2col_reshaped_info.tensor_shape(); |
| 309 | shape_gemm.set(0, mat_weights_cols); |
| 310 | shape_gemm.set(1, mat_input_rows); |
| 311 | const DataType gemm_data_type = is_quantized ? DataType::S32 : dt; |
| 312 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 313 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->fixed_point_position()); |
| 314 | info_gemm.set_quantization_info(output->quantization_info()); |
| 315 | |
| 316 | validate_mm(&im2col_reshaped_info, weights, &info_gemm); |
| 317 | |
| 318 | TensorInfo tmp_info(input->tensor_shape(), 1, DataType::QASYMM8, input->fixed_point_position()); |
| 319 | if(is_quantized) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 320 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 321 | float multiplier = input->quantization_info().scale * weights->quantization_info().scale / output->quantization_info().scale; |
| 322 | int output_multiplier, output_shift; |
| 323 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 324 | // Validate output stage for quantized case |
| 325 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&info_gemm, biases, &tmp_info, output->quantization_info().offset); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 326 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 327 | |
| 328 | // Validate Col2Im |
| 329 | CLCol2ImKernel::validate(is_quantized ? &tmp_info : &info_gemm, output, std::make_pair(conv_w, conv_h)); |
| 330 | |
| 331 | if(biases != nullptr) |
| 332 | { |
| 333 | if(is_quantized) |
| 334 | { |
| 335 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 340 | } |
| 341 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 342 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 343 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 344 | } |
| 345 | |
| 346 | return Status{}; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void CLGEMMConvolutionLayer::run() |
| 350 | { |
| 351 | // Run weights reshaping (Runs once for every configure) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 352 | _reshape_weights.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 353 | |
| 354 | _memory_group.acquire(); |
| 355 | |
| 356 | // Run im2col |
| 357 | CLScheduler::get().enqueue(_im2col_kernel); |
| 358 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 359 | // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions |
| 360 | if(_is_quantized) |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 361 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 362 | // Run gemmlowp |
| 363 | _mm_gemmlowp.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 364 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 365 | // Run output stage |
| 366 | _gemmlowp_output_stage.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 367 | } |
| 368 | else |
| 369 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame^] | 370 | // Run gemm |
| 371 | _mm_gemm.run(); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // Reshape output matrix |
| 375 | CLScheduler::get().enqueue(_col2im_kernel, false); |
| 376 | |
| 377 | _memory_group.release(); |
| 378 | } |