Compute Library
 23.08
CLDeconvolutionReshapeOutputKernel.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 
35 #include "support/StringSupport.h"
36 
37 namespace arm_compute
38 {
39 namespace
40 {
41 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const ITensorInfo *input_info, const ITensorInfo *weights_info,
42  const PadStrideInfo &deconv_info)
43 {
45  const DataLayout data_layout = input_info->data_layout();
46 
50 
51  const bool is_qasymm = is_data_type_quantized_asymmetric(input_info->data_type());
52 
53  ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_w) != deconv_info.stride().first);
54  ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_h) != deconv_info.stride().second);
55 
57  if(!is_qasymm)
58  {
60  }
61  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_info->dimension(idx_w) * weights_info->dimension(idx_h) * weights_info->dimension(idx_b));
62  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != input_info->dimension(idx_w));
63  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != input_info->dimension(idx_h));
64  ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) != input_info->dimension(idx_b));
65 
66  if(bias != nullptr)
67  {
68  if(is_qasymm)
69  {
71  }
72  else
73  {
75  }
76  ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(0) != weights_info->dimension(idx_b));
77  }
78 
79  if(output->total_size() != 0)
80  {
81  const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
82  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), stride_info);
83 
85 
87  }
88  return Status{};
89 }
90 
91 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output, const ITensorInfo *input_info, const ITensorInfo *weights_info, const PadStrideInfo &deconv_info)
92 {
94 
95  const DataLayout data_layout = input_info->data_layout();
98  const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
99 
100  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), stride_info);
101 
103 
104  auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout).set_quantization_info(input->quantization_info()));
105 
106  Window win = calculate_max_window(*input);
107 
108  return std::make_pair(Status{}, win);
109 }
110 } // namespace
111 
113  : _add_bias(false),
114  _bias(nullptr)
115 {
117 }
118 
120  const PadStrideInfo &deconv_info)
121 {
122  configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, input_info, weights_info, deconv_info);
123 }
124 
126  const ITensorInfo *weights_info,
127  const PadStrideInfo &deconv_info)
128 {
130  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr ? bias->info() : nullptr), output->info(), input_info, weights_info, deconv_info));
131 
132  auto padding_info = get_padding_info({ input, bias, output });
133  // Configure kernel window
134  auto win_config = validate_and_configure_window(input->info(), output->info(), input_info, weights_info, deconv_info);
135  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
136 
137  const DataLayout data_layout = input_info->data_layout();
141 
142  _input = input;
143  _output = output;
144  _add_bias = (bias != nullptr);
145  _bias = bias;
146 
147  const int filter_w = weights_info->dimension(idx_w);
148  const int filter_h = weights_info->dimension(idx_h);
149  const int filter_b = weights_info->dimension(idx_b);
150  const int img_w = input_info->dimension(idx_w);
151  const int img_h = input_info->dimension(idx_h);
152 
153  CLBuildOptions build_opts;
154  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
155  build_opts.add_option("-DFILTER_WIDTH=" + support::cpp11::to_string(filter_w));
156  build_opts.add_option("-DFILTER_HEIGHT=" + support::cpp11::to_string(filter_h));
157  build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(img_w));
158  build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(img_h));
159  build_opts.add_option_if(data_layout == DataLayout::NCHW, "-DNUM_FILTERS=" + support::cpp11::to_string(filter_b));
160  build_opts.add_option_if(_add_bias, "-DADD_BIAS");
161 
162  _kernel = create_kernel(compile_context, "deconvolution_reshape", build_opts.options());
163  ICLKernel::configure_internal(win_config.second);
164 
165  // Set config_id for enabling LWS tuning
166  _config_id = "deconvolution_reshape_output_";
167  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
168  _config_id += "_";
169  _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
170  _config_id += "_";
171  _config_id += support::cpp11::to_string(input->info()->dimension(0));
172  _config_id += "_";
173  _config_id += support::cpp11::to_string(input->info()->dimension(1));
174  _config_id += "_";
175  _config_id += support::cpp11::to_string(output->info()->dimension(0));
176  _config_id += "_";
177  _config_id += support::cpp11::to_string(output->info()->dimension(1));
179 }
180 
182  const PadStrideInfo &deconv_info)
183 {
185  return Status{};
186 }
187 
188 void CLDeconvolutionReshapeOutputKernel::run(const Window &window, cl::CommandQueue &queue)
189 {
193 
194  unsigned int idx = 0;
195  add_3D_tensor_argument(idx, _input, collapsed);
196  add_3D_tensor_argument(idx, _output, collapsed);
197  if(_add_bias)
198  {
199  add_1D_tensor_argument(idx, _bias, collapsed);
200  }
201  enqueue(queue, *this, collapsed, lws_hint());
202 }
203 } // namespace arm_compute
arm_compute::DataLayout::NCHW
@ NCHW
Num samples, channels, height, width.
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
StringSupport.h
arm_compute::ICLKernel::add_1D_tensor_argument
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:174
arm_compute::test::validation::input_info
input_info
Definition: DirectConvolutionLayer.cpp:547
arm_compute::CLDeconvolutionReshapeOutputKernel::configure
void configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const ITensorInfo *input_info, const ITensorInfo *weights_info, const PadStrideInfo &deconv_info)
Initialise the kernel's source and destination.
Definition: CLDeconvolutionReshapeOutputKernel.cpp:119
ICLTensor.h
Helpers.h
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:109
arm_compute::CLBuildOptions::options
const StringSet & options() const
Gets the current options list set.
Definition: CLCompileContext.cpp:72
arm_compute::calculate_max_window
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Definition: WindowHelpers.cpp:28
arm_compute::CLDeconvolutionReshapeOutputKernel::run
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: CLDeconvolutionReshapeOutputKernel.cpp:188
arm_compute::test::validation::output_shape
const auto output_shape
Definition: ConvolutionLayer.cpp:411
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:268
arm_compute::test::validation::weights_info
weights_info
Definition: BatchNormalizationLayer.cpp:165
arm_compute::DataType::QASYMM8
@ QASYMM8
quantized, asymmetric fixed-point 8-bit number unsigned
arm_compute::lower_string
std::string lower_string(const std::string &val)
Lower a given string.
Definition: StringUtils.cpp:38
arm_compute::cpu::kernels::validate_arguments
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
Definition: CpuDirectConv2dKernel.cpp:60
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:1004
arm_compute::Window::collapse_if_possible
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
arm_compute::string_from_data_type
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: DataTypeUtils.cpp:31
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:39
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:630
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:877
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
arm_compute::test::validation::data_layout
const auto data_layout
Definition: ConvolutionLayer.cpp:406
StringUtils.h
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:204
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:161
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:467
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:456
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:494
arm_compute::CLBuildOptions::add_option
void add_option(std::string option)
Adds option to the existing build option list.
Definition: CLCompileContext.cpp:41
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:297
CLDeconvolutionReshapeOutputKernel.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::create_kernel
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:404
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::DataType::QASYMM8_SIGNED
@ QASYMM8_SIGNED
quantized, asymmetric fixed-point 8-bit number signed
WindowHelpers.h
arm_compute::CLBuildOptions::add_option_if
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
Definition: CLCompileContext.cpp:46
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
bias
const int32_t * bias
Definition: working_space.hpp:322
arm_compute::cpu::kernels::validate_and_configure_window
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
Definition: CpuDirectConv2dKernel.cpp:92
arm_compute::PadStrideInfo
Definition: CoreTypes.h:138
AutoConfiguration.h
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::string_from_data_layout
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: DataLayoutUtils.cpp:30
arm_compute::get_cl_type_from_data_type
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:40
arm_compute::ICLKernel::add_3D_tensor_argument
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:222
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:203
ShapeCalculator.h
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::CLDeconvolutionReshapeOutputKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const ITensorInfo *input_info, const ITensorInfo *weights_info, const PadStrideInfo &deconv_info)
Static function to check if given info will lead to a valid configuration of CLDeconvolutionReshapeOu...
Definition: CLDeconvolutionReshapeOutputKernel.cpp:181
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:85
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::has_padding_changed
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:462
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
arm_compute::DataType::S32
@ S32
signed 32-bit number
arm_compute::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:371
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:163
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:43
arm_compute::DataLayoutDimension::BATCHES
@ BATCHES
batches
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
Validate.h
arm_compute::get_padding_info
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo * > infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:447
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
arm_compute::CLDeconvolutionReshapeOutputKernel::CLDeconvolutionReshapeOutputKernel
CLDeconvolutionReshapeOutputKernel()
Default constructor.
Definition: CLDeconvolutionReshapeOutputKernel.cpp:112
CLHelpers.h
arm_compute::enqueue
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:32