blob: 41b02d03f266f5de5638cf45ded3961cf06f9472 [file] [log] [blame]
Georgios Pinitas8be91482019-03-26 17:23:28 +00001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitas8be91482019-03-26 17:23:28 +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 */
24#include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Georgios Pinitas8be91482019-03-26 17:23:28 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "arm_compute/runtime/CPP/CPPScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032#include "src/core/CL/kernels/CLFFTDigitReverseKernel.h"
33#include "src/core/CL/kernels/CLFFTRadixStageKernel.h"
34#include "src/core/CL/kernels/CLFFTScaleKernel.h"
35#include "src/core/CL/kernels/CLFillBorderKernel.h"
36#include "src/core/CL/kernels/CLPadLayerKernel.h"
37#include "src/core/CL/kernels/CLReductionOperationKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010038#include "src/core/helpers/AutoConfiguration.h"
39#include "src/core/utils/helpers/fft.h"
40
Georgios Pinitas8be91482019-03-26 17:23:28 +000041namespace arm_compute
42{
43namespace
44{
45int pad_decomposable(int N)
46{
47 const auto supported_radix = CLFFTRadixStageKernel::supported_radix();
48
49 int pad = 0;
50 bool is_decomposed = false;
51 while(!is_decomposed)
52 {
53 const auto decomposed_vector = arm_compute::helpers::fft::decompose_stages(N++, supported_radix);
54 is_decomposed = !decomposed_vector.empty();
55 if(!is_decomposed)
56 {
57 ++pad;
58 }
59 }
60 return pad;
61}
62} // namespace
63CLFFTConvolutionLayer::CLFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
64 : _memory_group(memory_manager),
65 _flip_weights_func(),
66 _permute_input_func(),
67 _permute_output_func(),
68 _permute_weights_func(),
69 _permute_bias_func(),
70 _pad_input_func(),
71 _pad_weights_func(),
72 _transform_input_func(memory_manager),
Georgios Pinitas098516b2019-04-25 18:25:06 +010073 _transform_weights_func(),
Georgios Pinitas8be91482019-03-26 17:23:28 +000074 _itransform_output_func(memory_manager),
75 _prod_func(),
76 _reduce_func(),
77 _extract_output_func(),
78 _bias_add_func(),
79 _activation_layer_func(),
80 _permuted_input(),
81 _permuted_weights(),
82 _permuted_bias(),
83 _permuted_output(),
84 _padded_input(),
85 _padded_weights(),
86 _flip_axis(),
87 _flipped_weights(),
88 _transformed_input(),
89 _transformed_weights(),
90 _input_weights_product(),
91 _output_product(),
92 _output_reduced(),
93 _itransformed_output(),
94 _reshaped_output(),
95 _bias_output(),
96 _original_weights(nullptr),
97 _original_bias(nullptr),
98 _is_activationlayer_enabled(false),
99 _needs_permute(false),
100 _has_bias(false),
101 _is_prepared(false)
102{
103}
104
105void CLFFTConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000106 const ActivationLayerInfo &act_info, bool enable_fast_math)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000107{
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000108 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, act_info, enable_fast_math);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100109}
110
111void CLFFTConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000112 const ActivationLayerInfo &act_info, bool enable_fast_math)
Manuel Bottini2b84be52020-04-08 10:15:51 +0100113{
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000114 ARM_COMPUTE_UNUSED(enable_fast_math);
115 ARM_COMPUTE_ERROR_THROW_ON(CLFFTConvolutionLayer::validate(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), conv_info, act_info, enable_fast_math));
116
Georgios Pinitas8be91482019-03-26 17:23:28 +0000117 _original_weights = weights;
118 _original_bias = biases;
119
120 // Flat if bias addition is required
121 _has_bias = biases != nullptr;
122
123 // Get indices for the width and height
124 const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
125 const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
126
127 // Input shape, kernel size and output tile
128 const Size2D input_dims = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]);
129 const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]);
130 const Size2D pad_valid = Size2D(pad_decomposable(input_dims.x() + kernel_size.x() - 1),
131 pad_decomposable(input_dims.y() + kernel_size.y() - 1));
132 // Tensors to use
133 ICLTensor *input_to_use = input;
134 const ICLTensor *weights_to_use = weights;
135 ICLTensor *output_to_use = _has_bias ? &_bias_output : output;
136
137 // Permute bias
Georgios Pinitas68c6a792019-05-15 13:24:00 +0100138 if(biases != nullptr)
139 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100140 _permute_bias_func.configure(compile_context, biases, &_permuted_bias, PermutationVector(1U, 2U, 0U));
Georgios Pinitas68c6a792019-05-15 13:24:00 +0100141 _permuted_bias.info()->set_data_layout(DataLayout::NCHW);
142 }
Georgios Pinitas8be91482019-03-26 17:23:28 +0000143
144 // Permute input if needed
145 _needs_permute = input->info()->data_layout() == DataLayout::NHWC;
146 if(_needs_permute)
147 {
148 _memory_group.manage(&_permuted_input);
149 // Configure the function to transform the input tensor from NHWC -> NCHW
Manuel Bottini2b84be52020-04-08 10:15:51 +0100150 _permute_input_func.configure(compile_context, input, &_permuted_input, PermutationVector(1U, 2U, 0U));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000151 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
152
153 // Configure the function to transform the weights tensor from HWI -> IHW
Manuel Bottini2b84be52020-04-08 10:15:51 +0100154 _permute_weights_func.configure(compile_context, weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000155 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
156
157 input_to_use = &_permuted_input;
158 weights_to_use = &_permuted_weights;
159 }
160
161 // Flip weights
162 _flipped_weights.allocator()->init(weights_to_use->info()->clone()->set_is_resizable(true).reset_padding());
163 _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100164 _flip_weights_func.configure(compile_context, weights_to_use, &_flipped_weights, &_flip_axis);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000165
166 // Pad weights
167 const PaddingList padding_w = { { 0, input_dims.x() + pad_valid.x() - 1 }, { 0, input_dims.y() + pad_valid.y() - 1 } };
Manuel Bottini2b84be52020-04-08 10:15:51 +0100168 _pad_weights_func.configure(compile_context, &_flipped_weights, &_padded_weights, padding_w);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000169
170 // Transform weights
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000171 _transform_weights_func = std::make_unique<CLFFT2D>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100172 _transform_weights_func->configure(compile_context, &_padded_weights, &_transformed_weights, FFT2DInfo());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000173
174 // Pad input
175 const PaddingList padding_in = { { 0, kernel_size.x() + pad_valid.x() - 1 }, { 0, kernel_size.y() + pad_valid.y() - 1 } };
176 _memory_group.manage(&_padded_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100177 _pad_input_func.configure(compile_context, input_to_use, &_padded_input, padding_in);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000178 if(_needs_permute)
179 {
180 _permuted_input.allocator()->allocate();
181 }
182
183 // Transform input
184 _memory_group.manage(&_transformed_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100185 _transform_input_func.configure(compile_context, &_padded_input, &_transformed_input, FFT2DInfo());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000186 _padded_input.allocator()->allocate();
187
188 // Perform product
189 _memory_group.manage(&_output_product);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100190 _prod_func.configure(compile_context, &_transformed_input, &_transformed_weights, &_output_product);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000191 _transformed_input.allocator()->allocate();
192
193 // Perform reduction
194 _memory_group.manage(&_output_reduced);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100195 _reduce_func.configure(compile_context, &_output_product, &_output_reduced, 2, ReductionOperation::SUM);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000196 _output_product.allocator()->allocate();
197
198 // Transform output
199 _memory_group.manage(&_itransformed_output);
200 FFT2DInfo itranform_info;
201 itranform_info.direction = FFTDirection::Inverse;
202 _itransformed_output.allocator()->init(_output_reduced.info()->clone()->set_is_resizable(true).set_num_channels(1).reset_padding());
Manuel Bottini2b84be52020-04-08 10:15:51 +0100203 _itransform_output_func.configure(compile_context, &_output_reduced, &_itransformed_output, itranform_info);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000204 _output_reduced.allocator()->allocate();
205
206 // Reshape output
207 TensorShape reshaped_shape = _itransformed_output.info()->tensor_shape();
208 reshaped_shape.remove_dimension(2);
209 _reshaped_output.allocator()->init(_itransformed_output.info()->clone()->set_tensor_shape(reshaped_shape));
210
211 // Extract correct region
212 const int start_left = kernel_size.x() - conv_info.pad_left() - 1;
213 const int start_top = kernel_size.y() - conv_info.pad_top() - 1;
214 const int end_right = _reshaped_output.info()->tensor_shape().x() - (kernel_size.x() - conv_info.pad_right() - 1) - pad_valid.x();
215 const int end_botton = _reshaped_output.info()->tensor_shape().y() - (kernel_size.y() - conv_info.pad_bottom() - 1) - pad_valid.y();
216 if(_has_bias)
217 {
218 _memory_group.manage(&_bias_output);
219 }
220 else if(_needs_permute)
221 {
222 output_to_use = &_permuted_output;
223 _memory_group.manage(&_permuted_output);
224 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100225 _extract_output_func.configure(compile_context, &_reshaped_output, output_to_use, Coordinates(start_left, start_top), Coordinates(end_right, end_botton));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000226 _itransformed_output.allocator()->allocate();
227
228 // Add bias
229 if(biases != nullptr)
230 {
231 output_to_use = output;
232 if(_needs_permute)
233 {
234 output_to_use = &_permuted_output;
235 _memory_group.manage(&_permuted_output);
236 }
237 auto_init_if_empty(*output_to_use->info(), *_bias_output.info());
Manuel Bottini2b84be52020-04-08 10:15:51 +0100238 _bias_add_func.configure(compile_context, &_bias_output, &_permuted_bias, output_to_use, ConvertPolicy::WRAP);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000239 _bias_output.allocator()->allocate();
240 }
241
242 // Permute output
243 if(_needs_permute)
244 {
245 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
246 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100247 _permute_output_func.configure(compile_context, &_permuted_output, output, PermutationVector(2U, 0U, 1U));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000248
249 // Allocate tensors
250 _permuted_output.allocator()->allocate();
251 }
252
253 // Configure Activation Layer
254 _is_activationlayer_enabled = act_info.enabled();
255 if(_is_activationlayer_enabled)
256 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100257 _activation_layer_func.configure(compile_context, output, nullptr, act_info);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000258 }
259
260 // Setup flip axis data
261 _flip_axis.allocator()->allocate();
262 _flip_axis.map(true);
263 auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
264 axis_data[0] = 0;
265 axis_data[1] = 1;
266 _flip_axis.unmap();
267}
268
269Status CLFFTConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000270 const ActivationLayerInfo &act_info, bool enable_fast_math)
Georgios Pinitas8be91482019-03-26 17:23:28 +0000271{
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000272 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
273 ARM_COMPUTE_RETURN_ERROR_ON((input->data_type() == DataType::F16) && !enable_fast_math);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000274 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
275
276 // Get indices for the width and height
277 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
278 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
279
280 // Input shape, kernel size and output tile
281 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
282
283 // Strides
284 const auto strides = conv_info.stride();
285 ARM_COMPUTE_RETURN_ERROR_ON(strides.first != strides.second && strides.first != 1);
286 ARM_COMPUTE_RETURN_ERROR_ON(kernel_size.x() != kernel_size.y());
287 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_left() != (kernel_size.x() / 2) || conv_info.pad_right() != (kernel_size.x() / 2));
288 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_top() != (kernel_size.y() / 2) || conv_info.pad_bottom() != (kernel_size.y() / 2));
289
290 // Validate biases
291 if(biases != nullptr)
292 {
Georgios Pinitas8be91482019-03-26 17:23:28 +0000293 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Giorgio Arenaea7de7b2020-12-10 16:49:39 +0000294 ARM_COMPUTE_RETURN_ERROR_ON(weights->tensor_shape()[3] != biases->tensor_shape().x());
Georgios Pinitas8be91482019-03-26 17:23:28 +0000295 }
296
297 // Checks performed when output is configured
298 if((output != nullptr) && (output->total_size() != 0))
299 {
300 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100301 ARM_COMPUTE_RETURN_ERROR_ON((input->tensor_shape()[idx_height] != output->tensor_shape()[idx_height]) || (input->tensor_shape()[idx_width] != output->tensor_shape()[idx_width]));
Georgios Pinitas8be91482019-03-26 17:23:28 +0000302
303 // Validate Activation Layer
304 if(act_info.enabled())
305 {
306 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
307 }
308 }
309
310 return Status{};
311}
312
313void CLFFTConvolutionLayer::run()
314{
315 prepare();
316
Georgios Pinitas098516b2019-04-25 18:25:06 +0100317 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000318
319 // Transform input
320 if(_needs_permute)
321 {
322 _permute_input_func.run();
323 }
324 _pad_input_func.run();
325 _transform_input_func.run();
326
327 // Perform operations to frequency domain
328 _prod_func.run();
329 _reduce_func.run();
330
331 // Transform output
332 _itransform_output_func.run();
333 _reshaped_output.allocator()->import_memory(_itransformed_output.cl_buffer());
334 _extract_output_func.run();
335 // Add bias
336 if(_has_bias)
337 {
338 _bias_add_func.run();
339 }
340 if(_needs_permute)
341 {
342 _permute_output_func.run();
343 }
344
345 // Run activation layer
346 if(_is_activationlayer_enabled)
347 {
348 _activation_layer_func.run();
349 }
Georgios Pinitas8be91482019-03-26 17:23:28 +0000350}
351
352void CLFFTConvolutionLayer::prepare()
353{
354 if(!_is_prepared)
355 {
356 // Permute bias to NCHW
357 if(_original_bias != nullptr)
358 {
359 _permuted_bias.allocator()->allocate();
360 _permute_bias_func.run();
361 _original_bias->mark_as_unused();
362 }
363
364 const ICLTensor *cur_weights = _original_weights;
365 // Permute weights
366 if(_needs_permute)
367 {
368 ARM_COMPUTE_ERROR_ON(!cur_weights->is_used());
369
370 _permuted_weights.allocator()->allocate();
371 _permute_weights_func.run();
372 cur_weights->mark_as_unused();
373 cur_weights = &_permuted_weights;
374 }
375
376 // Flip weights
377 _flipped_weights.allocator()->allocate();
378 _flip_weights_func.run();
379 cur_weights->mark_as_unused();
380
381 // Pad weights
382 _padded_weights.allocator()->allocate();
383 _pad_weights_func.run();
384 _flipped_weights.mark_as_unused();
385 CLScheduler::get().queue().finish();
386 _flipped_weights.allocator()->free();
387
Georgios Pinitas098516b2019-04-25 18:25:06 +0100388 // Transform weights to frequency domain
Georgios Pinitas8be91482019-03-26 17:23:28 +0000389 _transformed_weights.allocator()->allocate();
Georgios Pinitas098516b2019-04-25 18:25:06 +0100390 _transform_weights_func->run();
Georgios Pinitas8be91482019-03-26 17:23:28 +0000391 _padded_weights.mark_as_unused();
392 CLScheduler::get().queue().finish();
Georgios Pinitas098516b2019-04-25 18:25:06 +0100393 // Delete object and release internal memory
394 _transform_weights_func.reset();
Georgios Pinitas8be91482019-03-26 17:23:28 +0000395 _padded_weights.allocator()->free();
396
397 _is_prepared = true;
398 }
399}
400} // namespace arm_compute