blob: d6bc5cfd9ab136e72724b73e450a1e075aca601b [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"
Pablo Tello89519332017-11-17 11:52:36 +000027#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +010029#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou2b3129e2018-04-25 18:10:13 +010031#include "arm_compute/runtime/NEON/AssemblyHelper.h"
Pablo Tello89519332017-11-17 11:52:36 +000032#include "arm_compute/runtime/NEON/NEScheduler.h"
33#include "support/ToolchainSupport.h"
34
Georgios Pinitas9fb11592018-04-26 20:34:58 +010035#include "arm_compute/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h"
Pablo Tellof6c572c2018-02-14 12:47:30 +000036
Georgios Pinitas4074c992018-01-30 18:13:46 +000037#include "arm_compute/core/NEON/kernels/convolution/winograd/winograd_gemm.hpp"
Pablo Tellod6ca4782018-01-23 09:36:04 +000038
Pablo Tello89519332017-11-17 11:52:36 +000039namespace arm_compute
40{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000041namespace
42{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010043inline Tensor4DShape internal_get_input_shape(const arm_compute::ITensor *input)
44{
45 const DataLayout data_layout = input->info()->data_layout();
46 const int in_width = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH));
47 const int in_height = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT));
48 const int in_channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
49 const int in_batches = input->info()->dimension(3);
50
51 return Tensor4DShape({ in_batches, in_height, in_width, in_channels });
52}
53
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000054Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
55{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010056 const DataLayout data_layout = input->data_layout();
57 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
58 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
59
60 ARM_COMPUTE_UNUSED(output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000061 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Andrew Mundy4d9379a2018-03-15 16:47:03 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 3 && weights->dimension(height_idx) != 5, "Only 3 and 5 kernels are supported");
Pablo Tello7df27862018-05-30 11:44:26 +010064 ARM_COMPUTE_RETURN_ERROR_ON(data_layout != DataLayout::NCHW); // COMPMID-1287
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000065 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
66
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +010067 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.stride().first != 1 || conv_info.stride().second != 1, "Winograd layer only supports unit strides.");
68
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000069 if(biases != nullptr)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
72 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
73 }
74
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000075 return Status{};
76}
Giorgio Arenaa3221e62018-05-03 15:57:48 +010077
78Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims)
79{
80 Size2D output_tile = Size2D{};
81
82 if(kernel_dims == Size2D(3U, 3U))
83 {
84 output_tile = (input_dims.width <= 4 && input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U);
85 }
86 else if(kernel_dims == Size2D(5U, 5U))
87 {
88 output_tile = Size2D(2U, 2U);
89 }
90
91 return output_tile;
92}
93
94bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
95{
96 // Check if we want to configure a Winograd configuration which requires fast math
97 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
98
99 std::vector<WinogradConfiguration> fast_math_winograd =
100 {
101 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)),
102 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
103 };
104
105 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
106 std::pair<int, int>(kernel_size.width, kernel_size.height));
107
108 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
109}
Pablo Tello7df27862018-05-30 11:44:26 +0100110
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000111} //namespace
112
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100113NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100114 : _memory_group(std::move(memory_manager)), _arm_gemm(nullptr), _gemm_kernel(nullptr), _transform_input_kernel(nullptr), _transform_output_kernel(nullptr), _transform_weights_kernel(nullptr),
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000115 _activationlayer_function(), _permute_input(), _permute_weights(), _permute_output(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(), _weights_hwio(),
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100116 _workspace(), _input(), _weights(), _output(), _reshaped_kernel(false), _is_activationlayer_enabled(false)
Pablo Tello89519332017-11-17 11:52:36 +0000117{
118} /* arm_compute */
119
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100120void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
121 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +0000122{
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000123 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000124 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 +0000125
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100126 // Get indices for the width and height
127 const DataLayout data_layout = input->info()->data_layout();
128 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
129 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
130 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
131
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100132 const Size2D input_dims = Size2D(input->info()->dimension(width_idx), input->info()->dimension(height_idx));
133 const Size2D kernel_size = Size2D(weights->info()->dimension(width_idx), weights->info()->dimension(height_idx));
134 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
135
136 // Check if the Winograd configuration requires fast math
137 if(!enable_fast_math)
138 {
139 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
140 }
141
142 _weights = weights;
143 _input = input;
144 _output = output;
145
Pablo Tellof6c572c2018-02-14 12:47:30 +0000146 std::unique_ptr<INEWinogradLayerTransformInputKernel<float>> transform_input_kernel;
147 std::unique_ptr<INEWinogradLayerTransformWeightsKernel<float>> transform_weights_kernel;
148 std::unique_ptr<INEWinogradLayerTransformOutputKernel<float>> transform_output_kernel;
149
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100150 int n_gemms = 0;
151 int N_BLOCK = 0; // Size of block used by GEMM.
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100152
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100153 switch(kernel_size.width)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000154 {
155 case 3:
156 {
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100157 if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4)
158 {
159 transform_input_kernel = support::cpp14::make_unique<NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>>();
160 transform_weights_kernel = support::cpp14::make_unique<NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>>();
161 transform_output_kernel = support::cpp14::make_unique<NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>>();
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100162 n_gemms = NEWinogradLayerBatchedGEMMKernel<float, float, 4, 4, 3, 3>::WinogradBase::N_GEMMS;
163 N_BLOCK = NEWinogradLayerBatchedGEMMKernel<float, float, 4, 4, 3, 3>::WinogradConv::N_BLOCK;
164 }
165 else
166 {
167 transform_input_kernel = support::cpp14::make_unique<NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>>();
168 transform_weights_kernel = support::cpp14::make_unique<NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>>();
169 transform_output_kernel = support::cpp14::make_unique<NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>>();
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100170 n_gemms = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 3, 3>::WinogradBase::N_GEMMS;
171 N_BLOCK = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 3, 3>::WinogradConv::N_BLOCK;
172 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000173 break;
174 }
175 case 5:
176 {
Pablo Tellof6c572c2018-02-14 12:47:30 +0000177 transform_input_kernel = support::cpp14::make_unique<NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>>();
178 transform_weights_kernel = support::cpp14::make_unique<NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>>();
179 transform_output_kernel = support::cpp14::make_unique<NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>>();
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100180 n_gemms = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 5, 5>::WinogradBase::N_GEMMS;
181 N_BLOCK = NEWinogradLayerBatchedGEMMKernel<float, float, 2, 2, 5, 5>::WinogradConv::N_BLOCK;
Pablo Tellof6c572c2018-02-14 12:47:30 +0000182 break;
183 }
184 default:
185 {
186 ARM_COMPUTE_ERROR("Not supported.");
187 break;
188 }
189 }
190
Pablo Tello679463a2018-02-06 11:47:59 +0000191 const PaddingType use_padding_type = (conv_info.pad_left() != 0u) ? PADDING_SAME : PADDING_VALID;
192 const bool use_same_padding = use_padding_type == PADDING_SAME;
193
Pablo Tello89519332017-11-17 11:52:36 +0000194 // Get convolved dimensions
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100195 const int in_channels = input->info()->dimension(channel_idx);
196 const int out_channels = output->info()->dimension(channel_idx);
Pablo Tello89519332017-11-17 11:52:36 +0000197
Pablo Tello89519332017-11-17 11:52:36 +0000198 const Tensor4DShape in_shape(internal_get_input_shape(input));
Pablo Tellod6ca4782018-01-23 09:36:04 +0000199 const size_t data_type_size = input->info()->element_size();
Pablo Tello89519332017-11-17 11:52:36 +0000200 // Get the memory required to instantiate a new Winograd operator.
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000201 constexpr size_t storage_alignment = 64;
Pablo Tellof6c572c2018-02-14 12:47:30 +0000202 const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels, in_channels) * data_type_size;
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000203 _kernel_storage.allocator()->init(TensorInfo(TensorShape{ (kernel_storage_size + storage_alignment - 1) }, 1, DataType::U8));
Pablo Tello89519332017-11-17 11:52:36 +0000204 _kernel_storage.allocator()->allocate();
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000205 // Input storage
Pablo Tellof6c572c2018-02-14 12:47:30 +0000206 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, use_same_padding) * data_type_size;
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000207 _input_workspace.allocator()->init(TensorInfo(TensorShape{ (input_storage_size + storage_alignment - 1) }, 1, DataType::U8));
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000208 _input_workspace.allocator()->allocate();
Pablo Tello89519332017-11-17 11:52:36 +0000209
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000210 // Output storage
Pablo Tellof6c572c2018-02-14 12:47:30 +0000211 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, use_same_padding) * data_type_size;
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000212 _output_workspace.allocator()->init(TensorInfo(TensorShape{ (output_storage_size + storage_alignment - 1) }, 1, DataType::U8));
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000213 _output_workspace.allocator()->allocate();
Pablo Tello89519332017-11-17 11:52:36 +0000214
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000215 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
216 TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0),
217 _output->info()->dimension(1), _output->info()->dimension(3)),
218 1, _output->info()->data_type());
219 _output_nhwc.allocator()->init(info);
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000220 _output_nhwc.allocator()->allocate();
Pablo Tello02541fb2017-12-15 09:48:59 +0000221
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100222 const KernelShape kernel_shape({ out_channels, static_cast<int>(kernel_size.height), static_cast<int>(kernel_size.width), in_channels });
Pablo Tello52140b42018-01-30 14:48:11 +0000223
224 // Configure the InputTransform
Pablo Tellof6c572c2018-02-14 12:47:30 +0000225 const int input_matrix_stride = transform_input_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type);
Pablo Tello7df27862018-05-30 11:44:26 +0100226
227 if(data_layout == DataLayout::NCHW)
228 {
229 // configure the kernel to transform the input tensor from NCHW -> NHWC
230 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
231 _input_nhwc.allocator()->allocate();
232 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,
233 reinterpret_cast<float *>(_input_workspace.buffer()), input_matrix_stride);
234 }
235 else
236 {
237 transform_input_kernel->configure(_input, in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, in_shape.n_channels, use_padding_type,
238 reinterpret_cast<float *>(_input_workspace.buffer()), input_matrix_stride);
239 }
Pablo Tello52140b42018-01-30 14:48:11 +0000240
241 // Configure WeightsTransform
Pablo Tellof6c572c2018-02-14 12:47:30 +0000242 const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(kernel_shape);
Pablo Tello7df27862018-05-30 11:44:26 +0100243 if(data_layout == DataLayout::NCHW)
244 {
245 // 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]
246 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 2U, 0U, 1U));
247
248 transform_weights_kernel->configure(&_weights_hwio, reinterpret_cast<float *>(_kernel_storage.buffer()), kernel_matrix_stride, out_channels, in_channels);
249 }
250 else
251 {
252 // 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]
253 _permute_weights.configure(weights, &_weights_hwio, PermutationVector(3U, 0U, 1U, 2U));
254
255 transform_weights_kernel->configure(&_weights_hwio, reinterpret_cast<float *>(_kernel_storage.buffer()), kernel_matrix_stride, out_channels, in_channels);
256 }
257 _weights_hwio.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000258
259 // Configure OutputTransform
260 //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 Tellof6c572c2018-02-14 12:47:30 +0000261 const int output_matrix_stride = transform_output_kernel->get_matrix_stride(kernel_shape, in_shape, use_padding_type);
262 const auto output_shape(transform_output_kernel->get_output_shape(kernel_shape, in_shape, use_padding_type));
Pablo Tellod6ca4782018-01-23 09:36:04 +0000263
Pablo Tello7df27862018-05-30 11:44:26 +0100264 if(data_layout == DataLayout::NCHW)
265 {
266 transform_output_kernel->configure(biases, reinterpret_cast<float *>(_output_workspace.buffer()),
267 output_matrix_stride, &_output_nhwc,
268 in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels);
269 }
270 else
271 {
272 transform_output_kernel->configure(biases, reinterpret_cast<float *>(_output_workspace.buffer()),
273 output_matrix_stride, _output,
274 in_shape.n_batches, output_shape.n_rows, output_shape.n_cols, out_channels);
275 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000276
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100277 // Configure GEMM
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100278 const int tile_rows = iceildiv(output_shape.n_rows, output_tile.height);
279 const int tile_cols = iceildiv(output_shape.n_cols, output_tile.width);
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100280 const int m = in_shape.n_batches * tile_rows * tile_cols;
281 const int k = in_shape.n_channels;
282 const int n = out_channels;
283 const int input_matrix_row_stride = in_shape.n_channels;
284 const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK);
285 const int output_matrix_row_stride = kernel_matrix_row_stride;
286 unsigned int num_threads = NEScheduler::get().num_threads();
Pablo Tello52140b42018-01-30 14:48:11 +0000287
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100288 _arm_gemm = arm_gemm::gemm<float, float>(NEScheduler::get().cpu_info(), m, n, k, 1, n_gemms, false, false, 1.f, 0.f, num_threads, false);
289 _arm_gemm->set_arrays(reinterpret_cast<float *>(_input_workspace.buffer()), input_matrix_row_stride, 0, input_matrix_stride, reinterpret_cast<float *>(_kernel_storage.buffer()),
290 kernel_matrix_row_stride, kernel_matrix_stride, reinterpret_cast<float *>(_output_workspace.buffer()), output_matrix_row_stride, 0, output_matrix_stride);
291
292 auto acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapper<arm_gemm::GemmCommon<float, float>>>();
293 acl_gemm_wrapper->configure(_arm_gemm.get());
294 const size_t workspace_size = _arm_gemm->get_working_size();
295
296 // Allocate workspace
297 if(workspace_size > 0)
298 {
299 const unsigned int alignment = 4096;
Georgios Pinitasb95e2102018-05-30 10:17:38 +0100300 // TODO (COMPMID-1248) : Add support for memory manager in NEWinogradConvolutionLayer
301 // Warning : Do not set a memory group in allocate_workspace, should be done under COMPMID-1248
302 allocate_workspace(workspace_size, _workspace, nullptr, alignment, 1);
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100303 _arm_gemm->set_working_space(reinterpret_cast<float *>(_workspace.buffer()));
304 }
305
306 const unsigned int window_size = _arm_gemm->get_window_size();
307 if(window_size < num_threads)
308 {
309 num_threads = window_size;
310 _arm_gemm->set_nthreads(num_threads);
311 }
312
313 _gemm_kernel = std::move(acl_gemm_wrapper);
Pablo Tello52140b42018-01-30 14:48:11 +0000314
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000315 // Reorder the convoluted output to ACL's ordering NCHW
316 _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U));
Pablo Tellof6c572c2018-02-14 12:47:30 +0000317
318 _transform_input_kernel = std::move(transform_input_kernel);
319 _transform_weights_kernel = std::move(transform_weights_kernel);
320 _transform_output_kernel = std::move(transform_output_kernel);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000321
322 //Configure Activation Layer
323 _is_activationlayer_enabled = act_info.enabled();
Pablo Tello7df27862018-05-30 11:44:26 +0100324 if(data_layout == DataLayout::NCHW && _is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000325 {
Pablo Tello7df27862018-05-30 11:44:26 +0100326 _activationlayer_function.configure(_output, nullptr, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000327 }
Pablo Tello89519332017-11-17 11:52:36 +0000328}
329
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100330void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +0000331{
Pablo Tello7df27862018-05-30 11:44:26 +0100332 const DataLayout data_layout = _input->info()->data_layout();
333
Pablo Tello89519332017-11-17 11:52:36 +0000334 _memory_group.acquire();
335 if(!_reshaped_kernel)
336 {
Pablo Tello89519332017-11-17 11:52:36 +0000337 _reshaped_kernel = true;
Pablo Tello02541fb2017-12-15 09:48:59 +0000338 _permute_weights.run();
Pablo Tellof6c572c2018-02-14 12:47:30 +0000339 NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX);
Pablo Tello89519332017-11-17 11:52:36 +0000340 }
Pablo Tello679463a2018-02-06 11:47:59 +0000341
Pablo Tello7df27862018-05-30 11:44:26 +0100342 if(data_layout == DataLayout::NCHW)
343 {
344 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
345 _permute_input.run();
346 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000347 // Transform input tensor to the winograd domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000348 NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000349
Pablo Tello89519332017-11-17 11:52:36 +0000350 //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100351 NEScheduler::get().schedule(_gemm_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000352
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000353 // Transform output tensor to the spatial domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000354 NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000355
Pablo Tello7df27862018-05-30 11:44:26 +0100356 if(data_layout == DataLayout::NCHW)
357 {
358 // Reorder the convoluted output to ACL's ordering NCHW
359 _permute_output.run();
360 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000361
362 if(_is_activationlayer_enabled)
363 {
364 _activationlayer_function.run();
365 }
Pablo Tello89519332017-11-17 11:52:36 +0000366 _memory_group.release();
Pablo Tello89519332017-11-17 11:52:36 +0000367}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000368
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100369Status 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 +0100370 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000371{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100372 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100373 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000374
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100375 // Get indices for the width and height
376 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
377 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
378
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100379 // Input shape, kernel size and output tile
380 const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height));
381 const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height));
382 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100383
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100384 // Check if the Winograd configuration requires fast math
385 if(!enable_fast_math)
386 {
387 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
388 }
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100389
390 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100391 kernel_size,
392 input_dims,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100393 conv_info,
394 input->data_layout());
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100395
396 // Validate input transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100397 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100398 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100399 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100400 {
401 case 3:
402 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100403 if(input_dims.width > 4 && input_dims.height > 4)
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100404 {
405 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, &input0, winograd_info)));
406 }
407 else
408 {
409 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, &input0, winograd_info)));
410 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100411 break;
412 }
413 case 5:
414 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100415 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, &input0, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100416 break;
417 }
418 default:
419 {
420 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
421 break;
422 }
423 }
424 // Validate filter transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100425 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100426 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
427
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100428 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100429 {
430 case 3:
431 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100432 if(input_dims.width > 4 && input_dims.height > 4)
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100433 {
434 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, &input1, winograd_info)));
435 }
436 else
437 {
438 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, &input1, winograd_info)));
439 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100440 break;
441 }
442 case 5:
443 {
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100444 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, &input1, winograd_info)));
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100445 break;
446 }
447 default:
448 {
449 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
450 break;
451 }
452 }
453 // Validate batched matrix multiply
454 TensorShape batched_mm_output_shape = input0.tensor_shape();
455 batched_mm_output_shape[0] = input1.tensor_shape()[0];
456 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100457 switch(weights->dimension(idx_width))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100458 {
459 case 3:
460 {
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100461 if(input_dims.width > 4 && input_dims.height > 4)
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100462 {
463 // Validate output transform
464 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
465 }
466 else
467 {
468 // Validate output transform
469 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(&batched_mm_output, biases, output, winograd_info)));
470 }
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100471 break;
472 }
473 case 5:
474 {
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100475 // Validate output transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100476 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 +0100477 break;
478 }
479 default:
480 {
481 ARM_COMPUTE_RETURN_ERROR_MSG("Only 3x3 and 5x5 kernels supported.");
482 break;
483 }
484 }
485
486 // Validate Activation Layer
487 if(act_info.enabled())
488 {
489 NEActivationLayer::validate(output, nullptr, act_info);
490 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000491 return Status{};
492}
493
Pablo Tello89519332017-11-17 11:52:36 +0000494} // namespace arm_compute