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