xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/common/bounding_box_transform.cl (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1/*
2 * Copyright (c) 2018-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#if defined(DATA_TYPE) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) && defined(SCALE_BEFORE) // Check for compile time constants
27
28/** Transform proposal bounding boxes to target bounding box using bounding box deltas.
29 *
30 * @attention The following variables must be passed at compile time:
31 * -# -DDATA_TYPE= Tensor data type. Supported data types: F16/F32
32 * -# -DWEIGHT{X,Y,W,H}= Weights [wx, wy, ww, wh] for the deltas
33 * -# -DIMG_WIDTH= Original image width
34 * -# -DIMG_HEIGHT= Original image height
35 * -# -DBOX_FIELDS= Number of fields that are used to represent a box in boxes
36 *
37 * @param[in]  boxes_ptr                                Pointer to the boxes tensor. Supported data types: F16/F32
38 * @param[in]  boxes_stride_x                           Stride of the boxes tensor in X dimension (in bytes)
39 * @param[in]  boxes_step_x                             boxes_stride_x * number of elements along X processed per workitem(in bytes)
40 * @param[in]  boxes_stride_y                           Stride of the boxes tensor in Y dimension (in bytes)
41 * @param[in]  boxes_step_y                             boxes_stride_y * number of elements along Y processed per workitem(in bytes)
42 * @param[in]  boxes_stride_z                           Stride of the source tensor in Z dimension (in bytes)
43 * @param[in]  boxes_step_z                             boxes_stride_z * number of elements along Z processed per workitem(in bytes)
44 * @param[in]  boxes_offset_first_element_in_bytes      The offset of the first element in the boxes tensor
45 * @param[out] pred_boxes_ptr                           Pointer to the predicted boxes. Supported data types: same as @p in_ptr
46 * @param[in]  pred_boxes_stride_x                      Stride of the predicted boxes in X dimension (in bytes)
47 * @param[in]  pred_boxes_step_x                        pred_boxes_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[in]  pred_boxes_stride_y                      Stride of the predicted boxes in Y dimension (in bytes)
49 * @param[in]  pred_boxes_step_y                        pred_boxes_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[in]  pred_boxes_stride_z                      Stride of the predicted boxes in Z dimension (in bytes)
51 * @param[in]  pred_boxes_step_z                        pred_boxes_stride_z * number of elements along Z processed per workitem(in bytes)
52 * @param[in]  pred_boxes_offset_first_element_in_bytes The offset of the first element in the predicted boxes
53 * @param[in]  deltas_ptr                               Pointer to the deltas tensor. Supported data types: same as @p in_ptr
54 * @param[in]  deltas_stride_x                          Stride of the deltas tensor in X dimension (in bytes)
55 * @param[in]  deltas_step_x                            deltas_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in]  deltas_stride_y                          Stride of the deltas tensor in Y dimension (in bytes)
57 * @param[in]  deltas_step_y                            deltas_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in]  deltas_stride_z                          Stride of the source tensor in Z dimension (in bytes)
59 * @param[in]  deltas_step_z                            deltas_stride_z * number of elements along Z processed per workitem(in bytes)
60 * @param[in]  deltas_offset_first_element_in_bytes     The offset of the first element in the deltas tensor
61 */
62__kernel void bounding_box_transform(
63    VECTOR_DECLARATION(boxes),
64    IMAGE_DECLARATION(pred_boxes),
65    IMAGE_DECLARATION(deltas))
66{
67    // Get pixels pointer
68    Vector boxes      = CONVERT_TO_VECTOR_STRUCT_NO_STEP(boxes);
69    Image  pred_boxes = CONVERT_TO_IMAGE_STRUCT(pred_boxes);
70    Image  deltas     = CONVERT_TO_IMAGE_STRUCT(deltas);
71
72    // Load delta and box values into registers
73    const DATA_TYPE one     = (DATA_TYPE)1.f;
74    const DATA_TYPE halfone = (DATA_TYPE)0.5f;
75
76    const int py = get_global_id(1); // box
77    const VEC_DATA_TYPE(DATA_TYPE, 4)
78    scale_before = (VEC_DATA_TYPE(DATA_TYPE, 4))SCALE_BEFORE;
79    VEC_DATA_TYPE(DATA_TYPE, 4)
80    delta = vload4(0, (__global DATA_TYPE *)deltas.ptr);
81    const VEC_DATA_TYPE(DATA_TYPE, 4)
82    box = vload4(0, (__global DATA_TYPE *)vector_offset(&boxes, BOX_FIELDS * py)) / scale_before;
83
84    // Calculate width and centers of the old boxes
85    const VEC_DATA_TYPE(DATA_TYPE, 2)
86    dims = box.s23 - box.s01 + one;
87    const VEC_DATA_TYPE(DATA_TYPE, 2)
88    ctr = box.s01 + halfone * dims;
89    const VEC_DATA_TYPE(DATA_TYPE, 4)
90    weights = (VEC_DATA_TYPE(DATA_TYPE, 4))(WEIGHT_X, WEIGHT_Y, WEIGHT_W, WEIGHT_H);
91    delta /= weights;
92    delta.s23 = min(delta.s23, (DATA_TYPE)BBOX_XFORM_CLIP);
93
94    // Calculate widths and centers of the new boxes (translation + aspect ratio transformation)
95    const VEC_DATA_TYPE(DATA_TYPE, 2)
96    pred_ctr = delta.s01 * dims + ctr;
97    const VEC_DATA_TYPE(DATA_TYPE, 2)
98    pred_dims = exp(delta.s23) * dims;
99
100    // Useful vector constant definitions
101    const VEC_DATA_TYPE(DATA_TYPE, 4)
102    max_values = (VEC_DATA_TYPE(DATA_TYPE, 4))(IMG_WIDTH - 1, IMG_HEIGHT - 1, IMG_WIDTH - 1, IMG_HEIGHT - 1);
103    const VEC_DATA_TYPE(DATA_TYPE, 4)
104    sign = (VEC_DATA_TYPE(DATA_TYPE, 4))(-1, -1, 1, 1);
105    const VEC_DATA_TYPE(DATA_TYPE, 4)
106    min_values = 0;
107
108    // Calculate the coordinates of the new boxes
109    VEC_DATA_TYPE(DATA_TYPE, 4)
110    pred_box = pred_ctr.s0101 + sign * halfone * pred_dims.s0101;
111#ifdef OFFSET // Possibly adjust the predicted boxes
112    pred_box.s23 -= one;
113#endif // Possibly adjust the predicted boxes
114    pred_box = CLAMP(pred_box, min_values, max_values);
115#ifdef SCALE_AFTER // Possibly scale the predicted boxes
116    pred_box *= (VEC_DATA_TYPE(DATA_TYPE, 4))SCALE_AFTER;
117#endif // Possibly scale the predicted boxes
118
119    // Store them into the output
120    vstore4(pred_box, 0, (__global DATA_TYPE *)pred_boxes.ptr);
121}
122
123#endif // defined(DATA_TYPE) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) && defined(SCALE_BEFORE)
124