Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +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/NEON/functions/NEGEMMConvolutionLayer.h" |
| 25 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Size2D.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 31 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 32 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/kernels/NECol2ImKernel.h" |
| 34 | #include "src/core/NEON/kernels/NEConvertQuantizedSignednessKernel.h" |
| 35 | #include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h" |
| 36 | #include "src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h" |
| 37 | #include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h" |
| 38 | #include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionOutputStageKernel.h" |
| 39 | #include "src/core/NEON/kernels/NEGEMMLowpReductionKernel.h" |
| 40 | #include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h" |
| 41 | #include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h" |
| 42 | #include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
| 43 | #include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" |
| 44 | #include "src/core/NEON/kernels/NEIm2ColKernel.h" |
| 45 | #include "src/core/NEON/kernels/NEWeightsReshapeKernel.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 46 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 47 | #include <set> |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 48 | #include <tuple> |
| 49 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 50 | namespace arm_compute |
| 51 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 52 | using namespace arm_compute::misc::shape_calculator; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 53 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 54 | NEConvolutionLayerReshapeWeights::~NEConvolutionLayerReshapeWeights() = default; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 55 | NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights() |
| 56 | : _weights_reshape_kernel() |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 60 | void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 61 | { |
| 62 | // Perform validation step |
| 63 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
| 64 | ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(), |
| 65 | (biases != nullptr) ? biases->info() : nullptr, |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 66 | output->info())); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 67 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 68 | const ITensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 69 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 70 | _weights_reshape_kernel = std::make_unique<NEWeightsReshapeKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 71 | _weights_reshape_kernel->configure(weights, biases_to_use, output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 72 | |
| 73 | output->info()->set_quantization_info(weights->info()->quantization_info()); |
| 74 | } |
| 75 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 76 | Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 77 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights); |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 79 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, |
| 80 | DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 81 | DataType::BFLOAT16, DataType::F16, DataType::F32); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 83 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 84 | if(biases != nullptr) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 85 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 86 | const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 87 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type())); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 89 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 90 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 91 | } |
| 92 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 93 | if((output != nullptr) && (output->total_size() != 0)) |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 94 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 96 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 97 | NEWeightsReshapeKernel::validate(weights, biases, output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return Status{}; |
| 101 | } |
| 102 | |
| 103 | void NEConvolutionLayerReshapeWeights::run() |
| 104 | { |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 105 | NEScheduler::get().schedule(_weights_reshape_kernel.get(), 3); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 108 | NEGEMMConvolutionLayer::~NEGEMMConvolutionLayer() = default; |
| 109 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 110 | NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager, IWeightsManager *weights_manager) |
| 111 | : _memory_group(memory_manager), _weights_manager(weights_manager), _reshape_weights(), _reshape_weights_managed(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 112 | _col2im_kernel(), _reshape_layer(), _original_weights(nullptr), _original_output(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _gemm_output_3d(), _tmp_output(), |
| 113 | _data_layout(DataLayout::NCHW), _skip_im2col(false), _skip_col2im(false), _is_quantized(false), _is_prepared(false) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 114 | { |
| 115 | } |
| 116 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 117 | void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act_info, int gemm_3d_depth) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 118 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 120 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output == nullptr ? nullptr : output->info(), |
| 121 | act_info, gemm_3d_depth, _skip_im2col)); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 122 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 123 | // Create GEMMInfo structure |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 124 | const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 125 | gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */, |
| 126 | false, GEMMLowpOutputStageInfo(), false, false, act_info); |
| 127 | |
| 128 | // Supported activations in GEMM |
| 129 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 130 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 131 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 132 | }; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 133 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 134 | if(_is_quantized) |
| 135 | { |
| 136 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 137 | // Extract and negate input and weights offset |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 138 | const QuantizationInfo iqinfo = input->info()->quantization_info(); |
| 139 | const QuantizationInfo wqinfo = weights->info()->quantization_info(); |
| 140 | const QuantizationInfo oqinfo = (output->info()->total_size() == 0) ? iqinfo : output->info()->quantization_info(); |
| 141 | const UniformQuantizationInfo uiqinfo = iqinfo.uniform(); |
| 142 | const UniformQuantizationInfo uoqinfo = oqinfo.uniform(); |
| 143 | const DataType data_type = input->info()->data_type(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 144 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 145 | input->info()->set_quantization_info(QuantizationInfo(uiqinfo.scale, -uiqinfo.offset)); |
| 146 | if(!is_data_type_quantized_per_channel(weights->info()->data_type())) |
| 147 | { |
| 148 | const UniformQuantizationInfo uwqinfo = wqinfo.uniform(); |
| 149 | weights->info()->set_quantization_info(QuantizationInfo(uwqinfo.scale, -uwqinfo.offset)); |
| 150 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 151 | |
| 152 | // Merge activation with output stage |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 153 | PixelValue type_min{}; |
| 154 | PixelValue type_max{}; |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 155 | std::tie(type_min, type_max) = get_min_max(data_type); |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 156 | int32_t min_activation = type_min.get<int32_t>(); |
| 157 | int32_t max_activation = type_max.get<int32_t>(); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 158 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 159 | if(supported_acts.count(act_info.activation()) != 0) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 160 | { |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 161 | std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, uoqinfo); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | GEMMLowpOutputStageInfo output_info; |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 165 | output_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 166 | output_info.gemmlowp_offset = uoqinfo.offset; |
| 167 | output_info.gemmlowp_min_bound = min_activation; |
| 168 | output_info.gemmlowp_max_bound = max_activation; |
| 169 | output_info.is_quantized_per_channel = (weights->info()->data_type() == DataType::QSYMM8_PER_CHANNEL); |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 170 | quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, output_info); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 171 | |
SiCong Li | 7b48166 | 2020-12-02 09:43:12 +0000 | [diff] [blame] | 172 | _mm_gemmlowp.configure(input, weights, biases, output, GEMMInfo(false, false, true, gemm_3d_depth, _skip_im2col, false, output_info, false, false, act_info)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 173 | |
| 174 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 175 | input->info()->set_quantization_info(iqinfo); |
| 176 | weights->info()->set_quantization_info(wqinfo); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 177 | } |
| 178 | else |
| 179 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 180 | // Configure matrix multiply function |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 181 | _mm_gemm.configure(input, weights, biases, output, 1.0f, 0.0f, gemm_info); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 185 | Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 186 | const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 187 | { |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 188 | const DataType data_type = input->data_type(); |
| 189 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
| 190 | const bool is_activation_enabled = act_info.enabled(); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 191 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 192 | // Create GEMMInfo structure |
| 193 | const GEMMInfo gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, |
| 194 | gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */, |
| 195 | false, GEMMLowpOutputStageInfo(), false, false, act_info); |
| 196 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 197 | if(is_quantized) |
| 198 | { |
| 199 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 200 | // Extract and negate input and weights offset |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 201 | const QuantizationInfo &iqinfo = input->quantization_info(); |
| 202 | const QuantizationInfo &wqinfo = weights->quantization_info(); |
| 203 | const QuantizationInfo &oqinfo = (output->total_size() == 0) ? iqinfo : output->quantization_info(); |
| 204 | const UniformQuantizationInfo uoqinfo = oqinfo.uniform(); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 205 | |
| 206 | // Merge activation with output stage |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 207 | PixelValue type_min{}; |
| 208 | PixelValue type_max{}; |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 209 | std::tie(type_min, type_max) = get_min_max(data_type); |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 210 | int32_t min_activation = type_min.get<int32_t>(); |
| 211 | int32_t max_activation = type_max.get<int32_t>(); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 212 | |
| 213 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 214 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 215 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 216 | }; |
| 217 | if(is_activation_enabled && supported_acts.count(act_info.activation()) != 0) |
| 218 | { |
Sang-Hoon Park | 4715cf9 | 2020-01-08 16:02:47 +0000 | [diff] [blame] | 219 | std::tie(min_activation, max_activation) = get_quantized_activation_min_max(act_info, data_type, uoqinfo); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | GEMMLowpOutputStageInfo output_info; |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 223 | output_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 224 | output_info.gemmlowp_offset = uoqinfo.offset; |
| 225 | output_info.gemmlowp_min_bound = min_activation; |
| 226 | output_info.gemmlowp_max_bound = max_activation; |
| 227 | output_info.is_quantized_per_channel = (weights->data_type() == DataType::QSYMM8_PER_CHANNEL); |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 228 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multipliers(iqinfo, wqinfo, oqinfo, output_info)); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 229 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 230 | // Perform validation step on GEMMLowp |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 231 | std::unique_ptr<ITensorInfo> input_qa = input->clone(); |
| 232 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
| 233 | input_qa->set_quantization_info(QuantizationInfo(iqinfo.uniform().scale, -iqinfo.uniform().offset)); |
| 234 | weights_qa->set_quantization_info(QuantizationInfo(wqinfo.uniform().scale, -wqinfo.uniform().offset)); |
SiCong Li | 7b48166 | 2020-12-02 09:43:12 +0000 | [diff] [blame] | 235 | return NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, GEMMInfo(false, false, true, gemm_3d_depth, skip_im2col, false, output_info, false, false, act_info)); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
| 239 | // Perform validation step on Matrix multiply function |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 240 | return NEGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 241 | } |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Giorgio Arena | 7a669a8 | 2019-11-13 17:07:13 +0000 | [diff] [blame] | 244 | Status NEGEMMConvolutionLayer::validate_gemm3d(const ITensorInfo *input_info, const ITensorInfo *weights_info, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 245 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 246 | const DataType data_type = input_info->data_type(); |
| 247 | const unsigned int mult_y = skip_im2col ? 1U : gemm_3d_depth; |
| 248 | const unsigned int mult_z = skip_im2col ? gemm_3d_depth : 1U; |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 249 | |
| 250 | // Set dummy tensor shapes for the validation |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 251 | const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type, input_info->quantization_info()); |
Giorgio Arena | 7a669a8 | 2019-11-13 17:07:13 +0000 | [diff] [blame] | 252 | const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type, weights_info->quantization_info()); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 253 | const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, data_type, input_info->quantization_info()); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 254 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 255 | return validate_mm(&dummy_input_info, &dummy_weights_info, nullptr, &dummy_output_info, act_info, gemm_3d_depth, skip_im2col); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 258 | void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 259 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 260 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 261 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 262 | ARM_COMPUTE_UNUSED(num_groups, weights_info); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 263 | ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(), |
| 264 | weights->info(), |
| 265 | biases != nullptr ? biases->info() : nullptr, |
| 266 | output->info(), |
| 267 | conv_info, |
| 268 | weights_info, |
| 269 | dilation, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 270 | act_info, |
| 271 | num_groups)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 272 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 273 | const DataType data_type = input->info()->data_type(); |
| 274 | const DataLayout data_layout = input->info()->data_layout(); |
| 275 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 276 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 277 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 278 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 279 | const unsigned int kernel_width = weights->info()->dimension(idx_width); |
| 280 | const unsigned int kernel_height = weights->info()->dimension(idx_height); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 281 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 282 | _is_prepared = weights_info.retain_internal_weights(); |
| 283 | _original_weights = weights; |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 284 | _original_output = output; |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 285 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 286 | _data_layout = data_layout; |
| 287 | _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 288 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 289 | const ITensor *gemm_input_to_use = input; |
| 290 | ITensor *gemm_output_to_use = output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 291 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 292 | // Get convolved dimensions |
| 293 | unsigned int conv_w = 0; |
| 294 | unsigned int conv_h = 0; |
| 295 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width), |
| 296 | input->info()->dimension(idx_height), |
| 297 | kernel_width, |
| 298 | kernel_height, |
| 299 | conv_info, |
| 300 | dilation); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 301 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 302 | // Check if GEMM3D is supported |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 303 | if(data_layout == DataLayout::NHWC) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 304 | { |
Giorgio Arena | 7a669a8 | 2019-11-13 17:07:13 +0000 | [diff] [blame] | 305 | _skip_col2im = bool(validate_gemm3d(input->info(), weights->info(), act_info, conv_h, true)); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 306 | // If not supported, we need to perform im2col and col2im (or reshape layer) |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 307 | if(!_skip_col2im) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 308 | { |
| 309 | _skip_im2col = false; |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 310 | } |
| 311 | } |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 312 | else |
| 313 | { |
| 314 | _skip_col2im = false; |
| 315 | } |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 316 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 317 | // Get parameters from conv_info |
| 318 | unsigned int stride_x = 0; |
| 319 | unsigned int stride_y = 0; |
| 320 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 321 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 322 | unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 323 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 324 | // _weights_reshaped will be auto configured in the kernel. |
| 325 | // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 326 | const ITensor *weights_to_use = weights; |
| 327 | |
| 328 | if(_weights_manager && _weights_manager->are_weights_managed(weights)) |
| 329 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 330 | _reshape_weights_managed.configure(weights, nullptr); |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 331 | weights_to_use = _weights_manager->acquire(weights, &_reshape_weights_managed); |
| 332 | } |
| 333 | else |
| 334 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 335 | _reshape_weights.configure(weights, nullptr, &_weights_reshaped); |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 336 | weights_to_use = &_weights_reshaped; |
| 337 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 338 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 339 | // Create tensor to store im2col reshaped inputs |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 340 | if(!_skip_im2col) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 341 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 342 | _memory_group.manage(&_im2col_output); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 343 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 344 | // Configure |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 345 | _im2col_kernel = std::make_unique<NEIm2ColKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 346 | _im2col_kernel->configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, false, dilation); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 347 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 348 | // Update GEMM input |
| 349 | gemm_input_to_use = &_im2col_output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 350 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 351 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 352 | // Create temporary GEMM output tensor in case we cannot skip col2im |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 353 | const DataType output_data_type = data_type == DataType::BFLOAT16 ? DataType::F32 : data_type; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 354 | if(!_skip_col2im) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 355 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 356 | TensorShape shape_gemm; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 357 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 358 | // Calculate GEMM output shape |
| 359 | shape_gemm = _im2col_output.info()->tensor_shape(); |
| 360 | shape_gemm.set(0, mat_weights_cols); |
| 361 | shape_gemm.set(1, conv_w * conv_h); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 362 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 363 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 364 | TensorInfo info_gemm(shape_gemm, 1, output_data_type); |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 365 | info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout()); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 366 | _gemm_output.allocator()->init(info_gemm); |
| 367 | _memory_group.manage(&_gemm_output); |
| 368 | |
| 369 | // Update GEMM output |
| 370 | gemm_output_to_use = &_gemm_output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 371 | } |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 372 | else |
| 373 | { |
| 374 | _gemm_output.allocator()->init(*output->info()); |
| 375 | _memory_group.manage(&_gemm_output); |
| 376 | _gemm_output_3d.allocator()->init(*output->info()); |
| 377 | |
| 378 | // Update GEMM output |
| 379 | gemm_output_to_use = &_gemm_output_3d; |
| 380 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 381 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 382 | // Configure GEMM |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 383 | // In case we need to skip col2im, GEMM3D (gemm_3d_depth != 0) must be called in order to avoid reshaping the output matrix |
| 384 | const unsigned int gemm_3d_depth = _skip_col2im ? conv_h : 0; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 385 | configure_mm(gemm_input_to_use, weights_to_use, biases, gemm_output_to_use, act_info, gemm_3d_depth); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 386 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 387 | if(!_skip_im2col) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 388 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 389 | _im2col_output.allocator()->allocate(); |
| 390 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 391 | |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 392 | if(!_skip_col2im) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 393 | { |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 394 | if(_data_layout == DataLayout::NCHW) |
| 395 | { |
| 396 | // Configure col2im |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 397 | _col2im_kernel = std::make_unique<NECol2ImKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 398 | _col2im_kernel->configure(gemm_output_to_use, output, Size2D(conv_w, conv_h)); |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 399 | } |
| 400 | else |
| 401 | { |
| 402 | // Configure reshape layer |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 403 | _reshape_layer.configure(gemm_output_to_use, output); |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 404 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 405 | } |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 406 | else |
| 407 | { |
| 408 | // Configure reshape layer |
| 409 | _reshape_layer.configure(gemm_output_to_use, output); |
| 410 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 411 | |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 412 | if(_is_quantized && !_skip_col2im) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 413 | { |
| 414 | _tmp_output.allocator()->allocate(); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 417 | _gemm_output.allocator()->allocate(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 418 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 419 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h), |
| 420 | "Output shape does not match the expected one"); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | Status NEGEMMConvolutionLayer::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] | 424 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 425 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 426 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 427 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 428 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32); |
| 429 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::BFLOAT16, DataType::F16, DataType::F32); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 430 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 431 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups > 1, "Grouping (num_groups != 1) is not supported on NEON"); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 432 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 433 | const DataLayout data_layout = input->data_layout(); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 434 | const DataType data_type = input->data_type(); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 435 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 436 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 437 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 438 | const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 439 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 440 | const unsigned int kernel_width = weights->dimension(idx_width); |
| 441 | const unsigned int kernel_height = weights->dimension(idx_height); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 442 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 443 | TensorInfo im2col_reshaped_info{}; |
| 444 | TensorInfo info_gemm{}; |
| 445 | TensorInfo tmp_info{}; |
| 446 | TensorInfo weights_reshaped_info{}; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 447 | const ITensorInfo *gemm_input_to_use = input; |
| 448 | const ITensorInfo *gemm_output_to_use = output; |
| 449 | const ITensorInfo *weights_to_use = weights; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 450 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 451 | const bool append_bias = false; |
| 452 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
Georgios Pinitas | afc630f | 2020-03-30 14:09:27 +0100 | [diff] [blame] | 453 | const bool is_bf16 = data_type == DataType::BFLOAT16; |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 454 | bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 455 | |
| 456 | // Get convolved dimensions |
| 457 | unsigned int conv_w = 0; |
| 458 | unsigned int conv_h = 0; |
| 459 | |
| 460 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), |
| 461 | input->dimension(idx_height), |
| 462 | kernel_width, |
| 463 | kernel_height, |
| 464 | conv_info, |
| 465 | dilation); |
| 466 | |
| 467 | // Check if GEMM3D is supported |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 468 | bool skip_col2im = false; |
| 469 | if(data_layout == DataLayout::NHWC) |
| 470 | { |
Giorgio Arena | 7a669a8 | 2019-11-13 17:07:13 +0000 | [diff] [blame] | 471 | skip_col2im = bool(validate_gemm3d(input, weights, act_info, conv_h, true)); |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 472 | // If not supported, we need to perform im2col and col2im (or reshape layer) |
| 473 | if(!skip_col2im) |
| 474 | { |
| 475 | skip_im2col = false; |
| 476 | } |
| 477 | } |
| 478 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 479 | if(skip_col2im) |
| 480 | { |
| 481 | // If not supported, we need to perform im2col and col2im (or reshape layer) |
Giorgio Arena | 7a669a8 | 2019-11-13 17:07:13 +0000 | [diff] [blame] | 482 | if(!bool(validate_gemm3d(input, weights, act_info, conv_h, skip_im2col))) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 483 | { |
| 484 | skip_im2col = false; |
| 485 | skip_col2im = false; |
| 486 | } |
| 487 | } |
| 488 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 489 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel)); |
| 490 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 491 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 492 | // Validate biases |
| 493 | if(biases != nullptr) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 494 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 495 | if(is_quantized) |
| 496 | { |
| 497 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 498 | } |
Georgios Pinitas | afc630f | 2020-03-30 14:09:27 +0100 | [diff] [blame] | 499 | else if(is_bf16) |
| 500 | { |
| 501 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32); |
| 502 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 503 | else |
| 504 | { |
| 505 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 506 | } |
| 507 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
| 508 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 509 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 510 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 511 | unsigned int mat_weights_cols = weights->dimension(idx_kernels); |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 512 | unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 513 | |
| 514 | // Output tensor auto inizialization if not yet initialized |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 515 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, nullptr, nullptr)); |
| 516 | weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, append_bias), 1, data_type); |
Georgios Pinitas | 4d600c7 | 2019-07-30 15:09:10 +0100 | [diff] [blame] | 517 | weights_reshaped_info.set_quantization_info(weights->quantization_info()); |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 518 | weights_to_use = &weights_reshaped_info; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 519 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 520 | if(!skip_im2col) |
| 521 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 522 | // Create tensor info for im2col reshaped inputs |
| 523 | // For NEON the batch size is on the fourth dimension |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 524 | // TODO (giaiod01): Auto-initialize the output shape of im2col COMPMID-1482 |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 525 | TensorShape shape_im2col = input->tensor_shape(); |
| 526 | shape_im2col.set(0, mat_weights_rows); |
| 527 | shape_im2col.set(1, conv_w * conv_h); |
| 528 | shape_im2col.set(2, 1); |
| 529 | |
| 530 | im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type); |
| 531 | im2col_reshaped_info.set_quantization_info(input->quantization_info()); |
| 532 | |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 533 | ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation)); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 534 | gemm_input_to_use = &im2col_reshaped_info; |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 535 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 536 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 537 | // Create temporary GEMM output tensor in case we cannot skip col2im |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 538 | const DataType output_data_type = data_type == DataType::BFLOAT16 ? DataType::F32 : data_type; |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 539 | if(!skip_col2im) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 540 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 541 | TensorShape shape_gemm = gemm_input_to_use->tensor_shape(); |
| 542 | shape_gemm.set(0, mat_weights_cols); |
| 543 | shape_gemm.set(1, conv_w * conv_h); |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 544 | info_gemm = TensorInfo(shape_gemm, 1, output_data_type); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 545 | } |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 546 | else |
| 547 | { |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 548 | info_gemm = TensorInfo(output->tensor_shape(), 1, output_data_type); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 549 | } |
| 550 | info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout()); |
| 551 | gemm_output_to_use = &info_gemm; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 552 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases, gemm_output_to_use, act_info, skip_col2im ? conv_h : 0, skip_im2col)); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 553 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 554 | // Validate Col2Im/ReshapeLayer |
| 555 | if(!skip_col2im && (data_layout == DataLayout::NCHW)) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 556 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 557 | ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(gemm_output_to_use, output, Size2D(conv_w, conv_h))); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 560 | return Status{}; |
| 561 | } |
| 562 | |
| 563 | void NEGEMMConvolutionLayer::run() |
| 564 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 565 | prepare(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 566 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 567 | MemoryGroupResourceScope scope_mg(_memory_group); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 568 | |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 569 | bool out_has_padding = _skip_col2im && (_original_output->info()->padding().bottom != 0 || _original_output->info()->padding().top != 0); |
| 570 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 571 | if(!_skip_im2col) |
| 572 | { |
| 573 | // Run input reshaping |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 574 | unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 575 | NEScheduler::get().schedule(_im2col_kernel.get(), y_dim); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 576 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 577 | |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 578 | // Handle the case where output has top/bottom padding |
| 579 | const ITensor *out_to_use = out_has_padding ? &_gemm_output : _original_output; |
| 580 | _gemm_output_3d.allocator()->import_memory(out_to_use->buffer()); |
| 581 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 582 | // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions |
| 583 | if(_is_quantized) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 584 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 585 | // Run gemmlowp |
| 586 | _mm_gemmlowp.run(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 587 | } |
| 588 | else |
| 589 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 590 | // Run gemm |
| 591 | _mm_gemm.run(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 594 | // Reshape output matrix |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 595 | if(!_skip_col2im) |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 596 | { |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 597 | if(_data_layout == DataLayout::NCHW) |
| 598 | { |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 599 | NEScheduler::get().schedule(_col2im_kernel.get(), Window::DimY); |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 600 | } |
| 601 | else |
| 602 | { |
| 603 | _reshape_layer.run(); |
| 604 | } |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 605 | } |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame^] | 606 | else if(out_has_padding) |
| 607 | { |
| 608 | _reshape_layer.run(); |
| 609 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 610 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 611 | |
| 612 | void NEGEMMConvolutionLayer::prepare() |
| 613 | { |
| 614 | if(!_is_prepared) |
| 615 | { |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 616 | if(_weights_manager && _weights_manager->are_weights_managed(_original_weights)) |
| 617 | { |
| 618 | _weights_manager->run(_original_weights, &_reshape_weights_managed); |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | // Run weights reshaping and mark original weights tensor as unused |
| 623 | _weights_reshaped.allocator()->allocate(); |
| 624 | _reshape_weights.run(); |
| 625 | _original_weights->mark_as_unused(); |
| 626 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 627 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 628 | // Prepare GEMM |
| 629 | _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 630 | if(!_weights_reshaped.is_used()) |
| 631 | { |
| 632 | _weights_reshaped.allocator()->free(); |
| 633 | } |
| 634 | |
| 635 | _is_prepared = true; |
| 636 | } |
| 637 | } |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 638 | } // namespace arm_compute |