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