Compute Library
 20.05
NEDeconvolutionLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 
33 
34 namespace arm_compute
35 {
36 namespace
37 {
38 PadStrideInfo compute_upsample_info(const PadStrideInfo &info, uint32_t deconv_pad_x, uint32_t deconv_pad_y)
39 {
40  const unsigned int pad_left = info.pad_left();
41  const unsigned int pad_right = info.pad_right();
42  const unsigned int pad_top = info.pad_top();
43  const unsigned int pad_bottom = info.pad_bottom();
44  const unsigned int stride_x = info.stride().first;
45  const unsigned int stride_y = info.stride().second;
46 
47  // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
48  unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
49  unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
50  deconv_pad_x -= deconv_pad_left + deconv_pad_right;
51  ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
52  deconv_pad_left += deconv_pad_x / 2;
53  deconv_pad_right += deconv_pad_x / 2;
54 
55  unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
56  unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
57  deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
58  ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
59  deconv_pad_top += deconv_pad_y / 2;
60  deconv_pad_bottom += deconv_pad_y / 2;
61 
62  return PadStrideInfo(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
63 }
64 
65 } // namespace
66 
67 NEDeconvolutionLayer::NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
68  : _memory_group(std::move(memory_manager)),
69  _conv_f(),
70  _upsample_f(),
71  _flip_weights(),
72  _scaled_output(),
73  _weights_flipped(),
74  _flip_axis(),
75  _original_weights(nullptr),
76  _input(nullptr),
77  _info(),
78  _is_prepared(false)
79 {
80 }
81 
83 {
88  const unsigned int width_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH);
89  const unsigned int height_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::HEIGHT);
90  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) != weights->dimension(height_idx));
91  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) < 1);
92 
93  auto out_dims = deconvolution_output_dimensions(input->dimension(width_idx), input->dimension(height_idx), weights->dimension(width_idx), weights->dimension(height_idx), info);
94 
96  if(bias != nullptr)
97  {
99  {
101  }
102  else
103  {
105  }
106  }
107 
108  if(output->tensor_shape().total_size() > 0)
109  {
111 
113 
114  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimX) != output_shape.x(), "Output's width is invalid.");
115  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimY) != output_shape.y(), "Output's height is invalid.");
116  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimZ) != output_shape.z(), "Output's depth is invalid.");
117  }
118 
119  uint32_t deconv_pad_x = 0;
120  uint32_t deconv_pad_y = 0;
121  const unsigned int stride_x = info.stride().first;
122  const unsigned int stride_y = info.stride().second;
123  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
124  TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape));
125  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
126 
127  const unsigned int batches_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
128  const unsigned int channel_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
129  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(batches_idx) != scale_out_info.dimension(batches_idx));
130  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != scale_out_info.dimension(channel_idx));
131 
133 
134  return Status{};
135 }
136 
138 {
139  // Perform validation step
141  ARM_COMPUTE_ERROR_THROW_ON(NEDeconvolutionLayer::validate(input->info(), weights->info(), (bias == nullptr) ? nullptr : bias->info(), output->info(), info));
142 
143  const DataLayout data_layout = input->info()->data_layout();
146  auto out_dims = deconvolution_output_dimensions(input->info()->dimension(width_idx), input->info()->dimension(height_idx),
147  weights->info()->dimension(width_idx), weights->info()->dimension(height_idx), info);
148 
150 
151  _input = input;
152  _original_weights = weights;
153  _info = info;
154  _is_prepared = false;
155 
156  const unsigned int stride_x = info.stride().first;
157  const unsigned int stride_y = info.stride().second;
158 
159  // Output auto initialization if not yet initialized
160  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
161 
162  _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
163  _memory_group.manage(&_scaled_output);
164 
165  _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
166  _flip_weights.configure(weights, &_weights_flipped, &_flip_axis);
167 
168  // setup the function to convolve the upscaled output
169  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
170  uint32_t deconv_pad_x = 0;
171  uint32_t deconv_pad_y = 0;
172 
173  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input->info(), *weights->info(),
174  stride_x, stride_y,
175  out_dims, deconv_pad_x, deconv_pad_y);
176 
177  const PadStrideInfo upsample_info = compute_upsample_info(info, deconv_pad_x, deconv_pad_y);
178 
179  TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
180  scale_out_info.set_data_layout(data_layout);
181  _scaled_output.allocator()->init(scale_out_info);
182 
183  _upsample_f.configure(input, &_scaled_output, upsample_info);
184 
185  _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info);
186 
187  // Setup flip axis data
188  _flip_axis.allocator()->allocate();
189  auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
190  axis_data[0] = static_cast<uint32_t>(width_idx);
191  axis_data[1] = static_cast<uint32_t>(height_idx);
192 
193  _scaled_output.allocator()->allocate();
194 }
195 
197 {
198  prepare();
199 
200  MemoryGroupResourceScope scope_mg(_memory_group);
201 
202  _upsample_f.run();
203  _conv_f.run();
204 }
205 
207 {
208  if(!_is_prepared)
209  {
210  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
211 
212  // Run weights flipping and mark original weights tensor as unused
213  _weights_flipped.allocator()->allocate();
214  _flip_weights.run();
215  _original_weights->mark_as_unused();
216 
217  // Prepare convolution
218  _conv_f.prepare();
219 
220  _is_prepared = true;
221  }
222 }
223 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
const DataLayout data_layout
Definition: Im2Col.cpp:146
void run() override final
Run the kernels contained in the function.
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
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
#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:375
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &info)
Static function to check if given info will lead to a valid configuration of NEDeconvolutionLayer.
void run() override
Run the kernels contained in the function.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
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: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
void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *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.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
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
Interface for NEON tensor.
Definition: ITensor.h:36
void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output, const PadStrideInfo &info)
Set the input, weights, biases and output tensors.
Copyright (c) 2017-2020 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
void configure(const ITensor *input, ITensor *output, const ITensor *axis)
Initialize the function.
Definition: NEReverse.cpp:31
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
Convolution Layer Weights Information class.
Definition: Types.h:1694
void run() override
Run the kernels contained in the function.
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
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
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:378
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
1 channel, 1 U32 per channel
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:91
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:171
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.
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, uint32_t &padx, uint32_t &pady)
Calculate the upsampled output shape used for deconvolution.
Padding and stride information class.
Definition: Types.h:689
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1153
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
NEDeconvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
void prepare() override
Prepare the function for executing.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void configure(const ITensor *input, ITensor *output, const PadStrideInfo &info)
Configure the upsample CPP kernel.
Definition: CPPUpsample.cpp:31
uint8_t * buffer() const override
Interface to be implemented by the child class to return a pointer to CPU memory.
Definition: Tensor.cpp:43
#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
void run() override final
Run the kernels contained in the function.
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
quantized, asymmetric fixed-point 8-bit number signed
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
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
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 NEConvolutionLayer.
void prepare() override
Prepare the function for executing.