47 using namespace misc::shape_calculator;
54 struct Im2ColConfiguration
80 const unsigned total_width = src->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
81 const unsigned total_height = src->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
84 if(dst->total_size() > 0)
86 const TensorInfo tensor_info_output = dst->clone()->set_tensor_shape(
compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups));
95 std::pair<Status, Window>
validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst,
const Size2D &kernel_dims,
const PadStrideInfo &conv_info,
bool has_bias,
const Size2D &dilation,
101 TensorShape expected_output_shape =
compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups);
108 const unsigned int input_width = src->dimension(width_idx);
109 const unsigned int input_height = src->dimension(height_idx);
112 bool window_changed =
false;
121 if(is_padding_required_nchw)
123 const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
125 Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
126 AccessWindowStatic input_access(src,
129 ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration),
130 input_height + border.bottom);
145 return std::make_pair(err, win);
148 Im2ColConfiguration configure_opencl_kernel(
const ITensorInfo *src,
const Size2D &kernel_dims,
const PadStrideInfo &conv_info,
bool has_bias,
const Size2D &dilation,
unsigned int num_groups)
155 const unsigned int input_width = src->dimension(width_idx);
156 const unsigned int input_height = src->dimension(height_idx);
157 const unsigned int input_channel = src->dimension(channel_idx);
159 const std::pair<unsigned int, unsigned int> convolved_dims =
scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
163 CLBuildOptions build_opts;
164 unsigned int num_elems_processed_per_iteration = 1;
165 bool is_padding_required_nchw =
false;
166 const UniformQuantizationInfo
qinfo = src->quantization_info().
uniform();
187 build_opts.add_option_if(has_bias,
"-DHAS_BIAS");
191 num_elems_processed_per_iteration = std::min(2U, input_channel);
192 is_padding_required_nchw =
false;
195 if(kernel_dims == Size2D(3U, 3U))
197 kernel_name =
"im2col3x3_";
198 build_opts.add_option(
"-DIM2COL_3X3");
200 else if(kernel_dims == Size2D(9U, 9U))
202 kernel_name =
"im2col9x9_";
203 build_opts.add_option(
"-DIM2COL_9X9");
207 build_opts.add_option(
"-DIM2COL_GENERIC");
214 const unsigned int partial_vec_size = input_channel % vec_size;
215 const unsigned int boundary_vec_size = vec_size - ((vec_size - partial_vec_size) % vec_size);
221 if(dilation == Size2D(1U, 1U))
223 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
227 switch(kernel_dims.width)
231 if(conv_info.stride().first == 1 && !conv_info.has_padding())
233 kernel_name =
"im2col1x1_stridex1_";
234 num_elems_processed_per_iteration = 4;
235 is_padding_required_nchw =
true;
239 kernel_name =
"im2col3x3_";
240 num_elems_processed_per_iteration = 1;
241 is_padding_required_nchw =
true;
244 kernel_name =
"im2col5x5_";
245 num_elems_processed_per_iteration = 1;
246 is_padding_required_nchw =
true;
250 if(!conv_info.has_padding())
252 kernel_name =
"im2col11x11_padx0_pady0_";
253 num_elems_processed_per_iteration = 1;
254 is_padding_required_nchw =
true;
258 kernel_name =
"im2col_generic_";
259 num_elems_processed_per_iteration = 1;
260 is_padding_required_nchw =
false;
264 else if(kernel_dims.width > 1 && !conv_info.has_padding())
266 kernel_name =
"im2col_generic_padx0_pady0_";
267 num_elems_processed_per_iteration = 1;
268 is_padding_required_nchw =
false;
277 const size_t vector_size = std::min(static_cast<size_t>(4), kernel_dims.width);
278 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
288 Im2ColConfiguration im2col_config;
290 im2col_config.build_options = build_opts.options();
294 return im2col_config;
299 : _data_layout(
DataLayout::
UNKNOWN), _convolved_dims(), _num_elems_processed_per_iteration(1), _kernel_dims(), _conv_info(), _num_groups()
322 Im2ColConfiguration im2col_config = configure_opencl_kernel(src, kernel_dims, conv_info, has_bias, dilation, num_groups);
325 _kernel =
create_kernel(compile_context, im2col_config.kernel_name, im2col_config.build_options);
334 auto win_config =
validate_and_configure_window(src, dst, kernel_dims, conv_info, has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
335 im2col_config.is_padding_required_nchw, num_groups);
337 IClKernel::configure_internal(win_config.second);
340 _config_id = im2col_config.kernel_name;
359 Im2ColConfiguration im2col_config = configure_opencl_kernel(src, kernel_dims, conv_info, has_bias, dilation, num_groups);
361 im2col_config.is_padding_required_nchw,
num_groups)
387 Window slice_in = first_slice_3d;
393 const int num_batches = tmp_win[3].end();
417 _kernel.setArg<cl_uint>(idx++,
static_cast<unsigned int>(
src->info()->strides_in_bytes()[3]));
418 _kernel.setArg<cl_uint>(idx++,
static_cast<unsigned int>(dst->info()->strides_in_bytes()[((
_num_groups == 1) ? 2 : 3)]));
421 unsigned int idx = 0;
433 while(window_collapsed.slide_window_slice_3D(slice) && window_output.
slide_window_slice_2D(slice_out) && window_collapsed.slide_window_slice_3D(slice_in));
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Window first_slice_window_2D() const
First 2D slice of the window.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
const Window & window() const
The maximum window the kernel can be executed on.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
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.
bool empty() const
Checks if pack is empty.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
bool is_padding_required_nchw
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
const size_t input_height
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Describe one of the image's dimensions with a start, end and step.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
std::string lower_string(const std::string &val)
Lower a given string.
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
std::set< std::string > build_options
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...
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
SimpleTensor< float > src
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Copyright (c) 2017-2023 Arm Limited.
size_t height
Height of the image region or rectangle.
1 channel, 1 F16 per channel
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode...
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
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.
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
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.
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.
quantized, asymmetric fixed-point 8-bit number unsigned
const unsigned int num_groups
unsigned int num_elems_processed_per_iteration
UniformQuantizationInfo uniform() const
Return per layer quantization info.
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
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...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
unsigned int _num_elems_processed_per_iteration
Padding and stride information class.
void end(TokenStream &in, bool &valid)
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Elementwise CL kernel type.
TensorShape compute_im2col_conv_shape(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, bool batch_size_on_z, unsigned int num_groups=1, unsigned int input_pad_right=0)
Calculate the im2col output shape of a tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
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.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx...
std::pair< Status, Window > validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
#define ARM_COMPUTE_CREATE_ERROR(error_code, msg)
Creates an error with a given message.
size_t width
Width of the image region or rectangle.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Class for specifying the size of an image or rectangle.
const QuantizationInfo qinfo
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
Wrapper to configure the Khronos OpenCL C++ header.
ClIm2ColKernel()
Default constructor.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
void configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
Set the input and output of the kernel.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U), unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration.
quantized, asymmetric fixed-point 8-bit number signed
Window first_slice_window_3D() const
First 3D slice of the window.
DataType
Available data types.
DataLayout
[DataLayout enum definition]
Describe a multidimensional execution window.
std::pair< unsigned int, unsigned int > _convolved_dims
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.