Compute Library
 22.08
pooling_layer.cl File Reference
#include "helpers.h"

Go to the source code of this file.

Macros

#define POOL_OP(x, y)   ((x) + (y))
 
#define POW2_OP(x, vec_size)   (x)
 
#define DIV_OP(x, y)   (x * (1.f / y))
 
#define SQRT_OP(x)   sqrt((x))
 
#define VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE(n, offset, ptr)   vload##n(offset, ptr)
 

Functions

ACC_DATA_TYPE calculate_avg_scale (const int pool_size_x, const int pool_size_y, const int upper_bound_w, const int upper_bound_h, const int pad_x, const int pad_y, const int stride_x, const int stride_y)
 
__kernel void pooling_layer_2_nchw_indices (__global uchar *src_ptr, uint src_stride_x, uint src_step_x, uint src_stride_y, uint src_step_y, uint src_stride_z, uint src_step_z, uint src_offset_first_element_in_bytes, __global uchar *dst_ptr, uint dst_stride_x, uint dst_step_x, uint dst_stride_y, uint dst_step_y, uint dst_stride_z, uint dst_step_z, uint dst_offset_first_element_in_bytes, __global uchar *indices_ptr, uint indices_stride_x, uint indices_step_x, uint indices_stride_y, uint indices_step_y, uint indices_stride_z, uint indices_step_z, uint indices_offset_first_element_in_bytes)
 Performs a MAX pooling of pool size equal to 2, and record max value indices for NCHW. More...
 

Macro Definition Documentation

◆ DIV_OP

#define DIV_OP (   x,
 
)    (x * (1.f / y))

Definition at line 42 of file pooling_layer.cl.

Referenced by calculate_avg_scale().

◆ POOL_OP

#define POOL_OP (   x,
 
)    ((x) + (y))

Definition at line 27 of file pooling_layer.cl.

Referenced by calculate_avg_scale(), and pooling_layer_2_nchw_indices().

◆ POW2_OP

#define POW2_OP (   x,
  vec_size 
)    (x)

Definition at line 39 of file pooling_layer.cl.

◆ SQRT_OP

#define SQRT_OP (   x)    sqrt((x))

Definition at line 43 of file pooling_layer.cl.

Referenced by calculate_avg_scale().

◆ VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE

#define VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE (   n,
  offset,
  ptr 
)    vload##n(offset, ptr)

Definition at line 49 of file pooling_layer.cl.

Referenced by calculate_avg_scale().

Function Documentation

◆ calculate_avg_scale()

ACC_DATA_TYPE calculate_avg_scale ( const int  pool_size_x,
const int  pool_size_y,
const int  upper_bound_w,
const int  upper_bound_h,
const int  pad_x,
const int  pad_y,
const int  stride_x,
const int  stride_y 
)

Definition at line 52 of file pooling_layer.cl.

References arm_compute::utility::clamp(), CONVERT, CONVERT_SAT, DIV_OP, arm_compute::test::validation::dst, POOL_OP, REVERSE, arm_compute::test::validation::reference::select(), SELECT_DATA_TYPE, SELECT_VEC_DATA_TYPE, SQRT_OP, arm_compute::test::validation::src, TENSOR3D_DECLARATION, VEC_DATA_TYPE, VEC_OFFS, and VLOAD_AND_CONVERT_TO_ACC_DATA_TYPE.

54 {
55  int start_x = get_global_id(0) * stride_x - pad_x;
56  int start_y = get_global_id(1) * stride_y - pad_y;
57  const int end_x = min(start_x + pool_size_x, upper_bound_w);
58  const int end_y = min(start_y + pool_size_y, upper_bound_h);
59 #if defined(EXCLUDE_PADDING)
60  start_x = max(0, start_x);
61  start_y = max(0, start_y);
62 #endif /* defined(EXCLUDE_PADDING) */
63  return ((end_y - start_y) * (end_x - start_x));
64 }

◆ pooling_layer_2_nchw_indices()

__kernel void pooling_layer_2_nchw_indices ( __global uchar *  src_ptr,
uint  src_stride_x,
uint  src_step_x,
uint  src_stride_y,
uint  src_step_y,
uint  src_stride_z,
uint  src_step_z,
uint  src_offset_first_element_in_bytes,
__global uchar *  dst_ptr,
uint  dst_stride_x,
uint  dst_step_x,
uint  dst_stride_y,
uint  dst_step_y,
uint  dst_stride_z,
uint  dst_step_z,
uint  dst_offset_first_element_in_bytes,
__global uchar *  indices_ptr,
uint  indices_stride_x,
uint  indices_step_x,
uint  indices_stride_y,
uint  indices_step_y,
uint  indices_stride_z,
uint  indices_step_z,
uint  indices_offset_first_element_in_bytes 
)

Performs a MAX pooling of pool size equal to 2, and record max value indices for NCHW.

Note
Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=half. Supported data types are F32
Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13;
Tensors width and height must be passed at compile time using -DMAX_WIDTH and -DMAX_HEIGHT
Pool strides must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y which are the steps of the window along the x and y directions
Parameters
[in]src_ptrPointer to the source tensor. Supported data types: F16/F32
[in]src_stride_xStride of the source tensor in X dimension (in bytes)
[in]src_step_xsrc_stride_x * number of elements along X processed per workitem(in bytes)
[in]src_stride_yStride of the source tensor in Y dimension (in bytes)
[in]src_step_ysrc_stride_y * number of elements along Y processed per workitem(in bytes)
[in]src_stride_zStride of the source tensor in Z dimension (in bytes)
[in]src_step_zsrc_stride_z * number of elements along Z processed per workitem(in bytes)
[in]src_offset_first_element_in_bytesThe offset of the first element in the source tensor
[out]dst_ptrPointer to the destination tensor. Supported data types: same as src_ptr
[in]dst_stride_xStride of the destination tensor in X dimension (in bytes)
[in]dst_step_xdst_stride_x * number of elements along X processed per workitem(in bytes)
[in]dst_stride_yStride of the destination tensor in Y dimension (in bytes)
[in]dst_step_ydst_stride_y * number of elements along Y processed per workitem(in bytes)
[in]dst_stride_zStride of the source tensor in Z dimension (in bytes)
[in]dst_step_zdst_stride_z * number of elements along Z processed per workitem(in bytes)
[in]dst_offset_first_element_in_bytesThe offset of the first element in the destination tensor
[in]indices_ptrPointer to the indices tensor. Supported data types: U32
[in]indices_stride_xStride of the indices tensor in X dimension (in bytes)
[in]indices_step_xindices_stride_x * number of elements along X processed per workitem(in bytes)
[in]indices_stride_yStride of the indices tensor in Y dimension (in bytes)
[in]indices_step_yindices_stride_y * number of elements along Y processed per workitem(in bytes)
[in]indices_stride_zStride of the indices tensor in Z dimension (in bytes)
[in]indices_step_zindices_stride_z * number of elements along Z processed per workitem(in bytes)
[in]indices_offset_first_element_in_bytesThe offset of the first element in the indices tensor

Definition at line 246 of file pooling_layer.cl.

References arm_compute::utility::clamp(), POOL_OP, arm_compute::test::validation::reference::select(), VEC_DATA_TYPE, VEC_OFFS, and VLOAD.

250 {
251  int id0 = get_global_id(0);
252  int id1 = get_global_id(1);
253  int id2 = get_global_id(2);
254 
255  int2 x_coords = clamp((int2)((id0 * STRIDE_X) - PAD_X), (int2)0, (int2)(SRC_WIDTH - 1));
256  int2 y_coords = clamp((int2)((id1 * STRIDE_Y) - PAD_Y) + VEC_OFFS(int, 2), (int2)0, (int2)(SRC_HEIGHT - 1));
257 
258  __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + id2 * src_stride_z;
259 
260  // Load data
261  VEC_DATA_TYPE(DATA_TYPE, 2)
262  data0 = VLOAD(2)(0, (__global DATA_TYPE *)(src_addr + x_coords.s0 * sizeof(DATA_TYPE) + y_coords.s0 * (int)src_stride_y));
263  VEC_DATA_TYPE(DATA_TYPE, 2)
264  data1 = VLOAD(2)(0, (__global DATA_TYPE *)(src_addr + x_coords.s1 * sizeof(DATA_TYPE) + y_coords.s1 * (int)src_stride_y));
265 
266  // Perform calculations
267  DATA_TYPE data0_max = POOL_OP(data0.s0, data0.s1);
268  DATA_TYPE data1_max = POOL_OP(data1.s0, data1.s1);
269  DATA_TYPE res = POOL_OP(data0_max, data1_max);
270  // Store result
271  *(__global DATA_TYPE *)(dst_ptr + dst_offset_first_element_in_bytes + id0 * sizeof(DATA_TYPE) + id1 * dst_stride_y + id2 * dst_stride_z) = res;
272 
273 #if defined(SRC_BATCH)
274 
275  uint offset_top = (x_coords.s0 + y_coords.s0 * SRC_WIDTH + id2 * (SRC_WIDTH * SRC_HEIGHT)) % SRC_BATCH;
276  uint offset_bottom = offset_top + SRC_WIDTH;
277 
278  uint index0 = select(offset_top + 1, offset_top, isgreaterequal(data0.s0, data0.s1));
279  uint index1 = select(offset_bottom + 1, offset_bottom, isgreaterequal(data1.s0, data1.s1));
280  uint index = select(index1, index0, isgreaterequal(data0_max, data1_max));
281 
282  *(__global uint *)(indices_ptr + indices_offset_first_element_in_bytes + id0 * sizeof(uint) + id1 * indices_stride_y + id2 * indices_stride_z) = index;
283 
284 #endif // defined(SRC_BATCH)
285 }
#define POOL_OP(x, y)
#define VEC_OFFS(dt, s)
Definition: helpers.h:200
DataType clamp(const DataType &n, const DataType &lower=std::numeric_limits< RangeType >::lowest(), const DataType &upper=std::numeric_limits< RangeType >::max())
Performs clamping among a lower and upper value.
Definition: Utility.h:101
SimpleTensor< T > select(const SimpleTensor< uint8_t > &c, const SimpleTensor< T > &x, const SimpleTensor< T > &y)
Definition: Select.cpp:38
#define VLOAD(size)
Definition: helpers.h:204
#define VEC_DATA_TYPE(type, size)
Definition: helpers.h:728