46 using namespace misc::shape_calculator;
50 struct Im2ColConfiguration
76 const unsigned total_width = input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
77 const unsigned total_height = input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
80 if(output->total_size() > 0)
82 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(
compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups));
91 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output,
const Size2D &kernel_dims,
const PadStrideInfo &conv_info,
bool has_bias,
const Size2D &dilation,
97 TensorShape expected_output_shape =
compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, num_groups == 1, num_groups);
104 const unsigned int input_width = input->dimension(width_idx);
105 const unsigned int input_height = input->dimension(height_idx);
108 bool window_changed =
false;
115 const int xin_start = 0;
116 const int xin_end = input->dimension(0);
117 const int yin_start = 0;
118 const int yin_end = input->dimension(1);
120 const int xout_start = 0;
121 const int xout_end = output->dimension(0);
122 const int yout_start = 0;
123 const int yout_end = output->dimension(1);
125 AccessWindowStatic input_access(input, xin_start, yin_start, xin_end, yin_end);
126 AccessWindowStatic output_access(output, xout_start, yout_start, xout_end, yout_end);
131 if(is_padding_required_nchw)
133 const BorderSize border(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
135 Steps(num_elems_processed_per_iteration * conv_info.stride().first, conv_info.stride().second));
136 AccessWindowStatic input_access(input,
139 ceil_to_multiple(input_width + border.right, kernel_dims.width * num_elems_processed_per_iteration),
140 input_height + border.bottom);
150 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
155 return std::make_pair(err, win);
158 Im2ColConfiguration configure_opencl_kernel(
const ITensorInfo *input,
const Size2D &kernel_dims,
const PadStrideInfo &conv_info,
bool has_bias,
const Size2D &dilation,
unsigned int num_groups)
165 const unsigned int input_width = input->dimension(width_idx);
166 const unsigned int input_height = input->dimension(height_idx);
167 const unsigned int input_channel = input->dimension(channel_idx);
169 const std::pair<unsigned int, unsigned int> convolved_dims =
scaled_dimensions(input_width, input_height, kernel_dims.width, kernel_dims.height, conv_info, dilation);
173 CLBuildOptions build_opts;
174 unsigned int num_elems_processed_per_iteration = 1;
175 bool is_padding_required_nchw =
false;
176 const UniformQuantizationInfo
qinfo = input->quantization_info().
uniform();
197 build_opts.add_option_if(has_bias,
"-DHAS_BIAS");
201 num_elems_processed_per_iteration = std::min(2U, input_channel);
202 is_padding_required_nchw =
false;
205 if(kernel_dims == Size2D(3U, 3U))
207 kernel_name =
"im2col3x3_";
209 else if(kernel_dims == Size2D(9U, 9U))
211 kernel_name =
"im2col9x9_";
218 const unsigned int partial_vec_size = input_channel % vec_size;
219 const unsigned int boundary_vec_size = vec_size - ((vec_size - partial_vec_size) % vec_size);
225 if(dilation == Size2D(1U, 1U))
227 const bool squared_im2col = kernel_dims.width == kernel_dims.height;
231 switch(kernel_dims.width)
235 if(conv_info.stride().first == 1 && !conv_info.has_padding())
237 kernel_name =
"im2col1x1_stridex1_";
238 num_elems_processed_per_iteration = 4;
239 is_padding_required_nchw =
true;
243 kernel_name =
"im2col3x3_";
244 num_elems_processed_per_iteration = 1;
245 is_padding_required_nchw =
true;
248 kernel_name =
"im2col5x5_";
249 num_elems_processed_per_iteration = 1;
250 is_padding_required_nchw =
true;
254 if(!conv_info.has_padding())
256 kernel_name =
"im2col11x11_padx0_pady0_";
257 num_elems_processed_per_iteration = 1;
258 is_padding_required_nchw =
true;
262 kernel_name =
"im2col_generic_";
263 num_elems_processed_per_iteration = 1;
264 is_padding_required_nchw =
false;
268 else if(kernel_dims.width > 1 && !conv_info.has_padding())
270 kernel_name =
"im2col_generic_padx0_pady0_";
271 num_elems_processed_per_iteration = 1;
272 is_padding_required_nchw =
false;
281 const size_t vector_size = std::min(static_cast<size_t>(4), kernel_dims.width);
282 const size_t width_mod_vector_size = kernel_dims.width % vector_size;
292 Im2ColConfiguration im2col_config;
294 im2col_config.build_options = build_opts.options();
298 return im2col_config;
303 : _input(nullptr), _output(nullptr), _data_layout(
DataLayout::
UNKNOWN), _convolved_dims(), _num_elems_processed_per_iteration(1), _kernel_dims(), _conv_info(), _num_groups()
334 _kernel =
create_kernel(compile_context, im2col_config.kernel_name, im2col_config.build_options);
345 auto win_config = validate_and_configure_window(input->
info(), output->
info(), kernel_dims,
conv_info,
has_bias, dilation, im2col_config.num_elems_processed_per_iteration,
346 im2col_config.is_padding_required_nchw,
num_groups);
348 ICLKernel::configure_internal(win_config.second);
351 _config_id = im2col_config.kernel_name;
370 Im2ColConfiguration im2col_config = configure_opencl_kernel(input, kernel_dims, conv_info, has_bias, dilation, num_groups);
372 im2col_config.is_padding_required_nchw,
num_groups)
393 Window slice_in = first_slice_3d;
399 const int num_batches = tmp_win[3].end();
427 unsigned int idx = 0;
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)
void configure(const ICLTensor *input, ICLTensor *output, 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.
const Window & window() const
The maximum window the kernel can be executed on.
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
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(...)
std::pair< unsigned int, unsigned int > _convolved_dims
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
const size_t input_height
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
const DataLayout data_layout
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
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.
std::string lower_string(const std::string &val)
Lower a given string.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
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.
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Copyright (c) 2017-2021 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(...)
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 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 of CLIm2ColKernel.
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.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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
std::set< std::string > build_options
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.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
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.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
#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...
Interface for OpenCL tensor.
bool is_padding_required_nchw
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.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
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.
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
Wrapper to configure the Khronos OpenCL C++ header.
unsigned int _num_elems_processed_per_iteration
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
quantized, asymmetric fixed-point 8-bit number signed
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Window first_slice_window_3D() const
First 3D slice of the window.
DataType
Available data types.
DataLayout
[DataLayout enum definition]
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)
Calculate the im2col output shape of a tensor.
Describe a multidimensional execution window.
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
CLIm2ColKernel()
Default constructor.