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