Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/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) |
Georgios Pinitas | ca1250d | 2018-11-22 19:38:27 +0000 | [diff] [blame] | 93 | : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(), |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 94 | _add_bias_kernel(), _reshape_layer(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _append_bias(false), |
| 95 | _skip_im2col(false), _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 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 99 | void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, 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); |
| 102 | ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info(), gemm_3d_depth, _skip_im2col)); |
| 103 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 104 | const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, |
| 105 | gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */); |
| 106 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 107 | if(_is_quantized) |
| 108 | { |
| 109 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 110 | // Extract and negate input and weights offset |
| 111 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 112 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 113 | |
| 114 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 115 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 116 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 117 | _mm_gemmlowp.configure(input, weights, nullptr, output, gemm_info); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 118 | |
| 119 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 120 | input->info()->set_quantization_info(input_quantization_info); |
| 121 | weights->info()->set_quantization_info(weights_quantization_info); |
| 122 | } |
| 123 | else |
| 124 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 125 | // Configure matrix multiply function |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 126 | _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] | 127 | } |
| 128 | } |
| 129 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 130 | Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col) |
| 131 | { |
| 132 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 133 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 134 | const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, |
| 135 | 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] | 136 | if(is_quantized) |
| 137 | { |
| 138 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 139 | // Extract and negate input and weights offset |
| 140 | const QuantizationInfo input_quantization_info = input->quantization_info(); |
| 141 | const QuantizationInfo weights_quantization_info = weights->quantization_info(); |
| 142 | |
| 143 | std::unique_ptr<ITensorInfo> input_qa = input->clone(); |
| 144 | std::unique_ptr<ITensorInfo> weights_qa = weights->clone(); |
| 145 | input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 146 | weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 147 | |
| 148 | // Perform validation step on GEMMLowp |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 149 | return NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), nullptr, output, gemm_info); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 150 | } |
| 151 | else |
| 152 | { |
| 153 | // Perform validation step on Matrix multiply function |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 154 | 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] | 155 | } |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | Status NEGEMMConvolutionLayer::validate_gemm3d(DataType data_type, int gemm_3d_depth, bool skip_im2col) |
| 159 | { |
| 160 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
| 161 | const DataType output_gemm_data_type = is_quantized ? DataType::S32 : data_type; |
| 162 | const unsigned int mult_y = skip_im2col ? 1U : gemm_3d_depth; |
| 163 | const unsigned int mult_z = skip_im2col ? gemm_3d_depth : 1U; |
| 164 | |
| 165 | // Set dummy tensor shapes for the validation |
| 166 | const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type); |
| 167 | const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type); |
| 168 | const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, output_gemm_data_type); |
| 169 | |
| 170 | return validate_mm(&dummy_input_info, &dummy_weights_info, &dummy_output_info, gemm_3d_depth, skip_im2col); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 173 | 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] | 174 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 175 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 176 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 177 | ARM_COMPUTE_UNUSED(num_groups); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 178 | ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(), |
| 179 | weights->info(), |
| 180 | biases != nullptr ? biases->info() : nullptr, |
| 181 | output->info(), |
| 182 | conv_info, |
| 183 | weights_info, |
| 184 | dilation, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 185 | act_info, |
| 186 | num_groups)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 187 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 188 | const DataType data_type = input->info()->data_type(); |
| 189 | const DataLayout data_layout = input->info()->data_layout(); |
| 190 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 191 | 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] | 192 | 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] | 193 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 194 | const unsigned int kernel_width = weights->info()->dimension(idx_width); |
| 195 | const unsigned int kernel_height = weights->info()->dimension(idx_height); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 196 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 197 | _is_prepared = weights_info.retain_internal_weights(); |
| 198 | _original_weights = weights; |
| 199 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 200 | _data_layout = data_layout; |
| 201 | _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] | 202 | _append_bias = (biases != nullptr) && (!_is_quantized); |
| 203 | _is_activationlayer_enabled = act_info.enabled(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 204 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 205 | const ITensor *gemm_input_to_use = input; |
| 206 | ITensor *gemm_output_to_use = output; |
| 207 | ITensor *gemm_output_staged_to_use = output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 208 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 209 | // Get convolved dimensions |
| 210 | unsigned int conv_w = 0; |
| 211 | unsigned int conv_h = 0; |
| 212 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width), |
| 213 | input->info()->dimension(idx_height), |
| 214 | kernel_width, |
| 215 | kernel_height, |
| 216 | conv_info, |
| 217 | dilation); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 218 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 219 | // Check if GEMM3D is supported |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 220 | if(data_layout == DataLayout::NHWC) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 221 | { |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 222 | _skip_col2im = bool(validate_gemm3d(input->info()->data_type(), conv_h, true)); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 223 | // 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] | 224 | if(!_skip_col2im) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 225 | { |
| 226 | _skip_im2col = false; |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 227 | } |
| 228 | } |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 229 | else |
| 230 | { |
| 231 | _skip_col2im = false; |
| 232 | } |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 233 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 234 | const ITensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr; |
| 235 | |
| 236 | // Get parameters from conv_info |
| 237 | unsigned int stride_x = 0; |
| 238 | unsigned int stride_y = 0; |
| 239 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 240 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 241 | unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 242 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 243 | // _weights_reshaped will be auto configured in the kernel. |
| 244 | // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM |
| 245 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped); |
| 246 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 247 | // Create tensor to store im2col reshaped inputs |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 248 | if(!_skip_im2col) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 249 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 250 | _memory_group.manage(&_im2col_output); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 251 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 252 | // Configure |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 253 | _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] | 254 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 255 | // Update GEMM input |
| 256 | gemm_input_to_use = &_im2col_output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 257 | } |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 258 | else if(_append_bias) |
| 259 | { |
| 260 | // Configure add bias kernel |
| 261 | _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE); |
| 262 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 263 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 264 | // Create temporary GEMM output tensor in case we cannot skip col2im |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 265 | if(!_skip_col2im || _is_quantized) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 266 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 267 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
| 268 | const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 269 | TensorShape shape_gemm; |
| 270 | |
| 271 | if(_is_quantized && _skip_col2im) |
| 272 | { |
| 273 | shape_gemm = output->info()->tensor_shape(); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | // Calculate GEMM output shape |
| 278 | shape_gemm = _im2col_output.info()->tensor_shape(); |
| 279 | shape_gemm.set(0, mat_weights_cols); |
| 280 | shape_gemm.set(1, conv_w * conv_h); |
| 281 | } |
| 282 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 283 | // FIXME: input->clone() doesn't work with subtensors for grouped convolutions. |
| 284 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type); |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 285 | 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] | 286 | _gemm_output.allocator()->init(info_gemm); |
| 287 | _memory_group.manage(&_gemm_output); |
| 288 | |
| 289 | // Update GEMM output |
| 290 | gemm_output_to_use = &_gemm_output; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 293 | // Configure GEMM |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 294 | // In case we need to skip col2im, GEMM3D (gemm_3d_depth != 0) must be called in order to avoid reshaping the output matrix |
| 295 | const unsigned int gemm_3d_depth = _skip_col2im ? conv_h : 0; |
| 296 | configure_mm(gemm_input_to_use, &_weights_reshaped, gemm_output_to_use, gemm_3d_depth); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 297 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 298 | if(!_skip_im2col) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 299 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 300 | _im2col_output.allocator()->allocate(); |
| 301 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 302 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 303 | // Configure output stage for quantized case |
| 304 | if(_is_quantized) |
| 305 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 306 | const QuantizationInfo input_quant_info = input->info()->quantization_info(); |
| 307 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input_quant_info : output->info()->quantization_info(); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 308 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 309 | float multiplier = input_quant_info.scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 310 | int output_multiplier, output_shift; |
| 311 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 312 | |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 313 | if(!_skip_col2im) |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 314 | { |
| 315 | _memory_group.manage(&_tmp_output); |
| 316 | gemm_output_staged_to_use = &_tmp_output; |
| 317 | } |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 318 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 319 | // Merge activation with output stage |
Georgios Pinitas | c73e2b8 | 2018-11-08 13:33:16 +0000 | [diff] [blame] | 320 | int min_activation = 0; |
| 321 | int max_activation = 0; |
| 322 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 323 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 324 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 325 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 326 | }; |
| 327 | if(_is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0) |
| 328 | { |
Georgios Pinitas | c73e2b8 | 2018-11-08 13:33:16 +0000 | [diff] [blame] | 329 | const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP); |
| 330 | const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP); |
| 331 | |
| 332 | min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int; |
| 333 | max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int; |
| 334 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 335 | _is_activationlayer_enabled = false; |
| 336 | } |
| 337 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 338 | _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset, min_activation, max_activation); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 341 | if(!_skip_col2im) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 342 | { |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 343 | if(_data_layout == DataLayout::NCHW) |
| 344 | { |
| 345 | // Configure col2im |
| 346 | _col2im_kernel.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output, Size2D(conv_w, conv_h)); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | // Configure reshape layer |
| 351 | _reshape_layer.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output); |
| 352 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 355 | if(_is_quantized && !_skip_col2im) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 356 | { |
| 357 | _tmp_output.allocator()->allocate(); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 360 | if(!_skip_col2im || _is_quantized) |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 361 | { |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 362 | _gemm_output.allocator()->allocate(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 365 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h), |
| 366 | "Output shape does not match the expected one"); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 367 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 368 | // Configure Activation Layer |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 369 | if(_is_activationlayer_enabled) |
| 370 | { |
| 371 | _activationlayer_function.configure(output, nullptr, act_info); |
| 372 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 373 | |
| 374 | ARM_COMPUTE_UNUSED(weights_info); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | 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] | 378 | 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] | 379 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 380 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 381 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!"); |
| 382 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 383 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 384 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 385 | 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] | 386 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 387 | const DataLayout data_layout = input->data_layout(); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 388 | const DataType data_type = input->data_type(); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 389 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 390 | 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] | 391 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 392 | 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] | 393 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 394 | const unsigned int kernel_width = weights->dimension(idx_width); |
| 395 | const unsigned int kernel_height = weights->dimension(idx_height); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 396 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 397 | TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info; |
| 398 | const ITensorInfo *gemm_input_to_use = input; |
| 399 | const ITensorInfo *gemm_output_to_use = output; |
| 400 | const ITensorInfo *gemm_output_staged_to_use = output; |
| 401 | const ITensorInfo *weights_to_use = weights; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 402 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 403 | const bool is_quantized = is_data_type_quantized_asymmetric(data_type); |
| 404 | const bool append_bias = (biases != nullptr) && (!is_quantized); |
| 405 | 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] | 406 | bool is_activation_enabled = act_info.enabled(); |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 407 | |
| 408 | // Get convolved dimensions |
| 409 | unsigned int conv_w = 0; |
| 410 | unsigned int conv_h = 0; |
| 411 | |
| 412 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), |
| 413 | input->dimension(idx_height), |
| 414 | kernel_width, |
| 415 | kernel_height, |
| 416 | conv_info, |
| 417 | dilation); |
| 418 | |
| 419 | // Check if GEMM3D is supported |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 420 | bool skip_col2im = false; |
| 421 | if(data_layout == DataLayout::NHWC) |
| 422 | { |
| 423 | skip_col2im = bool(validate_gemm3d(input->data_type(), conv_h, true)); |
| 424 | // If not supported, we need to perform im2col and col2im (or reshape layer) |
| 425 | if(!skip_col2im) |
| 426 | { |
| 427 | skip_im2col = false; |
| 428 | } |
| 429 | } |
| 430 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 431 | if(skip_col2im) |
| 432 | { |
| 433 | // If not supported, we need to perform im2col and col2im (or reshape layer) |
| 434 | if(!bool(validate_gemm3d(input->data_type(), conv_h, skip_im2col))) |
| 435 | { |
| 436 | skip_im2col = false; |
| 437 | skip_col2im = false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | const unsigned bias_element = (append_bias && !skip_im2col) ? 1 : 0; |
| 442 | const ITensorInfo *biases_to_use = (append_bias && !skip_im2col) ? biases : nullptr; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 443 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 444 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel)); |
| 445 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 446 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 447 | // Validate biases |
| 448 | if(biases != nullptr) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 449 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 450 | if(is_quantized) |
| 451 | { |
| 452 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 457 | } |
| 458 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels)); |
| 459 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 460 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 461 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 462 | if(act_info.enabled()) |
| 463 | { |
| 464 | ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a()); |
| 465 | } |
| 466 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 467 | unsigned int mat_weights_cols = weights->dimension(idx_kernels); |
| 468 | unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + bias_element; |
| 469 | |
| 470 | // Output tensor auto inizialization if not yet initialized |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 471 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases_to_use, nullptr)); |
| 472 | weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col)), 1, data_type); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 473 | weights_to_use = &weights_reshaped_info; |
| 474 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 475 | if(!skip_im2col) |
| 476 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 477 | // Create tensor info for im2col reshaped inputs |
| 478 | // For NEON the batch size is on the fourth dimension |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 479 | // TODO (giaiod01): Auto-initialize the output shape of im2col COMPMID-1482 |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 480 | TensorShape shape_im2col = input->tensor_shape(); |
| 481 | shape_im2col.set(0, mat_weights_rows); |
| 482 | shape_im2col.set(1, conv_w * conv_h); |
| 483 | shape_im2col.set(2, 1); |
| 484 | |
| 485 | im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type); |
| 486 | im2col_reshaped_info.set_quantization_info(input->quantization_info()); |
| 487 | |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 488 | 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] | 489 | gemm_input_to_use = &im2col_reshaped_info; |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 490 | } |
| 491 | else if(append_bias) |
| 492 | { |
| 493 | // Validate add bias kernel |
| 494 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE)); |
| 495 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 496 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 497 | // Create temporary GEMM output tensor in case we cannot skip col2im |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 498 | const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type; |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 499 | if(!skip_col2im) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 500 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 501 | TensorShape shape_gemm = gemm_input_to_use->tensor_shape(); |
| 502 | shape_gemm.set(0, mat_weights_cols); |
| 503 | shape_gemm.set(1, conv_w * conv_h); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 504 | info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 505 | } |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 506 | else |
| 507 | { |
| 508 | info_gemm = TensorInfo(output->tensor_shape(), 1, gemm_data_type); |
| 509 | } |
| 510 | info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout()); |
| 511 | gemm_output_to_use = &info_gemm; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 512 | |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 513 | ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, gemm_output_to_use, skip_col2im ? conv_h : 0, skip_im2col)); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 514 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 515 | if(is_quantized) |
| 516 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 517 | const QuantizationInfo input_quant_info = input->quantization_info(); |
| 518 | const QuantizationInfo output_quant_info = (output->total_size() == 0) ? input_quant_info : output->quantization_info(); |
| 519 | const float multiplier = input_quant_info.scale * weights_to_use->quantization_info().scale / output_quant_info.scale; |
| 520 | int output_multiplier, output_shift; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 521 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 522 | |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 523 | if(!skip_col2im) |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 524 | { |
| 525 | tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8); |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 526 | tmp_info.set_quantization_info(output->quantization_info()).set_data_layout(data_layout); |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 527 | gemm_output_staged_to_use = &tmp_info; |
| 528 | } |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 529 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 530 | // Merge activation with output stage |
Georgios Pinitas | c73e2b8 | 2018-11-08 13:33:16 +0000 | [diff] [blame] | 531 | int min_activation = 0; |
| 532 | int max_activation = 0; |
| 533 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 534 | const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU, |
| 535 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 536 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU |
| 537 | }; |
Georgios Pinitas | c73e2b8 | 2018-11-08 13:33:16 +0000 | [diff] [blame] | 538 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 539 | if(is_activation_enabled && supported_acts.count(act_info.activation()) != 0) |
| 540 | { |
Georgios Pinitas | c73e2b8 | 2018-11-08 13:33:16 +0000 | [diff] [blame] | 541 | const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP); |
| 542 | const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP); |
| 543 | |
| 544 | min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int; |
| 545 | max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int; |
| 546 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 547 | is_activation_enabled = false; |
| 548 | } |
| 549 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 550 | // Validate output stage for quantized case |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 551 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, min_activation, max_activation); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 554 | // Validate Col2Im/ReshapeLayer |
| 555 | if(!skip_col2im && (data_layout == DataLayout::NCHW)) |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 556 | { |
| 557 | ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, |
| 558 | output, |
| 559 | Size2D(conv_w, conv_h))); |
| 560 | } |
| 561 | |
| 562 | //Validate Activation Layer |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 563 | if(is_activation_enabled) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 564 | { |
| 565 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info)); |
| 566 | } |
| 567 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 568 | return Status{}; |
| 569 | } |
| 570 | |
| 571 | void NEGEMMConvolutionLayer::run() |
| 572 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 573 | prepare(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 574 | |
| 575 | _memory_group.acquire(); |
| 576 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 577 | if(!_skip_im2col) |
| 578 | { |
| 579 | // Run input reshaping |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 580 | unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 581 | NEScheduler::get().schedule(&_im2col_kernel, y_dim); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 582 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 583 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 584 | // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions |
| 585 | if(_is_quantized) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 586 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 587 | // Run gemmlowp |
| 588 | _mm_gemmlowp.run(); |
| 589 | |
| 590 | // Run output stage |
| 591 | _gemmlowp_output_stage.run(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 592 | } |
| 593 | else |
| 594 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 595 | // Run gemm |
| 596 | _mm_gemm.run(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 599 | if(_skip_im2col && _append_bias) |
| 600 | { |
| 601 | NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY); |
| 602 | } |
| 603 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 604 | // Reshape output matrix |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 605 | if(!_skip_col2im) |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 606 | { |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 607 | if(_data_layout == DataLayout::NCHW) |
| 608 | { |
| 609 | NEScheduler::get().schedule(&_col2im_kernel, Window::DimY); |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | _reshape_layer.run(); |
| 614 | } |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 615 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 616 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 617 | if(_is_activationlayer_enabled) |
| 618 | { |
| 619 | _activationlayer_function.run(); |
| 620 | } |
| 621 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 622 | _memory_group.release(); |
| 623 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 624 | |
| 625 | void NEGEMMConvolutionLayer::prepare() |
| 626 | { |
| 627 | if(!_is_prepared) |
| 628 | { |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 629 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 630 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 631 | // Run weights reshaping and mark original weights tensor as unused |
| 632 | _weights_reshaped.allocator()->allocate(); |
| 633 | _reshape_weights.run(); |
| 634 | _original_weights->mark_as_unused(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 635 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 636 | // Prepare GEMM |
| 637 | _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 638 | if(!_weights_reshaped.is_used()) |
| 639 | { |
| 640 | _weights_reshaped.allocator()->free(); |
| 641 | } |
| 642 | |
| 643 | _is_prepared = true; |
| 644 | } |
| 645 | } |