xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/common/roi_pooling_layer.cl (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1/*
2 * Copyright (c) 2017-2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "helpers.h"
25#include "helpers_asymm.h"
26
27#if DATA_SIZE == 32
28#define VEC_SIZE 4
29#define VEC_MAX vec4_max
30#elif DATA_SIZE == 16
31#define VEC_SIZE 8
32#define VEC_MAX vec8_max
33#elif DATA_SIZE == 8
34#define VEC_SIZE 16
35#define VEC_MAX vec16_max
36#else /* DATA_SIZE not equals 8, 16, 32 */
37#error "Unsupported data size"
38#endif /* DATA_SIZE == 32 */
39
40// Define whether to use max (Quantized datatype) or fmax (Float) functions
41#if defined(OFFSET_OUT) && defined(SCALE_OUT)
42#define MAX(x, y) max(x, y)
43#else // !(defined(OFFSET_OUT) && defined(SCALE_OUT)
44#define MAX(x, y) fmax(x, y)
45#endif // defined(OFFSET_OUT) && defined(SCALE_OUT)
46
47inline DATA_TYPE vec4_max(VEC_DATA_TYPE(DATA_TYPE, 4) vec)
48{
49    VEC_DATA_TYPE(DATA_TYPE, 2)
50    temp = MAX(vec.lo, vec.hi);
51    return MAX(temp.x, temp.y);
52}
53
54inline DATA_TYPE vec8_max(VEC_DATA_TYPE(DATA_TYPE, 8) vec)
55{
56    VEC_DATA_TYPE(DATA_TYPE, 4)
57    temp = MAX(vec.lo, vec.hi);
58    return vec4_max(temp);
59}
60
61inline DATA_TYPE vec16_max(VEC_DATA_TYPE(DATA_TYPE, 16) vec)
62{
63    VEC_DATA_TYPE(DATA_TYPE, 8)
64    temp = MAX(vec.lo, vec.hi);
65    return vec8_max(temp);
66}
67
68/** Performs a roi pooling on a single output pixel.
69 *
70 * @param[in] input          Pointer to input Tensor3D struct.
71 * @param[in] region_start_x Start x index projected onto the input tensor.
72 * @param[in] region_end_x   End x index projected onto the input tensor.
73 * @param[in] region_start_y Start y index projected onto the input tensor.
74 * @param[in] region_end_y   End y index projected onto the input tensor.
75 * @param[in] pz             z index of the input tensor.
76 *
77 * @return A max pooled value from the region specified in the input tensor.
78 */
79inline DATA_TYPE roi_pool_1x1(const Tensor3D *input, int region_start_x, int region_end_x, int region_start_y, int region_end_y, int pz)
80{
81    // Iterate through the pooling region
82    if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
83    {
84        return (DATA_TYPE)0;
85    }
86    else
87    {
88        int num_iter = (int)((region_end_x - region_start_x) / VEC_SIZE);
89        VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
90        curr_max = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))(MIN_VALUE);
91
92        for(int j = region_start_y; j < region_end_y; ++j)
93        {
94            int i = region_start_x;
95            for(; i < region_start_x + num_iter * VEC_SIZE; i += VEC_SIZE)
96            {
97                VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
98                val      = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(input, i, j, pz));
99                curr_max = MAX(val, curr_max);
100            }
101            for(; i < region_end_x; ++i)
102            {
103                DATA_TYPE val = *(__global DATA_TYPE *)tensor3D_offset(input, i, j, pz);
104                curr_max      = MAX(curr_max, val);
105            }
106        }
107
108        const DATA_TYPE temp = (DATA_TYPE)VEC_MAX(curr_max);
109
110#if defined(OFFSET_OUT) && defined(SCALE_OUT)
111        return QUANTIZE(temp, OFFSET_OUT, SCALE_OUT, DATA_TYPE, 1);
112#endif /* if quantized, requantize and return */
113
114        return temp;
115    }
116}
117
118/** Performs a roi pooling function.
119 *
120 * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16, F32, QASYMM8;
121 * @note Datasize must be passed using -DDATA_SIZE e.g. -DDATA_SIZE=32;
122 * @note Input dimensions must be passed using -DMAX_DIM_X, -DMAX_DIM_Y and -DMAX_DIM_Z;
123 * @note Pooled region dimensions must be passed using -DPOOLED_DIM_X and -DPOOLED_DIM_Y;
124 * @note Spatial scale must be passed using -DSPATIAL_SCALE;
125 *
126 * @param[in]  input_ptr                            Pointer to the source image. Supported data types: F16, F32, QASYMM8
127 * @param[in]  input_stride_x                       Stride of the source image in X dimension (in bytes)
128 * @param[in]  input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
129 * @param[in]  input_stride_y                       Stride of the source image in Y dimension (in bytes)
130 * @param[in]  input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
131 * @param[in]  input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
132 * @param[in]  input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
133 * @param[in]  input_offset_first_element_in_bytes  The offset of the first element in the pooled region of the source image as specifed by ROI
134 * @param[in]  rois_ptr                             Pointer to the ROIs tensor. Layout: { batch_index, x1, y1, x2, y2 }. Supported data types: same as @p input_ptr
135 * @param[in]  rois_stride_x                        Stride of the ROIs tensor in X dimension (in bytes)
136 * @param[in]  rois_step_x                          Step of the ROIs tensor in X dimension (in bytes)
137 * @param[in]  rois_stride_y                        Stride of the ROIs tensor in Y dimension (in bytes)
138 * @param[in]  rois_step_y                          Step of the ROIs tensor in Y dimension (in bytes)
139 * @param[in]  rois_offset_first_element_in_bytes   The offset of the first element in the ROIs tensor
140 * @param[out] output_ptr                           Pointer to the destination image. Supported data types: same as input
141 * @param[in]  output_stride_x                      Stride of the destination image in X dimension (in bytes)
142 * @param[in]  output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
143 * @param[in]  output_stride_y                      Stride of the destination image in Y dimension (in bytes)
144 * @param[in]  output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
145 * @param[in]  output_stride_z                      Stride of the destination tensor in Z dimension (in bytes)
146 * @param[in]  output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
147 * @param[in]  output_offset_first_element_in_bytes The offset of the first element in the destination image
148 * @param[in]  input_stride_w                       Stride of the source image in W dimension (in bytes)
149 * @param[in]  output_stride_w                      Stride of the destination image in W dimension (in bytes)
150 */
151__kernel void roi_pooling_layer(
152    TENSOR3D_DECLARATION(input),
153    IMAGE_DECLARATION(rois),
154    TENSOR3D_DECLARATION(output),
155    unsigned int input_stride_w, unsigned int output_stride_w)
156{
157    // Get pixels pointer
158    Tensor3D input  = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
159    Image    rois   = CONVERT_TO_IMAGE_STRUCT_NO_STEP(rois);
160    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
161
162    const int px = get_global_id(0);
163    const int py = get_global_id(1);
164    const int pw = get_global_id(2);
165
166    // Load roi parameters
167    // roi is laid out as follows { batch_index, x1, y1, x2, y2 }
168    const ushort roi_batch = (ushort) * ((__global ushort *)offset(&rois, 0, pw));
169    const VEC_DATA_TYPE(ushort, 4)
170    roi               = vload4(0, (__global ushort *)offset(&rois, 1, pw));
171    const int2 roi_anchor = convert_int2_sat(round(convert_float2(roi.s01) * (float)SPATIAL_SCALE));
172    const int2 roi_dims   = convert_int2_sat(fmax(round(convert_float2(roi.s23 - roi.s01) * (float)SPATIAL_SCALE), 1.f));
173
174    // Calculate pooled region start and end
175    const float2 spatial_indx     = (float2)(px, py);
176    const float2 pooled_dims      = (float2)(POOLED_DIM_X, POOLED_DIM_Y);
177    const int2   max_spatial_dims = (int2)(MAX_DIM_X, MAX_DIM_Y);
178    int2         region_start     = convert_int2_sat(floor(spatial_indx / pooled_dims * convert_float2(roi_dims))) + roi_anchor;
179    int2         region_end       = convert_int2_sat(floor((spatial_indx + 1) / pooled_dims * convert_float2(roi_dims))) + roi_anchor;
180
181    region_start = clamp(region_start, 0, max_spatial_dims);
182    region_end   = clamp(region_end, 0, max_spatial_dims);
183
184    // Move input and output pointer across the fourth dimension
185    input.ptr += roi_batch * input_stride_w;
186    output.ptr += pw * output_stride_w;
187
188    for(int pz = 0; pz < MAX_DIM_Z; ++pz)
189    {
190        *(__global DATA_TYPE *)tensor3D_offset(&output, px, py, pz) = (__global DATA_TYPE)roi_pool_1x1(&input,
191                                                                                                       region_start.x,
192                                                                                                       region_end.x,
193                                                                                                       region_start.y,
194                                                                                                       region_end.y, pz);
195    }
196}
197