xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/functions/CLFFTConvolutionLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2019-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/Utils.h"
28*c217d954SCole Faust #include "arm_compute/core/Validate.h"
29*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30*c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
31*c217d954SCole Faust #include "arm_compute/runtime/CPP/CPPScheduler.h"
32*c217d954SCole Faust #include "src/core/CL/kernels/CLFFTDigitReverseKernel.h"
33*c217d954SCole Faust #include "src/core/CL/kernels/CLFFTRadixStageKernel.h"
34*c217d954SCole Faust #include "src/core/CL/kernels/CLFFTScaleKernel.h"
35*c217d954SCole Faust #include "src/core/CL/kernels/CLFillBorderKernel.h"
36*c217d954SCole Faust #include "src/core/CL/kernels/CLPadLayerKernel.h"
37*c217d954SCole Faust #include "src/core/CL/kernels/CLReductionOperationKernel.h"
38*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
39*c217d954SCole Faust #include "src/core/utils/helpers/fft.h"
40*c217d954SCole Faust 
41*c217d954SCole Faust #include "src/common/utils/Log.h"
42*c217d954SCole Faust 
43*c217d954SCole Faust namespace arm_compute
44*c217d954SCole Faust {
45*c217d954SCole Faust namespace
46*c217d954SCole Faust {
pad_decomposable(int N)47*c217d954SCole Faust int pad_decomposable(int N)
48*c217d954SCole Faust {
49*c217d954SCole Faust     const auto supported_radix = CLFFTRadixStageKernel::supported_radix();
50*c217d954SCole Faust 
51*c217d954SCole Faust     int  pad           = 0;
52*c217d954SCole Faust     bool is_decomposed = false;
53*c217d954SCole Faust     while(!is_decomposed)
54*c217d954SCole Faust     {
55*c217d954SCole Faust         const auto decomposed_vector = arm_compute::helpers::fft::decompose_stages(N++, supported_radix);
56*c217d954SCole Faust         is_decomposed                = !decomposed_vector.empty();
57*c217d954SCole Faust         if(!is_decomposed)
58*c217d954SCole Faust         {
59*c217d954SCole Faust             ++pad;
60*c217d954SCole Faust         }
61*c217d954SCole Faust     }
62*c217d954SCole Faust     return pad;
63*c217d954SCole Faust }
64*c217d954SCole Faust } // namespace
CLFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)65*c217d954SCole Faust CLFFTConvolutionLayer::CLFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
66*c217d954SCole Faust     : _memory_group(memory_manager),
67*c217d954SCole Faust       _flip_weights_func(),
68*c217d954SCole Faust       _permute_input_func(),
69*c217d954SCole Faust       _permute_output_func(),
70*c217d954SCole Faust       _permute_weights_func(),
71*c217d954SCole Faust       _permute_bias_func(),
72*c217d954SCole Faust       _pad_input_func(),
73*c217d954SCole Faust       _pad_weights_func(),
74*c217d954SCole Faust       _transform_input_func(memory_manager),
75*c217d954SCole Faust       _transform_weights_func(),
76*c217d954SCole Faust       _itransform_output_func(memory_manager),
77*c217d954SCole Faust       _prod_func(),
78*c217d954SCole Faust       _reduce_func(),
79*c217d954SCole Faust       _extract_output_func(),
80*c217d954SCole Faust       _bias_add_func(),
81*c217d954SCole Faust       _activation_layer_func(),
82*c217d954SCole Faust       _permuted_input(),
83*c217d954SCole Faust       _permuted_weights(),
84*c217d954SCole Faust       _permuted_bias(),
85*c217d954SCole Faust       _permuted_output(),
86*c217d954SCole Faust       _padded_input(),
87*c217d954SCole Faust       _padded_weights(),
88*c217d954SCole Faust       _flip_axis(),
89*c217d954SCole Faust       _flipped_weights(),
90*c217d954SCole Faust       _transformed_input(),
91*c217d954SCole Faust       _transformed_weights(),
92*c217d954SCole Faust       _input_weights_product(),
93*c217d954SCole Faust       _output_product(),
94*c217d954SCole Faust       _output_reduced(),
95*c217d954SCole Faust       _itransformed_output(),
96*c217d954SCole Faust       _reshaped_output(),
97*c217d954SCole Faust       _bias_output(),
98*c217d954SCole Faust       _original_weights(nullptr),
99*c217d954SCole Faust       _original_bias(nullptr),
100*c217d954SCole Faust       _is_activationlayer_enabled(false),
101*c217d954SCole Faust       _needs_permute(false),
102*c217d954SCole Faust       _has_bias(false),
103*c217d954SCole Faust       _is_prepared(false)
104*c217d954SCole Faust {
105*c217d954SCole Faust }
106*c217d954SCole Faust 
configure(ICLTensor * input,const ICLTensor * weights,const ICLTensor * biases,ICLTensor * output,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,bool enable_fast_math)107*c217d954SCole Faust void CLFFTConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
108*c217d954SCole Faust                                       const ActivationLayerInfo &act_info, bool enable_fast_math)
109*c217d954SCole Faust {
110*c217d954SCole Faust     configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, act_info, enable_fast_math);
111*c217d954SCole Faust }
112*c217d954SCole Faust 
configure(const CLCompileContext & compile_context,ICLTensor * input,const ICLTensor * weights,const ICLTensor * biases,ICLTensor * output,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,bool enable_fast_math)113*c217d954SCole Faust void CLFFTConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
114*c217d954SCole Faust                                       const ActivationLayerInfo &act_info, bool enable_fast_math)
115*c217d954SCole Faust {
116*c217d954SCole Faust     ARM_COMPUTE_UNUSED(enable_fast_math);
117*c217d954SCole Faust     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));
118*c217d954SCole Faust     ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, act_info, enable_fast_math);
119*c217d954SCole Faust 
120*c217d954SCole Faust     _original_weights = weights;
121*c217d954SCole Faust     _original_bias    = biases;
122*c217d954SCole Faust 
123*c217d954SCole Faust     // Flat if bias addition is required
124*c217d954SCole Faust     _has_bias = biases != nullptr;
125*c217d954SCole Faust 
126*c217d954SCole Faust     // Get indices for the width and height
127*c217d954SCole Faust     const size_t idx_width  = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
128*c217d954SCole Faust     const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
129*c217d954SCole Faust 
130*c217d954SCole Faust     // Input shape, kernel size and output tile
131*c217d954SCole Faust     const Size2D input_dims  = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]);
132*c217d954SCole Faust     const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]);
133*c217d954SCole Faust     const Size2D pad_valid   = Size2D(pad_decomposable(input_dims.x() + kernel_size.x() - 1),
134*c217d954SCole Faust                                       pad_decomposable(input_dims.y() + kernel_size.y() - 1));
135*c217d954SCole Faust     // Tensors to use
136*c217d954SCole Faust     ICLTensor       *input_to_use   = input;
137*c217d954SCole Faust     const ICLTensor *weights_to_use = weights;
138*c217d954SCole Faust     ICLTensor       *output_to_use  = _has_bias ? &_bias_output : output;
139*c217d954SCole Faust 
140*c217d954SCole Faust     // Permute bias
141*c217d954SCole Faust     if(biases != nullptr)
142*c217d954SCole Faust     {
143*c217d954SCole Faust         _permute_bias_func.configure(compile_context, biases, &_permuted_bias, PermutationVector(1U, 2U, 0U));
144*c217d954SCole Faust         _permuted_bias.info()->set_data_layout(DataLayout::NCHW);
145*c217d954SCole Faust     }
146*c217d954SCole Faust 
147*c217d954SCole Faust     // Permute input if needed
148*c217d954SCole Faust     _needs_permute = input->info()->data_layout() == DataLayout::NHWC;
149*c217d954SCole Faust     if(_needs_permute)
150*c217d954SCole Faust     {
151*c217d954SCole Faust         _memory_group.manage(&_permuted_input);
152*c217d954SCole Faust         // Configure the function to transform the input tensor from NHWC -> NCHW
153*c217d954SCole Faust         _permute_input_func.configure(compile_context, input, &_permuted_input, PermutationVector(1U, 2U, 0U));
154*c217d954SCole Faust         _permuted_input.info()->set_data_layout(DataLayout::NCHW);
155*c217d954SCole Faust 
156*c217d954SCole Faust         // Configure the function to transform the weights tensor from HWI -> IHW
157*c217d954SCole Faust         _permute_weights_func.configure(compile_context, weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
158*c217d954SCole Faust         _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
159*c217d954SCole Faust 
160*c217d954SCole Faust         input_to_use   = &_permuted_input;
161*c217d954SCole Faust         weights_to_use = &_permuted_weights;
162*c217d954SCole Faust     }
163*c217d954SCole Faust 
164*c217d954SCole Faust     // Flip weights
165*c217d954SCole Faust     _flipped_weights.allocator()->init(weights_to_use->info()->clone()->set_is_resizable(true).reset_padding());
166*c217d954SCole Faust     _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
167*c217d954SCole Faust     _flip_weights_func.configure(compile_context, weights_to_use, &_flipped_weights, &_flip_axis);
168*c217d954SCole Faust 
169*c217d954SCole Faust     // Pad weights
170*c217d954SCole Faust     const PaddingList padding_w = { { 0, input_dims.x() + pad_valid.x() - 1 }, { 0, input_dims.y() + pad_valid.y() - 1 } };
171*c217d954SCole Faust     _pad_weights_func.configure(compile_context, &_flipped_weights, &_padded_weights, padding_w);
172*c217d954SCole Faust 
173*c217d954SCole Faust     // Transform weights
174*c217d954SCole Faust     _transform_weights_func = std::make_unique<CLFFT2D>();
175*c217d954SCole Faust     _transform_weights_func->configure(compile_context, &_padded_weights, &_transformed_weights, FFT2DInfo());
176*c217d954SCole Faust 
177*c217d954SCole Faust     // Pad input
178*c217d954SCole Faust     const PaddingList padding_in = { { 0, kernel_size.x() + pad_valid.x() - 1 }, { 0, kernel_size.y() + pad_valid.y() - 1 } };
179*c217d954SCole Faust     _memory_group.manage(&_padded_input);
180*c217d954SCole Faust     _pad_input_func.configure(compile_context, input_to_use, &_padded_input, padding_in);
181*c217d954SCole Faust     if(_needs_permute)
182*c217d954SCole Faust     {
183*c217d954SCole Faust         _permuted_input.allocator()->allocate();
184*c217d954SCole Faust     }
185*c217d954SCole Faust 
186*c217d954SCole Faust     // Transform input
187*c217d954SCole Faust     _memory_group.manage(&_transformed_input);
188*c217d954SCole Faust     _transform_input_func.configure(compile_context, &_padded_input, &_transformed_input, FFT2DInfo());
189*c217d954SCole Faust     _padded_input.allocator()->allocate();
190*c217d954SCole Faust 
191*c217d954SCole Faust     // Perform product
192*c217d954SCole Faust     _memory_group.manage(&_output_product);
193*c217d954SCole Faust     _prod_func.configure(compile_context, &_transformed_input, &_transformed_weights, &_output_product);
194*c217d954SCole Faust     _transformed_input.allocator()->allocate();
195*c217d954SCole Faust 
196*c217d954SCole Faust     // Perform reduction
197*c217d954SCole Faust     _memory_group.manage(&_output_reduced);
198*c217d954SCole Faust     _reduce_func.configure(compile_context, &_output_product, &_output_reduced, 2, ReductionOperation::SUM);
199*c217d954SCole Faust     _output_product.allocator()->allocate();
200*c217d954SCole Faust 
201*c217d954SCole Faust     // Transform output
202*c217d954SCole Faust     _memory_group.manage(&_itransformed_output);
203*c217d954SCole Faust     FFT2DInfo itranform_info;
204*c217d954SCole Faust     itranform_info.direction = FFTDirection::Inverse;
205*c217d954SCole Faust     _itransformed_output.allocator()->init(_output_reduced.info()->clone()->set_is_resizable(true).set_num_channels(1).reset_padding());
206*c217d954SCole Faust     _itransform_output_func.configure(compile_context, &_output_reduced, &_itransformed_output, itranform_info);
207*c217d954SCole Faust     _output_reduced.allocator()->allocate();
208*c217d954SCole Faust 
209*c217d954SCole Faust     // Reshape output
210*c217d954SCole Faust     TensorShape reshaped_shape = _itransformed_output.info()->tensor_shape();
211*c217d954SCole Faust     reshaped_shape.remove_dimension(2);
212*c217d954SCole Faust     _reshaped_output.allocator()->init(_itransformed_output.info()->clone()->set_tensor_shape(reshaped_shape));
213*c217d954SCole Faust 
214*c217d954SCole Faust     // Extract correct region
215*c217d954SCole Faust     const int start_left = kernel_size.x() - conv_info.pad_left() - 1;
216*c217d954SCole Faust     const int start_top  = kernel_size.y() - conv_info.pad_top() - 1;
217*c217d954SCole Faust     const int end_right  = _reshaped_output.info()->tensor_shape().x() - (kernel_size.x() - conv_info.pad_right() - 1) - pad_valid.x();
218*c217d954SCole Faust     const int end_botton = _reshaped_output.info()->tensor_shape().y() - (kernel_size.y() - conv_info.pad_bottom() - 1) - pad_valid.y();
219*c217d954SCole Faust     if(_has_bias)
220*c217d954SCole Faust     {
221*c217d954SCole Faust         _memory_group.manage(&_bias_output);
222*c217d954SCole Faust     }
223*c217d954SCole Faust     else if(_needs_permute)
224*c217d954SCole Faust     {
225*c217d954SCole Faust         output_to_use = &_permuted_output;
226*c217d954SCole Faust         _memory_group.manage(&_permuted_output);
227*c217d954SCole Faust     }
228*c217d954SCole Faust     _extract_output_func.configure(compile_context, &_reshaped_output, output_to_use, Coordinates(start_left, start_top), Coordinates(end_right, end_botton));
229*c217d954SCole Faust     _itransformed_output.allocator()->allocate();
230*c217d954SCole Faust 
231*c217d954SCole Faust     // Add bias
232*c217d954SCole Faust     if(biases != nullptr)
233*c217d954SCole Faust     {
234*c217d954SCole Faust         output_to_use = output;
235*c217d954SCole Faust         if(_needs_permute)
236*c217d954SCole Faust         {
237*c217d954SCole Faust             output_to_use = &_permuted_output;
238*c217d954SCole Faust             _memory_group.manage(&_permuted_output);
239*c217d954SCole Faust         }
240*c217d954SCole Faust         auto_init_if_empty(*output_to_use->info(), *_bias_output.info());
241*c217d954SCole Faust         _bias_add_func.configure(compile_context, &_bias_output, &_permuted_bias, output_to_use, ConvertPolicy::WRAP);
242*c217d954SCole Faust         _bias_output.allocator()->allocate();
243*c217d954SCole Faust     }
244*c217d954SCole Faust 
245*c217d954SCole Faust     // Permute output
246*c217d954SCole Faust     if(_needs_permute)
247*c217d954SCole Faust     {
248*c217d954SCole Faust         // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
249*c217d954SCole Faust         _permuted_output.info()->set_data_layout(DataLayout::NCHW);
250*c217d954SCole Faust         _permute_output_func.configure(compile_context, &_permuted_output, output, PermutationVector(2U, 0U, 1U));
251*c217d954SCole Faust 
252*c217d954SCole Faust         // Allocate tensors
253*c217d954SCole Faust         _permuted_output.allocator()->allocate();
254*c217d954SCole Faust     }
255*c217d954SCole Faust 
256*c217d954SCole Faust     // Configure Activation Layer
257*c217d954SCole Faust     _is_activationlayer_enabled = act_info.enabled();
258*c217d954SCole Faust     if(_is_activationlayer_enabled)
259*c217d954SCole Faust     {
260*c217d954SCole Faust         _activation_layer_func.configure(compile_context, output, nullptr, act_info);
261*c217d954SCole Faust     }
262*c217d954SCole Faust 
263*c217d954SCole Faust     // Setup flip axis data
264*c217d954SCole Faust     _flip_axis.allocator()->allocate();
265*c217d954SCole Faust     _flip_axis.map(true);
266*c217d954SCole Faust     auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
267*c217d954SCole Faust     axis_data[0]   = 0;
268*c217d954SCole Faust     axis_data[1]   = 1;
269*c217d954SCole Faust     _flip_axis.unmap();
270*c217d954SCole Faust }
271*c217d954SCole Faust 
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,bool enable_fast_math)272*c217d954SCole Faust Status CLFFTConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
273*c217d954SCole Faust                                        const ActivationLayerInfo &act_info, bool enable_fast_math)
274*c217d954SCole Faust {
275*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
276*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON((input->data_type() == DataType::F16) && !enable_fast_math);
277*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
278*c217d954SCole Faust 
279*c217d954SCole Faust     // Get indices for the width and height
280*c217d954SCole Faust     const size_t idx_width  = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
281*c217d954SCole Faust     const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
282*c217d954SCole Faust 
283*c217d954SCole Faust     // Input shape, kernel size and output tile
284*c217d954SCole Faust     const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
285*c217d954SCole Faust 
286*c217d954SCole Faust     // Strides
287*c217d954SCole Faust     const auto strides = conv_info.stride();
288*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(strides.first != strides.second && strides.first != 1);
289*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(kernel_size.x() != kernel_size.y());
290*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_left() != (kernel_size.x() / 2) || conv_info.pad_right() != (kernel_size.x() / 2));
291*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_top() != (kernel_size.y() / 2) || conv_info.pad_bottom() != (kernel_size.y() / 2));
292*c217d954SCole Faust 
293*c217d954SCole Faust     // Validate biases
294*c217d954SCole Faust     if(biases != nullptr)
295*c217d954SCole Faust     {
296*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
297*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON(weights->tensor_shape()[3] != biases->tensor_shape().x());
298*c217d954SCole Faust     }
299*c217d954SCole Faust 
300*c217d954SCole Faust     // Checks performed when output is configured
301*c217d954SCole Faust     if((output != nullptr) && (output->total_size() != 0))
302*c217d954SCole Faust     {
303*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
304*c217d954SCole Faust         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]));
305*c217d954SCole Faust 
306*c217d954SCole Faust         // Validate Activation Layer
307*c217d954SCole Faust         if(act_info.enabled())
308*c217d954SCole Faust         {
309*c217d954SCole Faust             ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
310*c217d954SCole Faust         }
311*c217d954SCole Faust     }
312*c217d954SCole Faust 
313*c217d954SCole Faust     return Status{};
314*c217d954SCole Faust }
315*c217d954SCole Faust 
run()316*c217d954SCole Faust void CLFFTConvolutionLayer::run()
317*c217d954SCole Faust {
318*c217d954SCole Faust     prepare();
319*c217d954SCole Faust 
320*c217d954SCole Faust     MemoryGroupResourceScope scope_mg(_memory_group);
321*c217d954SCole Faust 
322*c217d954SCole Faust     // Transform input
323*c217d954SCole Faust     if(_needs_permute)
324*c217d954SCole Faust     {
325*c217d954SCole Faust         _permute_input_func.run();
326*c217d954SCole Faust     }
327*c217d954SCole Faust     _pad_input_func.run();
328*c217d954SCole Faust     _transform_input_func.run();
329*c217d954SCole Faust 
330*c217d954SCole Faust     // Perform operations to frequency domain
331*c217d954SCole Faust     _prod_func.run();
332*c217d954SCole Faust     _reduce_func.run();
333*c217d954SCole Faust 
334*c217d954SCole Faust     // Transform output
335*c217d954SCole Faust     _itransform_output_func.run();
336*c217d954SCole Faust     _reshaped_output.allocator()->import_memory(_itransformed_output.cl_buffer());
337*c217d954SCole Faust     _extract_output_func.run();
338*c217d954SCole Faust     // Add bias
339*c217d954SCole Faust     if(_has_bias)
340*c217d954SCole Faust     {
341*c217d954SCole Faust         _bias_add_func.run();
342*c217d954SCole Faust     }
343*c217d954SCole Faust     if(_needs_permute)
344*c217d954SCole Faust     {
345*c217d954SCole Faust         _permute_output_func.run();
346*c217d954SCole Faust     }
347*c217d954SCole Faust 
348*c217d954SCole Faust     // Run activation layer
349*c217d954SCole Faust     if(_is_activationlayer_enabled)
350*c217d954SCole Faust     {
351*c217d954SCole Faust         _activation_layer_func.run();
352*c217d954SCole Faust     }
353*c217d954SCole Faust }
354*c217d954SCole Faust 
prepare()355*c217d954SCole Faust void CLFFTConvolutionLayer::prepare()
356*c217d954SCole Faust {
357*c217d954SCole Faust     if(!_is_prepared)
358*c217d954SCole Faust     {
359*c217d954SCole Faust         // Permute bias to NCHW
360*c217d954SCole Faust         if(_original_bias != nullptr)
361*c217d954SCole Faust         {
362*c217d954SCole Faust             _permuted_bias.allocator()->allocate();
363*c217d954SCole Faust             _permute_bias_func.run();
364*c217d954SCole Faust             _original_bias->mark_as_unused();
365*c217d954SCole Faust         }
366*c217d954SCole Faust 
367*c217d954SCole Faust         const ICLTensor *cur_weights = _original_weights;
368*c217d954SCole Faust         // Permute weights
369*c217d954SCole Faust         if(_needs_permute)
370*c217d954SCole Faust         {
371*c217d954SCole Faust             ARM_COMPUTE_ERROR_ON(!cur_weights->is_used());
372*c217d954SCole Faust 
373*c217d954SCole Faust             _permuted_weights.allocator()->allocate();
374*c217d954SCole Faust             _permute_weights_func.run();
375*c217d954SCole Faust             cur_weights->mark_as_unused();
376*c217d954SCole Faust             cur_weights = &_permuted_weights;
377*c217d954SCole Faust         }
378*c217d954SCole Faust 
379*c217d954SCole Faust         // Flip weights
380*c217d954SCole Faust         _flipped_weights.allocator()->allocate();
381*c217d954SCole Faust         _flip_weights_func.run();
382*c217d954SCole Faust         cur_weights->mark_as_unused();
383*c217d954SCole Faust 
384*c217d954SCole Faust         // Pad weights
385*c217d954SCole Faust         _padded_weights.allocator()->allocate();
386*c217d954SCole Faust         _pad_weights_func.run();
387*c217d954SCole Faust         _flipped_weights.mark_as_unused();
388*c217d954SCole Faust         CLScheduler::get().queue().finish();
389*c217d954SCole Faust         _flipped_weights.allocator()->free();
390*c217d954SCole Faust 
391*c217d954SCole Faust         // Transform weights to frequency domain
392*c217d954SCole Faust         _transformed_weights.allocator()->allocate();
393*c217d954SCole Faust         _transform_weights_func->run();
394*c217d954SCole Faust         _padded_weights.mark_as_unused();
395*c217d954SCole Faust         CLScheduler::get().queue().finish();
396*c217d954SCole Faust         // Delete object and release internal memory
397*c217d954SCole Faust         _transform_weights_func.reset();
398*c217d954SCole Faust         _padded_weights.allocator()->free();
399*c217d954SCole Faust 
400*c217d954SCole Faust         _is_prepared = true;
401*c217d954SCole Faust     }
402*c217d954SCole Faust }
403*c217d954SCole Faust } // namespace arm_compute
404