blob: 2888b435b1864142c7cc467a3804aee03a102a2a [file] [log] [blame]
Anthony Barbier06ea0482018-02-22 15:45:35 +00001/*
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
Anthony Barbier06ea0482018-02-22 15:45:35 +000026#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
Anthony Barbier06ea0482018-02-22 15:45:35 +000034#include <cmath>
35#include <tuple>
36
37namespace
38{
39arm_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
47namespace arm_compute
48{
49NEConvolutionLayerReshapeWeights::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
54void 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
91Status 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
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
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
134void 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
148namespace
149{
150TensorShape 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
Jenkinsb3a371b2018-05-23 11:36:53 +0100168Status 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,
170 bool &append_bias, bool &skip_im2col,
Anthony Barbier06ea0482018-02-22 15:45:35 +0000171 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height,
Jenkinsb3a371b2018-05-23 11:36:53 +0100172 bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized, bool &is_activationlayer_enabled,
Anthony Barbier06ea0482018-02-22 15:45:35 +0000173 unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
Jenkinsb3a371b2018-05-23 11:36:53 +0100174 unsigned int &conv_w, unsigned int &conv_h, const Size2D &dilation)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000175{
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);
Jenkinsb3a371b2018-05-23 11:36:53 +0100179 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));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000187 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()));
Jenkinsb3a371b2018-05-23 11:36:53 +0100189 ARM_COMPUTE_RETURN_ERROR_ON_MSG(data_layout == DataLayout::NHWC && input->data_type() != DataType::F32, "NHWC is only supported for FP32 data type.");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000190
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
Jenkinsb3a371b2018-05-23 11:36:53 +0100209 // If we have 1x1 convolution and data layout is NHWC we can disable im2col
Anthony Barbier06ea0482018-02-22 15:45:35 +0000210 append_bias = (biases != nullptr) && (!is_quantized);
211 are_weights_reshaped = weights_info.are_reshaped();
Jenkinsb3a371b2018-05-23 11:36:53 +0100212 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);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000214 mat_weights_cols = weights->dimension(3);
Jenkinsb3a371b2018-05-23 11:36:53 +0100215 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);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000217
Jenkinsb3a371b2018-05-23 11:36:53 +0100218 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), input->dimension(idx_height), kernel_width, kernel_height,
219 conv_info, dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000220
221 // Check if its a "fully connected" convolution
222 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
223 is_interleaved = (!is_fully_connected_convolution && !is_quantized);
Jenkinsb3a371b2018-05-23 11:36:53 +0100224 is_activationlayer_enabled = act_info.enabled();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000225
226 return Status{};
227}
228} // namespace
229
230NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Jenkinsb3a371b2018-05-23 11:36:53 +0100231 : _asm_glue(), _memory_group(memory_manager), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(),
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),
234 _is_interleaved(false), _is_activationlayer_enabled(false), _skip_im2col(false)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000235{
236}
237
238void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, bool is_interleaved, const GEMMReshapeInfo &reshape_info)
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 {
258 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info);
259 }
260}
261
Jenkinsb3a371b2018-05-23 11:36:53 +0100262void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
263 const Size2D &dilation, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000264{
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
Jenkinsb3a371b2018-05-23 11:36:53 +0100276 _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,
283 _are_weights_reshaped,
Anthony Barbier06ea0482018-02-22 15:45:35 +0000284 kernel_width, kernel_height,
Jenkinsb3a371b2018-05-23 11:36:53 +0100285 _is_fully_connected_convolution, _is_interleaved, _is_quantized, _is_activationlayer_enabled,
286 mat_weights_cols, mat_weights_rows, conv_w, conv_h, dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000287
288 ARM_COMPUTE_ERROR_THROW_ON(status);
289
Jenkinsb3a371b2018-05-23 11:36:53 +0100290 _original_weights = weights;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000291 const unsigned int fixed_point_position = input->info()->fixed_point_position();
292 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
293
Jenkinsb3a371b2018-05-23 11:36:53 +0100294 bool run_optimised = dt == DataType::F32;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000295
296 // Reshape weights if needed
Jenkinsb3a371b2018-05-23 11:36:53 +0100297 if(run_optimised)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000298 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100299 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
Anthony Barbier06ea0482018-02-22 15:45:35 +0000300
Jenkinsb3a371b2018-05-23 11:36:53 +0100301 // Create tensor to store the reshaped weights
302 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
303 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
304 weights = &_weights_reshaped;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000305 }
306 else
307 {
308 if(_are_weights_reshaped)
309 {
310 if(_is_fully_connected_convolution || _is_quantized)
311 {
312 mat_weights_cols = weights_info.num_kernels();
Jenkinsb3a371b2018-05-23 11:36:53 +0100313 mat_weights_rows = weights->info()->dimension(idx_height);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000314 }
315 else
316 {
317 mat_weights_cols = weights_info.num_kernels();
Jenkinsb3a371b2018-05-23 11:36:53 +0100318 mat_weights_rows = weights_info.kernel_size().first * weights_info.kernel_size().second * input->info()->dimension(idx_channel) + (_append_bias ? 1 : 0);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000319 }
320 }
321 else
322 {
323 TensorShape reshaped_weights_shape;
324
325 if(_is_fully_connected_convolution || _is_quantized)
326 {
327 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
328 }
329 else
330 {
331 // Create tensor to store transposed weights
332 const float transpose_width = 16.0f / input->info()->element_size();
333 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
334 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
335 }
336
337 // Create tensor to store the reshaped weights
338 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
339 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
340 weights = &_weights_reshaped;
341 }
342 }
343
Jenkinsb3a371b2018-05-23 11:36:53 +0100344 // In case we skip im2col we have to add bias
345 if(!_skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000346 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100347 const unsigned int mat_input_cols = mat_weights_rows;
348 const unsigned int mat_input_rows = conv_w * conv_h;
349
350 // Create tensor to store im2col reshaped inputs
351 TensorShape shape_im2col(input->info()->tensor_shape());
352 shape_im2col.set(0, mat_input_cols);
353 shape_im2col.set(1, mat_input_rows);
354 shape_im2col.set(2, 1);
355 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
356 _memory_group.manage(&_input_im2col_reshaped);
357
358 // Create tensor (interleave) to prepare input tensor for GEMM
359 if(!_is_fully_connected_convolution && !run_optimised && _is_interleaved)
360 {
361 TensorShape shape_interleaved(shape_im2col);
362 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
363 shape_interleaved.set(idx_height, std::ceil(shape_interleaved[idx_height] / 4.f));
364 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
365 _memory_group.manage(&_input_interleaved_reshaped);
366 }
367
368 // Create GEMM output tensor
369 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
370 shape_gemm.set(0, mat_weights_cols);
371 shape_gemm.set(1, mat_input_rows);
372 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
373 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
374 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
375 info_gemm.set_quantization_info(output->info()->quantization_info());
376 _gemm_output.allocator()->init(info_gemm);
377
378 // Configure im2col
379 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _append_bias, false, false, dilation);
380 }
381 else if(_append_bias)
382 {
383 // Configure add bias kernel
384 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000385 }
386
Anthony Barbier06ea0482018-02-22 15:45:35 +0000387 // Configure matrix multiply
Jenkinsb3a371b2018-05-23 11:36:53 +0100388 if(run_optimised)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000389 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100390 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))
Anthony Barbier06ea0482018-02-22 15:45:35 +0000391 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100392 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000393 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000394 }
395 else
396 {
397 if(_is_interleaved)
398 {
399 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
400 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
401
402 // Configure GEMM
Jenkinsb3a371b2018-05-23 11:36:53 +0100403 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(idx_height), 0 /* no transpose */,
404 _input_im2col_reshaped.info()->dimension(idx_width)));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000405 _input_interleaved_reshaped.allocator()->allocate();
406 }
407 else
408 {
409 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
410 }
411 }
412
Jenkinsb3a371b2018-05-23 11:36:53 +0100413 if(!_skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000414 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100415 _input_im2col_reshaped.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000416
Jenkinsb3a371b2018-05-23 11:36:53 +0100417 // Configure output stage for quantized case
418 if(_is_quantized)
419 {
420 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
421
422 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
423 int output_multiplier, output_shift;
424 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
425 _memory_group.manage(&_tmp_output);
426 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
427 }
428
429 // Configure Col2Im
430 if(!is_nhwc)
431 {
432 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
433 }
434
435 if(_is_quantized)
436 {
437 _tmp_output.allocator()->allocate();
438 }
439 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000440 }
441
Jenkinsb3a371b2018-05-23 11:36:53 +0100442 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");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000443
444 // Allocate intermediate tensor
445 if(!_are_weights_reshaped)
446 {
447 _weights_reshaped.allocator()->allocate();
448 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100449
450 //Configure Activation Layer
451 if(_is_activationlayer_enabled)
452 {
453 _activationlayer_function.configure(output, nullptr, act_info);
454 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000455}
456
457Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100458 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000459{
460 ARM_COMPUTE_UNUSED(output);
461
462 DataType dt{};
463 bool append_bias{};
Jenkinsb3a371b2018-05-23 11:36:53 +0100464 bool skip_im2col{};
Anthony Barbier06ea0482018-02-22 15:45:35 +0000465 bool are_weights_reshaped{};
466 bool is_fully_connected_convolution{};
467 bool is_interleaved{};
468 bool is_quantized{};
Jenkinsb3a371b2018-05-23 11:36:53 +0100469 bool is_activationlayer_enabled{};
Anthony Barbier06ea0482018-02-22 15:45:35 +0000470 unsigned int kernel_width = 0;
471 unsigned int kernel_height = 0;
472 unsigned int mat_weights_cols = 0;
473 unsigned int mat_weights_rows = 0;
474 unsigned int conv_w = 0;
475 unsigned int conv_h = 0;
476
Jenkinsb3a371b2018-05-23 11:36:53 +0100477 const DataLayout data_layout = input->data_layout();
478 const bool is_nhwc = data_layout == DataLayout::NHWC;
479 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
480 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
481
482 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,
483 is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows,
484 conv_w, conv_h, dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000485
486 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
487
488 ARM_COMPUTE_RETURN_ON_ERROR(status);
489
490 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
491 bool optimised_kernel = false;
492
Jenkinsb3a371b2018-05-23 11:36:53 +0100493 if(dt == DataType::F32)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000494 {
495 optimised_kernel = true;
496 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000497
Anthony Barbier06ea0482018-02-22 15:45:35 +0000498 const unsigned int mat_input_cols = mat_weights_rows;
499 const unsigned int mat_input_rows = conv_w * conv_h;
500 TensorShape shape_im2col = input->tensor_shape();
501 shape_im2col.set(0, mat_input_cols);
502 shape_im2col.set(1, mat_input_rows);
503 shape_im2col.set(2, 1);
504 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Jenkinsb3a371b2018-05-23 11:36:53 +0100505
506 if(!skip_im2col)
507 {
508 // Validate im2col
509 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation));
510 }
511 else if(append_bias)
512 {
513 // Validate add bias kernel
514 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
515 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000516
517 // Create GEMM output tensor
518 TensorShape shape_gemm(im2_col_info.tensor_shape());
519 shape_gemm.set(0, mat_weights_cols);
520 shape_gemm.set(1, mat_input_rows);
521 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
522
Jenkinsb3a371b2018-05-23 11:36:53 +0100523 // Reshape weights if needed
524 if(optimised_kernel)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000525 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100526 ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped);
527
528 // Create tensor to store the reshaped weights
529 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
530 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000531 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100532 else if(!is_quantized)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000533 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100534 TensorShape reshaped_weights_shape;
535
536 if(is_fully_connected_convolution || is_quantized)
537 {
538 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
539 }
540 else
541 {
542 // Create tensor to store transposed weights
543 const float transpose_width = 16.0f / input->element_size();
544 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
545 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
546 }
547
548 // Create tensor to store the reshaped weights
549 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
550 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
551 weights = reshaped_weights.get();
552
553 // Validate GEMM interleave and multiply
554 if(is_interleaved)
555 {
556 TensorShape shape_interleaved = shape_im2col;
557 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
558 shape_interleaved.set(idx_height, std::ceil(shape_interleaved.y() / 4.f));
559 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
560 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
561 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m
562 weights->tensor_shape()[0], // n
563 shape_im2col[0]) /* k */));
564 }
565 else
566 {
567 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
568 }
569 }
570 if(!is_nhwc)
571 {
572 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
573 }
574
575 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");
576
577 if(act_info.enabled())
578 {
579 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000580 }
581
582 return Status{};
583}
584
585void NEGEMMConvolutionLayer::run()
586{
587 // Run weights reshaping (Runs once for every configure)
588 if(!_are_weights_reshaped)
589 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100590 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
591
Anthony Barbier06ea0482018-02-22 15:45:35 +0000592 _are_weights_reshaped = true;
593 _reshape_weights.run();
Jenkinsb3a371b2018-05-23 11:36:53 +0100594
595 // Mark original weights tensor as unused
596 _original_weights->mark_as_unused();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000597 }
598
599 _memory_group.acquire();
600
Jenkinsb3a371b2018-05-23 11:36:53 +0100601 if(!_skip_im2col)
602 {
603 // Run input reshaping
604 unsigned int _y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
605 NEScheduler::get().schedule(&_input_im2col_kernel, _y_dim);
606 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000607
608 // Runs matrix multiply on reshaped matrices
Jenkinsb3a371b2018-05-23 11:36:53 +0100609 if(_asm_glue._optimised_kernel != nullptr)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000610 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100611 _asm_glue.run();
612 // Release weights in case buffer is pretransposed
613 if(!_weights_reshaped.is_used())
614 {
615 _weights_reshaped.allocator()->free();
616 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000617 }
618 else
619 {
620 if(_is_interleaved)
621 {
622 // Run interleave
623 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
624 }
625
626 // Runs matrix multiply on reshaped matrices
627 if(_is_quantized)
628 {
629 _mm_gemmlowp.run();
630 }
631 else
632 {
633 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
634 }
635 }
636
Jenkinsb3a371b2018-05-23 11:36:53 +0100637 if(_skip_im2col && _append_bias)
638 {
639 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
640 }
641
Anthony Barbier06ea0482018-02-22 15:45:35 +0000642 // Run output stage for quantized case
643 if(_is_quantized)
644 {
645 _gemmlowp_output_stage.run();
646 }
647
648 // Reshape output matrix
Jenkinsb3a371b2018-05-23 11:36:53 +0100649 if(_data_layout == DataLayout::NCHW)
650 {
651 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
652 }
653
654 if(_is_activationlayer_enabled)
655 {
656 _activationlayer_function.run();
657 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000658
659 _memory_group.release();
660}
661} // namespace arm_compute