blob: 57950d512630072d2809635eef26075b6f6a13b2 [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"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010032#include "src/core/NEON/kernels/NEWinogradConvolutionLayerKernel.h"
Pablo Tello89519332017-11-17 11:52:36 +000033
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/NEON/kernels/convolution/common/utils.hpp"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010035#include "src/core/NEON/kernels/convolution/winograd/winograd.hpp"
Pablo Tellod6ca4782018-01-23 09:36:04 +000036
Pablo Tello89519332017-11-17 11:52:36 +000037namespace arm_compute
38{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000039namespace
40{
Pablo Tello000d33a2018-09-03 16:59:20 +010041inline Status validate_kernel_3x3(const Size2D input_dims, const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
42 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
43{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010044 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
46
47 if(input->data_type() == DataType::F32)
Pablo Tello000d33a2018-09-03 16:59:20 +010048 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010049 if(input_dims.width > 4 && input_dims.height > 4)
50 {
51 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 4, 3, 3>::validate(input, input0, winograd_info)));
52 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 4, 3, 3>::validate(weights, input1, winograd_info)));
53 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 4, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
54 }
55 else
56 {
57 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 3, 3>::validate(input, input0, winograd_info)));
58 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 3, 3>::validate(weights, input1, winograd_info)));
59 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 3, 3>::validate(batched_mm_output, biases, output, winograd_info)));
60 }
Pablo Tello000d33a2018-09-03 16:59:20 +010061 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010062#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
SiCong Li6b6a16f2020-05-28 08:55:51 +010063 else if(input->data_type() == DataType::F16)
Pablo Tello000d33a2018-09-03 16:59:20 +010064 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010065 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<__fp16, 4, 4, 3, 3>::validate(input, input0, winograd_info)));
66 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<__fp16, 4, 4, 3, 3>::validate(weights, input1, winograd_info)));
67 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 +010068 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010069#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tello000d33a2018-09-03 16:59:20 +010070
71 if(act_info.enabled())
72 {
73 NEActivationLayer::validate(output, nullptr, act_info);
74 }
75 return Status{};
76}
77
78inline Status validate_kernel_5x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
79 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
80{
81 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 2, 5, 5>::validate(input, input0, winograd_info)));
82 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 2, 5, 5>::validate(weights, input1, winograd_info)));
83 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 2, 5, 5>::validate(batched_mm_output, biases, output, winograd_info)));
84 if(act_info.enabled())
85 {
86 NEActivationLayer::validate(output, nullptr, act_info);
87 }
88 return Status{};
89}
90
91inline Status validate_kernel_3x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
92 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
93{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +010094 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +010095 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 6, 1, 3>::validate(input, input0, winograd_info)));
96 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 6, 1, 3>::validate(weights, input1, winograd_info)));
97 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 6, 1, 3>::validate(batched_mm_output, biases, output, winograd_info)));
98 if(act_info.enabled())
99 {
100 NEActivationLayer::validate(output, nullptr, act_info);
101 }
102 return Status{};
103}
104
105inline Status validate_kernel_1x3(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
106 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
107{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100108 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100109 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 6, 1, 3, 1>::validate(input, input0, winograd_info)));
110 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 6, 1, 3, 1>::validate(weights, input1, winograd_info)));
111 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 6, 1, 3, 1>::validate(batched_mm_output, biases, output, winograd_info)));
112
113 if(act_info.enabled())
114 {
115 NEActivationLayer::validate(output, nullptr, act_info);
116 }
117 return Status{};
118}
119
120inline Status validate_kernel_5x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
121 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
122{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100123 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100124 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 4, 1, 5>::validate(input, input0, winograd_info)));
125 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 4, 1, 5>::validate(weights, input1, winograd_info)));
126 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 4, 1, 5>::validate(batched_mm_output, biases, output, winograd_info)));
127 if(act_info.enabled())
128 {
129 NEActivationLayer::validate(output, nullptr, act_info);
130 }
131 return Status{};
132}
133inline Status validate_kernel_1x5(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
134 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
135{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100136 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100137 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 4, 1, 5, 1>::validate(input, input0, winograd_info)));
138 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 4, 1, 5, 1>::validate(weights, input1, winograd_info)));
139 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 4, 1, 5, 1>::validate(batched_mm_output, biases, output, winograd_info)));
140 if(act_info.enabled())
141 {
142 NEActivationLayer::validate(output, nullptr, act_info);
143 }
144 return Status{};
145}
146
147inline Status validate_kernel_7x1(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
148 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
149{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100150 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100151 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 1, 2, 1, 7>::validate(input, input0, winograd_info)));
152 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 1, 2, 1, 7>::validate(weights, input1, winograd_info)));
153 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 1, 2, 1, 7>::validate(batched_mm_output, biases, output, winograd_info)));
154 if(act_info.enabled())
155 {
156 NEActivationLayer::validate(output, nullptr, act_info);
157 }
158 return Status{};
159}
160
161inline Status validate_kernel_1x7(const ITensorInfo *input, const TensorInfo *input0, const TensorInfo *input1, const TensorInfo *batched_mm_output,
162 const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info)
163{
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100164 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Pablo Tello000d33a2018-09-03 16:59:20 +0100165 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformInputKernel<float, 2, 1, 7, 1>::validate(input, input0, winograd_info)));
166 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformWeightsKernel<float, 2, 1, 7, 1>::validate(weights, input1, winograd_info)));
167 ARM_COMPUTE_RETURN_ON_ERROR((NEWinogradLayerTransformOutputKernel<float, 2, 1, 7, 1>::validate(batched_mm_output, biases, output, winograd_info)));
168
169 if(act_info.enabled())
170 {
171 NEActivationLayer::validate(output, nullptr, act_info);
172 }
173 return Status{};
174}
175
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100176inline Tensor4DShape internal_get_input_shape(const arm_compute::ITensor *input)
177{
178 const DataLayout data_layout = input->info()->data_layout();
179 const int in_width = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH));
180 const int in_height = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT));
181 const int in_channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL));
182 const int in_batches = input->info()->dimension(3);
183
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100184 return Tensor4DShape{ in_batches, in_height, in_width, in_channels };
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100185}
186
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000187Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
188{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100189 ARM_COMPUTE_UNUSED(output);
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100190 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
191
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100192 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 +0000193 if(biases != nullptr)
194 {
195 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
196 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
197 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100198 return INEWinogradLayerTransformWeightsKernel::validate(input, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000199}
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100200
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100201Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataType data_type)
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100202{
203 Size2D output_tile = Size2D{};
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100204 if(kernel_dims == Size2D(3U, 3U))
205 {
giuros01f44fe3d2019-08-14 16:49:27 +0100206 output_tile = (input_dims.width <= 4 || input_dims.height <= 4) ? Size2D(2U, 2U) : Size2D(4U, 4U);
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100207 if(data_type == DataType::F16)
208 {
209 output_tile = Size2D(4U, 4U);
210 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100211 }
212 else if(kernel_dims == Size2D(5U, 5U))
213 {
214 output_tile = Size2D(2U, 2U);
215 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100216 else if(kernel_dims == Size2D(1U, 3U))
217 {
218 output_tile = Size2D(1U, 6U);
219 }
220 else if(kernel_dims == Size2D(3U, 1U))
221 {
222 output_tile = Size2D(6U, 1U);
223 }
Pablo Tello000d33a2018-09-03 16:59:20 +0100224 else if(kernel_dims == Size2D(1U, 5U))
225 {
226 output_tile = Size2D(1U, 4U);
227 }
228 else if(kernel_dims == Size2D(5U, 1U))
229 {
230 output_tile = Size2D(4U, 1U);
231 }
232 else if(kernel_dims == Size2D(7U, 1U))
233 {
234 output_tile = Size2D(2U, 1U);
235 }
236 else if(kernel_dims == Size2D(1U, 7U))
237 {
238 output_tile = Size2D(1U, 2U);
239 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100240 return output_tile;
241}
242
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100243bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size, DataType data_type)
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100244{
245 // Check if we want to configure a Winograd configuration which requires fast math
246 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
247
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100248 const std::vector<WinogradConfiguration> fast_math_winograd_f16 =
249 {
250 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3))
251 };
252
253 const std::vector<WinogradConfiguration> fast_math_winograd_f32 =
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100254 {
255 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(5, 5)),
256 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
257 };
258
259 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
260 std::pair<int, int>(kernel_size.width, kernel_size.height));
261
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100262 switch(data_type)
263 {
264 case DataType::F16:
265 return std::find(fast_math_winograd_f16.begin(), fast_math_winograd_f16.end(), p) != fast_math_winograd_f16.end();
266 case DataType::F32:
267 return std::find(fast_math_winograd_f32.begin(), fast_math_winograd_f32.end(), p) != fast_math_winograd_f32.end();
268 default:
269 return false;
270 }
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100271}
Pablo Tello7df27862018-05-30 11:44:26 +0100272
Pablo Tello5264b7d2019-10-21 14:25:41 +0100273inline bool fuse_function_supported(const ActivationLayerInfo &act_info)
274{
Matthew Bentham92046462020-03-07 22:15:55 +0000275 return act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU || act_info.activation() == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU;
Pablo Tello5264b7d2019-10-21 14:25:41 +0100276}
277
278arm_gemm::Activation arm_gemm_activation_from_acl_activation(const ActivationLayerInfo &act_info)
279{
Matthew Bentham92046462020-03-07 22:15:55 +0000280 switch(act_info.activation())
281 {
282 case ActivationLayerInfo::ActivationFunction::RELU:
Pablo Tello5264b7d2019-10-21 14:25:41 +0100283 {
Matthew Bentham92046462020-03-07 22:15:55 +0000284 return arm_gemm::Activation(arm_gemm::Activation::Type::ReLU, act_info.a(), act_info.b());
Pablo Tello5264b7d2019-10-21 14:25:41 +0100285 }
Matthew Bentham92046462020-03-07 22:15:55 +0000286 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
287 {
288 return arm_gemm::Activation(arm_gemm::Activation::Type::BoundedReLU, act_info.a(), act_info.b());
289 }
290 default:
291 {
292 return arm_gemm::Activation(arm_gemm::Activation::Type::None);
293 }
294 }
Pablo Tello5264b7d2019-10-21 14:25:41 +0100295}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000296} //namespace
297
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100298NEWinogradConvolutionLayer::NEWinogradConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloa518f302018-09-19 11:33:03 +0100299 : _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 +0000300 _permute_input(), _permute_weights(), _permute_output(), _input_transformed(), _output_transformed(), _input_workspace(), _output_workspace(), _kernel_storage(), _input_nhwc(), _output_nhwc(),
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000301 _weights_hwio(), _input(), _weights(), _output(), _is_prepared(false), _is_activationlayer_enabled(false), _data_layout()
Pablo Tello89519332017-11-17 11:52:36 +0000302{
Pablo Tello8f43d742019-03-27 09:28:32 +0000303}
Pablo Tello89519332017-11-17 11:52:36 +0000304
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100305void NEWinogradConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
306 bool enable_fast_math)
Pablo Tello89519332017-11-17 11:52:36 +0000307{
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000308 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Andrew Mundy4d9379a2018-03-15 16:47:03 +0000309 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 +0000310
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100311 // Get indices for the width and height
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000312 _data_layout = input->info()->data_layout();
313 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
314 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
315 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100316
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100317 const Size2D input_dims = Size2D(input->info()->dimension(width_idx), input->info()->dimension(height_idx));
318 const Size2D kernel_size = Size2D(weights->info()->dimension(width_idx), weights->info()->dimension(height_idx));
319 const DataType data_type = input->info()->data_type();
320 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, data_type);
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100321
322 // Check if the Winograd configuration requires fast math
323 if(!enable_fast_math)
324 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100325 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size, data_type),
326 "This Winograd configuration requires enable_fast_math=true");
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100327 }
328
Georgios Pinitas72219332018-06-05 14:56:06 +0100329 _weights = weights;
330 _input = input;
331 _output = output;
332 _is_prepared = false;
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100333
Georgios Pinitas9a671782021-02-25 00:04:08 +0000334 int n_gemms = 1;
335 int N_BLOCK = 1; // Size of block used by GEMM.
Michalis Spyrou2b3129e2018-04-25 18:10:13 +0100336
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100337 std::unique_ptr<INEWinogradLayerTransformInputKernel> transform_input_kernel;
338 std::unique_ptr<INEWinogradLayerTransformWeightsKernel> transform_weights_kernel;
339 std::unique_ptr<INEWinogradLayerTransformOutputKernel> transform_output_kernel;
340
341 if(data_type == DataType::F32)
Pablo Tellof6c572c2018-02-14 12:47:30 +0000342 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100343 if(kernel_size == Size2D(3, 3))
Pablo Tellof6c572c2018-02-14 12:47:30 +0000344 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100345 if(input->info()->dimension(width_idx) > 4 && input->info()->dimension(height_idx) > 4)
346 {
347 using config = NEWinogradLayerConfiguration<float, float, 4, 4, 3, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000348 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
349 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
350 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100351 n_gemms = config::WinogradBase::N_GEMMS;
352 N_BLOCK = config::WinogradConv::N_BLOCK;
353 }
354 else
355 {
356 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 3, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000357 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
358 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
359 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100360 n_gemms = config::WinogradBase::N_GEMMS;
361 N_BLOCK = config::WinogradConv::N_BLOCK;
362 }
363 }
364 else if(kernel_size == Size2D(5, 5))
365 {
366 using config = NEWinogradLayerConfiguration<float, float, 2, 2, 5, 5>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000367 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
368 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
369 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100370 n_gemms = config::WinogradBase::N_GEMMS;
371 N_BLOCK = config::WinogradConv::N_BLOCK;
372 }
373 else if(kernel_size == Size2D(1, 3))
374 {
375 using config = NEWinogradLayerConfiguration<float, float, 6, 1, 3, 1>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000376 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
377 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
378 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100379 n_gemms = config::WinogradBase::N_GEMMS;
380 N_BLOCK = config::WinogradConv::N_BLOCK;
381 }
382 else if(kernel_size == Size2D(3, 1))
383 {
384 using config = NEWinogradLayerConfiguration<float, float, 1, 6, 1, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000385 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
386 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
387 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100388 n_gemms = config::WinogradBase::N_GEMMS;
389 N_BLOCK = config::WinogradConv::N_BLOCK;
390 }
391 else if(kernel_size == Size2D(1, 5))
392 {
393 using config = NEWinogradLayerConfiguration<float, float, 4, 1, 5, 1>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000394 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
395 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
396 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100397 n_gemms = config::WinogradBase::N_GEMMS;
398 N_BLOCK = config::WinogradConv::N_BLOCK;
399 }
400 else if(kernel_size == Size2D(5, 1))
401 {
402 using config = NEWinogradLayerConfiguration<float, float, 1, 4, 1, 5>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000403 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
404 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
405 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100406 n_gemms = config::WinogradBase::N_GEMMS;
407 N_BLOCK = config::WinogradConv::N_BLOCK;
408 }
409 else if(kernel_size == Size2D(1, 7))
410 {
411 using config = NEWinogradLayerConfiguration<float, float, 2, 1, 7, 1>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000412 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
413 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
414 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100415 n_gemms = config::WinogradBase::N_GEMMS;
416 N_BLOCK = config::WinogradConv::N_BLOCK;
417 }
418 else if(kernel_size == Size2D(7, 1))
419 {
420 using config = NEWinogradLayerConfiguration<float, float, 1, 2, 1, 7>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000421 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
422 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
423 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Pablo Tello000d33a2018-09-03 16:59:20 +0100424 n_gemms = config::WinogradBase::N_GEMMS;
425 N_BLOCK = config::WinogradConv::N_BLOCK;
426 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100427 else
Pablo Tellof6c572c2018-02-14 12:47:30 +0000428 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100429 ARM_COMPUTE_ERROR("Not supported.");
430 }
431 }
432#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
433 else if(data_type == DataType::F16)
434 {
435 if(kernel_size == Size2D(3, 3))
436 {
437 using config = NEWinogradLayerConfiguration<__fp16, __fp16, 4, 4, 3, 3>;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000438 transform_input_kernel = std::make_unique<config::TransformInputKernel>();
439 transform_weights_kernel = std::make_unique<config::TransformWeightsKernel>();
440 transform_output_kernel = std::make_unique<config::TransformOutputKernel>();
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100441 n_gemms = config::WinogradBase::N_GEMMS;
442 N_BLOCK = config::WinogradConv::N_BLOCK;
Pablo Tellof6c572c2018-02-14 12:47:30 +0000443 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100444 else
445 {
446 ARM_COMPUTE_ERROR("Not supported.");
447 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000448 }
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100449#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitas9a671782021-02-25 00:04:08 +0000450 else
451 {
452 ARM_COMPUTE_ERROR("Not supported.");
453 }
Pablo Tellof6c572c2018-02-14 12:47:30 +0000454
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100455 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 +0000456 const bool use_same_padding = use_padding_type == PADDING_SAME;
457
Pablo Tello89519332017-11-17 11:52:36 +0000458 // Get convolved dimensions
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100459 const int in_channels = input->info()->dimension(channel_idx);
460 const int out_channels = output->info()->dimension(channel_idx);
Pablo Tello89519332017-11-17 11:52:36 +0000461
Pablo Tello89519332017-11-17 11:52:36 +0000462 const Tensor4DShape in_shape(internal_get_input_shape(input));
Pablo Tellod6ca4782018-01-23 09:36:04 +0000463 const size_t data_type_size = input->info()->element_size();
Pablo Tello89519332017-11-17 11:52:36 +0000464 // Get the memory required to instantiate a new Winograd operator.
Georgios Pinitas72219332018-06-05 14:56:06 +0100465 constexpr size_t storage_alignment = 64;
466
467 // Kernel Storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100468 const size_t kernel_storage_size = transform_weights_kernel->get_weight_storage_size(out_channels,
Anthony Barbiere1553372018-07-16 18:53:52 +0100469 in_channels)
Georgios Pinitas71798372019-04-17 13:01:54 +0100470 * data_type_size;
Georgios Pinitas72219332018-06-05 14:56:06 +0100471
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000472 // Input storage
Anthony Barbier578225e2018-07-16 18:00:11 +0100473 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 +0100474 use_same_padding)
Georgios Pinitas71798372019-04-17 13:01:54 +0100475 * data_type_size;
Pablo Tello89519332017-11-17 11:52:36 +0000476
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000477 // Output storage
Pablo Tello5264b7d2019-10-21 14:25:41 +0100478 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;
479 const int kernel_matrix_stride = transform_weights_kernel->get_matrix_stride(out_channels, in_channels);
480 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);
481 const auto output_shape = transform_output_kernel->get_output_shape(in_shape.n_rows, in_shape.n_cols, use_padding_type == PADDING_SAME);
482 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 +0100483
484 // Configure GEMM
Pablo Tello5264b7d2019-10-21 14:25:41 +0100485 const int tile_rows = iceildiv(output_shape.first, output_tile.height);
486 const int tile_cols = iceildiv(output_shape.second, output_tile.width);
Anthony Barbier578225e2018-07-16 18:00:11 +0100487 const int m = in_shape.n_batches * tile_rows * tile_cols;
488 const int k = in_shape.n_channels;
489 const int n = out_channels;
490 const int kernel_matrix_row_stride = roundup(out_channels, N_BLOCK);
491 const int output_matrix_row_stride = kernel_matrix_row_stride;
492
493 TensorShape a_shape(k, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100494 Strides a_strides(data_type_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100495 a_strides.set(1, a_strides[0] * k);
Anthony Barbiere1553372018-07-16 18:53:52 +0100496 //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 +0100497 a_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100498 a_strides.set(3, data_type_size * input_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100499
500 TensorShape b_shape(n, k, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100501 Strides b_strides(data_type_size);
502 b_strides.set(1, data_type_size * kernel_matrix_row_stride);
503 b_strides.set(2, data_type_size * kernel_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100504
505 TensorShape d_shape(n, m, 1, n_gemms);
Anthony Barbiere1553372018-07-16 18:53:52 +0100506 Strides d_strides(data_type_size);
507 d_strides.set(1, data_type_size * output_matrix_row_stride);
508 //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 +0100509 d_strides.set(2, 0);
Anthony Barbiere1553372018-07-16 18:53:52 +0100510 d_strides.set(3, data_type_size * output_matrix_stride);
Anthony Barbier578225e2018-07-16 18:00:11 +0100511
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100512 TensorInfo a_info{};
513 TensorInfo b_info{};
514 TensorInfo d_info{};
Anthony Barbiere1553372018-07-16 18:53:52 +0100515 a_info.init(a_shape, 1, data_type, a_strides, 0, input_storage_size);
516 b_info.init(b_shape, 1, data_type, b_strides, 0, kernel_storage_size);
517 d_info.init(d_shape, 1, data_type, d_strides, 0, output_storage_size);
Anthony Barbier578225e2018-07-16 18:00:11 +0100518
Pablo Tello8f43d742019-03-27 09:28:32 +0000519 _input_transformed.allocator()->init(a_info, storage_alignment);
Anthony Barbier578225e2018-07-16 18:00:11 +0100520 _kernel_storage.allocator()->init(b_info, storage_alignment);
Pablo Tello8f43d742019-03-27 09:28:32 +0000521 _output_transformed.allocator()->init(d_info, storage_alignment);
Pablo Tello89519332017-11-17 11:52:36 +0000522
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000523 // configure and allocate dst tensor to be used to convert from winograd domain to spatial domain when calling to reshape_output()
524 TensorInfo info(TensorShape(_output->info()->dimension(2), _output->info()->dimension(0),
525 _output->info()->dimension(1), _output->info()->dimension(3)),
526 1, _output->info()->data_type());
527 _output_nhwc.allocator()->init(info);
Pablo Tello02541fb2017-12-15 09:48:59 +0000528
Georgios Pinitas71798372019-04-17 13:01:54 +0100529 const ITensor *input_to_use = _input;
530 ITensor *output_to_use = _output;
531 PermutationVector weights_permutation_vector(3U, 0U, 1U, 2U);
532 const unsigned int max_num_threads = NEScheduler::get().num_threads();
Pablo Tellof718ce22018-10-29 13:13:23 +0000533
Georgios Pinitas71798372019-04-17 13:01:54 +0100534 // Configure the kernel to transform the input tensor from NCHW -> NHWC
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000535 if(_data_layout == DataLayout::NCHW)
Pablo Tello7df27862018-05-30 11:44:26 +0100536 {
Georgios Pinitas71798372019-04-17 13:01:54 +0100537 _memory_group.manage(&_input_nhwc);
Pablo Tello7df27862018-05-30 11:44:26 +0100538 _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U));
Georgios Pinitas71798372019-04-17 13:01:54 +0100539 input_to_use = &_input_nhwc;
540 weights_permutation_vector = PermutationVector(3U, 2U, 0U, 1U);
Pablo Tello7df27862018-05-30 11:44:26 +0100541 }
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000542
Georgios Pinitas71798372019-04-17 13:01:54 +0100543 // Configure input transform kernel
544 _memory_group.manage(&_input_transformed);
545 _memory_group.manage(&_input_workspace);
546 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,
547 &_input_transformed, input_matrix_stride, &_input_workspace);
548 const size_t input_workspace_size = transform_input_kernel->get_working_space_size(max_num_threads);
549 TensorInfo input_workspace_info(TensorShape(input_workspace_size), 1, _input->info()->data_type());
Pablo Tello8f43d742019-03-27 09:28:32 +0000550 _input_workspace.allocator()->init(input_workspace_info);
Georgios Pinitas71798372019-04-17 13:01:54 +0100551 _input_workspace.allocator()->allocate();
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000552 if(_data_layout == DataLayout::NCHW)
Georgios Pinitas71798372019-04-17 13:01:54 +0100553 {
554 _input_nhwc.allocator()->allocate();
555 }
Pablo Tello8f43d742019-03-27 09:28:32 +0000556
Georgios Pinitas71798372019-04-17 13:01:54 +0100557 // 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]
558 _permute_weights.configure(weights, &_weights_hwio, weights_permutation_vector);
559 transform_weights_kernel->configure(&_weights_hwio, &_kernel_storage, kernel_matrix_stride, out_channels, in_channels);
Pablo Tello8f43d742019-03-27 09:28:32 +0000560
Georgios Pinitas71798372019-04-17 13:01:54 +0100561 // Configure GEMM function
562 _memory_group.manage(&_output_transformed);
Pablo Tello8f43d742019-03-27 09:28:32 +0000563 _gemm_function.configure(&_input_transformed, &_kernel_storage, nullptr, &_output_transformed, 1.0f, 0.f);
564 _input_transformed.allocator()->allocate();
Georgios Pinitas71798372019-04-17 13:01:54 +0100565
566 // Configure output transform function
567 // 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
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000568 if(_data_layout == DataLayout::NCHW)
Georgios Pinitas71798372019-04-17 13:01:54 +0100569 {
570 _memory_group.manage(&_output_nhwc);
571 output_to_use = &_output_nhwc;
572 }
Matthew Bentham92046462020-03-07 22:15:55 +0000573 const arm_gemm::Activation activation = arm_gemm_activation_from_acl_activation(act_info);
Pablo Tello5264b7d2019-10-21 14:25:41 +0100574
575 transform_output_kernel->configure(biases,
576 &_output_transformed,
577 output_matrix_stride,
578 output_to_use,
579 in_shape.n_batches,
580 output_shape.first,
581 output_shape.second,
582 out_channels,
583 &_output_workspace,
584 activation);
585
Georgios Pinitas71798372019-04-17 13:01:54 +0100586 const size_t output_workspace_size = transform_output_kernel->get_working_space_size(max_num_threads);
587 TensorInfo output_workspace_info(TensorShape(output_workspace_size), 1, _output->info()->data_type());
588 _output_workspace.allocator()->init(output_workspace_info);
Anthony Barbier20394d52018-08-02 11:29:09 +0100589 _output_workspace.allocator()->allocate();
Georgios Pinitas71798372019-04-17 13:01:54 +0100590 _output_transformed.allocator()->allocate();
Pablo Tello52140b42018-01-30 14:48:11 +0000591
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000592 // Reorder the convoluted output to ACL's ordering NCHW
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000593 if(_data_layout == DataLayout::NCHW)
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000594 {
595 _permute_output.configure(&_output_nhwc, _output, PermutationVector(1U, 2U, 0U));
596 _output_nhwc.allocator()->allocate();
597 }
Anthony Barbier20394d52018-08-02 11:29:09 +0100598
Pablo Tellof6c572c2018-02-14 12:47:30 +0000599 _transform_input_kernel = std::move(transform_input_kernel);
600 _transform_weights_kernel = std::move(transform_weights_kernel);
601 _transform_output_kernel = std::move(transform_output_kernel);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000602
603 //Configure Activation Layer
Matthew Bentham92046462020-03-07 22:15:55 +0000604 _is_activationlayer_enabled = act_info.enabled() && !fuse_function_supported(act_info);
Pablo Tello7282d562018-06-14 15:35:49 +0100605 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000606 {
Pablo Tello7df27862018-05-30 11:44:26 +0100607 _activationlayer_function.configure(_output, nullptr, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000608 }
Pablo Tello89519332017-11-17 11:52:36 +0000609}
610
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100611void NEWinogradConvolutionLayer::run()
Pablo Tello89519332017-11-17 11:52:36 +0000612{
Georgios Pinitas72219332018-06-05 14:56:06 +0100613 prepare();
614
Georgios Pinitasda953f22019-04-02 17:27:03 +0100615 MemoryGroupResourceScope scope_mg(_memory_group);
Pablo Tello679463a2018-02-06 11:47:59 +0000616
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000617 if(_data_layout == DataLayout::NCHW)
Pablo Tello7df27862018-05-30 11:44:26 +0100618 {
619 //Bring channels to the front as Winograd code expects the tensor to be in the format NHWC
620 _permute_input.run();
621 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100622
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000623 // Transform input tensor to the winograd domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000624 NEScheduler::get().schedule(_transform_input_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000625
Pablo Tello89519332017-11-17 11:52:36 +0000626 //Run 16 GEMMs in multiple threads, each kernel runs one or more GEMMs
Pablo Telloa518f302018-09-19 11:33:03 +0100627 _gemm_function.run();
Georgios Pinitas71798372019-04-17 13:01:54 +0100628
Pablo Tello9ceebbe2018-01-10 16:44:13 +0000629 // Transform output tensor to the spatial domain
Pablo Tellof6c572c2018-02-14 12:47:30 +0000630 NEScheduler::get().schedule(_transform_output_kernel.get(), Window::DimX);
Pablo Tellod6ca4782018-01-23 09:36:04 +0000631
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000632 if(_data_layout == DataLayout::NCHW)
Pablo Tello7df27862018-05-30 11:44:26 +0100633 {
634 // Reorder the convoluted output to ACL's ordering NCHW
635 _permute_output.run();
636 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000637
Matthew Bentham92046462020-03-07 22:15:55 +0000638 if(_is_activationlayer_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000639 {
640 _activationlayer_function.run();
641 }
Pablo Tello89519332017-11-17 11:52:36 +0000642}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000643
Georgios Pinitas9fb11592018-04-26 20:34:58 +0100644Status 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 +0100645 const ActivationLayerInfo &act_info, bool enable_fast_math)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000646{
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100647 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100648 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000649
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100650 // Get indices for the width and height
651 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
652 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
653
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100654 // Input shape, kernel size and output tile
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100655 const Size2D input_dims = Size2D(input->dimension(idx_width), input->dimension(idx_height));
656 const Size2D kernel_size = Size2D(weights->dimension(idx_width), weights->dimension(idx_height));
657 const DataType data_type = input->data_type();
658 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, data_type);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100659
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100660 // Check if the Winograd configuration requires fast math
661 if(!enable_fast_math)
662 {
Georgios Pinitas5ce897f2020-04-29 11:44:10 +0100663 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size, data_type),
664 "This Winograd configuration requires enable_fast_math=true");
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100665 }
Vidhya Sudhan Loganathancb0010b2018-05-11 16:23:53 +0100666
667 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Giorgio Arenaa3221e62018-05-03 15:57:48 +0100668 kernel_size,
669 input_dims,
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100670 conv_info,
671 input->data_layout());
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100672
673 // Validate input transform
Vidhya Sudhan Loganathan84ce1f92018-04-25 13:00:09 +0100674 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100675 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Pablo Tello000d33a2018-09-03 16:59:20 +0100676 // Validate filter transform
677 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
678 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
679 // Validate batched matrix multiply
680 TensorShape batched_mm_output_shape = input0.tensor_shape();
681 batched_mm_output_shape[0] = input1.tensor_shape()[0];
682 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Pablo Tello7282d562018-06-14 15:35:49 +0100683
Pablo Tello000d33a2018-09-03 16:59:20 +0100684 if(kernel_size == Size2D(3, 3))
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100685 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100686 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported");
687 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported");
688 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported");
689 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported");
690 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported");
691 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported");
692 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 +0100693 return validate_kernel_3x3(input_dims, input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
694 }
695 else if(kernel_size == Size2D(5, 5))
696 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100697 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported");
698 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported");
699 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported");
700 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported");
701 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != conv_info.pad_left(), "Only SAME or VALID padding supported");
702 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != conv_info.pad_bottom(), "Only SAME or VALID padding supported");
703 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 +0100704 return validate_kernel_5x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
705 }
706 if(kernel_size == Size2D(3, 1))
707 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100708 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 1, "Only SAME or VALID padding supported");
709 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 1, "Only SAME or VALID padding supported");
710 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 +0100711 return validate_kernel_3x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
712 }
713 else if(kernel_size == Size2D(1, 3))
714 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100715 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 1, "Only SAME or VALID padding supported");
716 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 1, "Only SAME or VALID padding supported");
717 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 +0100718 return validate_kernel_1x3(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
719 }
720 else if(kernel_size == Size2D(5, 1))
721 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100722 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 2, "Only SAME or VALID padding supported");
723 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 2, "Only SAME or VALID padding supported");
724 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 +0100725 return validate_kernel_5x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
726 }
727 else if(kernel_size == Size2D(1, 5))
728 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100729 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 2, "Only SAME or VALID padding supported");
730 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 2, "Only SAME or VALID padding supported");
731 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 +0100732 return validate_kernel_1x5(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
733 }
734 else if(kernel_size == Size2D(7, 1))
735 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100736 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_left() != 0u && conv_info.pad_left() != 3, "Only SAME or VALID padding supported");
737 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_right() != 0u && conv_info.pad_right() != 3, "Only SAME or VALID padding supported");
738 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 +0100739 return validate_kernel_7x1(input, &input0, &input1, &batched_mm_output, weights, biases, output, winograd_info, act_info);
740 }
741 else if(kernel_size == Size2D(1, 7))
742 {
Pablo Tellofe4b05f2018-09-24 16:28:25 +0100743 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_top() != 0u && conv_info.pad_top() != 3, "Only SAME or VALID padding supported");
744 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv_info.pad_bottom() != 0u && conv_info.pad_bottom() != 3, "Only SAME or VALID padding supported");
745 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 +0100746 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 +0100747 }
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100748 else
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100749 {
Pablo Tello000d33a2018-09-03 16:59:20 +0100750 ARM_COMPUTE_RETURN_ERROR_MSG("Kernel shape not supported");
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100751 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000752}
753
Georgios Pinitas72219332018-06-05 14:56:06 +0100754void NEWinogradConvolutionLayer::prepare()
755{
756 if(!_is_prepared)
757 {
758 // Permute weights
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000759 _weights_hwio.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100760 _permute_weights.run();
761 _weights->mark_as_unused();
762
763 // Transform weights
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000764 _kernel_storage.allocator()->allocate();
Georgios Pinitas72219332018-06-05 14:56:06 +0100765 NEScheduler::get().schedule(_transform_weights_kernel.get(), Window::DimX);
Pablo Tellobda6e4b2018-08-22 11:40:33 +0100766 _weights_hwio.allocator()->free();
Georgios Pinitasddd79f52021-01-15 09:42:26 +0000767
768 _gemm_function.prepare();
769 if(!_kernel_storage.is_used())
770 {
771 _kernel_storage.allocator()->free();
772 }
773
Georgios Pinitas72219332018-06-05 14:56:06 +0100774 _is_prepared = true;
775 }
776}
Pablo Tello89519332017-11-17 11:52:36 +0000777} // namespace arm_compute