blob: ff7934e23a34a1ec2a72b2baf12a39db4d2a9f39 [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Pablo Tello9ceebbe2018-01-10 16:44:13 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tello89519332017-11-17 11:52:36 +00003 *
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 */
Georgios Pinitas9fb11592018-04-26 20:34:58 +010024#include "arm_compute/runtime/NEON/functions/NEWinogradConvolutionLayer.h"
Pablo Tello89519332017-11-17 11:52:36 +000025
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000026#include "arm_compute/core/Error.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010027#include "arm_compute/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h"
Pablo Tello89519332017-11-17 11:52:36 +000028#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010030#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Pablo Tello89519332017-11-17 11:52:36 +000032#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010033#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
Pablo Tello89519332017-11-17 11:52:36 +000034#include "support/ToolchainSupport.h"
35
Georgios Pinitas4074c992018-01-30 18:13:46 +000036#include "arm_compute/core/NEON/kernels/convolution/winograd/winograd_gemm.hpp"
Pablo Tellod6ca4782018-01-23 09:36:04 +000037
Pablo Tello89519332017-11-17 11:52:36 +000038namespace arm_compute
39{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000040namespace
41{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010042inline Tensor4DShape internal_get_input_shape(const arm_compute::ITensor *input)
43{
44 const DataLayout data_layout = input->info()->data_layout();
45 const int in_width = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH));
46 const int in_height = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT));
47 const int in_channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
48 const int in_batches = input->info()->dimension(3);
49
50 return Tensor4DShape({ in_batches, in_height, in_width, in_channels });
51}
52
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000053Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
54{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010055 ARM_COMPUTE_UNUSED(output);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010056 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd layer only supports unit strides.");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000057 if(biases != nullptr)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
60 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
61 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +010062 return INEWinogradLayerTransformWeightsKernel<float>::validate(input, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000063}
Giorgio Arenaa3221e62018-05-03 15:57:48 +010064
65Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims)
66{
67 Size2D output_tile = Size2D{};
68
69 if(kernel_dims == Size2D(3U, 3U))
70 {
71 output_tile = (input_dims.width <= 4 && input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U);
72 }
73 else if(kernel_dims == Size2D(5U, 5U))
74 {
75 output_tile = Size2D(2U, 2U);
76 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +010077 else if(kernel_dims == Size2D(1U, 3U))
78 {
79 output_tile = Size2D(1U, 6U);
80 }
81 else if(kernel_dims == Size2D(3U, 1U))
82 {
83 output_tile = Size2D(6U, 1U);
84 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +010085 return output_tile;
86}
87
88bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
89{
90 // Check if we want to configure a Winograd configuration which requires fast math
91 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
92
Pablo Tellobda6e4b2018-08-22 11:40:33 +010093 const std::vector<WinogradConfiguration> fast_math_winograd =
Giorgio Arenaa3221e62018-05-03 15:57:48 +010094 {
95 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)),
96 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
97 };
98
99 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
100 std::pair<int, int>(kernel_size.width, kernel_size.height));
101
102 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
103}
Pablo Tello7df27862018-05-30 11:44:26 +0100104
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000105} //namespace
106
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100107NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier578225e2018-07-16 18:00:11 +0100108 : _memory_group(memory_manager), _asm_glue(memory_manager), _transform_input_kernel(nullptr), _transform_output_kernel(nullptr), _transform_weights_kernel(nullptr), _activationlayer_function(),
109 _permute_input(), _permute_weights(), _permute_output(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(), _weights_hwio(), _input(), _weights(), _output(),
110 _is_prepared(false), _is_activationlayer_enabled(false)
Pablo Tello89519332017-11-17 11:52:36 +0000111{
112} /* arm_compute */
113
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100114void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
115 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +0000116{
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000117 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000118 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info));
Pablo Tello89519332017-11-17 11:52:36 +0000119
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100120 // Get indices for the width and height
121 const DataLayout data_layout = input->info()->data_layout();
122 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
123 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
124 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
125
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100126 const Size2D input_dims = Size2D(input->info()->dimension(width_idx), input->info()->dimension(height_idx));
127 const Size2D kernel_size = Size2D(weights->info()->dimension(width_idx), weights->info()->dimension(height_idx));
128 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
129
130 // Check if the Winograd configuration requires fast math
131 if(!enable_fast_math)
132 {
133 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
134 }
135
Georgios Pinitas72219332018-06-05 14:56:06 +0100136 _weights = weights;
137 _input = input;
138 _output = output;
139 _is_prepared = false;
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100140
Pablo Tellof6c572c2018-02-14 12:47:30 +0000141 std::unique_ptr<INEWinogradLayerTransformInputKernel<float>> transform_input_kernel;
142 std::unique_ptr<INEWinogradLayerTransformWeightsKernel<float>> transform_weights_kernel;
143 std::unique_ptr<INEWinogradLayerTransformOutputKernel<float>> transform_output_kernel;
144
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100145 int n_gemms = 0;
146 int N_BLOCK = 0; // Size of block used by GEMM.
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100147
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100148 const bool square_kernel = kernel_size.width == kernel_size.height;
149
150 if(square_kernel)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000151 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100152 switch(kernel_size.width)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000153 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100154 case 3:
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100155 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100156 if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4)
157 {
158 using config = NEWinogradLayerConfiguration<float, float, 4, 4, 3, 3>;
159 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
160 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
161 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
162 n_gemms = config::WinogradBase::N_GEMMS;
163 N_BLOCK = config::WinogradConv::N_BLOCK;
164 }
165 else
166 {
167 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 3, 3>;
168 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
169 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
170 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
171 n_gemms = config::WinogradBase::N_GEMMS;
172 N_BLOCK = config::WinogradConv::N_BLOCK;
173 }
174 break;
175 }
176 case 5:
177 {
178 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 5, 5>;
Anthony Barbiere1553372018-07-16 18:53:52 +0100179 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
180 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
181 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
182 n_gemms = config::WinogradBase::N_GEMMS;
183 N_BLOCK = config::WinogradConv::N_BLOCK;
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100184 break;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100185 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100186 default:
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100187 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100188 ARM_COMPUTE_ERROR("Not supported.");
189 break;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100190 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000191 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100192 }
193 else
194 {
195 if(kernel_size == Size2D(1, 3))
Pablo Tellof6c572c2018-02-14 12:47:30 +0000196 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100197 using config = NEWinogradLayerConfiguration<float, float, 6, 1, 3, 1>;
Anthony Barbiere1553372018-07-16 18:53:52 +0100198 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
199 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
200 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
201 n_gemms = config::WinogradBase::N_GEMMS;
202 N_BLOCK = config::WinogradConv::N_BLOCK;
Pablo Tellof6c572c2018-02-14 12:47:30 +0000203 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100204 else if(kernel_size == Size2D(3, 1))
205 {
206 using config = NEWinogradLayerConfiguration<float, float, 1, 6, 1, 3>;
207 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
208 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
209 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
210 n_gemms = config::WinogradBase::N_GEMMS;
211 N_BLOCK = config::WinogradConv::N_BLOCK;
212 }
213 else
Pablo Tellof6c572c2018-02-14 12:47:30 +0000214 {
215 ARM_COMPUTE_ERROR("Not supported.");
Pablo Tellof6c572c2018-02-14 12:47:30 +0000216 }
217 }
218
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100219 const PaddingType use_padding_type = (conv_info.pad_top() != 0u || conv_info.pad_left() != 0) ? PADDING_SAME : PADDING_VALID;
Pablo Tello679463a2018-02-06 11:47:59 +0000220 const bool use_same_padding = use_padding_type == PADDING_SAME;
221
Pablo Tello89519332017-11-17 11:52:36 +0000222 // Get convolved dimensions
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100223 const int in_channels = input->info()->dimension(channel_idx);
224 const int out_channels = output->info()->dimension(channel_idx);
Pablo Tello89519332017-11-17 11:52:36 +0000225
Pablo Tello89519332017-11-17 11:52:36 +0000226 const Tensor4DShape in_shape(internal_get_input_shape(input));
Anthony Barbiere1553372018-07-16 18:53:52 +0100227 const DataType data_type = input->info()->data_type();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000228 const size_t data_type_size = input->info()->element_size();
Pablo Tello89519332017-11-17 11:52:36 +0000229 // Get the memory required to instantiate a new Winograd operator.
Georgios Pinitas72219332018-06-05 14:56:06 +0100230 constexpr size_t storage_alignment = 64;
231
232 // Kernel Storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100233 const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels,
Anthony Barbiere1553372018-07-16 18:53:52 +0100234 in_channels)
235 * data_type_size
236 + storage_alignment - 1; /* FIXME: remove alignment after COMPMID-1088 */
Georgios Pinitas72219332018-06-05 14:56:06 +0100237
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000238 // Input storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100239 const size_t input_storage_size = transform_input_kernel->get_input_storage_size(in_shape.n_batches, in_shape.n_channels, in_shape.n_rows, in_shape.n_cols,
Anthony Barbiere1553372018-07-16 18:53:52 +0100240 use_same_padding)
241 * data_type_size
242 + storage_alignment - 1; /* FIXME: remove alignment after COMPMID-1088 */
Pablo Tello89519332017-11-17 11:52:36 +0000243
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000244 // Output storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100245 const size_t output_storage_size = transform_output_kernel->get_output_storage_size(in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, out_channels,
Anthony Barbiere1553372018-07-16 18:53:52 +0100246 use_same_padding)
247 * data_type_size
248 + storage_alignment - 1; /* FIXME: remove alignment after COMPMID-1088 */
Anthony Barbier578225e2018-07-16 18:00:11 +0100249 ;
250 const KernelShape kernel_shape({ out_channels, static_cast<int>(kernel_size.height), static_cast<int>(kernel_size.width), in_channels });
251 const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(kernel_shape);
252
253 const int output_matrix_stride = transform_output_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type);
254 const auto output_shape(transform_output_kernel->get_output_shape(kernel_shape, in_shape, use_padding_type));
255
256 const int input_matrix_stride = transform_input_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type);
257
258 // Configure GEMM
259 const int tile_rows = iceildiv(output_shape.n_rows, output_tile.height);
260 const int tile_cols = iceildiv(output_shape.n_cols, output_tile.width);
261 const int m = in_shape.n_batches * tile_rows * tile_cols;
262 const int k = in_shape.n_channels;
263 const int n = out_channels;
264 const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK);
265 const int output_matrix_row_stride = kernel_matrix_row_stride;
266
267 TensorShape a_shape(k, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100268 Strides a_strides(data_type_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100269 a_strides.set(1, a_strides[0] * k);
Anthony Barbiere1553372018-07-16 18:53:52 +0100270 //a_strides.set(2, data_type_size * input_matrix_stride / n_gemms); FIXME: This is the real batch size, but RSH's code crashes if it's not 0.
Anthony Barbier578225e2018-07-16 18:00:11 +0100271 a_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100272 a_strides.set(3, data_type_size * input_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100273
274 TensorShape b_shape(n, k, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100275 Strides b_strides(data_type_size);
276 b_strides.set(1, data_type_size * kernel_matrix_row_stride);
277 b_strides.set(2, data_type_size * kernel_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100278
279 TensorShape d_shape(n, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100280 Strides d_strides(data_type_size);
281 d_strides.set(1, data_type_size * output_matrix_row_stride);
282 //d_strides.set(2, data_type_size * output_matrix_stride / n_gemms); FIXME: This is the real batch size, but RSH's code crashes if it's not 0.
Anthony Barbier578225e2018-07-16 18:00:11 +0100283 d_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100284 d_strides.set(3, data_type_size * output_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100285
286 TensorInfo a_info, b_info, d_info;
Anthony Barbiere1553372018-07-16 18:53:52 +0100287 a_info.init(a_shape, 1, data_type, a_strides, 0, input_storage_size);
288 b_info.init(b_shape, 1, data_type, b_strides, 0, kernel_storage_size);
289 d_info.init(d_shape, 1, data_type, d_strides, 0, output_storage_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100290
291 _input_workspace.allocator()->init(a_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100292 _kernel_storage.allocator()->init(b_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100293 _output_workspace.allocator()->init(d_info, storage_alignment);
Pablo Tello89519332017-11-17 11:52:36 +0000294
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000295 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
296 TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0),
297 _output->info()->dimension(1), _output->info()->dimension(3)),
298 1, _output->info()->data_type());
299 _output_nhwc.allocator()->init(info);
Pablo Tello02541fb2017-12-15 09:48:59 +0000300
Pablo Tello52140b42018-01-30 14:48:11 +0000301 // Configure the InputTransform
Anthony Barbier20394d52018-08-02 11:29:09 +0100302 _memory_group.manage(&_input_workspace);
Pablo Tello7df27862018-05-30 11:44:26 +0100303 if(data_layout == DataLayout::NCHW)
304 {
305 // configure the kernel to transform the input tensor from NCHW -> NHWC
306 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
307 _input_nhwc.allocator()->allocate();
308 transform_input_kernel->configure(&_input_nhwc, in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, in_shape.n_channels, use_padding_type,
Anthony Barbiere1553372018-07-16 18:53:52 +0100309 &_input_workspace, input_matrix_stride);
Pablo Tello7df27862018-05-30 11:44:26 +0100310 }
311 else
312 {
313 transform_input_kernel->configure(_input, in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, in_shape.n_channels, use_padding_type,
Anthony Barbiere1553372018-07-16 18:53:52 +0100314 &_input_workspace, input_matrix_stride);
Pablo Tello7df27862018-05-30 11:44:26 +0100315 }
Pablo Tello52140b42018-01-30 14:48:11 +0000316
317 // Configure WeightsTransform
Pablo Tello7df27862018-05-30 11:44:26 +0100318 if(data_layout == DataLayout::NCHW)
319 {
320 // Re-order a weight tensor from [Output feature map x Input feature map x Height x Width] to [Height x Width x Input feature map x Output feature map]
321 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 2U, 0U, 1U));
322
Anthony Barbiere1553372018-07-16 18:53:52 +0100323 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello7df27862018-05-30 11:44:26 +0100324 }
325 else
326 {
327 // Re-order a weight tensor from [Output feature map x Input feature map x Height x Width] to [Height x Width x Input feature map x Output feature map]
328 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 0U, 1U, 2U));
329
Anthony Barbiere1553372018-07-16 18:53:52 +0100330 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello7df27862018-05-30 11:44:26 +0100331 }
332 _weights_hwio.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000333
334 // Configure OutputTransform
335 //The biases tensor has not been allocated at this point in time, the output transform will add the biases to the final result in the run() method
Pablo Tellod6ca4782018-01-23 09:36:04 +0000336
Anthony Barbier20394d52018-08-02 11:29:09 +0100337 _memory_group.manage(&_output_workspace);
Pablo Tello7df27862018-05-30 11:44:26 +0100338 if(data_layout == DataLayout::NCHW)
339 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100340 transform_output_kernel->configure(biases, &_output_workspace,
Pablo Tello7df27862018-05-30 11:44:26 +0100341 output_matrix_stride, &_output_nhwc,
342 in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels);
343 }
344 else
345 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100346 transform_output_kernel->configure(biases, &_output_workspace,
Pablo Tello7df27862018-05-30 11:44:26 +0100347 output_matrix_stride, _output,
348 in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels);
349 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000350
Anthony Barbier578225e2018-07-16 18:00:11 +0100351 _asm_glue.configure(&_input_workspace, &_kernel_storage, &_output_workspace, 1.0f, 0.f, false);
Anthony Barbier20394d52018-08-02 11:29:09 +0100352 _input_workspace.allocator()->allocate();
353 _kernel_storage.allocator()->allocate();
354 _output_workspace.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000355
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000356 // Reorder the convoluted output to ACL's ordering NCHW
357 _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U));
Pablo Tellof6c572c2018-02-14 12:47:30 +0000358
Anthony Barbier20394d52018-08-02 11:29:09 +0100359 _output_nhwc.allocator()->allocate();
360
Pablo Tellof6c572c2018-02-14 12:47:30 +0000361 _transform_input_kernel = std::move(transform_input_kernel);
362 _transform_weights_kernel = std::move(transform_weights_kernel);
363 _transform_output_kernel = std::move(transform_output_kernel);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000364
365 //Configure Activation Layer
366 _is_activationlayer_enabled = act_info.enabled();
Pablo Tello7282d562018-06-14 15:35:49 +0100367 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000368 {
Pablo Tello7df27862018-05-30 11:44:26 +0100369 _activationlayer_function.configure(_output, nullptr, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000370 }
Pablo Tello89519332017-11-17 11:52:36 +0000371}
372
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100373void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +0000374{
Pablo Tello7df27862018-05-30 11:44:26 +0100375 const DataLayout data_layout = _input->info()->data_layout();
376
Georgios Pinitas72219332018-06-05 14:56:06 +0100377 prepare();
378
Pablo Tello89519332017-11-17 11:52:36 +0000379 _memory_group.acquire();
Pablo Tello679463a2018-02-06 11:47:59 +0000380
Pablo Tello7df27862018-05-30 11:44:26 +0100381 if(data_layout == DataLayout::NCHW)
382 {
383 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
384 _permute_input.run();
385 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100386
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000387 // Transform input tensor to the winograd domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000388 NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000389
Pablo Tello89519332017-11-17 11:52:36 +0000390 //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Anthony Barbier578225e2018-07-16 18:00:11 +0100391 _asm_glue.run();
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000392 // Transform output tensor to the spatial domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000393 NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000394
Pablo Tello7df27862018-05-30 11:44:26 +0100395 if(data_layout == DataLayout::NCHW)
396 {
397 // Reorder the convoluted output to ACL's ordering NCHW
398 _permute_output.run();
399 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000400
401 if(_is_activationlayer_enabled)
402 {
403 _activationlayer_function.run();
404 }
Pablo Tello7282d562018-06-14 15:35:49 +0100405
Pablo Tello89519332017-11-17 11:52:36 +0000406 _memory_group.release();
Pablo Tello89519332017-11-17 11:52:36 +0000407}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000408
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100409Status NEWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100410 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000411{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100412 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100413 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000414
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100415 // Get indices for the width and height
416 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
417 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
418
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100419 // Input shape, kernel size and output tile
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100420 const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height));
421 const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height));
422 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
423 const bool square_kernel = kernel_size.width == kernel_size.height;
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100424
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100425 // Check if the Winograd configuration requires fast math
426 if(!enable_fast_math)
427 {
428 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
429 }
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100430
431 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100432 kernel_size,
433 input_dims,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100434 conv_info,
435 input->data_layout());
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100436
437 // Validate input transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100438 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100439 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Pablo Tello7282d562018-06-14 15:35:49 +0100440
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100441 if(square_kernel)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100442 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100443 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100444 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100445 case 3:
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100446 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100447 if(input_dims.width > 4 && input_dims.height > 4)
448 {
449 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, &input0, winograd_info)));
450 }
451 else
452 {
453 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, &input0, winograd_info)));
454 }
455 break;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100456 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100457 case 5:
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100458 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100459 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, &input0, winograd_info)));
460 break;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100461 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100462 default:
463 {
464 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
465 break;
466 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100467 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100468 // Validate filter transform
469 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
470 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100471
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100472 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100473 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100474 case 3:
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100475 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100476 if(input_dims.width > 4 && input_dims.height > 4)
477 {
478 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, &input1, winograd_info)));
479 }
480 else
481 {
482 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, &input1, winograd_info)));
483 }
484 break;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100485 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100486 case 5:
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100487 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100488 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, &input1, winograd_info)));
489 break;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100490 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100491 default:
492 {
493 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
494 break;
495 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100496 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100497 // Validate batched matrix multiply
498 TensorShape batched_mm_output_shape = input0.tensor_shape();
499 batched_mm_output_shape[0] = input1.tensor_shape()[0];
500 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
501 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100502 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100503 case 3:
504 {
505 if(input_dims.width > 4 && input_dims.height > 4)
506 {
507 // Validate output transform
508 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
509 }
510 else
511 {
512 // Validate output transform
513 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
514 }
515 break;
516 }
517 case 5:
518 {
519 // Validate output transform
520 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>::validate(&batched_mm_output, biases, output, winograd_info)));
521 break;
522 }
523 default:
524 {
525 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
526 break;
527 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100528 }
529 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100530 else
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100531 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100532 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
533 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
534 TensorShape batched_mm_output_shape = input0.tensor_shape();
535 batched_mm_output_shape[0] = input1.tensor_shape()[0];
536 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
537
538 if(kernel_size == Size2D(3, 1))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100539 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100540 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 6, 1, 3>::validate(input, &input0, winograd_info)));
541 // Validate filter transform
542 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 6, 1, 3>::validate(weights, &input1, winograd_info)));
543 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 6, 1, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100544 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100545 else if(kernel_size == Size2D(1, 3))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100546 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100547 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 6, 1, 3, 1>::validate(input, &input0, winograd_info)));
548 // Validate filter transform
549 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 6, 1, 3, 1>::validate(weights, &input1, winograd_info)));
550 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 6, 1, 3, 1>::validate(&batched_mm_output, biases, output, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100551 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100552 else
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100553 {
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100554 ARM_COMPUTE_RETURN_ERROR_MSG("Kernel shape not supported");
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100555 }
556 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100557 // Validate Activation Layer
558 if(act_info.enabled())
559 {
560 NEActivationLayer::validate(output, nullptr, act_info);
561 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000562 return Status{};
563}
564
Georgios Pinitas72219332018-06-05 14:56:06 +0100565void NEWinogradConvolutionLayer::prepare()
566{
567 if(!_is_prepared)
568 {
569 // Permute weights
570 _permute_weights.run();
571 _weights->mark_as_unused();
572
573 // Transform weights
574 NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX);
Georgios Pinitas72219332018-06-05 14:56:06 +0100575
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100576 _weights_hwio.allocator()->free();
Georgios Pinitas72219332018-06-05 14:56:06 +0100577 _is_prepared = true;
578 }
579}
580
Pablo Tello89519332017-11-17 11:52:36 +0000581} // namespace arm_compute