blob: 8f7d940fcac79e4f3b877b6cdb946e046d921f56 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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/NEConvolutionLayer.h"
25
Moritz Pflanzer80373f62017-09-15 10:42:58 +010026#include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010027#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/PixelValue.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010029#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010033#include "support/ToolchainSupport.h"
34
35namespace arm_compute
36{
37#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Moritz Pflanzer80373f62017-09-15 10:42:58 +010038#include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010039#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp"
40} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
42#include <cmath>
43#include <tuple>
44
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010045namespace arm_compute
46{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000047namespace
48{
49TensorShape get_reshaped_weights_shape(const ITensorInfo *weights, bool has_bias)
50{
51 const unsigned int mat_weights_cols = weights->dimension(3);
52 const unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (has_bias ? 1 : 0);
53 return TensorShape(mat_weights_cols, mat_weights_rows);
54}
55} // namespace
56
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010057NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
58 : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059{
60}
61
62void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
63{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000064 // Perform validation step
65 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
66 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(),
67 (biases != nullptr) ? biases->info() : nullptr,
68 output->info(),
69 transpose1xW));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
71 // Check if bias are present, if yes they will be embedded to the weights matrix
72 const bool _has_bias = (biases != nullptr);
73
74 _transpose1xW = transpose1xW;
75
76 if(transpose1xW)
77 {
78 // Create tensor to store the reshaped weights
Giorgio Arena7c23ad02017-11-30 15:08:38 +000079 TensorInfo info_wr = weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(get_reshaped_weights_shape(weights->info(), _has_bias));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 _weights_reshaped.allocator()->init(info_wr);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010082 _memory_group.manage(&_weights_reshaped);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010083
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
85 _weights_transposed_kernel.configure(&_weights_reshaped, output);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010086
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 _weights_reshaped.allocator()->allocate();
88 }
89 else
90 {
91 _weights_reshape_kernel.configure(weights, biases, output);
92 }
93}
94
Giorgio Arena7c23ad02017-11-30 15:08:38 +000095Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW)
96{
97 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
100 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
101
102 if(biases != nullptr)
103 {
104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases);
106 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
107 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
108 }
109
110 // Check if bias are present, if yes they will be embedded to the weights matrix
111 const bool has_bias = (biases != nullptr);
112
113 // Checks performed when biases are present
114 if(has_bias)
115 {
116 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
117 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
118 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
119 }
120
121 if(transpose1xW)
122 {
123 TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, has_bias));
124 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped));
125 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output));
126 }
127 else
128 {
129 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output));
130 }
131
132 return Status{};
133}
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135void NEConvolutionLayerReshapeWeights::run()
136{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100137 _memory_group.acquire();
138
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100140
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 if(_transpose1xW)
142 {
143 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
144 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100145
146 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147}
148
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000149namespace
150{
151TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool has_bias, bool is_fully_connected_convolution)
152{
153 unsigned int mat_weights_cols = weights->dimension(3);
154 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (has_bias ? 1 : 0);
155
156 if(is_fully_connected_convolution)
157 {
158 // Create tensor to store the reshaped weights
159 return TensorShape(mat_weights_cols, mat_weights_rows);
160 }
161 else
162 {
163 // Create tensor to store transposed weights
164 const float transpose_width = 16.0f / weights->element_size();
165 return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
166 }
167}
168
169Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, DataType &dt,
170 bool &has_bias,
171 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height, bool &is_fully_connected_convolution, unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
172 unsigned int &conv_w, unsigned int &conv_h)
173{
174 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
175 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
176 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
177 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(2) != input->dimension(2));
178 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
179
180 if(biases != nullptr)
181 {
182 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
183 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
184 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3));
185 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
186 }
187
188 dt = input->data_type();
189 has_bias = (biases != nullptr);
190 are_weights_reshaped = weights_info.are_reshaped();
191 kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(0);
192 kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(1);
193 mat_weights_cols = weights->dimension(3);
194 mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (has_bias ? 1 : 0);
195
196 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
197 conv_info);
198
199 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
200
201 return Status{};
202}
203} // namespace
204
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100205NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100206 : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_optimised_kernel(nullptr), _output_col2im_kernel(),
207 _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _workspace(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208{
209}
210
211void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
212{
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000213 // Perform validate step
214 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000216 DataType dt{};
217 unsigned int kernel_width = 0;
218 unsigned int kernel_height = 0;
219 unsigned int mat_weights_cols = 0;
220 unsigned int mat_weights_rows = 0;
221 unsigned int conv_w = 0;
222 unsigned int conv_h = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000224 Status status = validate_and_initialize_values(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), conv_info, weights_info, dt, _has_bias, _are_weights_reshaped,
225 kernel_width, kernel_height,
226 _is_fully_connected_convolution,
227 mat_weights_cols, mat_weights_rows, conv_w, conv_h);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000229 ARM_COMPUTE_ERROR_THROW_ON(status);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000231 const unsigned int fixed_point_position = input->info()->fixed_point_position();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100233#if defined(__arm__)
234 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32)
235 {
236 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>();
237 }
238#elif defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100239 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32)
240 {
241 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>();
242 }
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100243#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100244
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245 // Reshape weights if needed
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100246 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100248 if(_are_weights_reshaped)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100250 mat_weights_cols = weights_info.num_kernels();
251 mat_weights_rows = weights->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252 }
253 else
254 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100255 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
256
257 // Create tensor to store the reshaped weights
258 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
259 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
260 weights = &_weights_reshaped;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100262 }
263 else
264 {
265 if(_are_weights_reshaped)
266 {
Georgios Pinitasb660dcf2017-12-13 10:48:06 +0000267 if(_is_fully_connected_convolution)
268 {
269 mat_weights_cols = weights_info.num_kernels();
270 mat_weights_rows = weights->info()->dimension(1);
271 }
272 else
273 {
274 const unsigned int transpose_width = 16 / input->info()->element_size();
275 mat_weights_cols = weights_info.num_kernels();
276 mat_weights_rows = weights->info()->dimension(0) / transpose_width + (_has_bias ? 1 : 0);
277 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100278 }
279 else
280 {
281 TensorShape reshaped_weights_shape;
282
283 if(_is_fully_connected_convolution)
284 {
285 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
286 }
287 else
288 {
289 // Create tensor to store transposed weights
290 const float transpose_width = 16.0f / input->info()->element_size();
291 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
292 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
293 }
294
295 // Create tensor to store the reshaped weights
296 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
297 _reshape_weights.configure(weights, biases, &_weights_reshaped, !_is_fully_connected_convolution /* 1xW transpose */);
298 weights = &_weights_reshaped;
299 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300 }
301
302 // Create tensor to store im2col reshaped inputs
303 const unsigned int mat_input_cols = mat_weights_rows;
304 const unsigned int mat_input_rows = conv_w * conv_h;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100305
306 TensorShape shape_im2col(input->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307 shape_im2col.set(0, mat_input_cols);
308 shape_im2col.set(1, mat_input_rows);
309 shape_im2col.set(2, 1);
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000310 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100311 _memory_group.manage(&_input_im2col_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312
313 // Create tensor (interleave) to prepare input tensor for GEMM
Georgios Pinitas1b2e2e52017-09-28 11:30:27 +0100314 if(!_is_fully_connected_convolution && _mm_optimised_kernel == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100316 TensorShape shape_interleaved(shape_im2col);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317 shape_interleaved.set(0, shape_interleaved.x() * 4);
318 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000319 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100320 _memory_group.manage(&_input_interleaved_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321 }
322
323 // Create GEMM output tensor
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100324 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325 shape_gemm.set(0, mat_weights_cols);
326 shape_gemm.set(1, mat_input_rows);
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000327 _gemm_output.allocator()->init(_input_im2col_reshaped.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_gemm));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100328 _memory_group.manage(&_gemm_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329
330 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100331 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100332
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100333#if defined(__arm__) || defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100334 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100336 struct CPUInfo ci = NEScheduler::get().cpu_info();
337
338 const int M = _gemm_output.info()->tensor_shape().y();
339 const int N = _gemm_output.info()->tensor_shape().x();
340 const int K = _input_im2col_reshaped.info()->tensor_shape().x();
341
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100342#if defined(__arm__)
343 GemmInterleaved<sgemm_8x6, float, float> gemm(&ci, M, N, K, false, false);
344#elif defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100345 GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false);
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100346#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100347
348 constexpr size_t alignment = 4096;
349 _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
350 _memory_group.manage(&_workspace);
351
352 // Configure matrix multiplication kernel
Georgios Pinitas08c5a062017-12-14 17:53:39 +0000353 _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100354
355 _workspace.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 }
357 else
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100358#endif /* defined(__arm__) || defined(__aarch64__) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100360 if(_is_fully_connected_convolution)
361 {
362 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
363 }
364 else
365 {
366 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
367 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
368 _input_interleaved_reshaped.allocator()->allocate();
369 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100371
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100372 _input_im2col_reshaped.allocator()->allocate();
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000373 _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100374 _gemm_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100375
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376 // Allocate intermediate tensor
377 if(!_are_weights_reshaped)
378 {
379 _weights_reshaped.allocator()->allocate();
380 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381}
382
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000383Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
384 const WeightsInfo &weights_info)
385{
386 DataType dt{};
387 bool has_bias{};
388 bool are_weights_reshaped{};
389 bool is_fully_connected_convolution{};
390 unsigned int kernel_width = 0;
391 unsigned int kernel_height = 0;
392 unsigned int mat_weights_cols = 0;
393 unsigned int mat_weights_rows = 0;
394 unsigned int conv_w = 0;
395 unsigned int conv_h = 0;
396
397 Status status = validate_and_initialize_values(input, weights, biases, conv_info, weights_info, dt, has_bias, are_weights_reshaped, kernel_width, kernel_height,
398 is_fully_connected_convolution, mat_weights_cols, mat_weights_rows,
399 conv_w, conv_h);
400
401 ARM_COMPUTE_RETURN_ON_ERROR(status);
402
403 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
404 bool optimised_kernel = false;
405
406#if defined(__arm__)
407 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32)
408 {
409 optimised_kernel = true;
410 }
411#elif defined(__aarch64__)
412 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32)
413 {
414 optimised_kernel = true;
415 }
416#endif /* defined(__arm__) || defined(__aarch64__) */
417
418 // Reshape weights if needed
419 if(optimised_kernel)
420 {
421 if(are_weights_reshaped)
422 {
423 mat_weights_cols = weights_info.num_kernels();
424 mat_weights_rows = weights->dimension(1);
425 }
426 else
427 {
428 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
429
430 // Create tensor to store the reshaped weights
431 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, has_bias, is_fully_connected_convolution));
432 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
433 weights = reshaped_weights.get();
434 }
435 }
436 else
437 {
438 if(are_weights_reshaped)
439 {
440 const unsigned int transpose_width = 16 / input->element_size();
441 mat_weights_cols = weights_info.num_kernels();
442 mat_weights_rows = weights->dimension(0) / transpose_width + (has_bias ? 1 : 0);
443 }
444 else
445 {
446 TensorShape reshaped_weights_shape;
447
448 if(is_fully_connected_convolution)
449 {
450 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
451 }
452 else
453 {
454 // Create tensor to store transposed weights
455 const float transpose_width = 16.0f / input->element_size();
456 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
457 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
458 }
459
460 // Create tensor to store the reshaped weights
461 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, has_bias, is_fully_connected_convolution));
462 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
463 weights = reshaped_weights.get();
464 }
465 }
466
467 // Validate im2col
468 const unsigned int mat_input_cols = mat_weights_rows;
469 const unsigned int mat_input_rows = conv_w * conv_h;
470 TensorShape shape_im2col = input->tensor_shape();
471 shape_im2col.set(0, mat_input_cols);
472 shape_im2col.set(1, mat_input_rows);
473 shape_im2col.set(2, 1);
474 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
475 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, Size2D(weights->dimension(0), weights->dimension(1)), conv_info, has_bias));
476
477 // Create GEMM output tensor
478 TensorShape shape_gemm(im2_col_info.tensor_shape());
479 shape_gemm.set(0, mat_weights_cols);
480 shape_gemm.set(1, mat_input_rows);
481 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
482
483 // Validate GEMM interleave and multiply
484 if(!is_fully_connected_convolution)
485 {
486 TensorShape shape_interleaved = shape_im2col;
487 shape_interleaved.set(0, shape_interleaved.x() * 4);
488 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
489 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
490 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
491 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info));
492 }
493 else
494 {
495 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info));
496 }
497
498 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
499
500 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one");
501
502 return Status{};
503}
504
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505void NEConvolutionLayer::run()
506{
507 // Run weights reshaping (Runs once for every configure)
508 if(!_are_weights_reshaped)
509 {
510 _are_weights_reshaped = true;
511 _reshape_weights.run();
512 }
513
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100514 _memory_group.acquire();
515
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100516 // Run input reshaping
517 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100518
519 // Runs matrix multiply on reshaped matrices
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100520 if(_mm_optimised_kernel != nullptr)
521 {
522 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
523 }
524 else
525 {
526 if(!_is_fully_connected_convolution)
527 {
528 // Run interleave
529 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
530 }
531
532 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
533 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100534
535 // Reshape output matrix
536 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100537
538 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100539}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100540} // namespace arm_compute