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/PixelValue.h" |
| 27 | #include "arm_compute/core/Size2D.h" |
| 28 | #include "arm_compute/core/Utils.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/core/utils/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> |
| 35 | #include <tuple> |
| 36 | |
| 37 | namespace |
| 38 | { |
| 39 | arm_compute::TensorShape get_reshaped_weights_shape(const arm_compute::ITensorInfo *weights, bool append_bias) |
| 40 | { |
| 41 | const unsigned int mat_weights_cols = weights->dimension(3); |
| 42 | const unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0); |
| 43 | return arm_compute::TensorShape(mat_weights_cols, mat_weights_rows); |
| 44 | } |
| 45 | } // namespace |
| 46 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
| 50 | : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW) |
| 55 | { |
| 56 | // Perform validation step |
| 57 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
| 58 | ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(), |
| 59 | (biases != nullptr) ? biases->info() : nullptr, |
| 60 | output->info(), |
| 61 | transpose1xW)); |
| 62 | |
| 63 | // Check if bias are present, if yes they will be embedded to the weights matrix |
| 64 | const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type()); |
| 65 | //const unsigned bias_element = (append_biases) ? 1 : 0; |
| 66 | const ITensor *biases_to_use = (append_biases) ? biases : nullptr; |
| 67 | |
| 68 | _transpose1xW = transpose1xW; |
| 69 | |
| 70 | if(transpose1xW) |
| 71 | { |
| 72 | // Create tensor to store the reshaped weights |
| 73 | TensorInfo info_wr = weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(get_reshaped_weights_shape(weights->info(), append_biases)); |
| 74 | |
| 75 | _weights_reshaped.allocator()->init(info_wr); |
| 76 | _memory_group.manage(&_weights_reshaped); |
| 77 | |
| 78 | _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped); |
| 79 | _weights_transposed_kernel.configure(&_weights_reshaped, output); |
| 80 | |
| 81 | _weights_reshaped.allocator()->allocate(); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | _weights_reshape_kernel.configure(weights, biases_to_use, output); |
| 86 | } |
| 87 | |
| 88 | output->info()->set_quantization_info(weights->info()->quantization_info()); |
| 89 | } |
| 90 | |
| 91 | Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW) |
| 92 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 93 | 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] | 94 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 95 | if(!is_data_type_quantized_asymmetric(weights->data_type())) |
| 96 | { |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 98 | } |
| 99 | // Check if bias are present, if yes they will be embedded to the weights matrix |
| 100 | const bool append_bias = (biases != nullptr); |
| 101 | |
| 102 | if(append_bias) |
| 103 | { |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type())); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 106 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 108 | } |
| 109 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 110 | // Checks performed when biases are present |
| 111 | if(append_bias) |
| 112 | { |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 114 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 116 | } |
| 117 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 118 | if(transpose1xW) |
| 119 | { |
| 120 | TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, append_bias)); |
| 121 | ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped)); |
| 122 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output)); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output)); |
| 127 | } |
| 128 | |
| 129 | return Status{}; |
| 130 | } |
| 131 | |
| 132 | void NEConvolutionLayerReshapeWeights::run() |
| 133 | { |
| 134 | _memory_group.acquire(); |
| 135 | |
| 136 | NEScheduler::get().schedule(&_weights_reshape_kernel, 3); |
| 137 | |
| 138 | if(_transpose1xW) |
| 139 | { |
| 140 | NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY); |
| 141 | } |
| 142 | |
| 143 | _memory_group.release(); |
| 144 | } |
| 145 | |
| 146 | namespace |
| 147 | { |
| 148 | TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool append_bias, bool is_fully_connected_convolution) |
| 149 | { |
| 150 | unsigned int mat_weights_cols = weights->dimension(3); |
| 151 | unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0); |
| 152 | |
| 153 | if(is_fully_connected_convolution) |
| 154 | { |
| 155 | // Create tensor to store the reshaped weights |
| 156 | return TensorShape(mat_weights_cols, mat_weights_rows); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | // Create tensor to store transposed weights |
| 161 | const float transpose_width = 16.0f / weights->element_size(); |
| 162 | return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width))); |
| 163 | } |
| 164 | } |
| 165 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 166 | Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
| 167 | const ActivationLayerInfo &act_info, DataType &dt, |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 168 | bool &append_bias, bool &skip_im2col, |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 169 | bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 170 | bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized, bool &is_activationlayer_enabled, |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 171 | unsigned int &mat_weights_cols, unsigned int &mat_weights_rows, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 172 | unsigned int &conv_w, unsigned int &conv_h, const Size2D &dilation) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 173 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 174 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 175 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 176 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
| 177 | |
| 178 | DataLayout data_layout = input->data_layout(); |
| 179 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 180 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 181 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 182 | |
| 183 | ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(idx_channel) != input->dimension(idx_channel)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 184 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 185 | ARM_COMPUTE_RETURN_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->data_type())); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 186 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(data_layout == DataLayout::NHWC && input->data_type() != DataType::F32, "NHWC is only supported for FP32 data type."); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 187 | |
| 188 | dt = input->data_type(); |
| 189 | is_quantized = is_data_type_quantized_asymmetric(dt); |
| 190 | |
| 191 | if(biases != nullptr) |
| 192 | { |
| 193 | if(is_quantized) |
| 194 | { |
| 195 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 200 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 201 | ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3)); |
| 202 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 203 | } |
| 204 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 205 | // If we have 1x1 convolution and data layout is NHWC we can disable im2col |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 206 | append_bias = (biases != nullptr) && (!is_quantized); |
| 207 | are_weights_reshaped = weights_info.are_reshaped(); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 208 | kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(idx_width); |
| 209 | kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(idx_height); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 210 | mat_weights_cols = weights->dimension(3); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 211 | mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + ((append_bias && !skip_im2col) ? 1 : 0); |
| 212 | skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 213 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 214 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), input->dimension(idx_height), kernel_width, kernel_height, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 215 | conv_info, dilation); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 216 | |
| 217 | // Check if its a "fully connected" convolution |
| 218 | is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 219 | is_interleaved = (!is_fully_connected_convolution && !is_quantized); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 220 | is_activationlayer_enabled = act_info.enabled(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 221 | |
| 222 | return Status{}; |
| 223 | } |
| 224 | } // namespace |
| 225 | |
| 226 | NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager) |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 227 | : _asm_glue(), _memory_group(memory_manager), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 228 | _output_col2im_kernel(), _activationlayer_function(), _add_bias_kernel(), _original_weights(nullptr), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), |
| 229 | _tmp_output(), _workspace(), _B_pretransposed(), _data_layout(DataLayout::NCHW), _append_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false), _is_quantized(false), |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 230 | _is_interleaved(false), _is_activationlayer_enabled(false), _skip_im2col(false), _is_prepared(false) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 231 | { |
| 232 | } |
| 233 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 234 | void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, bool is_interleaved, const GEMMReshapeInfo &reshape_info) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 235 | { |
| 236 | if(_is_quantized) |
| 237 | { |
| 238 | // Since we need negative offsets for computing convolution, we need to change QuantizationInfo() |
| 239 | // Extract and negate input and weights offset |
| 240 | const QuantizationInfo input_quantization_info = input->info()->quantization_info(); |
| 241 | const QuantizationInfo weights_quantization_info = weights->info()->quantization_info(); |
| 242 | |
| 243 | input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset)); |
| 244 | weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset)); |
| 245 | |
| 246 | _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
| 247 | |
| 248 | // Revert back QuantizatioInfo as input and weights could be used in other convolution layers |
| 249 | input->info()->set_quantization_info(input_quantization_info); |
| 250 | weights->info()->set_quantization_info(weights_quantization_info); |
| 251 | } |
| 252 | else |
| 253 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 254 | _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 258 | void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 259 | const Size2D &dilation, const ActivationLayerInfo &act_info) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 260 | { |
| 261 | // Perform validate step |
| 262 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 263 | |
| 264 | DataType dt{}; |
| 265 | unsigned int kernel_width = 0; |
| 266 | unsigned int kernel_height = 0; |
| 267 | unsigned int mat_weights_cols = 0; |
| 268 | unsigned int mat_weights_rows = 0; |
| 269 | unsigned int conv_w = 0; |
| 270 | unsigned int conv_h = 0; |
| 271 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 272 | _data_layout = input->info()->data_layout(); |
| 273 | const bool is_nhwc = _data_layout == DataLayout::NHWC; |
| 274 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 275 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 276 | const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
| 277 | |
| 278 | Status status = validate_and_initialize_values(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), conv_info, weights_info, act_info, dt, _append_bias, _skip_im2col, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 279 | _are_weights_reshaped, |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 280 | kernel_width, kernel_height, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 281 | _is_fully_connected_convolution, _is_interleaved, _is_quantized, _is_activationlayer_enabled, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 282 | mat_weights_cols, mat_weights_rows, conv_w, conv_h, dilation); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 283 | |
| 284 | ARM_COMPUTE_ERROR_THROW_ON(status); |
| 285 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 286 | _is_prepared = false; |
| 287 | _original_weights = weights; |
| 288 | const ITensor *biases_to_use = (_append_bias) ? biases : nullptr; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 289 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 290 | bool run_optimised = dt == DataType::F32; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 291 | |
| 292 | // Reshape weights if needed |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 293 | if(run_optimised) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 294 | { |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 295 | TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows }; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 296 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 297 | // Create tensor to store the reshaped weights |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 298 | _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt)); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 299 | _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */); |
| 300 | weights = &_weights_reshaped; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
| 304 | if(_are_weights_reshaped) |
| 305 | { |
| 306 | if(_is_fully_connected_convolution || _is_quantized) |
| 307 | { |
| 308 | mat_weights_cols = weights_info.num_kernels(); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 309 | mat_weights_rows = weights->info()->dimension(idx_height); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 310 | } |
| 311 | else |
| 312 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 313 | mat_weights_cols = weights_info.num_kernels(); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 314 | mat_weights_rows = weights_info.kernel_size().first * weights_info.kernel_size().second * input->info()->dimension(idx_channel) + (_append_bias ? 1 : 0); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | TensorShape reshaped_weights_shape; |
| 320 | |
| 321 | if(_is_fully_connected_convolution || _is_quantized) |
| 322 | { |
| 323 | reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows }; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | // Create tensor to store transposed weights |
| 328 | const float transpose_width = 16.0f / input->info()->element_size(); |
| 329 | reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width), |
| 330 | static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) }; |
| 331 | } |
| 332 | |
| 333 | // Create tensor to store the reshaped weights |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 334 | _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 335 | _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 336 | weights = &_weights_reshaped; |
| 337 | } |
| 338 | } |
| 339 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 340 | // In case we skip im2col we have to add bias |
| 341 | if(!_skip_im2col) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 342 | { |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 343 | const unsigned int mat_input_cols = mat_weights_rows; |
| 344 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 345 | |
| 346 | // Create tensor to store im2col reshaped inputs |
| 347 | TensorShape shape_im2col(input->info()->tensor_shape()); |
| 348 | shape_im2col.set(0, mat_input_cols); |
| 349 | shape_im2col.set(1, mat_input_rows); |
| 350 | shape_im2col.set(2, 1); |
| 351 | _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 352 | _memory_group.manage(&_input_im2col_reshaped); |
| 353 | |
| 354 | // Create tensor (interleave) to prepare input tensor for GEMM |
| 355 | if(!_is_fully_connected_convolution && !run_optimised && _is_interleaved) |
| 356 | { |
| 357 | TensorShape shape_interleaved(shape_im2col); |
| 358 | shape_interleaved.set(idx_width, shape_interleaved.x() * 4); |
| 359 | shape_interleaved.set(idx_height, std::ceil(shape_interleaved[idx_height] / 4.f)); |
| 360 | _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved)); |
| 361 | _memory_group.manage(&_input_interleaved_reshaped); |
| 362 | } |
| 363 | |
| 364 | // Create GEMM output tensor |
| 365 | TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape()); |
| 366 | shape_gemm.set(0, mat_weights_cols); |
| 367 | shape_gemm.set(1, mat_input_rows); |
| 368 | const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt; |
| 369 | // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame^] | 370 | TensorInfo info_gemm(shape_gemm, 1, gemm_data_type); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 371 | info_gemm.set_quantization_info(output->info()->quantization_info()); |
| 372 | _gemm_output.allocator()->init(info_gemm); |
Georgios Pinitas | 2345f43 | 2018-05-18 20:13:01 +0100 | [diff] [blame] | 373 | _memory_group.manage(&_gemm_output); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 374 | |
| 375 | // Configure im2col |
| 376 | _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _append_bias, false, false, dilation); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 377 | } |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 378 | else if(_append_bias) |
| 379 | { |
| 380 | // Configure add bias kernel |
| 381 | _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE); |
| 382 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 383 | |
| 384 | // Configure matrix multiply |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 385 | if(run_optimised) |
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 | if(!setup_assembly_kernel(_skip_im2col ? input : &_input_im2col_reshaped, weights, is_nhwc ? output : &_gemm_output, 1.f, 0.f, true, _workspace, _B_pretransposed, _memory_group, _asm_glue)) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 388 | { |
| 389 | ARM_COMPUTE_ERROR("setup_assembly_kernel failed."); |
| 390 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 394 | if(_is_interleaved) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 395 | { |
| 396 | // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel |
| 397 | _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped); |
| 398 | |
| 399 | // Configure GEMM |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 400 | configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(idx_height), 0 /* no transpose */, |
| 401 | _input_im2col_reshaped.info()->dimension(idx_width))); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 402 | _input_interleaved_reshaped.allocator()->allocate(); |
| 403 | } |
| 404 | else |
| 405 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 406 | configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 410 | if(!_skip_im2col) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 411 | { |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 412 | _input_im2col_reshaped.allocator()->allocate(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 413 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 414 | // Configure output stage for quantized case |
| 415 | if(_is_quantized) |
| 416 | { |
| 417 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 418 | |
| 419 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
| 420 | int output_multiplier, output_shift; |
| 421 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 422 | _memory_group.manage(&_tmp_output); |
| 423 | _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset); |
| 424 | } |
| 425 | |
| 426 | // Configure Col2Im |
| 427 | if(!is_nhwc) |
| 428 | { |
| 429 | _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h)); |
| 430 | } |
| 431 | |
| 432 | if(_is_quantized) |
| 433 | { |
| 434 | _tmp_output.allocator()->allocate(); |
| 435 | } |
| 436 | _gemm_output.allocator()->allocate(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 439 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h), "Output shape does not match the expected one"); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 440 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 441 | //Configure Activation Layer |
| 442 | if(_is_activationlayer_enabled) |
| 443 | { |
| 444 | _activationlayer_function.configure(output, nullptr, act_info); |
| 445 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 449 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 450 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 451 | ARM_COMPUTE_UNUSED(output); |
| 452 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 453 | DataType dt{}; |
| 454 | bool append_bias{}; |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 455 | bool skip_im2col{}; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 456 | bool are_weights_reshaped{}; |
| 457 | bool is_fully_connected_convolution{}; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 458 | bool is_interleaved{}; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 459 | bool is_quantized{}; |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 460 | bool is_activationlayer_enabled{}; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 461 | unsigned int kernel_width = 0; |
| 462 | unsigned int kernel_height = 0; |
| 463 | unsigned int mat_weights_cols = 0; |
| 464 | unsigned int mat_weights_rows = 0; |
| 465 | unsigned int conv_w = 0; |
| 466 | unsigned int conv_h = 0; |
| 467 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 468 | const DataLayout data_layout = input->data_layout(); |
| 469 | const bool is_nhwc = data_layout == DataLayout::NHWC; |
| 470 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 471 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 472 | |
| 473 | Status status = validate_and_initialize_values(input, weights, biases, conv_info, weights_info, act_info, dt, append_bias, skip_im2col, are_weights_reshaped, kernel_width, kernel_height, |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 474 | is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 475 | conv_w, conv_h, dilation); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 476 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 477 | const Size2D kernel_weights = Size2D(kernel_width, kernel_height); |
| 478 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 479 | ARM_COMPUTE_RETURN_ON_ERROR(status); |
| 480 | |
| 481 | std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone(); |
| 482 | bool optimised_kernel = false; |
| 483 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 484 | if(dt == DataType::F32) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 485 | { |
| 486 | optimised_kernel = true; |
| 487 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 488 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 489 | const unsigned int mat_input_cols = mat_weights_rows; |
| 490 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 491 | TensorShape shape_im2col = input->tensor_shape(); |
| 492 | shape_im2col.set(0, mat_input_cols); |
| 493 | shape_im2col.set(1, mat_input_rows); |
| 494 | shape_im2col.set(2, 1); |
| 495 | TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 496 | |
| 497 | if(!skip_im2col) |
| 498 | { |
| 499 | // Validate im2col |
| 500 | ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation)); |
| 501 | } |
| 502 | else if(append_bias) |
| 503 | { |
| 504 | // Validate add bias kernel |
| 505 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE)); |
| 506 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 507 | |
| 508 | // Create GEMM output tensor |
| 509 | TensorShape shape_gemm(im2_col_info.tensor_shape()); |
| 510 | shape_gemm.set(0, mat_weights_cols); |
| 511 | shape_gemm.set(1, mat_input_rows); |
| 512 | TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm); |
| 513 | |
Gian Marco Iodice | a72300a | 2018-04-12 11:41:26 +0100 | [diff] [blame] | 514 | // Reshape weights if needed |
| 515 | if(optimised_kernel) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 516 | { |
Gian Marco Iodice | a72300a | 2018-04-12 11:41:26 +0100 | [diff] [blame] | 517 | ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped); |
| 518 | |
| 519 | // Create tensor to store the reshaped weights |
| 520 | reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution)); |
| 521 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */)); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 522 | } |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 523 | else if(!is_quantized) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 524 | { |
Gian Marco Iodice | a72300a | 2018-04-12 11:41:26 +0100 | [diff] [blame] | 525 | TensorShape reshaped_weights_shape; |
| 526 | |
| 527 | if(is_fully_connected_convolution || is_quantized) |
| 528 | { |
| 529 | reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows }; |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | // Create tensor to store transposed weights |
| 534 | const float transpose_width = 16.0f / input->element_size(); |
| 535 | reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width), |
| 536 | static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) }; |
| 537 | } |
| 538 | |
| 539 | // Create tensor to store the reshaped weights |
| 540 | reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution)); |
| 541 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */)); |
| 542 | weights = reshaped_weights.get(); |
| 543 | |
| 544 | // Validate GEMM interleave and multiply |
| 545 | if(is_interleaved) |
| 546 | { |
| 547 | TensorShape shape_interleaved = shape_im2col; |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 548 | shape_interleaved.set(idx_width, shape_interleaved.x() * 4); |
| 549 | shape_interleaved.set(idx_height, std::ceil(shape_interleaved.y() / 4.f)); |
Gian Marco Iodice | a72300a | 2018-04-12 11:41:26 +0100 | [diff] [blame] | 550 | TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved); |
| 551 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info)); |
| 552 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m |
| 553 | weights->tensor_shape()[0], // n |
| 554 | shape_im2col[0]) /* k */)); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo())); |
| 559 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 560 | } |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 561 | if(!is_nhwc) |
| 562 | { |
| 563 | ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h))); |
| 564 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 565 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 566 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(idx_width) != conv_w) || (output->dimension(idx_height) != conv_h), "Output shape does not match the expected one"); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 567 | |
| 568 | if(act_info.enabled()) |
| 569 | { |
| 570 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info)); |
| 571 | } |
| 572 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 573 | return Status{}; |
| 574 | } |
| 575 | |
| 576 | void NEGEMMConvolutionLayer::run() |
| 577 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 578 | prepare(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 579 | |
| 580 | _memory_group.acquire(); |
| 581 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 582 | if(!_skip_im2col) |
| 583 | { |
| 584 | // Run input reshaping |
| 585 | unsigned int _y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 586 | NEScheduler::get().schedule(&_input_im2col_kernel, _y_dim); |
| 587 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 588 | |
| 589 | // Runs matrix multiply on reshaped matrices |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 590 | if(_asm_glue._optimised_kernel != nullptr) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 591 | { |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 592 | _asm_glue.run(); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 593 | } |
| 594 | else |
| 595 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 596 | if(_is_interleaved) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 597 | { |
| 598 | // Run interleave |
| 599 | NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY); |
| 600 | } |
| 601 | |
| 602 | // Runs matrix multiply on reshaped matrices |
| 603 | if(_is_quantized) |
| 604 | { |
| 605 | _mm_gemmlowp.run(); |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | NEScheduler::get().schedule(&_mm_kernel, Window::DimY); |
| 610 | } |
| 611 | } |
| 612 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 613 | if(_skip_im2col && _append_bias) |
| 614 | { |
| 615 | NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY); |
| 616 | } |
| 617 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 618 | // Run output stage for quantized case |
| 619 | if(_is_quantized) |
| 620 | { |
| 621 | _gemmlowp_output_stage.run(); |
| 622 | } |
| 623 | |
| 624 | // Reshape output matrix |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 625 | if(_data_layout == DataLayout::NCHW) |
| 626 | { |
| 627 | NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY); |
| 628 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 629 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 630 | if(_is_activationlayer_enabled) |
| 631 | { |
| 632 | _activationlayer_function.run(); |
| 633 | } |
| 634 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 635 | _memory_group.release(); |
| 636 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 637 | |
| 638 | void NEGEMMConvolutionLayer::prepare() |
| 639 | { |
| 640 | if(!_is_prepared) |
| 641 | { |
| 642 | // Run weights reshaping (Runs once for every configure) |
| 643 | if(!_are_weights_reshaped) |
| 644 | { |
| 645 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 646 | |
| 647 | _weights_reshaped.allocator()->allocate(); |
| 648 | _reshape_weights.run(); |
| 649 | _reshape_weights = NEConvolutionLayerReshapeWeights(); |
| 650 | _original_weights->mark_as_unused(); |
| 651 | _are_weights_reshaped = true; |
| 652 | } |
| 653 | |
| 654 | // Run GEMM prepare stage |
| 655 | if(_asm_glue._optimised_kernel) |
| 656 | { |
| 657 | _asm_glue.prepare(); |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | if(_is_quantized) |
| 662 | { |
| 663 | _mm_gemmlowp.prepare(); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Release weights in case buffer is pretransposed |
| 668 | if(!_weights_reshaped.is_used()) |
| 669 | { |
| 670 | _weights_reshaped.allocator()->free(); |
| 671 | } |
| 672 | |
| 673 | _is_prepared = true; |
| 674 | } |
| 675 | } |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 676 | } // namespace arm_compute |