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