Compute Library
 19.11
CLDirectDeconvolutionLayer.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"
31 
32 #include <memory>
33 #include <tuple>
34 
35 namespace arm_compute
36 {
38 
39 CLDirectDeconvolutionLayer::CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
40  : _memory_group(std::move(memory_manager)),
41  _scale_f(),
42  _conv_f(),
43  _flip_weights(),
44  _scaled_output(),
45  _original_weights(nullptr),
46  _weights_flipped(),
47  _flip_axis(),
48  _is_prepared(false)
49 {
50 }
51 
54 {
58  const DataLayout data_layout = input->data_layout();
59 
63 
64  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != weights->dimension(idx_h));
65  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1);
66 
67  auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), info);
68 
70 
72 
73  if(bias != nullptr)
74  {
76  {
78  }
79  else
80  {
82  }
84  }
85 
86  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w], "Output's width is invalid.");
87  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h], "Output's height is invalid.");
88  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c], "Output's depth is invalid.");
89 
90  unsigned int deconv_pad_x = 0;
91  unsigned int deconv_pad_y = 0;
92  const unsigned int stride_x = info.stride().first;
93  const unsigned int stride_y = info.stride().second;
94  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
95  TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape).set_data_layout(data_layout));
96  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
97 
100 
101  return Status{};
102 }
103 
105  const WeightsInfo &weights_info)
106 {
108 
109  const unsigned int pad_left = info.pad_left();
110  const unsigned int pad_right = info.pad_right();
111  const unsigned int pad_top = info.pad_top();
112  const unsigned int pad_bottom = info.pad_bottom();
113  const unsigned int stride_x = info.stride().first;
114  const unsigned int stride_y = info.stride().second;
115 
116  const DataLayout data_layout = input->info()->data_layout();
117 
120 
121  _original_weights = weights;
122  _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
123  _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
124  _flip_weights.configure(weights, &_weights_flipped, &_flip_axis);
125 
126  auto out_dims = deconvolution_output_dimensions(input->info()->dimension(idx_w), input->info()->dimension(idx_h), weights->info()->dimension(idx_w), weights->info()->dimension(idx_h), info);
127 
129 
130  // Output auto initialization if not yet initialized
131  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
132 
133  // Perform validation step
134  ARM_COMPUTE_ERROR_THROW_ON(CLDirectDeconvolutionLayer::validate(input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info));
135 
136  _is_prepared = weights_info.retain_internal_weights();
137 
138  _memory_group.manage(&_scaled_output);
139 
140  // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
141  unsigned int deconv_pad_x = 0;
142  unsigned int deconv_pad_y = 0;
143  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input->info(), *weights->info(), stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
144 
145  unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
146  unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
147  deconv_pad_x -= deconv_pad_left + deconv_pad_right;
148  ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
149  deconv_pad_left += deconv_pad_x / 2;
150  deconv_pad_right += deconv_pad_x / 2;
151 
152  unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
153  unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
154  deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
155  ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
156  deconv_pad_top += deconv_pad_y / 2;
157  deconv_pad_bottom += deconv_pad_y / 2;
158 
159  TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
160  scale_out_info.set_data_layout(data_layout);
161  _scaled_output.allocator()->init(scale_out_info);
162 
163  // configure scale function
164  const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
165  _scale_f.configure(input, &_scaled_output, upsample_info);
166 
167  // Setup the function to convolve the upscaled output
168  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
169  _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
170  _scaled_output.allocator()->allocate();
171 
172  // Setup flip axis data
173  _flip_axis.allocator()->allocate();
174  _flip_axis.map(true);
175  auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
177  {
178  axis_data[0] = 1;
179  axis_data[1] = 2;
180  }
181  else
182  {
183  axis_data[0] = 0;
184  axis_data[1] = 1;
185  }
186  _flip_axis.unmap();
187 }
188 
190 {
191  prepare();
192 
193  MemoryGroupResourceScope scope_mg(_memory_group);
194 
195  _scale_f.run();
196  _conv_f.run();
197 }
198 
200 {
201  if(!_is_prepared)
202  {
203  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
204 
205  // Run weights flipping and mark original weights tensor as unused
206  _weights_flipped.allocator()->allocate();
207  _flip_weights.run();
208  _original_weights->mark_as_unused();
209 
210  // Prepare convolution
211  _conv_f.prepare();
212 
213  // Free flipped weights
214  if(!_weights_flipped.is_used())
215  {
216  _weights_flipped.allocator()->free();
217  }
218 
219  _is_prepared = true;
220  }
221 }
222 } // namespace arm_compute
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, ITensorInfo *output, const PadStrideInfo &info, const WeightsInfo &weights_info=WeightsInfo())
Static function to check if given info will lead to a valid configuration of CLDirectDeconvolutionLay...
Shape of a tensor.
Definition: TensorShape.h:39
const DataLayout data_layout
Definition: Im2Col.cpp:146
void run() override
Run the kernels contained in the function.
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
Definition: TensorInfo.cpp:314
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:41
void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), bool enable_fast_math=false, unsigned int num_groups=1)
Set the input and output tensors.
void configure(ICLTensor *input, ICLTensor *output, const PadStrideInfo &info)
Initialize the function's source, destination, interpolation type and border_mode.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
DataLayout data_layout() const override
Get the data layout of the tensor.
Definition: TensorInfo.h:315
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
std::pair< unsigned int, unsigned int > deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, unsigned int kernel_width, unsigned int kernel_height, const PadStrideInfo &pad_stride_info)
Returns expected width and height of the deconvolution's output tensor.
Definition: Utils.cpp:382
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:232
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
void prepare() override
Prepare the function for executing.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor's metadata.
Definition: ITensorInfo.h:40
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:61
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), bool enable_fast_math=false, unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLConvolutionLayer.
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:296
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2019 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:202
1 channel, 1 F16 per channel
CLDirectDeconvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
Definition: CLTensor.cpp:66
Convolution Layer Weights Information class.
Definition: Types.h:1689
TensorShape compute_deconvolution_output_shape(const std::pair< unsigned int, unsigned int > &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
Calculate the output shape of the deconvolution layer.
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
1 channel, 1 S32 per channel
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
void run() override
Run the kernels contained in the function.
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:378
void run() override final
Run the kernels contained in the function.
1 channel, 1 U32 per channel
quantized, asymmetric fixed-point 8-bit number unsigned
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:685
TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy, std::pair< unsigned int, unsigned int > &out_dims, unsigned int &padx, unsigned int &pady)
Calculate the upsampled output shape used for deconvolution.
void run() override
Run the kernels contained in the function.
void configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &info, const WeightsInfo &weights_info=WeightsInfo())
Set the input, weights, biases and output tensors.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1044
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
void prepare() override
Prepare the function for executing.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PadStrideInfo &info)
Static function to check if given info will lead to a valid configuration of CLDeconvolutionLayerUpsa...
Num samples, height, width, channels.
void free() override
Free allocated OpenCL memory.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Store the tensor's metadata.
Definition: TensorInfo.h:45
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:327
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLTensor.cpp:71
DataLayout
[DataLayout enum definition]
Definition: Types.h:116
void configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *axis)
Initialize the function.
Definition: CLReverse.cpp:32