From e74b201ca1abca040ca9f30837fdf19aa610e7c4 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Wed, 18 Apr 2018 09:49:16 +0100 Subject: COMPMID-805 Add NHWC data format support for CL pooling Change-Id: I3d91fde78b971aba3f6349f633cd9b1c50e5cacf Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/124712 Reviewed-by: Gian Marco Iodice Tested-by: Jenkins Reviewed-by: Georgios Pinitas --- src/core/CL/cl_kernels/pooling_layer_quantized.cl | 82 ++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) (limited to 'src/core/CL/cl_kernels/pooling_layer_quantized.cl') diff --git a/src/core/CL/cl_kernels/pooling_layer_quantized.cl b/src/core/CL/cl_kernels/pooling_layer_quantized.cl index 98850c00a5..17d893a013 100644 --- a/src/core/CL/cl_kernels/pooling_layer_quantized.cl +++ b/src/core/CL/cl_kernels/pooling_layer_quantized.cl @@ -31,6 +31,8 @@ #define DIV_OP(x, y) (x * (1.f / y)) +#define DIV_OP_NHWC(x, y) (convert_float8(x) * (float8)(1.f / y)) + #if defined(POOL_L2) #error "L2 pooling is not supported" #endif /* defined(POOL_L2) */ @@ -49,7 +51,7 @@ int calculate_avg_scale(const int pool_size_x, const int pool_size_y, const int return ((end_y - start_y) * (end_x - start_x)); } -/** Performs a pooling function of pool size equal to N +/** Performs a pooling function of pool size equal to N (NCHW) * * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; * @note In case of average pooling the following information must be passed at compile time: @@ -75,7 +77,7 @@ int calculate_avg_scale(const int pool_size_x, const int pool_size_y, const int * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image */ -__kernel void pooling_layer_MxN_quantized( +__kernel void pooling_layer_MxN_quantized_nchw( TENSOR3D_DECLARATION(input), TENSOR3D_DECLARATION(output)) { @@ -119,3 +121,79 @@ __kernel void pooling_layer_MxN_quantized( // Store result *(__global uchar *)output.ptr = convert_uchar(res); } + +int calculate_avg_scale_nhwc(const int pool_size_x, const int pool_size_y, int upper_bound_w, int upper_bound_h, + const int pad_x, const int pad_y, const int stride_x, const int stride_y) +{ + int start_x = get_global_id(1) * stride_x - pad_x; + int start_y = get_global_id(2) * stride_y - pad_y; + + const int end_x = min(start_x + pool_size_x, upper_bound_w); + const int end_y = min(start_y + pool_size_y, upper_bound_h); + + start_x = max(0, start_x); + start_y = max(0, start_y); + + return ((end_y - start_y) * (end_x - start_x)); +} + +/** Performs a pooling function of pool size equal to N (NHWC) + * + * @note Pool sizes must be passed using -DPOOL_SIZE_X and -DPOOL_SIZE_Y e.g. -DPOOL_SIZE_X=13; + * @note Tensors width and height must be passed at compile time using -DMAX_WIDTH and -DMAX_HEIGHT + * @note 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 + * @note Pad values must be passed at compile time using -DPAD_X and -DPAD_Y which are the pooling paddings in x and y dimension + * @note In case of average pooling the following information must be passed at compile time: + * -DPOOL_AVG must be provided otherwise max pooling will be performed. + * + * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8 + * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) + * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) + * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image + * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr + * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) + * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) + * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image + */ +__kernel void pooling_layer_MxN_quantized_nhwc( + TENSOR3D_DECLARATION(input), + TENSOR3D_DECLARATION(output)) +{ + // Get pixels pointer + Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); + Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); + + int8 vdata = 0; + + const int idx_width = get_global_id(1) * STRIDE_X; + const int idx_height = get_global_id(2) * STRIDE_Y; + + for(int y = 0; y < POOL_SIZE_Y; ++y) + { + int y1 = select(y, PAD_Y - idx_height, y + idx_height < PAD_Y || y + idx_height > MAX_HEIGHT); + for(int x = 0; x < POOL_SIZE_X; ++x) + { + int x1 = select(x, PAD_X - idx_width - 1, x + idx_width < PAD_X || x + idx_width > MAX_WIDTH); + x1 = select(x1, PAD_X - idx_width - 1, y != y1); + uchar8 data = vload8(0, (__global uchar *)tensor3D_offset(&input, 0, x1 - PAD_X, y1 - PAD_Y)); + int8 data0 = convert_int8(data); + vdata = POOL_OP(vdata, data0); + } + } + +#if defined(POOL_AVG) + // Divide by pool region in case of average pooling + vdata = convert_int8(round(DIV_OP_NHWC(vdata, calculate_avg_scale_nhwc(POOL_SIZE_X, POOL_SIZE_Y, MAX_WIDTH, MAX_HEIGHT, PAD_X, PAD_Y, STRIDE_X, STRIDE_Y)))); +#endif /* defined(POOL_AVG) */ + + // Store result + vstore8(convert_uchar8(vdata), 0, (__global uchar *)output.ptr); +} \ No newline at end of file -- cgit v1.2.1