50 using ClPoolingConfig = std::pair<unsigned int, BorderSize>;
52 void auto_init(
const ITensorInfo *
src, ITensorInfo *
dst, ITensorInfo *indices, PoolingLayerInfo pool_info)
62 Status
validate_arguments(
const ITensorInfo *src,
const ITensorInfo *dst,
const PoolingLayerInfo &pool_info,
const ITensorInfo *indices)
68 "Unsupported combination of parameters!");
77 if(indices->total_size() != 0)
85 if(dst->total_size() != 0)
89 TensorInfo out_info(TensorInfo(
compute_pool_shape(*src, pool_info), 1, dst->data_type()));
96 std::tuple<Status, Window, ClPoolingConfig> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst,
const PoolingLayerInfo &pool_info, ITensorInfo *indices =
nullptr)
106 int pool_stride_y = 0;
107 unsigned int pooled_w = 0;
108 unsigned int pooled_h = 0;
109 int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
110 int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
111 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
112 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
113 const int pool_pad_right = pad_stride_info.pad_right();
114 const int pool_pad_top = pad_stride_info.pad_top();
115 const int pool_pad_left = pad_stride_info.pad_left();
116 const int pool_pad_bottom = pad_stride_info.pad_bottom();
117 BorderSize border_size = BorderSize();
119 auto_init(src, dst, indices, pool_info);
120 pooled_w = dst->tensor_shape()[
idx_width];
125 const int src_width = src->dimension(idx_width);
126 const int src_height = src->dimension(idx_height);
129 bool window_changed =
false;
136 border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left);
139 const bool can_optimize = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3) && !
is_data_type_quantized(data_type);
140 num_elems_processed_per_iteration = can_optimize ? 4 : 1;
141 const unsigned int num_elems_read_per_iteration = (num_elems_processed_per_iteration - 1) * pool_stride_x + pool_size_x;
144 const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration;
147 const int upper_bound_w = ((num_iterations_x - 1) * num_elems_processed_per_iteration * pool_stride_x - pool_pad_left + num_elems_read_per_iteration) - src_width;
148 const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - src_height;
150 border_size.right = std::max(upper_bound_w, pool_pad_right);
151 border_size.bottom = std::max(upper_bound_h, pool_pad_bottom);
155 AccessWindowRectangle src_access(src, -pool_pad_left, -pool_pad_top, num_elems_read_per_iteration, pool_size_y,
156 pool_stride_x, pool_stride_y);
157 AccessWindowHorizontal dst_access(dst, 0, num_elems_processed_per_iteration);
162 AccessWindowHorizontal indices_access(indices, 0, num_elems_processed_per_iteration);
164 indices_access.set_valid_region(win, ValidRegion(Coordinates(), indices->tensor_shape()));
171 dst_access.set_valid_region(win, ValidRegion(Coordinates(), dst->tensor_shape()));
177 border_size = BorderSize();
178 num_elems_processed_per_iteration =
adjust_vec_size(4, dst->dimension(0));
181 if(indices !=
nullptr)
183 indices->set_valid_region(ValidRegion(Coordinates(), indices->tensor_shape()));
186 dst->set_valid_region(ValidRegion(Coordinates(), dst->tensor_shape()));
194 return std::make_tuple(err, win, ClPoolingConfig(num_elems_processed_per_iteration, border_size));
199 : _pool_info(), _data_layout(
DataLayout::
UNKNOWN), _border_size(0), _num_elems_processed_per_iteration(1)
217 int pool_stride_x = 0;
218 int pool_stride_y = 0;
228 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.
stride();
229 const int pool_pad_top = pad_stride_info.
pad_top();
230 const int pool_pad_left = pad_stride_info.
pad_left();
237 auto win_config = validate_and_configure_window(src, dst, pool_info, indices);
240 ICLKernel::configure_internal(std::get<1>(win_config));
242 ClPoolingConfig pooling_config = std::get<2>(win_config);
272 auto_init(src, dst, indices, pool_info);
314 const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type !=
PoolingType::MAX);
316 build_opts.
add_option(
"-DACC_DATA_TYPE=" + acc_data_type);
317 build_opts.
add_option_if(use_wider_accumulator,
"-DFP_MIXED_PRECISION");
321 build_opts.
add_option_if(exclude_padding,
"-DEXCLUDE_PADDING");
328 const bool is_pool3x3_stride_le3 = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3);
330 std::string
kernel_name = ((is_pool3x3_stride_le3) ?
"pooling_layer_optimized_" :
"pooling_layer_")
339 std::string
kernel_name =
"pooling_layer_2_nchw_indices_fp32";
344 std::string
kernel_name =
"pooling_layer_2_nchw_indices_fp16";
365 if(use_fp_mixed_precision)
375 build_opts.
add_option_if(use_fp_mixed_precision,
"-DFP_MIXED_PRECISION");
376 build_opts.
add_option_if(exclude_padding,
"-DEXCLUDE_PADDING");
387 std::string
kernel_name =
"pooling_layer_2x2_nhwc";
402 _config_id =
"pooling_layer_";
431 unsigned int pool_stride_x = 0;
432 unsigned int pool_stride_y = 0;
446 Window slice = window_collapsed.first_slice_window_3D();
459 unsigned int idx = 0;
468 while(window_collapsed.slide_window_slice_3D(slice));
473 const size_t batch_size = dst->info()->tensor_shape().total_size_upper(3);
475 Window slice = window_collapsed.first_slice_window_4D();
484 unsigned int idx = 0;
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
unsigned int top
top of the border
Class describing the value of a pixel for any image format.
ClPoolingKernel()
Default constructor.
unsigned int _num_elems_processed_per_iteration
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.
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
const StringSet & options() const
Gets the current options list set.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
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
#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
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.
unsigned int bottom
bottom of the border
unsigned int pad_top() const
Get the top padding.
std::string lower_string(const std::string &val)
Lower a given string.
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...
SimpleTensor< float > src
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
void configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices=nullptr)
Configure kernel for a given list of arguments.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
Calculate the output pool shape of a tensor.
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.
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.
1 channel, 1 U32 per channel
std::string float_to_string_with_full_precision(float val)
Create a string with the float in full precision.
quantized, asymmetric fixed-point 8-bit number unsigned
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Pooling Layer Information struct.
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.
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
Padding and stride information class.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
virtual PaddingSize padding() const =0
Padding of tensor.
unsigned int left
left of the border
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
unsigned int right
right of the border
#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.
Num samples, channels, height, width.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
__constant DATA_TYPE16 type_min
BorderSize border_size() const override
The size of the border for that kernel.
PoolingLayerInfo _pool_info
static constexpr size_t DimY
Alias for dimension 1 also known as Y 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...
PoolingType
Available pooling types.
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.
PadStrideInfo pad_stride_info
#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.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Num samples, height, width, channels.
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
#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)
Window first_slice_window_4D() const
First 4D slice of the window.
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
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, getting rounded down to its closest valid vector size.
quantized, asymmetric fixed-point 8-bit number signed
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices=nullptr)
Static function to check if given info will lead to a valid configuration of ClPoolingKernel.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
constexpr int end() const
Return the end of the dimension.
DataType
Available data types.
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx...
unsigned int pad_left() const
Get the left padding.
DataLayout
[DataLayout enum definition]
const std::string & string_from_pooling_type(PoolingType type)
Translates a given pooling type to a string.
constexpr int start() const
Return the start of the dimension.
std::tuple< PixelValue, PixelValue > get_min_max(DataType dt)
Compute the mininum and maximum values a data type can take.
Describe a multidimensional execution window.
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
constexpr const Dimension & x() const
Alias to access the first dimension of the window.