blob: 11bb2d881b2ccc5619a62147c474f8598449ae2b [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 const DataLayout data_layout = input->data_layout();
56 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
57 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
58
59 ARM_COMPUTE_UNUSED(output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000060 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Andrew Mundy4d9379a2018-03-15 16:47:03 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010062 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 3 && weights->dimension(height_idx) != 5, "Only 3 and 5 kernels are supported");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000063 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
64
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd layer only supports unit strides.");
66
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000067 if(biases != nullptr)
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
70 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
71 }
72
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000073 return Status{};
74}
Giorgio Arenaa3221e62018-05-03 15:57:48 +010075
76Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims)
77{
78 Size2D output_tile = Size2D{};
79
80 if(kernel_dims == Size2D(3U, 3U))
81 {
82 output_tile = (input_dims.width <= 4 && input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U);
83 }
84 else if(kernel_dims == Size2D(5U, 5U))
85 {
86 output_tile = Size2D(2U, 2U);
87 }
88
89 return output_tile;
90}
91
92bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
93{
94 // Check if we want to configure a Winograd configuration which requires fast math
95 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
96
97 std::vector<WinogradConfiguration> fast_math_winograd =
98 {
99 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)),
100 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
101 };
102
103 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
104 std::pair<int, int>(kernel_size.width, kernel_size.height));
105
106 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
107}
Pablo Tello7df27862018-05-30 11:44:26 +0100108
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000109} //namespace
110
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100111NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier578225e2018-07-16 18:00:11 +0100112 : _memory_group(memory_manager), _asm_glue(memory_manager), _transform_input_kernel(nullptr), _transform_output_kernel(nullptr), _transform_weights_kernel(nullptr), _activationlayer_function(),
113 _permute_input(), _permute_weights(), _permute_output(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(), _weights_hwio(), _input(), _weights(), _output(),
114 _is_prepared(false), _is_activationlayer_enabled(false)
Pablo Tello89519332017-11-17 11:52:36 +0000115{
116} /* arm_compute */
117
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100118void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
119 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +0000120{
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000121 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000122 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 +0000123
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100124 // Get indices for the width and height
125 const DataLayout data_layout = input->info()->data_layout();
126 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
127 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
128 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
129
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100130 const Size2D input_dims = Size2D(input->info()->dimension(width_idx), input->info()->dimension(height_idx));
131 const Size2D kernel_size = Size2D(weights->info()->dimension(width_idx), weights->info()->dimension(height_idx));
132 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
133
134 // Check if the Winograd configuration requires fast math
135 if(!enable_fast_math)
136 {
137 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
138 }
139
Georgios Pinitas72219332018-06-05 14:56:06 +0100140 _weights = weights;
141 _input = input;
142 _output = output;
143 _is_prepared = false;
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100144
Pablo Tellof6c572c2018-02-14 12:47:30 +0000145 std::unique_ptr<INEWinogradLayerTransformInputKernel<float>> transform_input_kernel;
146 std::unique_ptr<INEWinogradLayerTransformWeightsKernel<float>> transform_weights_kernel;
147 std::unique_ptr<INEWinogradLayerTransformOutputKernel<float>> transform_output_kernel;
148
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100149 int n_gemms = 0;
150 int N_BLOCK = 0; // Size of block used by GEMM.
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100151
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100152 switch(kernel_size.width)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000153 {
154 case 3:
155 {
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100156 if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4)
157 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100158 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;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100164 }
165 else
166 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100167 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;
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100173 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000174 break;
175 }
176 case 5:
177 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100178 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 5, 5>;
179 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 Tellof6c572c2018-02-14 12:47:30 +0000184 break;
185 }
186 default:
187 {
188 ARM_COMPUTE_ERROR("Not supported.");
189 break;
190 }
191 }
192
Pablo Tello679463a2018-02-06 11:47:59 +0000193 const PaddingType use_padding_type = (conv_info.pad_left() != 0u) ? PADDING_SAME : PADDING_VALID;
194 const bool use_same_padding = use_padding_type == PADDING_SAME;
195
Pablo Tello89519332017-11-17 11:52:36 +0000196 // Get convolved dimensions
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100197 const int in_channels = input->info()->dimension(channel_idx);
198 const int out_channels = output->info()->dimension(channel_idx);
Pablo Tello89519332017-11-17 11:52:36 +0000199
Pablo Tello89519332017-11-17 11:52:36 +0000200 const Tensor4DShape in_shape(internal_get_input_shape(input));
Anthony Barbiere1553372018-07-16 18:53:52 +0100201 const DataType data_type = input->info()->data_type();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000202 const size_t data_type_size = input->info()->element_size();
Pablo Tello89519332017-11-17 11:52:36 +0000203 // Get the memory required to instantiate a new Winograd operator.
Georgios Pinitas72219332018-06-05 14:56:06 +0100204 constexpr size_t storage_alignment = 64;
205
206 // Kernel Storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100207 const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels,
Anthony Barbiere1553372018-07-16 18:53:52 +0100208 in_channels)
209 * data_type_size
210 + storage_alignment - 1; /* FIXME: remove alignment after COMPMID-1088 */
Georgios Pinitas72219332018-06-05 14:56:06 +0100211
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000212 // Input storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100213 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 +0100214 use_same_padding)
215 * data_type_size
216 + storage_alignment - 1; /* FIXME: remove alignment after COMPMID-1088 */
Pablo Tello89519332017-11-17 11:52:36 +0000217
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000218 // Output storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100219 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 +0100220 use_same_padding)
221 * data_type_size
222 + storage_alignment - 1; /* FIXME: remove alignment after COMPMID-1088 */
Anthony Barbier578225e2018-07-16 18:00:11 +0100223 ;
224 const KernelShape kernel_shape({ out_channels, static_cast<int>(kernel_size.height), static_cast<int>(kernel_size.width), in_channels });
225 const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(kernel_shape);
226
227 const int output_matrix_stride = transform_output_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type);
228 const auto output_shape(transform_output_kernel->get_output_shape(kernel_shape, in_shape, use_padding_type));
229
230 const int input_matrix_stride = transform_input_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type);
231
232 // Configure GEMM
233 const int tile_rows = iceildiv(output_shape.n_rows, output_tile.height);
234 const int tile_cols = iceildiv(output_shape.n_cols, output_tile.width);
235 const int m = in_shape.n_batches * tile_rows * tile_cols;
236 const int k = in_shape.n_channels;
237 const int n = out_channels;
238 const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK);
239 const int output_matrix_row_stride = kernel_matrix_row_stride;
240
241 TensorShape a_shape(k, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100242 Strides a_strides(data_type_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100243 a_strides.set(1, a_strides[0] * k);
Anthony Barbiere1553372018-07-16 18:53:52 +0100244 //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 +0100245 a_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100246 a_strides.set(3, data_type_size * input_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100247
248 TensorShape b_shape(n, k, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100249 Strides b_strides(data_type_size);
250 b_strides.set(1, data_type_size * kernel_matrix_row_stride);
251 b_strides.set(2, data_type_size * kernel_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100252
253 TensorShape d_shape(n, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100254 Strides d_strides(data_type_size);
255 d_strides.set(1, data_type_size * output_matrix_row_stride);
256 //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 +0100257 d_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100258 d_strides.set(3, data_type_size * output_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100259
260 TensorInfo a_info, b_info, d_info;
Anthony Barbiere1553372018-07-16 18:53:52 +0100261 a_info.init(a_shape, 1, data_type, a_strides, 0, input_storage_size);
262 b_info.init(b_shape, 1, data_type, b_strides, 0, kernel_storage_size);
263 d_info.init(d_shape, 1, data_type, d_strides, 0, output_storage_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100264
265 _input_workspace.allocator()->init(a_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100266 _kernel_storage.allocator()->init(b_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100267 _output_workspace.allocator()->init(d_info, storage_alignment);
Pablo Tello89519332017-11-17 11:52:36 +0000268
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000269 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
270 TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0),
271 _output->info()->dimension(1), _output->info()->dimension(3)),
272 1, _output->info()->data_type());
273 _output_nhwc.allocator()->init(info);
Pablo Tello02541fb2017-12-15 09:48:59 +0000274
Pablo Tello52140b42018-01-30 14:48:11 +0000275 // Configure the InputTransform
Anthony Barbier20394d52018-08-02 11:29:09 +0100276 _memory_group.manage(&_input_workspace);
Pablo Tello7df27862018-05-30 11:44:26 +0100277 if(data_layout == DataLayout::NCHW)
278 {
279 // configure the kernel to transform the input tensor from NCHW -> NHWC
280 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
281 _input_nhwc.allocator()->allocate();
282 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 +0100283 &_input_workspace, input_matrix_stride);
Pablo Tello7df27862018-05-30 11:44:26 +0100284 }
285 else
286 {
287 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 +0100288 &_input_workspace, input_matrix_stride);
Pablo Tello7df27862018-05-30 11:44:26 +0100289 }
Pablo Tello52140b42018-01-30 14:48:11 +0000290
291 // Configure WeightsTransform
Pablo Tello7df27862018-05-30 11:44:26 +0100292 if(data_layout == DataLayout::NCHW)
293 {
294 // 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]
295 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 2U, 0U, 1U));
296
Anthony Barbiere1553372018-07-16 18:53:52 +0100297 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello7df27862018-05-30 11:44:26 +0100298 }
299 else
300 {
301 // 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]
302 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 0U, 1U, 2U));
303
Anthony Barbiere1553372018-07-16 18:53:52 +0100304 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello7df27862018-05-30 11:44:26 +0100305 }
306 _weights_hwio.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000307
308 // Configure OutputTransform
309 //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 +0000310
Anthony Barbier20394d52018-08-02 11:29:09 +0100311 _memory_group.manage(&_output_workspace);
Pablo Tello7df27862018-05-30 11:44:26 +0100312 if(data_layout == DataLayout::NCHW)
313 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100314 transform_output_kernel->configure(biases, &_output_workspace,
Pablo Tello7df27862018-05-30 11:44:26 +0100315 output_matrix_stride, &_output_nhwc,
316 in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels);
317 }
318 else
319 {
Anthony Barbiere1553372018-07-16 18:53:52 +0100320 transform_output_kernel->configure(biases, &_output_workspace,
Pablo Tello7df27862018-05-30 11:44:26 +0100321 output_matrix_stride, _output,
322 in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels);
323 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000324
Anthony Barbier578225e2018-07-16 18:00:11 +0100325 _asm_glue.configure(&_input_workspace, &_kernel_storage, &_output_workspace, 1.0f, 0.f, false);
Anthony Barbier20394d52018-08-02 11:29:09 +0100326 _input_workspace.allocator()->allocate();
327 _kernel_storage.allocator()->allocate();
328 _output_workspace.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000329
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000330 // Reorder the convoluted output to ACL's ordering NCHW
331 _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U));
Pablo Tellof6c572c2018-02-14 12:47:30 +0000332
Anthony Barbier20394d52018-08-02 11:29:09 +0100333 _output_nhwc.allocator()->allocate();
334
Pablo Tellof6c572c2018-02-14 12:47:30 +0000335 _transform_input_kernel = std::move(transform_input_kernel);
336 _transform_weights_kernel = std::move(transform_weights_kernel);
337 _transform_output_kernel = std::move(transform_output_kernel);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000338
339 //Configure Activation Layer
340 _is_activationlayer_enabled = act_info.enabled();
Pablo Tello7282d562018-06-14 15:35:49 +0100341 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000342 {
Pablo Tello7df27862018-05-30 11:44:26 +0100343 _activationlayer_function.configure(_output, nullptr, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000344 }
Pablo Tello89519332017-11-17 11:52:36 +0000345}
346
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100347void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +0000348{
Pablo Tello7df27862018-05-30 11:44:26 +0100349 const DataLayout data_layout = _input->info()->data_layout();
350
Georgios Pinitas72219332018-06-05 14:56:06 +0100351 prepare();
352
Pablo Tello89519332017-11-17 11:52:36 +0000353 _memory_group.acquire();
Pablo Tello679463a2018-02-06 11:47:59 +0000354
Pablo Tello7df27862018-05-30 11:44:26 +0100355 if(data_layout == DataLayout::NCHW)
356 {
357 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
358 _permute_input.run();
359 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000360 // Transform input tensor to the winograd domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000361 NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000362
Pablo Tello89519332017-11-17 11:52:36 +0000363 //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Anthony Barbier578225e2018-07-16 18:00:11 +0100364 _asm_glue.run();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000365
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000366 // Transform output tensor to the spatial domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000367 NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000368
Pablo Tello7df27862018-05-30 11:44:26 +0100369 if(data_layout == DataLayout::NCHW)
370 {
371 // Reorder the convoluted output to ACL's ordering NCHW
372 _permute_output.run();
373 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000374
375 if(_is_activationlayer_enabled)
376 {
377 _activationlayer_function.run();
378 }
Pablo Tello7282d562018-06-14 15:35:49 +0100379
Pablo Tello89519332017-11-17 11:52:36 +0000380 _memory_group.release();
Pablo Tello89519332017-11-17 11:52:36 +0000381}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000382
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100383Status 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 +0100384 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000385{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100386 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100387 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000388
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100389 // Get indices for the width and height
390 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
391 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
392
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100393 // Input shape, kernel size and output tile
394 const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height));
395 const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height));
396 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100397
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100398 // Check if the Winograd configuration requires fast math
399 if(!enable_fast_math)
400 {
401 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
402 }
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100403
404 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100405 kernel_size,
406 input_dims,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100407 conv_info,
408 input->data_layout());
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100409
410 // Validate input transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100411 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100412 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Pablo Tello7282d562018-06-14 15:35:49 +0100413
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100414 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100415 {
416 case 3:
417 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100418 if(input_dims.width > 4 && input_dims.height > 4)
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100419 {
420 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, &input0, winograd_info)));
421 }
422 else
423 {
424 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, &input0, winograd_info)));
425 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100426 break;
427 }
428 case 5:
429 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100430 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, &input0, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100431 break;
432 }
433 default:
434 {
435 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
436 break;
437 }
438 }
439 // Validate filter transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100440 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100441 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
442
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100443 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100444 {
445 case 3:
446 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100447 if(input_dims.width > 4 && input_dims.height > 4)
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100448 {
449 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, &input1, winograd_info)));
450 }
451 else
452 {
453 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, &input1, winograd_info)));
454 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100455 break;
456 }
457 case 5:
458 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100459 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, &input1, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100460 break;
461 }
462 default:
463 {
464 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
465 break;
466 }
467 }
468 // Validate batched matrix multiply
469 TensorShape batched_mm_output_shape = input0.tensor_shape();
470 batched_mm_output_shape[0] = input1.tensor_shape()[0];
471 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100472 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100473 {
474 case 3:
475 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100476 if(input_dims.width > 4 && input_dims.height > 4)
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100477 {
478 // Validate output transform
479 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
480 }
481 else
482 {
483 // Validate output transform
484 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
485 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100486 break;
487 }
488 case 5:
489 {
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100490 // Validate output transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100491 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>::validate(&batched_mm_output, biases, output, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100492 break;
493 }
494 default:
495 {
496 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
497 break;
498 }
499 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100500 // Validate Activation Layer
501 if(act_info.enabled())
502 {
503 NEActivationLayer::validate(output, nullptr, act_info);
504 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000505 return Status{};
506}
507
Georgios Pinitas72219332018-06-05 14:56:06 +0100508void NEWinogradConvolutionLayer::prepare()
509{
510 if(!_is_prepared)
511 {
512 // Permute weights
513 _permute_weights.run();
514 _weights->mark_as_unused();
515
516 // Transform weights
517 NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX);
518 _weights_hwio.allocator()->free();
519
520 _is_prepared = true;
521 }
522}
523
Pablo Tello89519332017-11-17 11:52:36 +0000524} // namespace arm_compute