Compute Library
 23.08
CLPadLayerKernel.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 
32 #include "support/StringSupport.h"
33 
34 namespace arm_compute
35 {
36 namespace
37 {
38 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
39 {
41  ARM_COMPUTE_UNUSED(constant_value);
43  ARM_COMPUTE_RETURN_ERROR_ON((padding.size() < 1) || (padding.size() > input->num_dimensions()));
45  {
46  ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 3);
47 
48  const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
49  for(size_t i = 0; i < padding.size(); ++i)
50  {
51  ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).first > (input->dimension(i) - is_reflect));
52  ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).second > (input->dimension(i) - is_reflect));
53  }
54  }
55 
56  if(output->total_size() > 0)
57  {
58  TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding);
59 
61  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape);
62  }
63 
64  return Status{};
65 }
66 } // namespace
67 
69  : _input(nullptr), _output(nullptr), _4d_enabled(false)
70 {
72 }
73 
74 void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
75 {
76  configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode);
77 }
78 
79 void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
80 {
82  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding)));
83  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode));
84 
85  auto padding_info = get_padding_info({ input, output });
86 
87  _input = input;
88  _output = output;
89  _4d_enabled = (mode == PaddingMode::CONSTANT) && (padding.size() > 3);
90 
91  // Set build options
92  const DataType &data_type = input->info()->data_type();
93  const unsigned int input_width = input->info()->dimension(0);
94  const unsigned int input_height = input->info()->dimension(1);
95  const unsigned int input_depth = input->info()->dimension(2);
96  const unsigned int pad_x_before = padding.at(0).first;
97  const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0;
98  const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0;
99  const unsigned int vec_size = adjust_vec_size(std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->info()->data_type()))), input_width);
100  const unsigned int pad_right_start = input_width + pad_x_before;
101  const unsigned int pad_x_before_remainder = pad_x_before % vec_size;
102  const unsigned int vec_size_leftover_write = vec_size - (ceil_to_multiple(output->info()->dimension(0), vec_size) - output->info()->dimension(0));
103 
104  CLBuildOptions build_opts;
105  build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
106  build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
107  build_opts.add_option("-DPAD_X_BEFORE=" + support::cpp11::to_string(pad_x_before));
108  build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
109  build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder));
110  build_opts.add_option("-DVEC_SIZE_LEFTOVER_WRITE=" + support::cpp11::to_string(vec_size_leftover_write));
111  if(padding.size() > 1)
112  {
113  build_opts.add_option("-DPAD_Y_BEFORE=" + support::cpp11::to_string(pad_y_before));
114  build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
115 
116  if(padding.size() > 2)
117  {
118  build_opts.add_option("-DPAD_Z_BEFORE=" + support::cpp11::to_string(pad_z_before));
119  build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_depth));
120  }
121  }
122 
123  std::string kernel_name = "pad_layer_";
124  switch(mode)
125  {
127  {
128  kernel_name += "constant";
129 
130  const unsigned int vec_size_leftover_read = vec_size - (ceil_to_multiple(pad_right_start, vec_size) - pad_right_start);
131 
132  build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, data_type));
133  build_opts.add_option("-DVEC_SIZE_LEFTOVER_READ=" + support::cpp11::to_string(vec_size_leftover_read));
134 
135  if(pad_x_before >= vec_size)
136  {
137  build_opts.add_option("-DTHREADS_TO_SKIP_BEFORE=" + support::cpp11::to_string(pad_x_before / vec_size));
138  build_opts.add_option("-DTHREADS_TO_SKIP_AFTER=" + support::cpp11::to_string(pad_right_start / vec_size));
139  }
140  if(_4d_enabled)
141  {
142  build_opts.add_option("-DPAD_W_BEFORE=" + support::cpp11::to_string(padding.at(3).first));
143  build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3)));
144  }
145 
146  break;
147  }
150  {
151  kernel_name += "symmetric_reflect";
152 
153  const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT);
154 
155  const unsigned int pad_x_after_remainder = pad_right_start % vec_size;
156  const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect;
157  const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size);
158 
159  build_opts.add_option("-DIS_REFLECT=" + support::cpp11::to_string(is_reflect));
160  build_opts.add_option("-DPAD_X_AFTER_REMAINDER=" + support::cpp11::to_string(pad_x_after_remainder));
161  build_opts.add_option("-DPAD_X_BEFORE_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_before_remainder + is_reflect) % vec_size));
162  build_opts.add_option("-DPAD_X_AFTER_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_after_remainder - is_reflect) % vec_size));
163  build_opts.add_option("-DAFTER_PAD_FACT_X=" + support::cpp11::to_string(after_pad_fact_x));
164  build_opts.add_option_if(after_pad_fact_x < output_last_x, "-DAFTER_PAD_REM=" + support::cpp11::to_string(after_pad_fact_x % vec_size));
165 
166  break;
167  }
168  default:
169  ARM_COMPUTE_ERROR("Padding mode not supported.");
170  }
171 
172  // Create kernel
173  _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
174 
175  // Configure window
176  Window win = calculate_max_window(*output->info(), Steps(vec_size));
177  ICLKernel::configure_internal(win);
178 
180 }
181 
182 Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode)
183 {
184  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode));
185  return Status{};
186 }
187 
188 void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue)
189 {
192 
194  unsigned int batch = 0;
195  do
196  {
197  unsigned int idx = 0;
198  add_3D_tensor_argument(idx, _input, slice);
199  add_3D_tensor_argument(idx, _output, slice);
200  if(_4d_enabled)
201  {
202  add_argument<unsigned int>(idx, batch++);
203  }
204 
205  enqueue(queue, *this, slice, lws_hint());
206  }
208 }
209 } // namespace arm_compute
arm_compute::Steps
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
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
AdjustVecSize.h
ICLTensor.h
arm_compute::PixelValue
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:35
arm_compute::PaddingList
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:413
input_width
const size_t input_width
Definition: impl.cpp:63
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::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::string_from_pixel_value
std::string string_from_pixel_value(const PixelValue &value, const DataType data_type)
Convert a PixelValue to a string, represented through the specific data type.
Definition: Utils.cpp:173
arm_compute::ICLTensor
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:353
arm_compute::element_size_from_data_type
size_t element_size_from_data_type(DataType dt)
The size in bytes of the data type.
Definition: DataTypeUtils.h:78
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::PaddingMode
PaddingMode
Padding mode to use for PadLayer.
Definition: Types.h:125
arm_compute::CLPadLayerKernel::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: CLPadLayerKernel.cpp:188
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::utils::cast::U
U
Definition: SaturateCast.h:64
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::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::PaddingMode::REFLECT
@ REFLECT
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
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
WindowHelpers.h
ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
input_depth
const size_t input_depth
Definition: impl.cpp:64
arm_compute::Window::slide_window_slice_3D
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:349
arm_compute::test::validation::batch
const unsigned int batch
Definition: GEMMMatrixMultiplyNative.cpp:362
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
AutoConfiguration.h
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:223
arm_compute::ceil_to_multiple
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Math.h:50
arm_compute::Window::first_slice_window_3D
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:305
arm_compute::misc::shape_calculator::compute_padded_shape
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
Definition: ShapeCalculator.h:1276
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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
ShapeCalculator.h
arm_compute::CLPadLayerKernel::CLPadLayerKernel
CLPadLayerKernel()
Default constructor.
Definition: CLPadLayerKernel.cpp:68
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::ELEMENTWISE
@ ELEMENTWISE
Elementwise CL kernel type.
Definition: CLTypes.h:85
arm_compute::CLPadLayerKernel::configure
void configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Set the input and output tensor.
Definition: CLPadLayerKernel.cpp:74
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::PaddingMode::SYMMETRIC
@ SYMMETRIC
clang_tidy_rules.mode
mode
Definition: clang_tidy_rules.py:196
arm_compute::adjust_vec_size
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input's first dimension,...
Definition: AdjustVecSize.h:38
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::ICLKernel::lws_hint
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:371
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
arm_compute::CLPadLayerKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value=PixelValue(), PaddingMode mode=PaddingMode::CONSTANT)
Static function to check if given info will lead to a valid configuration of CLPadLayerKernel.
Definition: CLPadLayerKernel.cpp:182
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:43
CLPadLayerKernel.h
arm_compute::PaddingMode::CONSTANT
@ CONSTANT
arm_compute::DataType::UNKNOWN
@ UNKNOWN
Unknown data type.
arm_compute::CLBuildOptions
Build options.
Definition: CLCompileContext.h:38
arm_compute::DataType
DataType
Available data types.
Definition: CoreTypes.h:82
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::reference::slice
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
Definition: SliceOperations.cpp:38
input_height
const size_t input_height
Definition: impl.cpp:62
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
CLHelpers.h
kernel_name
std::string kernel_name
Definition: ClIm2ColKernel.cpp:57
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