Compute Library
 23.11
CLDirectDeconvolutionLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021, 2023 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 
28 #include "arm_compute/core/Utils.h"
32 
33 #include "src/common/utils/Log.h"
37 
38 #include <memory>
39 #include <tuple>
40 
41 namespace arm_compute
42 {
44 
45 CLDirectDeconvolutionLayer::CLDirectDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
46  : _memory_group(std::move(memory_manager)),
47  _scale_f(),
48  _conv_f(),
49  _flip_weights(),
50  _scaled_output(),
51  _original_weights(nullptr),
52  _weights_flipped(),
53  _flip_axis(),
54  _is_prepared(false)
55 {
56 }
57 
59  const ITensorInfo *weights,
60  const ITensorInfo *bias,
61  ITensorInfo *output,
62  const PadStrideInfo &info,
64 {
69  const DataLayout data_layout = input->data_layout();
70 
74 
75  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) < 1);
76  ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) < 1);
77 
78  auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h),
79  weights->dimension(idx_w), weights->dimension(idx_h), info);
80 
82 
84 
85  if (input->data_type() != weights->data_type())
86  {
89  }
90 
91  if (bias != nullptr)
92  {
93  if (is_data_type_quantized_asymmetric(input->data_type()))
94  {
96  }
97  else
98  {
100  }
102  }
103 
104  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_w) != output_shape[idx_w], "Output's width is invalid.");
105  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_h) != output_shape[idx_h], "Output's height is invalid.");
106  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(idx_c) != output_shape[idx_c], "Output's depth is invalid.");
107 
108  unsigned int deconv_pad_x = 0;
109  unsigned int deconv_pad_y = 0;
110  const unsigned int stride_x = info.stride().first;
111  const unsigned int stride_y = info.stride().second;
112  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(*input, *weights, stride_x, stride_y,
113  out_dims, deconv_pad_x, deconv_pad_y);
114  TensorInfo scale_out_info(input->clone()
115  ->set_is_resizable(true)
116  .reset_padding()
117  .set_tensor_shape(scale_out_shape)
118  .set_data_layout(data_layout));
119  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
120 
123  CLConvolutionLayer::validate(&scale_out_info, weights, bias, output, conv_info, weights_info));
124 
125  return Status{};
126 }
127 
129  ICLTensor *weights,
130  const ICLTensor *bias,
131  ICLTensor *output,
132  const PadStrideInfo &info,
133  const WeightsInfo &weights_info)
134 {
135  configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, info, weights_info);
136 }
137 
139  ICLTensor *input,
140  ICLTensor *weights,
141  const ICLTensor *bias,
142  ICLTensor *output,
143  const PadStrideInfo &info,
144  const WeightsInfo &weights_info)
145 {
146  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
147  ARM_COMPUTE_LOG_PARAMS(input, weights, bias, output, info, weights_info);
148 
149  const unsigned int pad_left = info.pad_left();
150  const unsigned int pad_right = info.pad_right();
151  const unsigned int pad_top = info.pad_top();
152  const unsigned int pad_bottom = info.pad_bottom();
153  const unsigned int stride_x = info.stride().first;
154  const unsigned int stride_y = info.stride().second;
155 
156  const DataLayout data_layout = input->info()->data_layout();
157 
160 
161  _original_weights = weights;
162  _flip_axis.allocator()->init(TensorInfo(TensorShape(2U), 1, DataType::U32));
163  _weights_flipped.allocator()->init(weights->info()->clone()->set_data_layout(data_layout));
164  _flip_weights.configure(compile_context, weights, &_weights_flipped, &_flip_axis, /* use_inverted_axis */ false);
165 
166  auto out_dims =
167  deconvolution_output_dimensions(input->info()->dimension(idx_w), input->info()->dimension(idx_h),
168  weights->info()->dimension(idx_w), weights->info()->dimension(idx_h), info);
169 
170  const TensorShape output_shape = compute_deconvolution_output_shape(out_dims, *input->info(), *weights->info());
171 
172  // Output auto initialization if not yet initialized
173  auto_init_if_empty(*output->info(),
174  input->info()->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout));
175 
176  // Perform validation step
178  input->info(), weights->info(), bias == nullptr ? nullptr : bias->info(), output->info(), info));
179 
180  _is_prepared = weights_info.retain_internal_weights();
181 
182  _memory_group.manage(&_scaled_output);
183 
184  // Find the upsampled dimensions and the padding needed for the convolution with stride 1 in order to match output shape
185  unsigned int deconv_pad_x = 0;
186  unsigned int deconv_pad_y = 0;
187  const TensorShape scale_out_shape = compute_deconvolution_upsampled_shape(
188  *input->info(), *weights->info(), stride_x, stride_y, out_dims, deconv_pad_x, deconv_pad_y);
189 
190  unsigned int deconv_pad_left = pad_right > pad_left ? pad_right - pad_left : 0;
191  unsigned int deconv_pad_right = pad_left > pad_right ? pad_left - pad_right : 0;
192  deconv_pad_x -= deconv_pad_left + deconv_pad_right;
193  ARM_COMPUTE_ERROR_ON((deconv_pad_x % 2) != 0);
194  deconv_pad_left += deconv_pad_x / 2;
195  deconv_pad_right += deconv_pad_x / 2;
196 
197  unsigned int deconv_pad_top = pad_bottom > pad_top ? pad_bottom - pad_top : 0;
198  unsigned int deconv_pad_bottom = pad_top > pad_bottom ? pad_top - pad_bottom : 0;
199  deconv_pad_y -= deconv_pad_top + deconv_pad_bottom;
200  ARM_COMPUTE_ERROR_ON((deconv_pad_y % 2) != 0);
201  deconv_pad_top += deconv_pad_y / 2;
202  deconv_pad_bottom += deconv_pad_y / 2;
203 
204  TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->quantization_info());
205  scale_out_info.set_data_layout(data_layout);
206  _scaled_output.allocator()->init(scale_out_info);
207 
208  // configure scale function
209  const PadStrideInfo upsample_info(stride_x, stride_y, deconv_pad_left, deconv_pad_right, deconv_pad_top,
210  deconv_pad_bottom, DimensionRoundingType::FLOOR);
211  _scale_f.configure(compile_context, input, &_scaled_output, upsample_info);
212 
213  // Setup the function to convolve the upscaled output
214  const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL);
215  _conv_f.configure(compile_context, &_scaled_output, &_weights_flipped, bias, output, conv_info, weights_info);
216  _scaled_output.allocator()->allocate();
217 
218  // Setup flip axis data
219  _flip_axis.allocator()->allocate();
220  _flip_axis.map(true);
221  auto axis_data = reinterpret_cast<uint32_t *>(_flip_axis.buffer());
222  if (weights->info()->data_layout() == DataLayout::NHWC)
223  {
224  axis_data[0] = 1;
225  axis_data[1] = 2;
226  }
227  else
228  {
229  axis_data[0] = 0;
230  axis_data[1] = 1;
231  }
232  _flip_axis.unmap();
233 }
234 
236 {
237  prepare();
238 
239  MemoryGroupResourceScope scope_mg(_memory_group);
240 
241  _scale_f.run();
242  _conv_f.run();
243 }
244 
246 {
247  if (!_is_prepared)
248  {
249  ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
250 
251  // Run weights flipping and mark original weights tensor as unused
252  _weights_flipped.allocator()->allocate();
253  _flip_weights.run();
254  _original_weights->mark_as_unused();
255 
256  // Prepare convolution
257  _conv_f.prepare();
258 
259  // Free flipped weights
260  if (!_weights_flipped.is_used())
261  {
262  _weights_flipped.allocator()->free();
263  }
264  _is_prepared = true;
265  }
266 }
267 } // namespace arm_compute
arm_compute::DataType::QSYMM8_PER_CHANNEL
@ QSYMM8_PER_CHANNEL
quantized, symmetric per channel fixed-point 8-bit number
arm_compute::ITensorInfo::data_layout
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
arm_compute::CLTensor::unmap
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLTensor.cpp:71
arm_compute::WeightsInfo
Convolution Layer Weights Information class.
Definition: Types.h:1670
arm_compute::MemoryGroup::manage
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:76
Helpers.h
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
arm_compute::DataLayoutDimension::CHANNEL
@ CHANNEL
channel
arm_compute::test::validation::output_shape
TensorShape output_shape
Definition: LSTMLayerQuantized.cpp:469
arm_compute::deconvolution_output_dimensions
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:266
arm_compute::DataLayout::NHWC
@ NHWC
Num samples, height, width, channels.
arm_compute::test::validation::weights_info
weights_info
Definition: BatchNormalizationLayer.cpp:165
arm_compute::CLDeconvolutionLayerUpsample::configure
void configure(ICLTensor *input, ICLTensor *output, const PadStrideInfo &info)
Initialize the function's source, destination, interpolation type and border_mode.
Definition: CLDeconvolutionLayerUpsample.cpp:49
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::ITensorAllocator::init
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Definition: ITensorAllocator.cpp:33
arm_compute::CLConvolutionLayer::validate
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.
Definition: CLConvolutionLayer.cpp:134
arm_compute::CLDirectDeconvolutionLayer::CLDirectDeconvolutionLayer
CLDirectDeconvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Definition: CLDirectDeconvolutionLayer.cpp:45
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:41
arm_compute::CLDirectDeconvolutionLayer::configure
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.
Definition: CLDirectDeconvolutionLayer.cpp:128
arm_compute::CLTensor::map
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
Definition: CLTensor.cpp:66
arm_compute::CLDeconvolutionLayerUpsample::run
void run() override
Run the kernels contained in the function.
Definition: CLDeconvolutionLayerUpsample.cpp:68
arm_compute::DimensionRoundingType::CEIL
@ CEIL
Ceil rounding.
arm_compute::CLConvolutionLayer::prepare
void prepare() override
Prepare the function for executing.
Definition: CLConvolutionLayer.cpp:209
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:41
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:677
arm_compute::DataLayoutDimension::WIDTH
@ WIDTH
width
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:952
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
arm_compute::cpu::data_layout
constexpr auto data_layout
Definition: impl.h:36
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
arm_compute::ITensorInfo::dimension
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::CLConvolutionLayer::run
void run() override
Run the kernels contained in the function.
Definition: CLConvolutionLayer.cpp:193
arm_compute::ICLTensor::buffer
uint8_t * buffer() const override
Interface to be implemented by the child class to return a pointer to CPU memory.
Definition: ICLTensor.cpp:52
arm_compute::utils::cast::U
U
Definition: SaturateCast.h:65
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
ARM_COMPUTE_ERROR_ON
#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
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::DataType::U32
@ U32
unsigned 32-bit number
arm_compute::misc::shape_calculator::compute_deconvolution_output_shape
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.
Definition: ShapeCalculator.h:545
arm_compute::CLDeconvolutionLayerUpsample::validate
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...
Definition: CLDeconvolutionLayerUpsample.cpp:44
arm_compute::CLCompileContext
CLCompileContext class.
Definition: CLCompileContext.h:204
arm_compute::DataLayoutDimension::HEIGHT
@ HEIGHT
height
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
CLFillBorderKernel.h
arm_compute::auto_init_if_empty
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: AutoConfiguration.h:43
arm_compute::ITensor::mark_as_unused
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:167
CLDirectDeconvolutionLayer.h
arm_compute::TensorInfo::set_data_layout
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:404
arm_compute::Status
Status class.
Definition: Error.h:52
CLDeconvolutionLayerUpsampleKernel.h
arm_compute::DataType::QASYMM8_SIGNED
@ QASYMM8_SIGNED
quantized, asymmetric fixed-point 8-bit number signed
CLScheduler.h
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
arm_compute::ITensorInfo::data_type
virtual DataType data_type() const =0
Data type used for each element of the tensor.
bias
const int32_t * bias
Definition: working_space.hpp:322
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:625
arm_compute::CLTensorAllocator::allocate
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
Definition: CLTensorAllocator.cpp:131
arm_compute::CLConvolutionLayer::configure
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.
Definition: CLConvolutionLayer.cpp:61
arm_compute::CLTensor::allocator
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:61
arm_compute::PadStrideInfo
Definition: CoreTypes.h:139
AutoConfiguration.h
Utils.h
arm_compute::misc::ICloneable::clone
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
arm_compute::get_data_layout_dimension_index
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:201
ShapeCalculator.h
arm_compute::TensorInfo
Store the tensor's metadata.
Definition: TensorInfo.h:41
arm_compute::DimensionRoundingType::FLOOR
@ FLOOR
Floor rounding.
arm_compute::CLDirectDeconvolutionLayer::validate
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...
Definition: CLDirectDeconvolutionLayer.cpp:58
arm_compute::CLDirectDeconvolutionLayer::prepare
void prepare() override
Prepare the function for executing.
Definition: CLDirectDeconvolutionLayer.cpp:245
arm_compute::ITensor::is_used
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
arm_compute::CLReverse::configure
void configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *axis, bool use_inverted_axis)
Initialize the function.
Definition: CLReverse.cpp:33
arm_compute::CLTensorAllocator::free
void free() override
Free allocated OpenCL memory.
Definition: CLTensorAllocator.cpp:163
ARM_COMPUTE_RETURN_ERROR_ON_MSG
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:245
arm_compute::MemoryGroupResourceScope
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::test::validation::conv_info
conv_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::DataType::S32
@ S32
signed 32-bit number
Log.h
arm_compute::is_data_type_quantized_asymmetric
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: DataTypeUtils.h:346
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::misc::shape_calculator::compute_deconvolution_upsampled_shape
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.
Definition: ShapeCalculator.h:505
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::misc::shape_calculator
Definition: ShapeCalculator.h:41
arm_compute::CLDirectDeconvolutionLayer::run
void run() override
Run the kernels contained in the function.
Definition: CLDirectDeconvolutionLayer.cpp:235
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ARM_COMPUTE_LOG_PARAMS
#define ARM_COMPUTE_LOG_PARAMS(...)
Definition: Log.h:35
Validate.h
arm_compute::ICLSimpleFunction::run
void run() override final
Run the kernels contained in the function.
Definition: ICLSimpleFunction.cpp:42
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486