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/NEON/functions/NEFFTConvolutionLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/ITensor.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 "src/common/utils/Log.h"
31*c217d954SCole Faust #include "src/core/NEON/kernels/NEFFTDigitReverseKernel.h"
32*c217d954SCole Faust #include "src/core/NEON/kernels/NEFFTRadixStageKernel.h"
33*c217d954SCole Faust #include "src/core/NEON/kernels/NEFFTScaleKernel.h"
34*c217d954SCole Faust #include "src/core/NEON/kernels/NEPadLayerKernel.h"
35*c217d954SCole Faust #include "src/core/NEON/kernels/NEReductionOperationKernel.h"
36*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
37*c217d954SCole Faust #include "src/core/utils/helpers/fft.h"
38*c217d954SCole Faust
39*c217d954SCole Faust namespace arm_compute
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace
42*c217d954SCole Faust {
pad_decomposable(int N)43*c217d954SCole Faust int pad_decomposable(int N)
44*c217d954SCole Faust {
45*c217d954SCole Faust const auto supported_radix = NEFFTRadixStageKernel::supported_radix();
46*c217d954SCole Faust
47*c217d954SCole Faust int pad = 0;
48*c217d954SCole Faust bool is_decomposed = false;
49*c217d954SCole Faust while(!is_decomposed)
50*c217d954SCole Faust {
51*c217d954SCole Faust const auto decomposed_vector = arm_compute::helpers::fft::decompose_stages(N++, supported_radix);
52*c217d954SCole Faust is_decomposed = !decomposed_vector.empty();
53*c217d954SCole Faust if(!is_decomposed)
54*c217d954SCole Faust {
55*c217d954SCole Faust ++pad;
56*c217d954SCole Faust }
57*c217d954SCole Faust }
58*c217d954SCole Faust return pad;
59*c217d954SCole Faust }
60*c217d954SCole Faust } // namespace
61*c217d954SCole Faust
NEFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)62*c217d954SCole Faust NEFFTConvolutionLayer::NEFFTConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
63*c217d954SCole Faust : _memory_group(memory_manager),
64*c217d954SCole Faust _flip_weights_func(),
65*c217d954SCole Faust _permute_input_func(),
66*c217d954SCole Faust _permute_output_func(),
67*c217d954SCole Faust _permute_weights_func(),
68*c217d954SCole Faust _permute_bias_func(),
69*c217d954SCole Faust _pad_input_func(),
70*c217d954SCole Faust _pad_weights_func(),
71*c217d954SCole Faust _transform_input_func(memory_manager),
72*c217d954SCole Faust _transform_weights_func(),
73*c217d954SCole Faust _itransform_output_func(memory_manager),
74*c217d954SCole Faust _prod_func(),
75*c217d954SCole Faust _reduce_func(),
76*c217d954SCole Faust _extract_output_func(),
77*c217d954SCole Faust _bias_add_func(),
78*c217d954SCole Faust _activation_layer_func(),
79*c217d954SCole Faust _permuted_input(),
80*c217d954SCole Faust _permuted_weights(),
81*c217d954SCole Faust _permuted_bias(),
82*c217d954SCole Faust _permuted_output(),
83*c217d954SCole Faust _padded_input(),
84*c217d954SCole Faust _padded_weights(),
85*c217d954SCole Faust _flip_axis(),
86*c217d954SCole Faust _flipped_weights(),
87*c217d954SCole Faust _transformed_input(),
88*c217d954SCole Faust _transformed_weights(),
89*c217d954SCole Faust _input_weights_product(),
90*c217d954SCole Faust _output_product(),
91*c217d954SCole Faust _output_reduced(),
92*c217d954SCole Faust _itransformed_output(),
93*c217d954SCole Faust _reshaped_output(),
94*c217d954SCole Faust _bias_output(),
95*c217d954SCole Faust _original_weights(nullptr),
96*c217d954SCole Faust _original_bias(nullptr),
97*c217d954SCole Faust _is_activationlayer_enabled(false),
98*c217d954SCole Faust _needs_permute(false),
99*c217d954SCole Faust _has_bias(false),
100*c217d954SCole Faust _is_prepared(false)
101*c217d954SCole Faust {
102*c217d954SCole Faust }
103*c217d954SCole Faust NEFFTConvolutionLayer::~NEFFTConvolutionLayer() = default;
104*c217d954SCole Faust
configure(ITensor * input,const ITensor * weights,const ITensor * biases,ITensor * output,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,bool enable_fast_math)105*c217d954SCole Faust void NEFFTConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
106*c217d954SCole Faust const ActivationLayerInfo &act_info, bool enable_fast_math)
107*c217d954SCole Faust {
108*c217d954SCole Faust ARM_COMPUTE_UNUSED(enable_fast_math);
109*c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, act_info, enable_fast_math);
110*c217d954SCole Faust
111*c217d954SCole Faust _original_weights = weights;
112*c217d954SCole Faust _original_bias = biases;
113*c217d954SCole Faust
114*c217d954SCole Faust // Flat if bias addition is required
115*c217d954SCole Faust _has_bias = biases != nullptr;
116*c217d954SCole Faust
117*c217d954SCole Faust // Get indices for the width and height
118*c217d954SCole Faust const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
119*c217d954SCole Faust const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
120*c217d954SCole Faust
121*c217d954SCole Faust // Input shape, kernel size and output tile
122*c217d954SCole Faust const Size2D input_dims = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]);
123*c217d954SCole Faust const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]);
124*c217d954SCole Faust const Size2D pad_valid = Size2D(pad_decomposable(input_dims.x() + kernel_size.x() - 1),
125*c217d954SCole Faust pad_decomposable(input_dims.y() + kernel_size.y() - 1));
126*c217d954SCole Faust // Tensors to use
127*c217d954SCole Faust ITensor *input_to_use = input;
128*c217d954SCole Faust const ITensor *weights_to_use = weights;
129*c217d954SCole Faust ITensor *output_to_use = _has_bias ? &_bias_output : output;
130*c217d954SCole Faust
131*c217d954SCole Faust // Permute bias
132*c217d954SCole Faust if(biases != nullptr)
133*c217d954SCole Faust {
134*c217d954SCole Faust _permute_bias_func.configure(biases, &_permuted_bias, PermutationVector(1U, 2U, 0U));
135*c217d954SCole Faust _permuted_bias.info()->set_data_layout(DataLayout::NCHW);
136*c217d954SCole Faust }
137*c217d954SCole Faust
138*c217d954SCole Faust // Permute input if needed
139*c217d954SCole Faust _needs_permute = input->info()->data_layout() == DataLayout::NHWC;
140*c217d954SCole Faust if(_needs_permute)
141*c217d954SCole Faust {
142*c217d954SCole Faust _memory_group.manage(&_permuted_input);
143*c217d954SCole Faust // Configure the function to transform the input tensor from NHWC -> NCHW
144*c217d954SCole Faust _permute_input_func.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
145*c217d954SCole Faust _permuted_input.info()->set_data_layout(DataLayout::NCHW);
146*c217d954SCole Faust
147*c217d954SCole Faust // Configure the function to transform the weights tensor from HWI -> IHW
148*c217d954SCole Faust _permute_weights_func.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
149*c217d954SCole Faust _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
150*c217d954SCole Faust
151*c217d954SCole Faust input_to_use = &_permuted_input;
152*c217d954SCole Faust weights_to_use = &_permuted_weights;
153*c217d954SCole Faust }
154*c217d954SCole Faust
155*c217d954SCole Faust // Flip weights
156*c217d954SCole Faust _flipped_weights.allocator()->init(weights_to_use->info()->clone()->set_is_resizable(true).reset_padding());
157*c217d954SCole Faust _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
158*c217d954SCole Faust _flip_weights_func.configure(weights_to_use, &_flipped_weights, &_flip_axis);
159*c217d954SCole Faust
160*c217d954SCole Faust // Pad weights
161*c217d954SCole Faust const PaddingList padding_w = { { 0, input_dims.x() + pad_valid.x() - 1 }, { 0, input_dims.y() + pad_valid.y() - 1 } };
162*c217d954SCole Faust _pad_weights_func.configure(&_flipped_weights, &_padded_weights, padding_w);
163*c217d954SCole Faust
164*c217d954SCole Faust // Transform weights
165*c217d954SCole Faust _transform_weights_func = std::make_unique<NEFFT2D>();
166*c217d954SCole Faust _transform_weights_func->configure(&_padded_weights, &_transformed_weights, FFT2DInfo());
167*c217d954SCole Faust
168*c217d954SCole Faust // Pad input
169*c217d954SCole Faust const PaddingList padding_in = { { 0, kernel_size.x() + pad_valid.x() - 1 }, { 0, kernel_size.y() + pad_valid.y() - 1 } };
170*c217d954SCole Faust _memory_group.manage(&_padded_input);
171*c217d954SCole Faust _pad_input_func.configure(input_to_use, &_padded_input, padding_in);
172*c217d954SCole Faust if(_needs_permute)
173*c217d954SCole Faust {
174*c217d954SCole Faust _permuted_input.allocator()->allocate();
175*c217d954SCole Faust }
176*c217d954SCole Faust
177*c217d954SCole Faust // Transform input
178*c217d954SCole Faust _memory_group.manage(&_transformed_input);
179*c217d954SCole Faust _transform_input_func.configure(&_padded_input, &_transformed_input, FFT2DInfo());
180*c217d954SCole Faust _padded_input.allocator()->allocate();
181*c217d954SCole Faust
182*c217d954SCole Faust // Perform product
183*c217d954SCole Faust _memory_group.manage(&_output_product);
184*c217d954SCole Faust _prod_func.configure(&_transformed_input, &_transformed_weights, &_output_product);
185*c217d954SCole Faust _transformed_input.allocator()->allocate();
186*c217d954SCole Faust
187*c217d954SCole Faust // Perform reduction
188*c217d954SCole Faust _memory_group.manage(&_output_reduced);
189*c217d954SCole Faust _reduce_func.configure(&_output_product, &_output_reduced, 2, ReductionOperation::SUM);
190*c217d954SCole Faust _output_product.allocator()->allocate();
191*c217d954SCole Faust
192*c217d954SCole Faust // Transform output
193*c217d954SCole Faust _memory_group.manage(&_itransformed_output);
194*c217d954SCole Faust FFT2DInfo itranform_info;
195*c217d954SCole Faust itranform_info.direction = FFTDirection::Inverse;
196*c217d954SCole Faust _itransformed_output.allocator()->init(_output_reduced.info()->clone()->set_is_resizable(true).set_num_channels(1).reset_padding());
197*c217d954SCole Faust _itransform_output_func.configure(&_output_reduced, &_itransformed_output, itranform_info);
198*c217d954SCole Faust _output_reduced.allocator()->allocate();
199*c217d954SCole Faust
200*c217d954SCole Faust // Reshape output
201*c217d954SCole Faust TensorShape reshaped_shape = _itransformed_output.info()->tensor_shape();
202*c217d954SCole Faust reshaped_shape.remove_dimension(2);
203*c217d954SCole Faust _reshaped_output.allocator()->init(_itransformed_output.info()->clone()->set_tensor_shape(reshaped_shape));
204*c217d954SCole Faust
205*c217d954SCole Faust // Extract correct region
206*c217d954SCole Faust const int start_left = kernel_size.x() - conv_info.pad_left() - 1;
207*c217d954SCole Faust const int start_top = kernel_size.y() - conv_info.pad_top() - 1;
208*c217d954SCole Faust const int end_right = _reshaped_output.info()->tensor_shape().x() - (kernel_size.x() - conv_info.pad_right() - 1) - pad_valid.x();
209*c217d954SCole Faust const int end_botton = _reshaped_output.info()->tensor_shape().y() - (kernel_size.y() - conv_info.pad_bottom() - 1) - pad_valid.y();
210*c217d954SCole Faust if(_has_bias)
211*c217d954SCole Faust {
212*c217d954SCole Faust _memory_group.manage(&_bias_output);
213*c217d954SCole Faust }
214*c217d954SCole Faust else if(_needs_permute)
215*c217d954SCole Faust {
216*c217d954SCole Faust output_to_use = &_permuted_output;
217*c217d954SCole Faust _memory_group.manage(&_permuted_output);
218*c217d954SCole Faust }
219*c217d954SCole Faust _extract_output_func.configure(&_reshaped_output, output_to_use, Coordinates(start_left, start_top), Coordinates(end_right, end_botton));
220*c217d954SCole Faust _reshaped_output.allocator()->allocate();
221*c217d954SCole Faust _itransformed_output.allocator()->allocate();
222*c217d954SCole Faust
223*c217d954SCole Faust // Add bias
224*c217d954SCole Faust if(biases != nullptr)
225*c217d954SCole Faust {
226*c217d954SCole Faust output_to_use = output;
227*c217d954SCole Faust if(_needs_permute)
228*c217d954SCole Faust {
229*c217d954SCole Faust output_to_use = &_permuted_output;
230*c217d954SCole Faust _memory_group.manage(&_permuted_output);
231*c217d954SCole Faust }
232*c217d954SCole Faust auto_init_if_empty(*output_to_use->info(), *_bias_output.info());
233*c217d954SCole Faust _bias_add_func.configure(&_bias_output, &_permuted_bias, output_to_use, ConvertPolicy::WRAP);
234*c217d954SCole Faust _bias_output.allocator()->allocate();
235*c217d954SCole Faust }
236*c217d954SCole Faust
237*c217d954SCole Faust // Permute output
238*c217d954SCole Faust if(_needs_permute)
239*c217d954SCole Faust {
240*c217d954SCole Faust // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
241*c217d954SCole Faust _permuted_output.info()->set_data_layout(DataLayout::NCHW);
242*c217d954SCole Faust _permute_output_func.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
243*c217d954SCole Faust
244*c217d954SCole Faust // Allocate tensors
245*c217d954SCole Faust _permuted_output.allocator()->allocate();
246*c217d954SCole Faust }
247*c217d954SCole Faust
248*c217d954SCole Faust // Configure Activation Layer
249*c217d954SCole Faust _is_activationlayer_enabled = act_info.enabled();
250*c217d954SCole Faust if(_is_activationlayer_enabled)
251*c217d954SCole Faust {
252*c217d954SCole Faust _activation_layer_func.configure(output, nullptr, act_info);
253*c217d954SCole Faust }
254*c217d954SCole Faust
255*c217d954SCole Faust // Setup flip axis data
256*c217d954SCole Faust _flip_axis.allocator()->allocate();
257*c217d954SCole Faust
258*c217d954SCole Faust auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
259*c217d954SCole Faust axis_data[0] = 0;
260*c217d954SCole Faust axis_data[1] = 1;
261*c217d954SCole Faust }
262*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)263*c217d954SCole Faust Status NEFFTConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
264*c217d954SCole Faust const ActivationLayerInfo &act_info, bool enable_fast_math)
265*c217d954SCole Faust {
266*c217d954SCole Faust ARM_COMPUTE_UNUSED(enable_fast_math);
267*c217d954SCole Faust
268*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
269*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
270*c217d954SCole Faust
271*c217d954SCole Faust // Get indices for the width and height
272*c217d954SCole Faust const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
273*c217d954SCole Faust const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
274*c217d954SCole Faust
275*c217d954SCole Faust // Input shape, kernel size and output tile
276*c217d954SCole Faust const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
277*c217d954SCole Faust
278*c217d954SCole Faust // Strides
279*c217d954SCole Faust const auto strides = conv_info.stride();
280*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(strides.first != strides.second && strides.first != 1);
281*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(kernel_size.x() != kernel_size.y());
282*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_left() != (kernel_size.x() / 2) || conv_info.pad_right() != (kernel_size.x() / 2));
283*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_top() != (kernel_size.y() / 2) || conv_info.pad_bottom() != (kernel_size.y() / 2));
284*c217d954SCole Faust
285*c217d954SCole Faust // Validate biases
286*c217d954SCole Faust if(biases != nullptr)
287*c217d954SCole Faust {
288*c217d954SCole Faust const size_t idx_channels = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
289*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
290*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channels] != biases->tensor_shape().x());
291*c217d954SCole Faust }
292*c217d954SCole Faust
293*c217d954SCole Faust // Checks performed when output is configured
294*c217d954SCole Faust if((output != nullptr) && (output->total_size() != 0))
295*c217d954SCole Faust {
296*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
297*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]));
298*c217d954SCole Faust
299*c217d954SCole Faust // Validate Activation Layer
300*c217d954SCole Faust if(act_info.enabled())
301*c217d954SCole Faust {
302*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
303*c217d954SCole Faust }
304*c217d954SCole Faust }
305*c217d954SCole Faust
306*c217d954SCole Faust return Status{};
307*c217d954SCole Faust }
308*c217d954SCole Faust
run()309*c217d954SCole Faust void NEFFTConvolutionLayer::run()
310*c217d954SCole Faust {
311*c217d954SCole Faust prepare();
312*c217d954SCole Faust
313*c217d954SCole Faust MemoryGroupResourceScope scope_mg(_memory_group);
314*c217d954SCole Faust
315*c217d954SCole Faust // Transform input
316*c217d954SCole Faust if(_needs_permute)
317*c217d954SCole Faust {
318*c217d954SCole Faust _permute_input_func.run();
319*c217d954SCole Faust }
320*c217d954SCole Faust _pad_input_func.run();
321*c217d954SCole Faust _transform_input_func.run();
322*c217d954SCole Faust
323*c217d954SCole Faust // Perform operations to frequency domain
324*c217d954SCole Faust _prod_func.run();
325*c217d954SCole Faust
326*c217d954SCole Faust _reduce_func.run();
327*c217d954SCole Faust
328*c217d954SCole Faust // Transform output
329*c217d954SCole Faust _itransform_output_func.run();
330*c217d954SCole Faust _reshaped_output.allocator()->import_memory(_itransformed_output.buffer());
331*c217d954SCole Faust _extract_output_func.run();
332*c217d954SCole Faust
333*c217d954SCole Faust // Add bias
334*c217d954SCole Faust if(_has_bias)
335*c217d954SCole Faust {
336*c217d954SCole Faust _bias_add_func.run();
337*c217d954SCole Faust }
338*c217d954SCole Faust if(_needs_permute)
339*c217d954SCole Faust {
340*c217d954SCole Faust _permute_output_func.run();
341*c217d954SCole Faust }
342*c217d954SCole Faust
343*c217d954SCole Faust // Run activation layer
344*c217d954SCole Faust if(_is_activationlayer_enabled)
345*c217d954SCole Faust {
346*c217d954SCole Faust _activation_layer_func.run();
347*c217d954SCole Faust }
348*c217d954SCole Faust }
349*c217d954SCole Faust
prepare()350*c217d954SCole Faust void NEFFTConvolutionLayer::prepare()
351*c217d954SCole Faust {
352*c217d954SCole Faust if(!_is_prepared)
353*c217d954SCole Faust {
354*c217d954SCole Faust // Permute bias to NCHW
355*c217d954SCole Faust if(_original_bias != nullptr)
356*c217d954SCole Faust {
357*c217d954SCole Faust _permuted_bias.allocator()->allocate();
358*c217d954SCole Faust _permute_bias_func.run();
359*c217d954SCole Faust _original_bias->mark_as_unused();
360*c217d954SCole Faust }
361*c217d954SCole Faust
362*c217d954SCole Faust const ITensor *cur_weights = _original_weights;
363*c217d954SCole Faust
364*c217d954SCole Faust // Permute weights
365*c217d954SCole Faust if(_needs_permute)
366*c217d954SCole Faust {
367*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!cur_weights->is_used());
368*c217d954SCole Faust
369*c217d954SCole Faust _permuted_weights.allocator()->allocate();
370*c217d954SCole Faust _permute_weights_func.run();
371*c217d954SCole Faust cur_weights->mark_as_unused();
372*c217d954SCole Faust cur_weights = &_permuted_weights;
373*c217d954SCole Faust }
374*c217d954SCole Faust
375*c217d954SCole Faust // Flip weights
376*c217d954SCole Faust _flipped_weights.allocator()->allocate();
377*c217d954SCole Faust _flip_weights_func.run();
378*c217d954SCole Faust cur_weights->mark_as_unused();
379*c217d954SCole Faust
380*c217d954SCole Faust // Pad weights
381*c217d954SCole Faust _padded_weights.allocator()->allocate();
382*c217d954SCole Faust _pad_weights_func.run();
383*c217d954SCole Faust _flipped_weights.mark_as_unused();
384*c217d954SCole Faust _flipped_weights.allocator()->free();
385*c217d954SCole Faust
386*c217d954SCole Faust // Transform weights to frequency domain
387*c217d954SCole Faust _transformed_weights.allocator()->allocate();
388*c217d954SCole Faust _transform_weights_func->run();
389*c217d954SCole Faust _transform_weights_func.reset();
390*c217d954SCole Faust
391*c217d954SCole Faust _padded_weights.mark_as_unused();
392*c217d954SCole Faust _padded_weights.allocator()->free();
393*c217d954SCole Faust
394*c217d954SCole Faust _is_prepared = true;
395*c217d954SCole Faust }
396*c217d954SCole Faust }
397*c217d954SCole Faust } // namespace arm_compute
398