xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/common/generate_proposals.cl (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1/*
2 * Copyright (c) 2019-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
26/** Generate all the region of interests based on the image size and the anchors passed in. For each element (x,y) of the
27 * grid, it will generate NUM_ANCHORS rois, given by shifting the grid position to match the anchor.
28 *
29 * @attention The following variables must be passed at compile time:
30 * -# -DDATA_TYPE= Tensor data type. Supported data types: F16/F32
31 * -# -DHEIGHT= Height of the feature map on which this kernel is applied
32 * -# -DWIDTH= Width of the feature map on which this kernel is applied
33 * -# -DNUM_ANCHORS= Number of anchors to be used to generate the rois per each pixel
34 * -# -DSTRIDE= Stride to be applied at each different pixel position (i.e., x_range = (1:WIDTH)*STRIDE and y_range = (1:HEIGHT)*STRIDE
35 * -# -DNUM_ROI_FIELDS= Number of fields used to represent a roi
36 *
37 * @param[in]  anchors_ptr                           Pointer to the anchors tensor. Supported data types: F16/F32
38 * @param[in]  anchors_stride_x                      Stride of the anchors tensor in X dimension (in bytes)
39 * @param[in]  anchors_step_x                        anchors_stride_x * number of elements along X processed per workitem(in bytes)
40 * @param[in]  anchors_stride_y                      Stride of the anchors tensor in Y dimension (in bytes)
41 * @param[in]  anchors_step_y                        anchors_stride_y * number of elements along Y processed per workitem(in bytes)
42 * @param[in]  anchors_stride_z                      Stride of the source tensor in Z dimension (in bytes)
43 * @param[in]  anchors_step_z                        anchors_stride_z * number of elements along Z processed per workitem(in bytes)
44 * @param[in]  anchors_offset_first_element_in_bytes The offset of the first element in the boxes tensor
45 * @param[out] rois_ptr                              Pointer to the rois. Supported data types: same as @p in_ptr
46 * @param[out] rois_stride_x                         Stride of the rois in X dimension (in bytes)
47 * @param[out] rois_step_x                           pred_boxes_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[out] rois_stride_y                         Stride of the rois in Y dimension (in bytes)
49 * @param[out] rois_step_y                           pred_boxes_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[out] rois_stride_z                         Stride of the rois in Z dimension (in bytes)
51 * @param[out] rois_step_z                           pred_boxes_stride_z * number of elements along Z processed per workitem(in bytes)
52 * @param[out] rois_offset_first_element_in_bytes    The offset of the first element in the rois
53 */
54#if defined(DATA_TYPE) && defined(WIDTH) && defined(HEIGHT) && defined(NUM_ANCHORS) && defined(STRIDE) && defined(NUM_ROI_FIELDS)
55__kernel void generate_proposals_compute_all_anchors(
56    VECTOR_DECLARATION(anchors),
57    VECTOR_DECLARATION(rois))
58{
59    Vector anchors = CONVERT_TO_VECTOR_STRUCT_NO_STEP(anchors);
60    Vector rois    = CONVERT_TO_VECTOR_STRUCT(rois);
61
62    const size_t idx = get_global_id(0);
63    // Find the index of the anchor
64    const size_t anchor_idx = idx % NUM_ANCHORS;
65
66    // Find which shift is this thread using
67    const size_t shift_idx = idx / NUM_ANCHORS;
68
69    // Compute the shift on the X and Y direction (the shift depends exclusively by the index thread id)
70    const DATA_TYPE
71    shift_x = (DATA_TYPE)(shift_idx % WIDTH) * STRIDE;
72    const DATA_TYPE
73    shift_y = (DATA_TYPE)(shift_idx / WIDTH) * STRIDE;
74
75    const VEC_DATA_TYPE(DATA_TYPE, NUM_ROI_FIELDS)
76    shift = (VEC_DATA_TYPE(DATA_TYPE, NUM_ROI_FIELDS))(shift_x, shift_y, shift_x, shift_y);
77
78    // Read the given anchor
79    const VEC_DATA_TYPE(DATA_TYPE, NUM_ROI_FIELDS)
80    anchor = vload4(0, (__global DATA_TYPE *)vector_offset(&anchors, anchor_idx * NUM_ROI_FIELDS));
81
82    // Apply the shift to the anchor
83    const VEC_DATA_TYPE(DATA_TYPE, NUM_ROI_FIELDS)
84    shifted_anchor = anchor + shift;
85
86    vstore4(shifted_anchor, 0, (__global DATA_TYPE *)rois.ptr);
87}
88#endif //defined(DATA_TYPE) && defined(WIDTH) && defined(HEIGHT) && defined(NUM_ANCHORS) && defined(STRIDE) && defined(NUM_ROI_FIELDS)
89