blob: 81190fbf0ee81d03eda098aaf61099f63a4848e0 [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Matthew Bentham92046462020-03-07 22:15:55 +00002 * Copyright (c) 2017-2020 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"
Matthew Bentham92046462020-03-07 22:15:55 +000034#include "support/MemorySupport.h"
Pablo Tello89519332017-11-17 11:52:36 +000035
Pablo Tello5264b7d2019-10-21 14:25:41 +010036#include "arm_compute/core/NEON/kernels/convolution/common/utils.hpp"
Pablo Tello8f43d742019-03-27 09:28:32 +000037#include "arm_compute/core/NEON/kernels/convolution/winograd/winograd.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{
Pablo Tello000d33a2018-09-03 16:59:20 +010043inline Status validate_kernel_3x3(const Size2D input_dims, const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
44 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
45{
46 if(input_dims.width > 4 && input_dims.height > 4)
47 {
48 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, input0, winograd_info)));
49 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, input1, winograd_info)));
50 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
51 }
52 else
53 {
54 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, input0, winograd_info)));
55 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, input1, winograd_info)));
56 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
57 }
58
59 if(act_info.enabled())
60 {
61 NEActivationLayer::validate(output, nullptr, act_info);
62 }
63 return Status{};
64}
65
66inline Status validate_kernel_5x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
67 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
68{
69 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, input0, winograd_info)));
70 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, input1, winograd_info)));
71 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>::validate(batched_mm_output, biases, output, winograd_info)));
72 if(act_info.enabled())
73 {
74 NEActivationLayer::validate(output, nullptr, act_info);
75 }
76 return Status{};
77}
78
79inline Status validate_kernel_3x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
80 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
81{
82 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 6, 1, 3>::validate(input, input0, winograd_info)));
83 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 6, 1, 3>::validate(weights, input1, winograd_info)));
84 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 6, 1, 3>::validate(batched_mm_output, biases, output, winograd_info)));
85 if(act_info.enabled())
86 {
87 NEActivationLayer::validate(output, nullptr, act_info);
88 }
89 return Status{};
90}
91
92inline Status validate_kernel_1x3(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
93 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
94{
95 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 6, 1, 3, 1>::validate(input, input0, winograd_info)));
96 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 6, 1, 3, 1>::validate(weights, input1, winograd_info)));
97 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 6, 1, 3, 1>::validate(batched_mm_output, biases, output, winograd_info)));
98
99 if(act_info.enabled())
100 {
101 NEActivationLayer::validate(output, nullptr, act_info);
102 }
103 return Status{};
104}
105
106inline Status validate_kernel_5x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
107 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
108{
109 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 4, 1, 5>::validate(input, input0, winograd_info)));
110 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 4, 1, 5>::validate(weights, input1, winograd_info)));
111 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 4, 1, 5>::validate(batched_mm_output, biases, output, winograd_info)));
112 if(act_info.enabled())
113 {
114 NEActivationLayer::validate(output, nullptr, act_info);
115 }
116 return Status{};
117}
118inline Status validate_kernel_1x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
119 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
120{
121 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 1, 5, 1>::validate(input, input0, winograd_info)));
122 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 1, 5, 1>::validate(weights, input1, winograd_info)));
123 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 1, 5, 1>::validate(batched_mm_output, biases, output, winograd_info)));
124 if(act_info.enabled())
125 {
126 NEActivationLayer::validate(output, nullptr, act_info);
127 }
128 return Status{};
129}
130
131inline Status validate_kernel_7x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
132 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
133{
134 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 2, 1, 7>::validate(input, input0, winograd_info)));
135 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 2, 1, 7>::validate(weights, input1, winograd_info)));
136 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 2, 1, 7>::validate(batched_mm_output, biases, output, winograd_info)));
137 if(act_info.enabled())
138 {
139 NEActivationLayer::validate(output, nullptr, act_info);
140 }
141 return Status{};
142}
143
144inline Status validate_kernel_1x7(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
145 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
146{
147 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 1, 7, 1>::validate(input, input0, winograd_info)));
148 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 1, 7, 1>::validate(weights, input1, winograd_info)));
149 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 1, 7, 1>::validate(batched_mm_output, biases, output, winograd_info)));
150
151 if(act_info.enabled())
152 {
153 NEActivationLayer::validate(output, nullptr, act_info);
154 }
155 return Status{};
156}
157
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100158inline Tensor4DShape internal_get_input_shape(const arm_compute::ITensor *input)
159{
160 const DataLayout data_layout = input->info()->data_layout();
161 const int in_width = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH));
162 const int in_height = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT));
163 const int in_channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
164 const int in_batches = input->info()->dimension(3);
165
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100166 return Tensor4DShape{ in_batches, in_height, in_width, in_channels };
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100167}
168
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000169Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
170{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100171 ARM_COMPUTE_UNUSED(output);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100172 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 +0000173 if(biases != nullptr)
174 {
175 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
176 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
177 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100178 return INEWinogradLayerTransformWeightsKernel<float>::validate(input, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000179}
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100180
181Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims)
182{
183 Size2D output_tile = Size2D{};
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100184 if(kernel_dims == Size2D(3U, 3U))
185 {
giuros01f44fe3d2019-08-14 16:49:27 +0100186 output_tile = (input_dims.width <= 4 || input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U);
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100187 }
188 else if(kernel_dims == Size2D(5U, 5U))
189 {
190 output_tile = Size2D(2U, 2U);
191 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100192 else if(kernel_dims == Size2D(1U, 3U))
193 {
194 output_tile = Size2D(1U, 6U);
195 }
196 else if(kernel_dims == Size2D(3U, 1U))
197 {
198 output_tile = Size2D(6U, 1U);
199 }
Pablo Tello000d33a2018-09-03 16:59:20 +0100200 else if(kernel_dims == Size2D(1U, 5U))
201 {
202 output_tile = Size2D(1U, 4U);
203 }
204 else if(kernel_dims == Size2D(5U, 1U))
205 {
206 output_tile = Size2D(4U, 1U);
207 }
208 else if(kernel_dims == Size2D(7U, 1U))
209 {
210 output_tile = Size2D(2U, 1U);
211 }
212 else if(kernel_dims == Size2D(1U, 7U))
213 {
214 output_tile = Size2D(1U, 2U);
215 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100216 return output_tile;
217}
218
219bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
220{
221 // Check if we want to configure a Winograd configuration which requires fast math
222 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
223
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100224 const std::vector<WinogradConfiguration> fast_math_winograd =
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100225 {
226 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)),
227 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
228 };
229
230 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
231 std::pair<int, int>(kernel_size.width, kernel_size.height));
232
233 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
234}
Pablo Tello7df27862018-05-30 11:44:26 +0100235
Pablo Tello5264b7d2019-10-21 14:25:41 +0100236inline bool fuse_function_supported(const ActivationLayerInfo &act_info)
237{
Matthew Bentham92046462020-03-07 22:15:55 +0000238 return act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU || act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU;
Pablo Tello5264b7d2019-10-21 14:25:41 +0100239}
240
241arm_gemm::Activation arm_gemm_activation_from_acl_activation(const ActivationLayerInfo &act_info)
242{
Matthew Bentham92046462020-03-07 22:15:55 +0000243 switch(act_info.activation())
244 {
245 case ActivationLayerInfo::ActivationFunction::RELU:
Pablo Tello5264b7d2019-10-21 14:25:41 +0100246 {
Matthew Bentham92046462020-03-07 22:15:55 +0000247 return arm_gemm::Activation(arm_gemm::Activation::Type::ReLU, act_info.a(), act_info.b());
Pablo Tello5264b7d2019-10-21 14:25:41 +0100248 }
Matthew Bentham92046462020-03-07 22:15:55 +0000249 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
250 {
251 return arm_gemm::Activation(arm_gemm::Activation::Type::BoundedReLU, act_info.a(), act_info.b());
252 }
253 default:
254 {
255 return arm_gemm::Activation(arm_gemm::Activation::Type::None);
256 }
257 }
Pablo Tello5264b7d2019-10-21 14:25:41 +0100258}
259
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000260} //namespace
261
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100262NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloa518f302018-09-19 11:33:03 +0100263 : _memory_group(memory_manager), _gemm_function(memory_manager), _transform_input_kernel(nullptr), _transform_output_kernel(nullptr), _transform_weights_kernel(nullptr), _activationlayer_function(),
Pablo Tello8f43d742019-03-27 09:28:32 +0000264 _permute_input(), _permute_weights(), _permute_output(), _input_transformed(), _output_transformed(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(),
265 _weights_hwio(), _input(), _weights(), _output(), _is_prepared(false), _is_activationlayer_enabled(false)
Pablo Tello89519332017-11-17 11:52:36 +0000266{
Pablo Tello8f43d742019-03-27 09:28:32 +0000267}
Pablo Tello89519332017-11-17 11:52:36 +0000268
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100269void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
270 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +0000271{
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000272 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000273 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 +0000274
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100275 // Get indices for the width and height
276 const DataLayout data_layout = input->info()->data_layout();
277 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
278 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
279 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
280
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100281 const Size2D input_dims = Size2D(input->info()->dimension(width_idx), input->info()->dimension(height_idx));
282 const Size2D kernel_size = Size2D(weights->info()->dimension(width_idx), weights->info()->dimension(height_idx));
283 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
284
285 // Check if the Winograd configuration requires fast math
286 if(!enable_fast_math)
287 {
288 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
289 }
290
Georgios Pinitas72219332018-06-05 14:56:06 +0100291 _weights = weights;
292 _input = input;
293 _output = output;
294 _is_prepared = false;
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100295
Pablo Tellof6c572c2018-02-14 12:47:30 +0000296 std::unique_ptr<INEWinogradLayerTransformInputKernel<float>> transform_input_kernel;
297 std::unique_ptr<INEWinogradLayerTransformWeightsKernel<float>> transform_weights_kernel;
298 std::unique_ptr<INEWinogradLayerTransformOutputKernel<float>> transform_output_kernel;
299
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100300 int n_gemms = 0;
301 int N_BLOCK = 0; // Size of block used by GEMM.
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100302
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100303 if(kernel_size == Size2D(3, 3))
Pablo Tellof6c572c2018-02-14 12:47:30 +0000304 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100305 if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000306 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100307 using config = NEWinogradLayerConfiguration<float, float, 4, 4, 3, 3>;
Pablo Tello000d33a2018-09-03 16:59:20 +0100308 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
309 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
310 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
311 n_gemms = config::WinogradBase::N_GEMMS;
312 N_BLOCK = config::WinogradConv::N_BLOCK;
313 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100314 else
Pablo Tellof6c572c2018-02-14 12:47:30 +0000315 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100316 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 3, 3>;
317 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
318 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
319 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
320 n_gemms = config::WinogradBase::N_GEMMS;
321 N_BLOCK = config::WinogradConv::N_BLOCK;
Pablo Tellof6c572c2018-02-14 12:47:30 +0000322 }
323 }
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100324 else if(kernel_size == Size2D(5, 5))
325 {
326 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 5, 5>;
327 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
328 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
329 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
330 n_gemms = config::WinogradBase::N_GEMMS;
331 N_BLOCK = config::WinogradConv::N_BLOCK;
332 }
333 else if(kernel_size == Size2D(1, 3))
334 {
335 using config = NEWinogradLayerConfiguration<float, float, 6, 1, 3, 1>;
336 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
337 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
338 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
339 n_gemms = config::WinogradBase::N_GEMMS;
340 N_BLOCK = config::WinogradConv::N_BLOCK;
341 }
342 else if(kernel_size == Size2D(3, 1))
343 {
344 using config = NEWinogradLayerConfiguration<float, float, 1, 6, 1, 3>;
345 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
346 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
347 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
348 n_gemms = config::WinogradBase::N_GEMMS;
349 N_BLOCK = config::WinogradConv::N_BLOCK;
350 }
351 else if(kernel_size == Size2D(1, 5))
352 {
353 using config = NEWinogradLayerConfiguration<float, float, 4, 1, 5, 1>;
354 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
355 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
356 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
357 n_gemms = config::WinogradBase::N_GEMMS;
358 N_BLOCK = config::WinogradConv::N_BLOCK;
359 }
360 else if(kernel_size == Size2D(5, 1))
361 {
362 using config = NEWinogradLayerConfiguration<float, float, 1, 4, 1, 5>;
363 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
364 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
365 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
366 n_gemms = config::WinogradBase::N_GEMMS;
367 N_BLOCK = config::WinogradConv::N_BLOCK;
368 }
369 else if(kernel_size == Size2D(1, 7))
370 {
371 using config = NEWinogradLayerConfiguration<float, float, 2, 1, 7, 1>;
372 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
373 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
374 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
375 n_gemms = config::WinogradBase::N_GEMMS;
376 N_BLOCK = config::WinogradConv::N_BLOCK;
377 }
378 else if(kernel_size == Size2D(7, 1))
379 {
380 using config = NEWinogradLayerConfiguration<float, float, 1, 2, 1, 7>;
381 transform_input_kernel = support::cpp14::make_unique<config::TransformInputKernel>();
382 transform_weights_kernel = support::cpp14::make_unique<config::TransformWeightsKernel>();
383 transform_output_kernel = support::cpp14::make_unique<config::TransformOutputKernel>();
384 n_gemms = config::WinogradBase::N_GEMMS;
385 N_BLOCK = config::WinogradConv::N_BLOCK;
386 }
387 else
388 {
389 ARM_COMPUTE_ERROR("Not supported.");
390 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000391
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100392 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 +0000393 const bool use_same_padding = use_padding_type == PADDING_SAME;
394
Pablo Tello89519332017-11-17 11:52:36 +0000395 // Get convolved dimensions
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100396 const int in_channels = input->info()->dimension(channel_idx);
397 const int out_channels = output->info()->dimension(channel_idx);
Pablo Tello89519332017-11-17 11:52:36 +0000398
Pablo Tello89519332017-11-17 11:52:36 +0000399 const Tensor4DShape in_shape(internal_get_input_shape(input));
Anthony Barbiere1553372018-07-16 18:53:52 +0100400 const DataType data_type = input->info()->data_type();
Pablo Tellod6ca4782018-01-23 09:36:04 +0000401 const size_t data_type_size = input->info()->element_size();
Pablo Tello89519332017-11-17 11:52:36 +0000402 // Get the memory required to instantiate a new Winograd operator.
Georgios Pinitas72219332018-06-05 14:56:06 +0100403 constexpr size_t storage_alignment = 64;
404
405 // Kernel Storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100406 const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels,
Anthony Barbiere1553372018-07-16 18:53:52 +0100407 in_channels)
Georgios Pinitas71798372019-04-17 13:01:54 +0100408 * data_type_size;
Georgios Pinitas72219332018-06-05 14:56:06 +0100409
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000410 // Input storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100411 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 +0100412 use_same_padding)
Georgios Pinitas71798372019-04-17 13:01:54 +0100413 * data_type_size;
Pablo Tello89519332017-11-17 11:52:36 +0000414
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000415 // Output storage
Pablo Tello5264b7d2019-10-21 14:25:41 +0100416 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) * data_type_size;
417 const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(out_channels, in_channels);
418 const int output_matrix_stride = transform_output_kernel->get_matrix_stride(in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, out_channels);
419 const auto output_shape = transform_output_kernel->get_output_shape(in_shape.n_rows, in_shape.n_cols, use_padding_type == PADDING_SAME);
420 const int input_matrix_stride = transform_input_kernel->get_matrix_stride(in_shape.n_batches, in_channels, in_shape.n_rows, in_shape.n_cols, use_padding_type == PADDING_SAME);
Anthony Barbier578225e2018-07-16 18:00:11 +0100421
422 // Configure GEMM
Pablo Tello5264b7d2019-10-21 14:25:41 +0100423 const int tile_rows = iceildiv(output_shape.first, output_tile.height);
424 const int tile_cols = iceildiv(output_shape.second, output_tile.width);
Anthony Barbier578225e2018-07-16 18:00:11 +0100425 const int m = in_shape.n_batches * tile_rows * tile_cols;
426 const int k = in_shape.n_channels;
427 const int n = out_channels;
428 const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK);
429 const int output_matrix_row_stride = kernel_matrix_row_stride;
430
431 TensorShape a_shape(k, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100432 Strides a_strides(data_type_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100433 a_strides.set(1, a_strides[0] * k);
Anthony Barbiere1553372018-07-16 18:53:52 +0100434 //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 +0100435 a_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100436 a_strides.set(3, data_type_size * input_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100437
438 TensorShape b_shape(n, k, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100439 Strides b_strides(data_type_size);
440 b_strides.set(1, data_type_size * kernel_matrix_row_stride);
441 b_strides.set(2, data_type_size * kernel_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100442
443 TensorShape d_shape(n, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100444 Strides d_strides(data_type_size);
445 d_strides.set(1, data_type_size * output_matrix_row_stride);
446 //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 +0100447 d_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100448 d_strides.set(3, data_type_size * output_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100449
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100450 TensorInfo a_info{};
451 TensorInfo b_info{};
452 TensorInfo d_info{};
Anthony Barbiere1553372018-07-16 18:53:52 +0100453 a_info.init(a_shape, 1, data_type, a_strides, 0, input_storage_size);
454 b_info.init(b_shape, 1, data_type, b_strides, 0, kernel_storage_size);
455 d_info.init(d_shape, 1, data_type, d_strides, 0, output_storage_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100456
Pablo Tello8f43d742019-03-27 09:28:32 +0000457 _input_transformed.allocator()->init(a_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100458 _kernel_storage.allocator()->init(b_info, storage_alignment);
Pablo Tello8f43d742019-03-27 09:28:32 +0000459 _output_transformed.allocator()->init(d_info, storage_alignment);
Pablo Tello89519332017-11-17 11:52:36 +0000460
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000461 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
462 TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0),
463 _output->info()->dimension(1), _output->info()->dimension(3)),
464 1, _output->info()->data_type());
465 _output_nhwc.allocator()->init(info);
Pablo Tello02541fb2017-12-15 09:48:59 +0000466
Georgios Pinitas71798372019-04-17 13:01:54 +0100467 const ITensor *input_to_use = _input;
468 ITensor *output_to_use = _output;
469 PermutationVector weights_permutation_vector(3U, 0U, 1U, 2U);
470 const unsigned int max_num_threads = NEScheduler::get().num_threads();
Pablo Tellof718ce22018-10-29 13:13:23 +0000471
Georgios Pinitas71798372019-04-17 13:01:54 +0100472 // Configure the kernel to transform the input tensor from NCHW -> NHWC
Pablo Tello7df27862018-05-30 11:44:26 +0100473 if(data_layout == DataLayout::NCHW)
474 {
Georgios Pinitas71798372019-04-17 13:01:54 +0100475 _memory_group.manage(&_input_nhwc);
Pablo Tello7df27862018-05-30 11:44:26 +0100476 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
Georgios Pinitas71798372019-04-17 13:01:54 +0100477 input_to_use = &_input_nhwc;
478 weights_permutation_vector = PermutationVector(3U, 2U, 0U, 1U);
Pablo Tello7df27862018-05-30 11:44:26 +0100479 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000480
Georgios Pinitas71798372019-04-17 13:01:54 +0100481 // Configure input transform kernel
482 _memory_group.manage(&_input_transformed);
483 _memory_group.manage(&_input_workspace);
484 transform_input_kernel->configure(input_to_use, in_shape.n_batches, in_shape.n_rows, in_shape.n_cols, in_shape.n_channels, use_padding_type,
485 &_input_transformed, input_matrix_stride, &_input_workspace);
486 const size_t input_workspace_size = transform_input_kernel->get_working_space_size(max_num_threads);
487 TensorInfo input_workspace_info(TensorShape(input_workspace_size), 1, _input->info()->data_type());
Pablo Tello8f43d742019-03-27 09:28:32 +0000488 _input_workspace.allocator()->init(input_workspace_info);
Georgios Pinitas71798372019-04-17 13:01:54 +0100489 _input_workspace.allocator()->allocate();
490 if(data_layout == DataLayout::NCHW)
491 {
492 _input_nhwc.allocator()->allocate();
493 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000494
Georgios Pinitas71798372019-04-17 13:01:54 +0100495 // 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]
496 _permute_weights.configure(weights, &_weights_hwio, weights_permutation_vector);
497 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello8f43d742019-03-27 09:28:32 +0000498
Georgios Pinitas71798372019-04-17 13:01:54 +0100499 // Configure GEMM function
500 _memory_group.manage(&_output_transformed);
Pablo Tello8f43d742019-03-27 09:28:32 +0000501 _gemm_function.configure(&_input_transformed, &_kernel_storage, nullptr, &_output_transformed, 1.0f, 0.f);
502 _input_transformed.allocator()->allocate();
Georgios Pinitas71798372019-04-17 13:01:54 +0100503
504 // Configure output transform function
505 // 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
506 if(data_layout == DataLayout::NCHW)
507 {
508 _memory_group.manage(&_output_nhwc);
509 output_to_use = &_output_nhwc;
510 }
Matthew Bentham92046462020-03-07 22:15:55 +0000511 const arm_gemm::Activation activation = arm_gemm_activation_from_acl_activation(act_info);
Pablo Tello5264b7d2019-10-21 14:25:41 +0100512
513 transform_output_kernel->configure(biases,
514 &_output_transformed,
515 output_matrix_stride,
516 output_to_use,
517 in_shape.n_batches,
518 output_shape.first,
519 output_shape.second,
520 out_channels,
521 &_output_workspace,
522 activation);
523
Georgios Pinitas71798372019-04-17 13:01:54 +0100524 const size_t output_workspace_size = transform_output_kernel->get_working_space_size(max_num_threads);
525 TensorInfo output_workspace_info(TensorShape(output_workspace_size), 1, _output->info()->data_type());
526 _output_workspace.allocator()->init(output_workspace_info);
Anthony Barbier20394d52018-08-02 11:29:09 +0100527 _output_workspace.allocator()->allocate();
Georgios Pinitas71798372019-04-17 13:01:54 +0100528 _output_transformed.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000529
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000530 // Reorder the convoluted output to ACL's ordering NCHW
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000531 if(data_layout == DataLayout::NCHW)
532 {
533 _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U));
534 _output_nhwc.allocator()->allocate();
535 }
Anthony Barbier20394d52018-08-02 11:29:09 +0100536
Pablo Tellof6c572c2018-02-14 12:47:30 +0000537 _transform_input_kernel = std::move(transform_input_kernel);
538 _transform_weights_kernel = std::move(transform_weights_kernel);
539 _transform_output_kernel = std::move(transform_output_kernel);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000540
541 //Configure Activation Layer
Matthew Bentham92046462020-03-07 22:15:55 +0000542 _is_activationlayer_enabled = act_info.enabled() && !fuse_function_supported(act_info);
Pablo Tello7282d562018-06-14 15:35:49 +0100543 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000544 {
Pablo Tello7df27862018-05-30 11:44:26 +0100545 _activationlayer_function.configure(_output, nullptr, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000546 }
Pablo Tello89519332017-11-17 11:52:36 +0000547}
548
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100549void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +0000550{
Pablo Tello7df27862018-05-30 11:44:26 +0100551 const DataLayout data_layout = _input->info()->data_layout();
552
Georgios Pinitas72219332018-06-05 14:56:06 +0100553 prepare();
554
Georgios Pinitasda953f22019-04-02 17:27:03 +0100555 MemoryGroupResourceScope scope_mg(_memory_group);
Pablo Tello679463a2018-02-06 11:47:59 +0000556
Pablo Tello7df27862018-05-30 11:44:26 +0100557 if(data_layout == DataLayout::NCHW)
558 {
559 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
560 _permute_input.run();
561 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100562
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000563 // Transform input tensor to the winograd domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000564 NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000565
Pablo Tello89519332017-11-17 11:52:36 +0000566 //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Pablo Telloa518f302018-09-19 11:33:03 +0100567 _gemm_function.run();
Georgios Pinitas71798372019-04-17 13:01:54 +0100568
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000569 // Transform output tensor to the spatial domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000570 NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000571
Pablo Tello7df27862018-05-30 11:44:26 +0100572 if(data_layout == DataLayout::NCHW)
573 {
574 // Reorder the convoluted output to ACL's ordering NCHW
575 _permute_output.run();
576 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000577
Matthew Bentham92046462020-03-07 22:15:55 +0000578 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000579 {
580 _activationlayer_function.run();
581 }
Pablo Tello89519332017-11-17 11:52:36 +0000582}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000583
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100584Status 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 +0100585 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000586{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100587 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100588 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000589
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100590 // Get indices for the width and height
591 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
592 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
593
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100594 // Input shape, kernel size and output tile
Pablo Tello000d33a2018-09-03 16:59:20 +0100595 const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height));
596 const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height));
597 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100598
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100599 // Check if the Winograd configuration requires fast math
600 if(!enable_fast_math)
601 {
602 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
603 }
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100604
605 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100606 kernel_size,
607 input_dims,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100608 conv_info,
609 input->data_layout());
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100610
611 // Validate input transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100612 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100613 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Pablo Tello000d33a2018-09-03 16:59:20 +0100614 // Validate filter transform
615 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
616 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
617 // Validate batched matrix multiply
618 TensorShape batched_mm_output_shape = input0.tensor_shape();
619 batched_mm_output_shape[0] = input1.tensor_shape()[0];
620 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Pablo Tello7282d562018-06-14 15:35:49 +0100621
Pablo Tello000d33a2018-09-03 16:59:20 +0100622 if(kernel_size == Size2D(3, 3))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100623 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100624 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported");
625 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported");
626 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported");
627 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported");
628 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported");
629 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported");
630 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_left(), "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100631 return validate_kernel_3x3(input_dims, input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
632 }
633 else if(kernel_size == Size2D(5, 5))
634 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100635 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported");
636 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported");
637 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported");
638 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported");
639 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported");
640 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported");
641 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_left(), "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100642 return validate_kernel_5x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
643 }
644 if(kernel_size == Size2D(3, 1))
645 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100646 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported");
647 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported");
648 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_bottom() != 0, "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100649 return validate_kernel_3x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
650 }
651 else if(kernel_size == Size2D(1, 3))
652 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100653 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported");
654 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported");
655 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_right() != 0, "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100656 return validate_kernel_1x3(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
657 }
658 else if(kernel_size == Size2D(5, 1))
659 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100660 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported");
661 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported");
662 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_bottom() != 0, "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100663 return validate_kernel_5x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
664 }
665 else if(kernel_size == Size2D(1, 5))
666 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100667 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported");
668 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported");
669 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_right() != 0, "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100670 return validate_kernel_1x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
671 }
672 else if(kernel_size == Size2D(7, 1))
673 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100674 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 3, "Only SAME or VALID padding supported");
675 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 3, "Only SAME or VALID padding supported");
676 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_bottom() != 0, "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100677 return validate_kernel_7x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
678 }
679 else if(kernel_size == Size2D(1, 7))
680 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100681 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 3, "Only SAME or VALID padding supported");
682 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 3, "Only SAME or VALID padding supported");
683 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_right() != 0, "Only SAME or VALID padding supported");
Pablo Tello000d33a2018-09-03 16:59:20 +0100684 return validate_kernel_1x7(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100685 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100686 else
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100687 {
Pablo Tello000d33a2018-09-03 16:59:20 +0100688 ARM_COMPUTE_RETURN_ERROR_MSG("Kernel shape not supported");
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100689 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000690}
691
Georgios Pinitas72219332018-06-05 14:56:06 +0100692void NEWinogradConvolutionLayer::prepare()
693{
694 if(!_is_prepared)
695 {
696 // Permute weights
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000697 _weights_hwio.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100698 _permute_weights.run();
699 _weights->mark_as_unused();
700
701 // Transform weights
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000702 _kernel_storage.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100703 NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX);
Georgios Pinitas72219332018-06-05 14:56:06 +0100704
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100705 _weights_hwio.allocator()->free();
Georgios Pinitas72219332018-06-05 14:56:06 +0100706 _is_prepared = true;
707 }
708}
709
Pablo Tello89519332017-11-17 11:52:36 +0000710} // namespace arm_compute