blob: 6899b952e056392b9cfb40e6ffa513af6dbac3b9 [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
Suhail Munshi4ed7b392021-03-22 13:13:55 +00002 * Copyright (c) 2017-2021 Arm Limited.
SiCong Li3e363692017-07-04 15:02:10 +01003 *
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"
Suhail Munshi4ed7b392021-03-22 13:13:55 +000025#include "helpers_asymm.h"
SiCong Li3e363692017-07-04 15:02:10 +010026
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
Suhail Munshi4ed7b392021-03-22 13:13:55 +000033#elif DATA_SIZE == 8
34#define VEC_SIZE 16
35#define VEC_MAX vec16_max
36#else /* DATA_SIZE not equals 8, 16, 32 */
SiCong Li3e363692017-07-04 15:02:10 +010037#error "Unsupported data size"
38#endif /* DATA_SIZE == 32 */
39
Suhail Munshi4ed7b392021-03-22 13:13:55 +000040// 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
SiCong Li3e363692017-07-04 15:02:10 +010047inline DATA_TYPE vec4_max(VEC_DATA_TYPE(DATA_TYPE, 4) vec)
48{
49 VEC_DATA_TYPE(DATA_TYPE, 2)
Suhail Munshi4ed7b392021-03-22 13:13:55 +000050 temp = MAX(vec.lo, vec.hi);
51 return MAX(temp.x, temp.y);
SiCong Li3e363692017-07-04 15:02:10 +010052}
53
54inline DATA_TYPE vec8_max(VEC_DATA_TYPE(DATA_TYPE, 8) vec)
55{
56 VEC_DATA_TYPE(DATA_TYPE, 4)
Suhail Munshi4ed7b392021-03-22 13:13:55 +000057 temp = MAX(vec.lo, vec.hi);
SiCong Li3e363692017-07-04 15:02:10 +010058 return vec4_max(temp);
59}
60
Suhail Munshi4ed7b392021-03-22 13:13:55 +000061inline 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
SiCong Li3e363692017-07-04 15:02:10 +010068/** 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)
Suhail Munshi4ed7b392021-03-22 13:13:55 +000090 curr_max = (VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE))(MIN_VALUE);
91
SiCong Li3e363692017-07-04 15:02:10 +010092 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));
Suhail Munshi4ed7b392021-03-22 13:13:55 +000099 curr_max = MAX(val, curr_max);
SiCong Li3e363692017-07-04 15:02:10 +0100100 }
101 for(; i < region_end_x; ++i)
102 {
103 DATA_TYPE val = *(__global DATA_TYPE *)tensor3D_offset(input, i, j, pz);
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000104 curr_max = MAX(curr_max, val);
SiCong Li3e363692017-07-04 15:02:10 +0100105 }
106 }
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000107
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;
SiCong Li3e363692017-07-04 15:02:10 +0100115 }
116}
117
118/** Performs a roi pooling function.
119 *
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000120 * @note Datatype must be passed using -DDATA_TYPE e.g. -DDATA_TYPE=float. Supported data types are F16, F32, QASYMM8;
SiCong Li3e363692017-07-04 15:02:10 +0100121 * @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 *
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000126 * @param[in] input_ptr Pointer to the source image. Supported data types: F16, F32, QASYMM8
SiCong Li3e363692017-07-04 15:02:10 +0100127 * @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
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000134 * @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
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000140 * @param[out] output_ptr Pointer to the destination image. Supported data types: same as input
SiCong Li3e363692017-07-04 15:02:10 +0100141 * @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),
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000153 IMAGE_DECLARATION(rois),
SiCong Li3e363692017-07-04 15:02:10 +0100154 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);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000159 Image rois = CONVERT_TO_IMAGE_STRUCT_NO_STEP(rois);
SiCong Li3e363692017-07-04 15:02:10 +0100160 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
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000167 // roi is laid out as follows { batch_index, x1, y1, x2, y2 }
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000168 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));
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000171 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));
SiCong Li3e363692017-07-04 15:02:10 +0100173
174 // Calculate pooled region start and end
175 const float2 spatial_indx = (float2)(px, py);
SiCong Licfb65532017-09-12 19:06:28 +0100176 const float2 pooled_dims = (float2)(POOLED_DIM_X, POOLED_DIM_Y);
SiCong Li3e363692017-07-04 15:02:10 +0100177 const int2 max_spatial_dims = (int2)(MAX_DIM_X, MAX_DIM_Y);
SiCong Licfb65532017-09-12 19:06:28 +0100178 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;
SiCong Li3e363692017-07-04 15:02:10 +0100180
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
SiCong Licfb65532017-09-12 19:06:28 +0100185 input.ptr += roi_batch * input_stride_w;
SiCong Li3e363692017-07-04 15:02:10 +0100186 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}