Compute Library
 20.02.1
NEDeconvolutionLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
33 
34 namespace arm_compute
35 {
36 NEDeconvolutionLayer::NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
37  : _memory_group(std::move(memory_manager)),
38  _conv_f(),
39  _upsample_f(),
40  _flip_weights(),
41  _permute_input(),
42  _permute_weights(),
43  _permute_output(),
44  _scaled_output(),
45  _weights_flipped(),
46  _permuted_input(),
47  _permuted_weights(),
48  _permuted_output(),
49  _is_nchw(false),
50  _original_weights(nullptr),
51  _input(nullptr),
52  _info(),
53  _is_prepared(false)
54 {
55 }
56 
58 {
63  const unsigned int width_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH);
64  const unsigned int height_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::HEIGHT);
65  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) != weights->dimension(height_idx));
66  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) < 1);
67 
68  auto out_dims = deconvolution_output_dimensions(input->dimension(width_idx), input->dimension(height_idx), weights->dimension(width_idx), weights->dimension(height_idx), info);
69 
71  if(bias != nullptr)
72  {
74  {
76  }
77  else
78  {
80  }
81  }
82 
83  if(output->tensor_shape().total_size() > 0)
84  {
86 
88 
89  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimX) != output_shape.x(), "Output's width is invalid.");
90  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimY) != output_shape.y(), "Output's height is invalid.");
91  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(Window::DimZ) != output_shape.z(), "Output's depth is invalid.");
92  }
93 
94  unsigned int deconv_pad_x = 0;
95  unsigned int deconv_pad_y = 0;
96  const unsigned int stride_x = info.stride().first;
97  const unsigned int stride_y = info.stride().second;
98  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
99  TensorInfo scale_out_info(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(scale_out_shape));
100  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
101 
102  const unsigned int batches_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
103  const unsigned int channel_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
104  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(batches_idx) != scale_out_info.dimension(batches_idx));
105  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(channel_idx) != scale_out_info.dimension(channel_idx));
106 
108 
109  return Status{};
110 }
111 
113 {
114  // Perform validation step
116  ARM_COMPUTE_ERROR_THROW_ON(NEDeconvolutionLayer::validate(input->info(), weights->info(), (bias == nullptr) ? nullptr : bias->info(), output->info(), info));
117 
118  const DataLayout data_layout = input->info()->data_layout();
119 
120  _input = input;
121  _original_weights = weights;
122  _info = info;
123  _is_prepared = false;
124  _is_nchw = data_layout == DataLayout::NCHW;
125 
126  const unsigned int pad_left = info.pad_left();
127  const unsigned int pad_right = info.pad_right();
128  const unsigned int pad_top = info.pad_top();
129  const unsigned int pad_bottom = info.pad_bottom();
130  const unsigned int stride_x = info.stride().first;
131  const unsigned int stride_y = info.stride().second;
132 
135  auto out_dims = deconvolution_output_dimensions(input->info()->dimension(width_idx), input->info()->dimension(height_idx),
136  weights->info()->dimension(width_idx), weights->info()->dimension(height_idx), info);
137 
139  // Output auto initialization if not yet initialized
140  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->quantization_info());
141 
142  _memory_group.manage(&_scaled_output);
143 
144  if(!_is_nchw)
145  {
146  _memory_group.manage(&_permuted_input);
147  _memory_group.manage(&_permuted_output);
148 
149  // Configure the function to transform the input tensor from NHWC -> NCHW
150  _permuted_input.info()->set_quantization_info(input->info()->quantization_info());
151  _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
152  _permuted_input.info()->set_data_layout(DataLayout::NCHW);
153 
154  // Configure the function to transform the weights tensor from NHWC -> NCHW
155  _permuted_weights.info()->set_quantization_info(weights->info()->quantization_info());
156  _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
157  _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
158 
159  // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
160  unsigned int deconv_pad_x = 0;
161  unsigned int deconv_pad_y = 0;
162  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*_permuted_input.info(), *_permuted_weights.info(), stride_x, stride_y, out_dims,
163  deconv_pad_x, deconv_pad_y);
164 
165  unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
166  unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
167  deconv_pad_x -= deconv_pad_left + deconv_pad_right;
168  ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
169  deconv_pad_left += deconv_pad_x / 2;
170  deconv_pad_right += deconv_pad_x / 2;
171 
172  unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
173  unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
174  deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
175  ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
176  deconv_pad_top += deconv_pad_y / 2;
177  deconv_pad_bottom += deconv_pad_y / 2;
178 
179  TensorInfo scale_out_info(scale_out_shape, 1, _permuted_input.info()->data_type(), _permuted_input.info()->quantization_info());
180  scale_out_info.set_data_layout(DataLayout::NCHW);
181  _scaled_output.allocator()->init(scale_out_info);
182 
183  const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
184  _upsample_f.configure(&_permuted_input, &_scaled_output, upsample_info);
185 
186  _weights_flipped.allocator()->init(*_permuted_weights.info()->clone());
187  _weights_flipped.info()->set_quantization_info(weights->info()->quantization_info());
188  _flip_weights.configure(&_permuted_weights, &_weights_flipped);
189 
190  // setup the function to convolve the upscaled output
191  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
192 
193  _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
194  _conv_f.configure(&_scaled_output, &_weights_flipped, bias, &_permuted_output, conv_info);
195 
196  // Configure the function to transform the convoluted output to NHWC
197  _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
198  _permuted_output.info()->set_data_layout(DataLayout::NCHW);
199 
200  _permuted_input.allocator()->allocate();
201  _permuted_output.allocator()->allocate();
202  }
203  else
204  {
205  // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
206  unsigned int deconv_pad_x = 0;
207  unsigned int deconv_pad_y = 0;
208  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input->info(), *weights->info(), stride_x, stride_y,
209  out_dims, deconv_pad_x, deconv_pad_y);
210 
211  unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
212  unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
213  deconv_pad_x -= deconv_pad_left + deconv_pad_right;
214  ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
215  deconv_pad_left += deconv_pad_x / 2;
216  deconv_pad_right += deconv_pad_x / 2;
217 
218  unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
219  unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
220  deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
221  ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
222  deconv_pad_top += deconv_pad_y / 2;
223  deconv_pad_bottom += deconv_pad_y / 2;
224 
225  TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
226  scale_out_info.set_data_layout(data_layout);
227  _scaled_output.allocator()->init(scale_out_info);
228 
229  const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top, deconv_pad_bottom, DimensionRoundingType::FLOOR);
230  _upsample_f.configure(input, &_scaled_output, upsample_info);
231 
232  _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
233  _flip_weights.configure(weights, &_weights_flipped);
234 
235  // setup the function to convolve the upscaled output
236  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
237  _conv_f.configure(&_scaled_output, &_weights_flipped, bias, output, conv_info);
238  }
239  _scaled_output.allocator()->allocate();
240 }
241 
243 {
244  prepare();
245 
246  MemoryGroupResourceScope scope_mg(_memory_group);
247 
248  // Permute input
249  if(!_is_nchw)
250  {
251  _permute_input.run();
252  }
253 
254  _upsample_f.run();
255  _conv_f.run();
256 
257  // Permute output
258  if(!_is_nchw)
259  {
260  _permute_output.run();
261  }
262 }
263 
265 {
266  if(!_is_prepared)
267  {
268  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
269  // Permute weights
270  if(!_is_nchw)
271  {
272  // Manually manage _permuted_weights
273  _permuted_weights.allocator()->allocate();
274  _permute_weights.run();
275  }
276 
277  // Run weights flipping and mark original weights tensor as unused
278  _weights_flipped.allocator()->allocate();
279  NEScheduler::get().schedule(&_flip_weights, Window::DimZ);
280  _original_weights->mark_as_unused();
281 
282  // Prepare convolution
283  _conv_f.prepare();
284 
285  if(!_weights_flipped.is_used())
286  {
287  _weights_flipped.allocator()->free();
288  }
289 
290  if(!_is_nchw)
291  {
292  // Manually manage _permuted_weights
293  // Free _permuted_weights as it not used after this method (prepare)
294  _permuted_weights.allocator()->free();
295  }
296 
297  _is_prepared = true;
298  }
299 }
300 } // 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.
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 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:382
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
size_t dimension(size_t index) const override
Return the size of the requested dimension.
Definition: TensorInfo.h:232
virtual DataType data_type() const =0
Data type used for each element of the tensor.
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:311
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
1 channel, 1 F32 per channel
Strides PermutationVector
Permutation vector.
Definition: Types.h:48
#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
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
void configure(const ITensor *input, ITensor *output)
Set the input and output of the kernel.
Convolution Layer Weights Information class.
Definition: Types.h:1757
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
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
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual ITensorInfo & set_data_layout(const DataLayout &data_layout)=0
Set the data layout of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
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.
Padding and stride information class.
Definition: Types.h:686
virtual ITensorInfo & set_quantization_info(const QuantizationInfo &quantization_info)=0
Set the quantization settings (scale and offset) of the tensor.
void free() override
Free allocated CPU memory.
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.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
Num samples, channels, height, width.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1139
#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
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
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
#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.
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 configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
Configure the permute NEON kernel.
Definition: NEPermute.cpp:31
DataLayout
[DataLayout enum definition]
Definition: Types.h:117
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.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:95