blob: dc3bbbe562a0c718b5be490ce80162c7089d41ed [file] [log] [blame]
Pablo Tello89519332017-11-17 11:52:36 +00001/*
Georgios Pinitasddd79f52021-01-15 09:42:26 +00002 * Copyright (c) 2017-2021 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/utils/misc/ShapeCalculator.h"
Pablo Tello89519332017-11-17 11:52:36 +000030#include "arm_compute/runtime/NEON/NEScheduler.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010032#include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
33#include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h"
34#include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
35#include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010036#include "src/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h"
Georgios Pinitasec2256b2020-12-03 18:51:58 +000037#include "src/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
Pablo Tello89519332017-11-17 11:52:36 +000038
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010039#include "src/core/NEON/kernels/convolution/common/utils.hpp"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010040#include "src/core/NEON/kernels/convolution/winograd/winograd.hpp"
Pablo Tellod6ca4782018-01-23 09:36:04 +000041
Pablo Tello89519332017-11-17 11:52:36 +000042namespace arm_compute
43{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000044namespace
45{
Pablo Tello000d33a2018-09-03 16:59:20 +010046inline Status validate_kernel_3x3(const Size2D input_dims, const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
47 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
48{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010049 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
51
52 if(input->data_type() == DataType::F32)
Pablo Tello000d33a2018-09-03 16:59:20 +010053 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010054 if(input_dims.width > 4 && input_dims.height > 4)
55 {
56 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, input0, winograd_info)));
57 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, input1, winograd_info)));
58 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
59 }
60 else
61 {
62 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, input0, winograd_info)));
63 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, input1, winograd_info)));
64 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
65 }
Pablo Tello000d33a2018-09-03 16:59:20 +010066 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010067#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
SiCong Li6b6a16f2020-05-28 08:55:51 +010068 else if(input->data_type() == DataType::F16)
Pablo Tello000d33a2018-09-03 16:59:20 +010069 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010070 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<__fp16, 4, 4, 3, 3>::validate(input, input0, winograd_info)));
71 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<__fp16, 4, 4, 3, 3>::validate(weights, input1, winograd_info)));
72 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<__fp16, 4, 4, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
Pablo Tello000d33a2018-09-03 16:59:20 +010073 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010074#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello000d33a2018-09-03 16:59:20 +010075
76 if(act_info.enabled())
77 {
78 NEActivationLayer::validate(output, nullptr, act_info);
79 }
80 return Status{};
81}
82
83inline Status validate_kernel_5x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
84 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
85{
86 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, input0, winograd_info)));
87 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, input1, winograd_info)));
88 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>::validate(batched_mm_output, biases, output, winograd_info)));
89 if(act_info.enabled())
90 {
91 NEActivationLayer::validate(output, nullptr, act_info);
92 }
93 return Status{};
94}
95
96inline Status validate_kernel_3x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
97 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
98{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010099 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100100 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 6, 1, 3>::validate(input, input0, winograd_info)));
101 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 6, 1, 3>::validate(weights, input1, winograd_info)));
102 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 6, 1, 3>::validate(batched_mm_output, biases, output, winograd_info)));
103 if(act_info.enabled())
104 {
105 NEActivationLayer::validate(output, nullptr, act_info);
106 }
107 return Status{};
108}
109
110inline Status validate_kernel_1x3(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
111 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
112{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100113 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100114 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 6, 1, 3, 1>::validate(input, input0, winograd_info)));
115 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 6, 1, 3, 1>::validate(weights, input1, winograd_info)));
116 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 6, 1, 3, 1>::validate(batched_mm_output, biases, output, winograd_info)));
117
118 if(act_info.enabled())
119 {
120 NEActivationLayer::validate(output, nullptr, act_info);
121 }
122 return Status{};
123}
124
125inline Status validate_kernel_5x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
126 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
127{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100128 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100129 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 4, 1, 5>::validate(input, input0, winograd_info)));
130 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 4, 1, 5>::validate(weights, input1, winograd_info)));
131 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 4, 1, 5>::validate(batched_mm_output, biases, output, winograd_info)));
132 if(act_info.enabled())
133 {
134 NEActivationLayer::validate(output, nullptr, act_info);
135 }
136 return Status{};
137}
138inline Status validate_kernel_1x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
139 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
140{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100141 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100142 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 1, 5, 1>::validate(input, input0, winograd_info)));
143 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 1, 5, 1>::validate(weights, input1, winograd_info)));
144 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 1, 5, 1>::validate(batched_mm_output, biases, output, winograd_info)));
145 if(act_info.enabled())
146 {
147 NEActivationLayer::validate(output, nullptr, act_info);
148 }
149 return Status{};
150}
151
152inline Status validate_kernel_7x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
153 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
154{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100155 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100156 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 2, 1, 7>::validate(input, input0, winograd_info)));
157 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 2, 1, 7>::validate(weights, input1, winograd_info)));
158 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 2, 1, 7>::validate(batched_mm_output, biases, output, winograd_info)));
159 if(act_info.enabled())
160 {
161 NEActivationLayer::validate(output, nullptr, act_info);
162 }
163 return Status{};
164}
165
166inline Status validate_kernel_1x7(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
167 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
168{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100169 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100170 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 1, 7, 1>::validate(input, input0, winograd_info)));
171 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 1, 7, 1>::validate(weights, input1, winograd_info)));
172 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 1, 7, 1>::validate(batched_mm_output, biases, output, winograd_info)));
173
174 if(act_info.enabled())
175 {
176 NEActivationLayer::validate(output, nullptr, act_info);
177 }
178 return Status{};
179}
180
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100181inline Tensor4DShape internal_get_input_shape(const arm_compute::ITensor *input)
182{
183 const DataLayout data_layout = input->info()->data_layout();
184 const int in_width = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH));
185 const int in_height = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT));
186 const int in_channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
187 const int in_batches = input->info()->dimension(3);
188
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100189 return Tensor4DShape{ in_batches, in_height, in_width, in_channels };
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100190}
191
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000192Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
193{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100194 ARM_COMPUTE_UNUSED(output);
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100195 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
196
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100197 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 +0000198 if(biases != nullptr)
199 {
200 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
201 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
202 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100203 return INEWinogradLayerTransformWeightsKernel::validate(input, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000204}
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100205
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100206Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataType data_type)
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100207{
208 Size2D output_tile = Size2D{};
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100209 if(kernel_dims == Size2D(3U, 3U))
210 {
giuros01f44fe3d2019-08-14 16:49:27 +0100211 output_tile = (input_dims.width <= 4 || input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U);
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100212 if(data_type == DataType::F16)
213 {
214 output_tile = Size2D(4U, 4U);
215 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100216 }
217 else if(kernel_dims == Size2D(5U, 5U))
218 {
219 output_tile = Size2D(2U, 2U);
220 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100221 else if(kernel_dims == Size2D(1U, 3U))
222 {
223 output_tile = Size2D(1U, 6U);
224 }
225 else if(kernel_dims == Size2D(3U, 1U))
226 {
227 output_tile = Size2D(6U, 1U);
228 }
Pablo Tello000d33a2018-09-03 16:59:20 +0100229 else if(kernel_dims == Size2D(1U, 5U))
230 {
231 output_tile = Size2D(1U, 4U);
232 }
233 else if(kernel_dims == Size2D(5U, 1U))
234 {
235 output_tile = Size2D(4U, 1U);
236 }
237 else if(kernel_dims == Size2D(7U, 1U))
238 {
239 output_tile = Size2D(2U, 1U);
240 }
241 else if(kernel_dims == Size2D(1U, 7U))
242 {
243 output_tile = Size2D(1U, 2U);
244 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100245 return output_tile;
246}
247
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100248bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size, DataType data_type)
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100249{
250 // Check if we want to configure a Winograd configuration which requires fast math
251 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
252
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100253 const std::vector<WinogradConfiguration> fast_math_winograd_f16 =
254 {
255 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3))
256 };
257
258 const std::vector<WinogradConfiguration> fast_math_winograd_f32 =
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100259 {
260 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)),
261 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
262 };
263
264 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
265 std::pair<int, int>(kernel_size.width, kernel_size.height));
266
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100267 switch(data_type)
268 {
269 case DataType::F16:
270 return std::find(fast_math_winograd_f16.begin(), fast_math_winograd_f16.end(), p) != fast_math_winograd_f16.end();
271 case DataType::F32:
272 return std::find(fast_math_winograd_f32.begin(), fast_math_winograd_f32.end(), p) != fast_math_winograd_f32.end();
273 default:
274 return false;
275 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100276}
Pablo Tello7df27862018-05-30 11:44:26 +0100277
Pablo Tello5264b7d2019-10-21 14:25:41 +0100278inline bool fuse_function_supported(const ActivationLayerInfo &act_info)
279{
Matthew Bentham92046462020-03-07 22:15:55 +0000280 return act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU || act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU;
Pablo Tello5264b7d2019-10-21 14:25:41 +0100281}
282
283arm_gemm::Activation arm_gemm_activation_from_acl_activation(const ActivationLayerInfo &act_info)
284{
Matthew Bentham92046462020-03-07 22:15:55 +0000285 switch(act_info.activation())
286 {
287 case ActivationLayerInfo::ActivationFunction::RELU:
Pablo Tello5264b7d2019-10-21 14:25:41 +0100288 {
Matthew Bentham92046462020-03-07 22:15:55 +0000289 return arm_gemm::Activation(arm_gemm::Activation::Type::ReLU, act_info.a(), act_info.b());
Pablo Tello5264b7d2019-10-21 14:25:41 +0100290 }
Matthew Bentham92046462020-03-07 22:15:55 +0000291 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
292 {
293 return arm_gemm::Activation(arm_gemm::Activation::Type::BoundedReLU, act_info.a(), act_info.b());
294 }
295 default:
296 {
297 return arm_gemm::Activation(arm_gemm::Activation::Type::None);
298 }
299 }
Pablo Tello5264b7d2019-10-21 14:25:41 +0100300}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000301} //namespace
302
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100303NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloa518f302018-09-19 11:33:03 +0100304 : _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 +0000305 _permute_input(), _permute_weights(), _permute_output(), _input_transformed(), _output_transformed(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(),
306 _weights_hwio(), _input(), _weights(), _output(), _is_prepared(false), _is_activationlayer_enabled(false)
Pablo Tello89519332017-11-17 11:52:36 +0000307{
Pablo Tello8f43d742019-03-27 09:28:32 +0000308}
Pablo Tello89519332017-11-17 11:52:36 +0000309
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100310void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
311 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +0000312{
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000313 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000314 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 +0000315
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100316 // Get indices for the width and height
317 const DataLayout data_layout = input->info()->data_layout();
318 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
319 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
320 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
321
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100322 const Size2D input_dims = Size2D(input->info()->dimension(width_idx), input->info()->dimension(height_idx));
323 const Size2D kernel_size = Size2D(weights->info()->dimension(width_idx), weights->info()->dimension(height_idx));
324 const DataType data_type = input->info()->data_type();
325 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, data_type);
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100326
327 // Check if the Winograd configuration requires fast math
328 if(!enable_fast_math)
329 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100330 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size, data_type),
331 "This Winograd configuration requires enable_fast_math=true");
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100332 }
333
Georgios Pinitas72219332018-06-05 14:56:06 +0100334 _weights = weights;
335 _input = input;
336 _output = output;
337 _is_prepared = false;
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100338
Georgios Pinitas9a671782021-02-25 00:04:08 +0000339 int n_gemms = 1;
340 int N_BLOCK = 1; // Size of block used by GEMM.
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100341
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100342 std::unique_ptr<INEWinogradLayerTransformInputKernel> transform_input_kernel;
343 std::unique_ptr<INEWinogradLayerTransformWeightsKernel> transform_weights_kernel;
344 std::unique_ptr<INEWinogradLayerTransformOutputKernel> transform_output_kernel;
345
346 if(data_type == DataType::F32)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000347 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100348 if(kernel_size == Size2D(3, 3))
Pablo Tellof6c572c2018-02-14 12:47:30 +0000349 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100350 if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4)
351 {
352 using config = NEWinogradLayerConfiguration<float, float, 4, 4, 3, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000353 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
354 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
355 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100356 n_gemms = config::WinogradBase::N_GEMMS;
357 N_BLOCK = config::WinogradConv::N_BLOCK;
358 }
359 else
360 {
361 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 3, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000362 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
363 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
364 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100365 n_gemms = config::WinogradBase::N_GEMMS;
366 N_BLOCK = config::WinogradConv::N_BLOCK;
367 }
368 }
369 else if(kernel_size == Size2D(5, 5))
370 {
371 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 5, 5>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000372 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
373 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
374 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100375 n_gemms = config::WinogradBase::N_GEMMS;
376 N_BLOCK = config::WinogradConv::N_BLOCK;
377 }
378 else if(kernel_size == Size2D(1, 3))
379 {
380 using config = NEWinogradLayerConfiguration<float, float, 6, 1, 3, 1>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000381 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
382 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
383 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100384 n_gemms = config::WinogradBase::N_GEMMS;
385 N_BLOCK = config::WinogradConv::N_BLOCK;
386 }
387 else if(kernel_size == Size2D(3, 1))
388 {
389 using config = NEWinogradLayerConfiguration<float, float, 1, 6, 1, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000390 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
391 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
392 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100393 n_gemms = config::WinogradBase::N_GEMMS;
394 N_BLOCK = config::WinogradConv::N_BLOCK;
395 }
396 else if(kernel_size == Size2D(1, 5))
397 {
398 using config = NEWinogradLayerConfiguration<float, float, 4, 1, 5, 1>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000399 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
400 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
401 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100402 n_gemms = config::WinogradBase::N_GEMMS;
403 N_BLOCK = config::WinogradConv::N_BLOCK;
404 }
405 else if(kernel_size == Size2D(5, 1))
406 {
407 using config = NEWinogradLayerConfiguration<float, float, 1, 4, 1, 5>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000408 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
409 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
410 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100411 n_gemms = config::WinogradBase::N_GEMMS;
412 N_BLOCK = config::WinogradConv::N_BLOCK;
413 }
414 else if(kernel_size == Size2D(1, 7))
415 {
416 using config = NEWinogradLayerConfiguration<float, float, 2, 1, 7, 1>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000417 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
418 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
419 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100420 n_gemms = config::WinogradBase::N_GEMMS;
421 N_BLOCK = config::WinogradConv::N_BLOCK;
422 }
423 else if(kernel_size == Size2D(7, 1))
424 {
425 using config = NEWinogradLayerConfiguration<float, float, 1, 2, 1, 7>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000426 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
427 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
428 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Pablo Tello000d33a2018-09-03 16:59:20 +0100429 n_gemms = config::WinogradBase::N_GEMMS;
430 N_BLOCK = config::WinogradConv::N_BLOCK;
431 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100432 else
Pablo Tellof6c572c2018-02-14 12:47:30 +0000433 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100434 ARM_COMPUTE_ERROR("Not supported.");
435 }
436 }
437#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
438 else if(data_type == DataType::F16)
439 {
440 if(kernel_size == Size2D(3, 3))
441 {
442 using config = NEWinogradLayerConfiguration<__fp16, __fp16, 4, 4, 3, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000443 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
444 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
445 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100446 n_gemms = config::WinogradBase::N_GEMMS;
447 N_BLOCK = config::WinogradConv::N_BLOCK;
Pablo Tellof6c572c2018-02-14 12:47:30 +0000448 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100449 else
450 {
451 ARM_COMPUTE_ERROR("Not supported.");
452 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000453 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100454#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas9a671782021-02-25 00:04:08 +0000455 else
456 {
457 ARM_COMPUTE_ERROR("Not supported.");
458 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000459
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100460 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 +0000461 const bool use_same_padding = use_padding_type == PADDING_SAME;
462
Pablo Tello89519332017-11-17 11:52:36 +0000463 // Get convolved dimensions
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100464 const int in_channels = input->info()->dimension(channel_idx);
465 const int out_channels = output->info()->dimension(channel_idx);
Pablo Tello89519332017-11-17 11:52:36 +0000466
Pablo Tello89519332017-11-17 11:52:36 +0000467 const Tensor4DShape in_shape(internal_get_input_shape(input));
Pablo Tellod6ca4782018-01-23 09:36:04 +0000468 const size_t data_type_size = input->info()->element_size();
Pablo Tello89519332017-11-17 11:52:36 +0000469 // Get the memory required to instantiate a new Winograd operator.
Georgios Pinitas72219332018-06-05 14:56:06 +0100470 constexpr size_t storage_alignment = 64;
471
472 // Kernel Storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100473 const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels,
Anthony Barbiere1553372018-07-16 18:53:52 +0100474 in_channels)
Georgios Pinitas71798372019-04-17 13:01:54 +0100475 * data_type_size;
Georgios Pinitas72219332018-06-05 14:56:06 +0100476
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000477 // Input storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100478 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 +0100479 use_same_padding)
Georgios Pinitas71798372019-04-17 13:01:54 +0100480 * data_type_size;
Pablo Tello89519332017-11-17 11:52:36 +0000481
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000482 // Output storage
Pablo Tello5264b7d2019-10-21 14:25:41 +0100483 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;
484 const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(out_channels, in_channels);
485 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);
486 const auto output_shape = transform_output_kernel->get_output_shape(in_shape.n_rows, in_shape.n_cols, use_padding_type == PADDING_SAME);
487 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 +0100488
489 // Configure GEMM
Pablo Tello5264b7d2019-10-21 14:25:41 +0100490 const int tile_rows = iceildiv(output_shape.first, output_tile.height);
491 const int tile_cols = iceildiv(output_shape.second, output_tile.width);
Anthony Barbier578225e2018-07-16 18:00:11 +0100492 const int m = in_shape.n_batches * tile_rows * tile_cols;
493 const int k = in_shape.n_channels;
494 const int n = out_channels;
495 const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK);
496 const int output_matrix_row_stride = kernel_matrix_row_stride;
497
498 TensorShape a_shape(k, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100499 Strides a_strides(data_type_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100500 a_strides.set(1, a_strides[0] * k);
Anthony Barbiere1553372018-07-16 18:53:52 +0100501 //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 +0100502 a_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100503 a_strides.set(3, data_type_size * input_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100504
505 TensorShape b_shape(n, k, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100506 Strides b_strides(data_type_size);
507 b_strides.set(1, data_type_size * kernel_matrix_row_stride);
508 b_strides.set(2, data_type_size * kernel_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100509
510 TensorShape d_shape(n, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100511 Strides d_strides(data_type_size);
512 d_strides.set(1, data_type_size * output_matrix_row_stride);
513 //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 +0100514 d_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100515 d_strides.set(3, data_type_size * output_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100516
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100517 TensorInfo a_info{};
518 TensorInfo b_info{};
519 TensorInfo d_info{};
Anthony Barbiere1553372018-07-16 18:53:52 +0100520 a_info.init(a_shape, 1, data_type, a_strides, 0, input_storage_size);
521 b_info.init(b_shape, 1, data_type, b_strides, 0, kernel_storage_size);
522 d_info.init(d_shape, 1, data_type, d_strides, 0, output_storage_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100523
Pablo Tello8f43d742019-03-27 09:28:32 +0000524 _input_transformed.allocator()->init(a_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100525 _kernel_storage.allocator()->init(b_info, storage_alignment);
Pablo Tello8f43d742019-03-27 09:28:32 +0000526 _output_transformed.allocator()->init(d_info, storage_alignment);
Pablo Tello89519332017-11-17 11:52:36 +0000527
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000528 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
529 TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0),
530 _output->info()->dimension(1), _output->info()->dimension(3)),
531 1, _output->info()->data_type());
532 _output_nhwc.allocator()->init(info);
Pablo Tello02541fb2017-12-15 09:48:59 +0000533
Georgios Pinitas71798372019-04-17 13:01:54 +0100534 const ITensor *input_to_use = _input;
535 ITensor *output_to_use = _output;
536 PermutationVector weights_permutation_vector(3U, 0U, 1U, 2U);
537 const unsigned int max_num_threads = NEScheduler::get().num_threads();
Pablo Tellof718ce22018-10-29 13:13:23 +0000538
Georgios Pinitas71798372019-04-17 13:01:54 +0100539 // Configure the kernel to transform the input tensor from NCHW -> NHWC
Pablo Tello7df27862018-05-30 11:44:26 +0100540 if(data_layout == DataLayout::NCHW)
541 {
Georgios Pinitas71798372019-04-17 13:01:54 +0100542 _memory_group.manage(&_input_nhwc);
Pablo Tello7df27862018-05-30 11:44:26 +0100543 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
Georgios Pinitas71798372019-04-17 13:01:54 +0100544 input_to_use = &_input_nhwc;
545 weights_permutation_vector = PermutationVector(3U, 2U, 0U, 1U);
Pablo Tello7df27862018-05-30 11:44:26 +0100546 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000547
Georgios Pinitas71798372019-04-17 13:01:54 +0100548 // Configure input transform kernel
549 _memory_group.manage(&_input_transformed);
550 _memory_group.manage(&_input_workspace);
551 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,
552 &_input_transformed, input_matrix_stride, &_input_workspace);
553 const size_t input_workspace_size = transform_input_kernel->get_working_space_size(max_num_threads);
554 TensorInfo input_workspace_info(TensorShape(input_workspace_size), 1, _input->info()->data_type());
Pablo Tello8f43d742019-03-27 09:28:32 +0000555 _input_workspace.allocator()->init(input_workspace_info);
Georgios Pinitas71798372019-04-17 13:01:54 +0100556 _input_workspace.allocator()->allocate();
557 if(data_layout == DataLayout::NCHW)
558 {
559 _input_nhwc.allocator()->allocate();
560 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000561
Georgios Pinitas71798372019-04-17 13:01:54 +0100562 // 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]
563 _permute_weights.configure(weights, &_weights_hwio, weights_permutation_vector);
564 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello8f43d742019-03-27 09:28:32 +0000565
Georgios Pinitas71798372019-04-17 13:01:54 +0100566 // Configure GEMM function
567 _memory_group.manage(&_output_transformed);
Pablo Tello8f43d742019-03-27 09:28:32 +0000568 _gemm_function.configure(&_input_transformed, &_kernel_storage, nullptr, &_output_transformed, 1.0f, 0.f);
569 _input_transformed.allocator()->allocate();
Georgios Pinitas71798372019-04-17 13:01:54 +0100570
571 // Configure output transform function
572 // 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
573 if(data_layout == DataLayout::NCHW)
574 {
575 _memory_group.manage(&_output_nhwc);
576 output_to_use = &_output_nhwc;
577 }
Matthew Bentham92046462020-03-07 22:15:55 +0000578 const arm_gemm::Activation activation = arm_gemm_activation_from_acl_activation(act_info);
Pablo Tello5264b7d2019-10-21 14:25:41 +0100579
580 transform_output_kernel->configure(biases,
581 &_output_transformed,
582 output_matrix_stride,
583 output_to_use,
584 in_shape.n_batches,
585 output_shape.first,
586 output_shape.second,
587 out_channels,
588 &_output_workspace,
589 activation);
590
Georgios Pinitas71798372019-04-17 13:01:54 +0100591 const size_t output_workspace_size = transform_output_kernel->get_working_space_size(max_num_threads);
592 TensorInfo output_workspace_info(TensorShape(output_workspace_size), 1, _output->info()->data_type());
593 _output_workspace.allocator()->init(output_workspace_info);
Anthony Barbier20394d52018-08-02 11:29:09 +0100594 _output_workspace.allocator()->allocate();
Georgios Pinitas71798372019-04-17 13:01:54 +0100595 _output_transformed.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000596
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000597 // Reorder the convoluted output to ACL's ordering NCHW
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000598 if(data_layout == DataLayout::NCHW)
599 {
600 _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U));
601 _output_nhwc.allocator()->allocate();
602 }
Anthony Barbier20394d52018-08-02 11:29:09 +0100603
Pablo Tellof6c572c2018-02-14 12:47:30 +0000604 _transform_input_kernel = std::move(transform_input_kernel);
605 _transform_weights_kernel = std::move(transform_weights_kernel);
606 _transform_output_kernel = std::move(transform_output_kernel);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000607
608 //Configure Activation Layer
Matthew Bentham92046462020-03-07 22:15:55 +0000609 _is_activationlayer_enabled = act_info.enabled() && !fuse_function_supported(act_info);
Pablo Tello7282d562018-06-14 15:35:49 +0100610 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000611 {
Pablo Tello7df27862018-05-30 11:44:26 +0100612 _activationlayer_function.configure(_output, nullptr, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000613 }
Pablo Tello89519332017-11-17 11:52:36 +0000614}
615
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100616void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +0000617{
Pablo Tello7df27862018-05-30 11:44:26 +0100618 const DataLayout data_layout = _input->info()->data_layout();
619
Georgios Pinitas72219332018-06-05 14:56:06 +0100620 prepare();
621
Georgios Pinitasda953f22019-04-02 17:27:03 +0100622 MemoryGroupResourceScope scope_mg(_memory_group);
Pablo Tello679463a2018-02-06 11:47:59 +0000623
Pablo Tello7df27862018-05-30 11:44:26 +0100624 if(data_layout == DataLayout::NCHW)
625 {
626 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
627 _permute_input.run();
628 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100629
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000630 // Transform input tensor to the winograd domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000631 NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000632
Pablo Tello89519332017-11-17 11:52:36 +0000633 //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Pablo Telloa518f302018-09-19 11:33:03 +0100634 _gemm_function.run();
Georgios Pinitas71798372019-04-17 13:01:54 +0100635
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000636 // Transform output tensor to the spatial domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000637 NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000638
Pablo Tello7df27862018-05-30 11:44:26 +0100639 if(data_layout == DataLayout::NCHW)
640 {
641 // Reorder the convoluted output to ACL's ordering NCHW
642 _permute_output.run();
643 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000644
Matthew Bentham92046462020-03-07 22:15:55 +0000645 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000646 {
647 _activationlayer_function.run();
648 }
Pablo Tello89519332017-11-17 11:52:36 +0000649}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000650
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100651Status 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 +0100652 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000653{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100654 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100655 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000656
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100657 // Get indices for the width and height
658 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
659 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
660
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100661 // Input shape, kernel size and output tile
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100662 const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height));
663 const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height));
664 const DataType data_type = input->data_type();
665 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, data_type);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100666
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100667 // Check if the Winograd configuration requires fast math
668 if(!enable_fast_math)
669 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100670 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size, data_type),
671 "This Winograd configuration requires enable_fast_math=true");
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100672 }
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100673
674 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100675 kernel_size,
676 input_dims,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100677 conv_info,
678 input->data_layout());
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100679
680 // Validate input transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100681 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100682 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Pablo Tello000d33a2018-09-03 16:59:20 +0100683 // Validate filter transform
684 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
685 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
686 // Validate batched matrix multiply
687 TensorShape batched_mm_output_shape = input0.tensor_shape();
688 batched_mm_output_shape[0] = input1.tensor_shape()[0];
689 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Pablo Tello7282d562018-06-14 15:35:49 +0100690
Pablo Tello000d33a2018-09-03 16:59:20 +0100691 if(kernel_size == Size2D(3, 3))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100692 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100693 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported");
694 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported");
695 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported");
696 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported");
697 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported");
698 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported");
699 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 +0100700 return validate_kernel_3x3(input_dims, input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
701 }
702 else if(kernel_size == Size2D(5, 5))
703 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100704 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported");
705 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported");
706 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported");
707 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported");
708 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported");
709 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported");
710 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 +0100711 return validate_kernel_5x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
712 }
713 if(kernel_size == Size2D(3, 1))
714 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100715 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported");
716 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported");
717 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 +0100718 return validate_kernel_3x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
719 }
720 else if(kernel_size == Size2D(1, 3))
721 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100722 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported");
723 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported");
724 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 +0100725 return validate_kernel_1x3(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
726 }
727 else if(kernel_size == Size2D(5, 1))
728 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100729 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported");
730 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported");
731 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 +0100732 return validate_kernel_5x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
733 }
734 else if(kernel_size == Size2D(1, 5))
735 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100736 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported");
737 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported");
738 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 +0100739 return validate_kernel_1x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
740 }
741 else if(kernel_size == Size2D(7, 1))
742 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100743 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 3, "Only SAME or VALID padding supported");
744 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 3, "Only SAME or VALID padding supported");
745 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 +0100746 return validate_kernel_7x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
747 }
748 else if(kernel_size == Size2D(1, 7))
749 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100750 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 3, "Only SAME or VALID padding supported");
751 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 3, "Only SAME or VALID padding supported");
752 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 +0100753 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 +0100754 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100755 else
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100756 {
Pablo Tello000d33a2018-09-03 16:59:20 +0100757 ARM_COMPUTE_RETURN_ERROR_MSG("Kernel shape not supported");
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100758 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000759}
760
Georgios Pinitas72219332018-06-05 14:56:06 +0100761void NEWinogradConvolutionLayer::prepare()
762{
763 if(!_is_prepared)
764 {
765 // Permute weights
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000766 _weights_hwio.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100767 _permute_weights.run();
768 _weights->mark_as_unused();
769
770 // Transform weights
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000771 _kernel_storage.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100772 NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX);
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100773 _weights_hwio.allocator()->free();
Georgios Pinitasddd79f52021-01-15 09:42:26 +0000774
775 _gemm_function.prepare();
776 if(!_kernel_storage.is_used())
777 {
778 _kernel_storage.allocator()->free();
779 }
780
Georgios Pinitas72219332018-06-05 14:56:06 +0100781 _is_prepared = true;
782 }
783}
Pablo Tello89519332017-11-17 11:52:36 +0000784} // namespace arm_compute