Compute Library
 19.08
CLFFTConvolutionLayer.cpp
Go to the documentation of this file.
1 /*
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  */
25 
27 #include "arm_compute/core/Utils.h"
33 
34 namespace arm_compute
35 {
36 namespace
37 {
38 int 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
56 CLFFTConvolutionLayer::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),
66  _transform_weights_func(),
67  _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 
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  if(biases != nullptr)
123  {
124  _permute_bias_func.configure(biases, &_permuted_bias, PermutationVector(1U, 2U, 0U));
125  _permuted_bias.info()->set_data_layout(DataLayout::NCHW);
126  }
127 
128  // Permute input if needed
129  _needs_permute = input->info()->data_layout() == DataLayout::NHWC;
130  if(_needs_permute)
131  {
132  _memory_group.manage(&_permuted_input);
133  // Configure the function to transform the input tensor from NHWC -> NCHW
134  _permute_input_func.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
135  _permuted_input.info()->set_data_layout(DataLayout::NCHW);
136 
137  // Configure the function to transform the weights tensor from HWI -> IHW
138  _permute_weights_func.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
139  _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
140 
141  input_to_use = &_permuted_input;
142  weights_to_use = &_permuted_weights;
143  }
144 
145  // Flip weights
146  _flipped_weights.allocator()->init(weights_to_use->info()->clone()->set_is_resizable(true).reset_padding());
147  _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
148  _flip_weights_func.configure(weights_to_use, &_flipped_weights, &_flip_axis);
149 
150  // Pad weights
151  const PaddingList padding_w = { { 0, input_dims.x() + pad_valid.x() - 1 }, { 0, input_dims.y() + pad_valid.y() - 1 } };
152  _pad_weights_func.configure(&_flipped_weights, &_padded_weights, padding_w);
153 
154  // Transform weights
155  _transform_weights_func = support::cpp14::make_unique<CLFFT2D>();
156  _transform_weights_func->configure(&_padded_weights, &_transformed_weights, FFT2DInfo());
157 
158  // Pad input
159  const PaddingList padding_in = { { 0, kernel_size.x() + pad_valid.x() - 1 }, { 0, kernel_size.y() + pad_valid.y() - 1 } };
160  _memory_group.manage(&_padded_input);
161  _pad_input_func.configure(input_to_use, &_padded_input, padding_in);
162  if(_needs_permute)
163  {
164  _permuted_input.allocator()->allocate();
165  }
166 
167  // Transform input
168  _memory_group.manage(&_transformed_input);
169  _transform_input_func.configure(&_padded_input, &_transformed_input, FFT2DInfo());
170  _padded_input.allocator()->allocate();
171 
172  // Perform product
173  _memory_group.manage(&_output_product);
174  _prod_func.configure(&_transformed_input, &_transformed_weights, &_output_product);
175  _transformed_input.allocator()->allocate();
176 
177  // Perform reduction
178  _memory_group.manage(&_output_reduced);
179  _reduce_func.configure(&_output_product, &_output_reduced, 2, ReductionOperation::SUM);
180  _output_product.allocator()->allocate();
181 
182  // Transform output
183  _memory_group.manage(&_itransformed_output);
184  FFT2DInfo itranform_info;
185  itranform_info.direction = FFTDirection::Inverse;
186  _itransformed_output.allocator()->init(_output_reduced.info()->clone()->set_is_resizable(true).set_num_channels(1).reset_padding());
187  _itransform_output_func.configure(&_output_reduced, &_itransformed_output, itranform_info);
188  _output_reduced.allocator()->allocate();
189 
190  // Reshape output
191  TensorShape reshaped_shape = _itransformed_output.info()->tensor_shape();
192  reshaped_shape.remove_dimension(2);
193  _reshaped_output.allocator()->init(_itransformed_output.info()->clone()->set_tensor_shape(reshaped_shape));
194 
195  // Extract correct region
196  const int start_left = kernel_size.x() - conv_info.pad_left() - 1;
197  const int start_top = kernel_size.y() - conv_info.pad_top() - 1;
198  const int end_right = _reshaped_output.info()->tensor_shape().x() - (kernel_size.x() - conv_info.pad_right() - 1) - pad_valid.x();
199  const int end_botton = _reshaped_output.info()->tensor_shape().y() - (kernel_size.y() - conv_info.pad_bottom() - 1) - pad_valid.y();
200  if(_has_bias)
201  {
202  _memory_group.manage(&_bias_output);
203  }
204  else if(_needs_permute)
205  {
206  output_to_use = &_permuted_output;
207  _memory_group.manage(&_permuted_output);
208  }
209  _extract_output_func.configure(&_reshaped_output, output_to_use, Coordinates(start_left, start_top), Coordinates(end_right, end_botton));
210  _itransformed_output.allocator()->allocate();
211 
212  // Add bias
213  if(biases != nullptr)
214  {
215  output_to_use = output;
216  if(_needs_permute)
217  {
218  output_to_use = &_permuted_output;
219  _memory_group.manage(&_permuted_output);
220  }
221  auto_init_if_empty(*output_to_use->info(), *_bias_output.info());
222  _bias_add_func.configure(&_bias_output, &_permuted_bias, output_to_use, ConvertPolicy::WRAP);
223  _bias_output.allocator()->allocate();
224  }
225 
226  // Permute output
227  if(_needs_permute)
228  {
229  // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
230  _permuted_output.info()->set_data_layout(DataLayout::NCHW);
231  _permute_output_func.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
232 
233  // Allocate tensors
234  _permuted_output.allocator()->allocate();
235  }
236 
237  // Configure Activation Layer
238  _is_activationlayer_enabled = act_info.enabled();
239  if(_is_activationlayer_enabled)
240  {
241  _activation_layer_func.configure(output, nullptr, act_info);
242  }
243 
244  // Setup flip axis data
245  _flip_axis.allocator()->allocate();
246  _flip_axis.map(true);
247  auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
248  axis_data[0] = 0;
249  axis_data[1] = 1;
250  _flip_axis.unmap();
251 }
252 
255 {
258 
259  // Get indices for the width and height
260  const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
261  const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
262 
263  // Input shape, kernel size and output tile
264  const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
265 
266  // Strides
267  const auto strides = conv_info.stride();
268  ARM_COMPUTE_RETURN_ERROR_ON(strides.first != strides.second && strides.first != 1);
269  ARM_COMPUTE_RETURN_ERROR_ON(kernel_size.x() != kernel_size.y());
270  ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_left() != (kernel_size.x() / 2) || conv_info.pad_right() != (kernel_size.x() / 2));
271  ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_top() != (kernel_size.y() / 2) || conv_info.pad_bottom() != (kernel_size.y() / 2));
272 
273  // Validate biases
274  if(biases != nullptr)
275  {
276  const size_t idx_channels = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
278  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channels] != biases->tensor_shape().x());
279  }
280 
281  // Checks performed when output is configured
282  if((output != nullptr) && (output->total_size() != 0))
283  {
285  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]));
286 
287  // Validate Activation Layer
288  if(act_info.enabled())
289  {
291  }
292  }
293 
294  return Status{};
295 }
296 
298 {
299  prepare();
300 
301  MemoryGroupResourceScope scope_mg(_memory_group);
302 
303  // Transform input
304  if(_needs_permute)
305  {
306  _permute_input_func.run();
307  }
308  _pad_input_func.run();
309  _transform_input_func.run();
310 
311  // Perform operations to frequency domain
312  _prod_func.run();
313  _reduce_func.run();
314 
315  // Transform output
316  _itransform_output_func.run();
317  _reshaped_output.allocator()->import_memory(_itransformed_output.cl_buffer());
318  _extract_output_func.run();
319  // Add bias
320  if(_has_bias)
321  {
322  _bias_add_func.run();
323  }
324  if(_needs_permute)
325  {
326  _permute_output_func.run();
327  }
328 
329  // Run activation layer
330  if(_is_activationlayer_enabled)
331  {
332  _activation_layer_func.run();
333  }
334 }
335 
337 {
338  if(!_is_prepared)
339  {
340  // Permute bias to NCHW
341  if(_original_bias != nullptr)
342  {
343  _permuted_bias.allocator()->allocate();
344  _permute_bias_func.run();
345  _original_bias->mark_as_unused();
346  }
347 
348  const ICLTensor *cur_weights = _original_weights;
349  // Permute weights
350  if(_needs_permute)
351  {
352  ARM_COMPUTE_ERROR_ON(!cur_weights->is_used());
353 
354  _permuted_weights.allocator()->allocate();
355  _permute_weights_func.run();
356  cur_weights->mark_as_unused();
357  cur_weights = &_permuted_weights;
358  }
359 
360  // Flip weights
361  _flipped_weights.allocator()->allocate();
362  _flip_weights_func.run();
363  cur_weights->mark_as_unused();
364 
365  // Pad weights
366  _padded_weights.allocator()->allocate();
367  _pad_weights_func.run();
368  _flipped_weights.mark_as_unused();
369  CLScheduler::get().queue().finish();
370  _flipped_weights.allocator()->free();
371 
372  // Transform weights to frequency domain
373  _transformed_weights.allocator()->allocate();
374  _transform_weights_func->run();
375  _padded_weights.mark_as_unused();
376  CLScheduler::get().queue().finish();
377  // Delete object and release internal memory
378  _transform_weights_func.reset();
379  _padded_weights.allocator()->free();
380 
381  _is_prepared = true;
382  }
383 }
384 } // namespace arm_compute
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
Static function to check if given info will lead to a valid configuration of CLActivationLayer.
Shape of a tensor.
Definition: TensorShape.h:39
FFTDirection direction
Direction of the FFT.
void remove_dimension(size_t n)
Accessor to remove the dimension n from the tensor shape.
Definition: TensorShape.h:110
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
Definition: TensorInfo.cpp:306
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:35
void configure(const ICLTensor *input, ICLTensor *output, const FFT2DInfo &config)
Initialise the function's source, destinations and border mode.
Definition: CLFFT2D.cpp:37
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:445
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
std::vector< unsigned int > decompose_stages(unsigned int N, const std::set< unsigned int > &supported_factors)
Decompose a given 1D input size using the provided supported factors.
Definition: fft.cpp:34
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
void run() override
Run the kernels contained in the function.
const cl::Buffer & cl_buffer() const override
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
Definition: CLTensor.cpp:45
Descriptor used by the FFT2D function.
void configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
Set the input and output tensors.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:791
1 channel, 1 F32 per channel
Strides PermutationVector
Permutation vector.
Definition: Types.h:47
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
void run() override
Run the kernels contained in the function.
Definition: CLFFT2D.cpp:86
void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, ConvertPolicy policy)
Initialise the kernel's inputs, output and conversion policy.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:55
void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Set the input and output tensors.
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
Activation Layer Information class.
Definition: Types.h:1517
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2018 ARM Limited.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
Definition: Helpers.inl:201
Status import_memory(cl::Buffer buffer)
Import an existing memory as a tensor's backing memory.
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
Definition: CLTensor.cpp:60
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
uint8_t * buffer() const override
Interface to be implemented by the child class to return a pointer to CPU memory.
Definition: ICLTensor.cpp:53
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:370
void manage(TensorType *obj)
Sets a object to be managed by the given memory group.
void run() override final
Run the kernels contained in the function.
1 channel, 1 U32 per channel
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void run() override
Run the kernels contained in the function.
void configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Initialize the function.
Definition: CLPadLayer.cpp:164
Coordinates of an item.
Definition: Coordinates.h:37
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
Padding and stride information class.
Definition: Types.h:676
Num samples, channels, height, width.
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.h:102
static std::set< unsigned int > supported_radix()
Returns the radix that are support by the FFT kernel.
void configure(const ICLTensor *input, ICLTensor *output, const Coordinates &starts, const Coordinates &ends)
Configure kernel.
Definition: CLSlice.cpp:34
void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output)
Initialise the kernel's inputs, output.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:46
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Num samples, height, width, channels.
CLFFTConvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLFFTConvolutionLayer.
void free() override
Free allocated OpenCL memory.
void configure(ICLTensor *input, ICLTensor *output, ActivationLayerInfo act_info)
Set the input and output tensor.
Store the tensor's metadata.
Definition: TensorInfo.h:45
void configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm)
Set the input and output tensors.
Definition: CLPermute.cpp:33
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
void run() override
Run the kernels contained in the function.
Definition: CLPadLayer.cpp:274
void prepare() override
Prepare the function for executing.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:326
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:252
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLTensor.cpp:65
void configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *axis)
Initialize the function.
Definition: CLReverse.cpp:32
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.