Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +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" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 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; |
| 39 | using namespace arm_compute::misc::shape_calculator; |
| 40 | |
| 41 | CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
| 42 | : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output) |
| 47 | { |
| 48 | // Perform validation step |
| 49 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
| 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); |
| 67 | |
| 68 | if(biases != nullptr) |
| 69 | { |
| 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); |
| 74 | } |
| 75 | |
| 76 | if((output != nullptr) && (output->total_size() != 0)) |
| 77 | { |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output); |
| 80 | |
| 81 | CLWeightsReshapeKernel::validate(weights, biases, output); |
| 82 | } |
| 83 | |
| 84 | return Status{}; |
| 85 | } |
| 86 | |
| 87 | void CLConvolutionLayerReshapeWeights::run() |
| 88 | { |
| 89 | _memory_group.acquire(); |
| 90 | |
| 91 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
| 92 | |
| 93 | _memory_group.release(); |
| 94 | } |
| 95 | |
| 96 | CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 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), _is_first_run(true) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
| 103 | { |
| 104 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
| 105 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info())); |
| 106 | |
| 107 | if(_is_quantized) |
| 108 | { |
| 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(); |
| 113 | |
| 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)); |
| 116 | |
| 117 | _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
| 118 | |
| 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); |
| 122 | } |
| 123 | else |
| 124 | { |
| 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*/)); |
| 127 | } |
| 128 | } |
| 129 | |
| 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 | |
| 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); |
| 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)); |
| 168 | |
| 169 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 170 | |
| 171 | const DataType dt = input->info()->data_type(); |
| 172 | |
| 173 | // Set the GPU target for im2col and col2im |
| 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); |
| 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 | |
| 191 | const unsigned int kernel_width = weights->info()->dimension(0); |
| 192 | const unsigned int kernel_height = weights->info()->dimension(1); |
| 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 | |
| 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 | |
| 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); |
| 202 | |
| 203 | weights = &_weights_reshaped; |
| 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 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position()); |
| 213 | im2col_reshaped_info.set_quantization_info(input->info()->quantization_info()); |
| 214 | _im2col_output.allocator()->init(im2col_reshaped_info); |
| 215 | _memory_group.manage(&_im2col_output); |
| 216 | |
| 217 | // Create GEMM output tensor |
| 218 | TensorShape shape_gemm = _im2col_output.info()->tensor_shape(); |
| 219 | shape_gemm.set(0, mat_weights_cols); |
| 220 | shape_gemm.set(1, mat_input_rows); |
| 221 | const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt; |
| 222 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 223 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position()); |
| 224 | info_gemm.set_quantization_info(output->info()->quantization_info()); |
| 225 | _gemm_output.allocator()->init(info_gemm); |
| 226 | _memory_group.manage(&_gemm_output); |
| 227 | |
| 228 | // Configure im2col |
| 229 | _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias); |
| 230 | |
| 231 | // Configure GEMM |
| 232 | configure_mm(&_im2col_output, weights, &_gemm_output); |
| 233 | |
| 234 | _im2col_output.allocator()->allocate(); |
| 235 | |
| 236 | // Configure output stage for quantized case |
| 237 | if(_is_quantized) |
| 238 | { |
| 239 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 240 | |
| 241 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
| 242 | int output_multiplier, output_shift; |
| 243 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 244 | _memory_group.manage(&_tmp_output); |
| 245 | _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset); |
| 246 | } |
| 247 | |
| 248 | // Configure Col2Im |
| 249 | _col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, std::make_pair(conv_w, conv_h)); |
| 250 | if(_is_quantized) |
| 251 | { |
| 252 | _tmp_output.allocator()->allocate(); |
| 253 | } |
| 254 | _gemm_output.allocator()->allocate(); |
| 255 | |
| 256 | 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"); |
| 257 | |
| 258 | // Allocate intermediate tensor |
| 259 | _weights_reshaped.allocator()->allocate(); |
| 260 | |
| 261 | ARM_COMPUTE_UNUSED(weights_info); |
| 262 | } |
| 263 | |
| 264 | Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 265 | const WeightsInfo &weights_info) |
| 266 | { |
| 267 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 268 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
| 269 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 270 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 271 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights); |
| 272 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2)); |
| 273 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 274 | |
| 275 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 276 | const bool append_bias = (biases != nullptr) && (!is_quantized); |
| 277 | const unsigned bias_element = (append_bias) ? 1 : 0; |
| 278 | const DataType dt = input->data_type(); |
| 279 | |
| 280 | // Get convolved dimensions |
| 281 | unsigned int conv_w = 0; |
| 282 | unsigned int conv_h = 0; |
| 283 | |
| 284 | const unsigned int kernel_width = weights->dimension(0); |
| 285 | const unsigned int kernel_height = weights->dimension(1); |
| 286 | |
| 287 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, conv_info); |
| 288 | |
| 289 | unsigned int mat_weights_cols = weights->dimension(3); |
| 290 | unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + bias_element; |
| 291 | |
| 292 | CLConvolutionLayerReshapeWeights::validate(weights, biases, nullptr); |
| 293 | |
| 294 | // Create tensor info for im2col reshaped inputs |
| 295 | const unsigned int mat_input_cols = mat_weights_rows; |
| 296 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 297 | TensorShape shape_im2col = input->tensor_shape(); |
| 298 | shape_im2col.set(0, mat_input_cols); |
| 299 | shape_im2col.set(1, mat_input_rows); |
| 300 | shape_im2col.set(2, 1); |
| 301 | TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->fixed_point_position()); |
| 302 | im2col_reshaped_info.set_quantization_info(input->quantization_info()); |
| 303 | CLIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias); |
| 304 | |
| 305 | // Create GEMM output tensor |
| 306 | TensorShape shape_gemm = im2col_reshaped_info.tensor_shape(); |
| 307 | shape_gemm.set(0, mat_weights_cols); |
| 308 | shape_gemm.set(1, mat_input_rows); |
| 309 | const DataType gemm_data_type = is_quantized ? DataType::S32 : dt; |
| 310 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 311 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->fixed_point_position()); |
| 312 | info_gemm.set_quantization_info(output->quantization_info()); |
| 313 | |
| 314 | validate_mm(&im2col_reshaped_info, weights, &info_gemm); |
| 315 | |
| 316 | TensorInfo tmp_info(input->tensor_shape(), 1, DataType::QASYMM8, input->fixed_point_position()); |
| 317 | if(is_quantized) |
| 318 | { |
| 319 | float multiplier = input->quantization_info().scale * weights->quantization_info().scale / output->quantization_info().scale; |
| 320 | int output_multiplier, output_shift; |
| 321 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 322 | // Validate output stage for quantized case |
| 323 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&info_gemm, biases, &tmp_info, output->quantization_info().offset); |
| 324 | } |
| 325 | |
| 326 | // Validate Col2Im |
| 327 | CLCol2ImKernel::validate(is_quantized ? &tmp_info : &info_gemm, output, std::make_pair(conv_w, conv_h)); |
| 328 | |
| 329 | if(biases != nullptr) |
| 330 | { |
| 331 | if(is_quantized) |
| 332 | { |
| 333 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 338 | } |
| 339 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 340 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 341 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 342 | } |
| 343 | |
| 344 | return Status{}; |
| 345 | } |
| 346 | |
| 347 | void CLGEMMConvolutionLayer::run() |
| 348 | { |
| 349 | // Run weights reshaping (Runs once for every configure) |
| 350 | if(_is_first_run) |
| 351 | { |
| 352 | _reshape_weights.run(); |
| 353 | |
| 354 | _is_first_run = false; |
| 355 | } |
| 356 | |
| 357 | _memory_group.acquire(); |
| 358 | |
| 359 | // Run im2col |
| 360 | CLScheduler::get().enqueue(_im2col_kernel); |
| 361 | |
| 362 | // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions |
| 363 | if(_is_quantized) |
| 364 | { |
| 365 | // Run gemmlowp |
| 366 | _mm_gemmlowp.run(); |
| 367 | |
| 368 | // Run output stage |
| 369 | _gemmlowp_output_stage.run(); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | // Run gemm |
| 374 | _mm_gemm.run(); |
| 375 | } |
| 376 | |
| 377 | // Reshape output matrix |
| 378 | CLScheduler::get().enqueue(_col2im_kernel, false); |
| 379 | |
| 380 | _memory_group.release(); |
| 381 | } |