xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/common/pad_layer.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#if defined(DATA_TYPE) && defined(VEC_SIZE) && defined(PAD_X_BEFORE) && defined(SRC_WIDTH) && defined(PAD_X_BEFORE_REMAINDER) && defined(VEC_SIZE_LEFTOVER_WRITE)
27
28#define VEC_TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
29#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
30#define VEC_SELECT SELECT_VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
31#define OFFSETS VEC_OFFS(SELECT_DATA_TYPE(DATA_TYPE), VEC_SIZE)
32#define SCALAR_COND(x) CONVERT((VEC_SELECT)x == (VEC_SELECT)1, VEC_SELECT)
33
34#if defined(CONST_VAL) && defined(VEC_SIZE_LEFTOVER_READ)
35/** Perform a pad operation when PaddingMode is CONSTANT
36 *
37 * @note Data type can be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float
38 * @note Vector size must be passed using the -DVEC_SIZE compile flag, e.g. -DVEC_SIZE=4
39 * @note Constant value used to fill the pads must be passed using the -DCONST_VAL compile flag, e.g. -DCONST_VAL=1.27
40 * @note Pad to add to the left must be passed using the -DPAD_X_BEFORE compile flag, e.g. -DPAD_X_BEFORE=5
41 * @note Input tensor's width must be passed using the -DSRC_WIDTH compile flag, e.g. -DSRC_WIDTH=224
42 * @note In case pad left is more than the vector size, the number of threads to skip along the X axis must be passed using the
43 *       -DTHREADS_TO_SKIP_BEFORE compile flag, e.g. -DTHREADS_TO_SKIP_BEFORE=1. This is defined as (PAD_X_BEFORE / VEC_SIZE)
44 * @note In case pad left is more than the vector size, the thread from which to skip along the X axis for pad right must be passed using the
45 *       -DTHREADS_TO_SKIP_AFTER compile flag, e.g. -THREADS_TO_SKIP_AFTER=1. This is defined as ((SRC_WIDTH + PAD_X_BEFORE) / VEC_SIZE)
46 * @note If pad also needs to be added to the top of the tensor, the following compile flags must be passed at compile time:
47 *       -# -DPAD_Y_BEFORE: Pad to add to the top of the input tensor (e.g. -DPAD_Y_BEFORE=3)
48 *       -# -DSRC_HEIGHT: Input tensor's height (e.g. -DSRC_HEIGHT=127)
49 * @note If pad also needs to be added to the depth of the tensor, the following compile flags must be passed at compile time:
50 *       -# -DPAD_Z_BEFORE: Pad to add before the first plane of the input tensor (e.g. -DPAD_Z_BEFORE=3)
51 *       -# -DSRC_DEPTH: Input tensor's depth (e.g. -DSRC_DEPTH=32)
52 * @note If pad also needs to be added to the batch of the tensor, the following compile flags must be passed at compile time:
53 *       -# -DPAD_W_BEFORE: Pad to add before the first batch of the input tensor (e.g. -DPAD_W_BEFORE=3)
54 *       -# -DSRC_BATCH: Input tensor's batch size (e.g. -DSRC_BATCH=4)
55 *
56 * @param[in]  src_ptr                           Pointer to the source image. Supported data types: All
57 * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
58 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
59 * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
60 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
61 * @param[in]  src_stride_z                      Stride of the source image in Z dimension (in bytes)
62 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
63 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
64 * @param[out] dst_ptr                           Pointer to the destination image. Supported data types: same as @p src_ptr
65 * @param[in]  dst_stride_x                      Stride of the destination image in X dimension (in bytes)
66 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
67 * @param[in]  dst_stride_y                      Stride of the destination image in Y dimension (in bytes)
68 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
69 * @param[in]  dst_stride_z                      Stride of the destination image in Z dimension (in bytes)
70 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
71 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination image
72 * @param[in]  batch                             (Optional) Batch index if 4D pad must be applied
73 */
74__kernel void pad_layer_constant(TENSOR3D_DECLARATION(src),
75                                 TENSOR3D_DECLARATION(dst)
76#if defined(PAD_W_BEFORE)
77                                 ,
78                                 uint batch
79#endif // defined(PAD_W_BEFORE)
80                                )
81{
82    Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
83
84    int x = get_global_id(0);
85    int y = get_global_id(1);
86    int z = get_global_id(2);
87
88    // If true, write only padding values; no reads performed
89    uint cond = 0;
90#if defined(THREADS_TO_SKIP_BEFORE)
91    cond |= x < THREADS_TO_SKIP_BEFORE || x > THREADS_TO_SKIP_AFTER;
92#endif // defined(THREADS_TO_SKIP_BEFORE)
93#if defined(PAD_Y_BEFORE)
94    cond |= y < PAD_Y_BEFORE || y >= (SRC_HEIGHT + PAD_Y_BEFORE);
95#endif // defined(PAD_Y_BEFORE)
96#if defined(PAD_Z_BEFORE)
97    cond |= z < PAD_Z_BEFORE || z >= (SRC_DEPTH + PAD_Z_BEFORE);
98#endif // defined(PAD_Z_BEFORE)
99#if defined(PAD_W_BEFORE)
100    cond |= batch < PAD_W_BEFORE || batch >= (SRC_BATCH + PAD_W_BEFORE);
101#endif // defined(PAD_W_BEFORE)
102
103    if(cond)
104    {
105        VEC_TYPE const_vals0 = (VEC_TYPE)CONST_VAL;
106        STORE_VECTOR_SELECT(const_vals, DATA_TYPE, dst.ptr, VEC_SIZE, VEC_SIZE_LEFTOVER_WRITE, get_global_id(0) == (get_global_size(0) - 1));
107    }
108    else
109    {
110        // Calculate input's coordinates based on output's
111        int w = 0;
112#if defined(THREADS_TO_SKIP_BEFORE)
113        x -= THREADS_TO_SKIP_BEFORE;
114#endif // defined(THREADS_TO_SKIP_BEFORE)
115#if defined(PAD_Y_BEFORE)
116        y -= PAD_Y_BEFORE;
117#endif // defined(PAD_Y_BEFORE)
118#if defined(PAD_Z_BEFORE)
119        z -= PAD_Z_BEFORE;
120#endif // defined(PAD_Z_BEFORE)
121#if defined(PAD_W_BEFORE)
122        w -= PAD_W_BEFORE * SRC_DEPTH;
123#endif // defined(PAD_W_BEFORE)
124        x *= VEC_SIZE;
125        x -= PAD_X_BEFORE_REMAINDER;
126
127        // Check for out of bound reads and clamp X coordinate
128        uint cond_left  = x < 0;
129        uint cond_right = (x + VEC_SIZE) > SRC_WIDTH;
130        x               = clamp(x, 0, (SRC_WIDTH - VEC_SIZE));
131
132        // Calculate input's address
133        __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * src_stride_x + y * src_stride_y + z * src_stride_z + w * (int)src_stride_z;
134
135        // Read values and rotate them properly if they would have been across paddings
136        VEC_TYPE src_vals0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src_addr);
137        src_vals0          = select(src_vals0, ROTATE(src_vals0, VEC_SIZE, PAD_X_BEFORE_REMAINDER), SCALAR_COND(cond_left));
138        src_vals0          = select(src_vals0, ROTATE(src_vals0, VEC_SIZE, VEC_SIZE_LEFTOVER_READ), SCALAR_COND(cond_right));
139
140        // Check what values would be padding and replace them with the constant value
141        VEC_INT xs_out = (VEC_INT)(get_global_id(0) * VEC_SIZE) + VEC_OFFS(int, VEC_SIZE);
142        VEC_INT conds  = xs_out < (VEC_INT)PAD_X_BEFORE || xs_out >= (VEC_INT)(SRC_WIDTH + PAD_X_BEFORE);
143        src_vals0      = select(src_vals0, (VEC_TYPE)CONST_VAL, CONVERT(conds, VEC_SELECT));
144
145        // Store values in bounds
146        STORE_VECTOR_SELECT(src_vals, DATA_TYPE, dst.ptr, VEC_SIZE, VEC_SIZE_LEFTOVER_WRITE, get_global_id(0) == (get_global_size(0) - 1));
147    }
148}
149#endif // defined(CONST_VAL) && defined(VEC_SIZE_LEFTOVER_READ)
150
151#if defined(IS_REFLECT) && defined(PAD_X_AFTER_REMAINDER) && defined(PAD_X_BEFORE_REMAINDER_REFL) && defined(PAD_X_AFTER_REMAINDER_REFL) && defined(AFTER_PAD_FACT_X)
152
153#define ROTATE_REVERSE(x, n) ROTATE(REVERSE(x, VEC_SIZE), VEC_SIZE, n)
154#define SYMM_REFL_LEFT(x, n0, n1) select(ROTATE_REVERSE(x, n1), ROTATE(x, VEC_SIZE, n0), OFFSETS >= (VEC_SELECT)n0)
155#define SYMM_REFL_RIGHT(x, n0, n1) select(ROTATE(x, VEC_SIZE, n0), ROTATE_REVERSE(x, n1), OFFSETS >= (VEC_SELECT)n0)
156
157/** Perform a pad operation when PaddingMode is SYMMETRIC
158 *
159 * @note Data type can be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float
160 * @note Vector size must be passed using the -DVEC_SIZE compile flag, e.g. -DVEC_SIZE=4
161 * @note Constant value must be passed using the -DCONST_VAL compile flag, e.g. -DCONST_VAL=1.27
162 * @note Pad to add to the left must be passed using the -DPAD_X_BEFORE compile flag, e.g. -DPAD_X_BEFORE=5
163 * @note Input tensor's width must be passed using the -DSRC_WIDTH compile flag, e.g. -DSRC_WIDTH=224
164 * @note Number of values to the left when operating across left padding must be passed using the -DPAD_X_BEFORE_REMAINDER compile flag, e.g. -DPAD_X_BEFORE_REMAINDER=5
165 * @note Number of values to the left when operating across right padding must be passed using the -DPAD_X_AFTER_REMAINDER compile flag, e.g. -DPAD_X_AFTER_REMAINDER=6
166 * @note To rearrange the vectors properly, (PAD_X_BEFORE_REMAINDER + 1) must be passed when mode is REFLECT using the -DPAD_X_BEFORE_REMAINDER_REFL compile flag, e.g. -DPAD_X_BEFORE_REMAINDER=6
167 * @note To rearrange the vectors properly, (PAD_X_AFTER_REMAINDER - 1) must be passed using the -DPAD_X_AFTER_REMAINDER_REFL compile flag, e.g. -DPAD_X_AFTER_REMAINDER=5
168 * @note When after pad X, starting point to read backward from must be passed using the -DAFTER_PAD_FACT_X compile flag, e.g. -DAFTER_PAD_FACT_X=253
169 * @note If padding mode is REFLECT, the -DIS_REFLECT compile flag must be set to 1, else it must be set to 0
170 * @note If pad also needs to be added to the top of the tensor, the following compile flags must be passed at compile time:
171 *       -# -DPAD_Y_BEFORE: Pad to add to the top of the input tensor (e.g. -DPAD_Y_BEFORE=3)
172 *       -# -DSRC_HEIGHT: Input tensor's height (e.g. -DSRC_HEIGHT=127)
173 * @note If pad also needs to be added to the depth of the tensor, the following compile flags must be passed at compile time:
174 *       -# -DPAD_Z_BEFORE: Pad to add before the first plane of the input tensor (e.g. -DPAD_Z_BEFORE=3)
175 *       -# -DSRC_DEPTH: Input tensor's depth (e.g. -DSRC_DEPTH=32)
176 * @note If the starting point to read backward from is less than the output's last element accessed in the X, the following compile flags must be passed at compile time to avoid negative offsets:
177 *       -# -DAFTER_PAD_REM: Defines how much to rotate the vector if the backward calculation attempted to read from a negative offset (e.g. -DAFTER_PAD_REM=3)
178 *
179 * @param[in]  src_ptr                           Pointer to the source image. Supported data types: All
180 * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
181 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
182 * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
183 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
184 * @param[in]  src_stride_z                      Stride of the source image in Z dimension (in bytes)
185 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
186 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
187 * @param[out] dst_ptr                           Pointer to the destination image. Supported data types: same as @p src_ptr
188 * @param[in]  dst_stride_x                      Stride of the destination image in X dimension (in bytes)
189 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
190 * @param[in]  dst_stride_y                      Stride of the destination image in Y dimension (in bytes)
191 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
192 * @param[in]  dst_stride_z                      Stride of the destination image in Z dimension (in bytes)
193 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
194 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination image
195 */
196__kernel void pad_layer_symmetric_reflect(TENSOR3D_DECLARATION(src),
197                                          TENSOR3D_DECLARATION(dst))
198{
199    // Get current thread position
200    const int x = get_global_id(0);
201    const int y = get_global_id(1);
202    const int z = get_global_id(2);
203
204    // Define conditions based on the thread X position w.r.t. pad left and right
205    const int x_out_first         = x * VEC_SIZE;
206    const int x_out_last          = x_out_first + VEC_SIZE;
207    const int is_before_pad_left  = (x_out_last <= PAD_X_BEFORE);
208    const int is_across_pad_left  = (x_out_first < PAD_X_BEFORE) && (x_out_last > PAD_X_BEFORE);
209    const int is_inside_input     = (x_out_first >= PAD_X_BEFORE) && (x_out_last <= (SRC_WIDTH + PAD_X_BEFORE));
210    const int is_across_pad_right = (x_out_first < (SRC_WIDTH + PAD_X_BEFORE)) && (x_out_last > (SRC_WIDTH + PAD_X_BEFORE));
211    const int is_after_pad_right  = (x_out_first >= (SRC_WIDTH + PAD_X_BEFORE));
212
213    // Calculate base pointers
214    __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes;
215    Tensor3D        dst      = CONVERT_TO_TENSOR3D_STRUCT(dst);
216
217    // Calculate input tensor's offset based on the defined conditions
218    int x_offset = 0;
219    x_offset     = select(x_offset, PAD_X_BEFORE - x_out_last + IS_REFLECT, is_before_pad_left);
220    x_offset     = select(x_offset, x_out_first - PAD_X_BEFORE, is_inside_input);
221    x_offset     = select(x_offset, SRC_WIDTH - VEC_SIZE, is_across_pad_right);
222    x_offset     = select(x_offset, AFTER_PAD_FACT_X - x_out_last, is_after_pad_right);
223
224#if defined(AFTER_PAD_REM)
225    int neg_offs = x_offset < 0;
226    x_offset     = max(x_offset, 0);
227#endif // defined(AFTER_PAD_REM)
228
229    // Load input values from the computed offset
230    int y_in = y;
231    int z_in = z;
232#if defined(PAD_Y_BEFORE)
233    y_in = select(y - PAD_Y_BEFORE, PAD_Y_BEFORE - y + IS_REFLECT - 1, y < PAD_Y_BEFORE);
234    y_in = select(y_in, 2 * SRC_HEIGHT + PAD_Y_BEFORE - y - IS_REFLECT - 1, y >= (SRC_HEIGHT + PAD_Y_BEFORE));
235#endif // defined(PAD_Y_BEFORE)
236#if defined(PAD_Z_BEFORE)
237    z_in = select(z - PAD_Z_BEFORE, PAD_Z_BEFORE - z + IS_REFLECT - 1, z < PAD_Z_BEFORE);
238    z_in = select(z_in, 2 * SRC_DEPTH + PAD_Z_BEFORE - z - IS_REFLECT - 1, z >= (SRC_DEPTH + PAD_Z_BEFORE));
239#endif // defined(PAD_Y_BEFORE)
240
241    src_addr += x_offset * src_stride_x + y_in * src_step_y + z_in * src_step_z;
242
243#if SRC_WIDTH == 1
244    VSTORE(VEC_SIZE)
245    ((VEC_TYPE)(*(__global DATA_TYPE *)src_addr), 0, (__global DATA_TYPE *)dst.ptr);
246#else // SRC_WIDTH == 1
247
248    VEC_TYPE src_vals0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src_addr);
249
250    // Choose rearrangement policy based on the defined conditions
251    src_vals0 = select(src_vals0, SYMM_REFL_LEFT(src_vals0, PAD_X_BEFORE_REMAINDER, PAD_X_BEFORE_REMAINDER_REFL), SCALAR_COND(is_across_pad_left));
252    src_vals0 = select(src_vals0, SYMM_REFL_RIGHT(src_vals0, PAD_X_AFTER_REMAINDER, PAD_X_AFTER_REMAINDER_REFL), SCALAR_COND(is_across_pad_right));
253    src_vals0 = select(src_vals0, REVERSE(src_vals0, VEC_SIZE), SCALAR_COND((is_before_pad_left || is_after_pad_right)));
254#if defined(AFTER_PAD_REM)
255    src_vals0 = select(src_vals0, ROTATE(src_vals0, VEC_SIZE, AFTER_PAD_REM), SCALAR_COND(neg_offs));
256#endif // defined(AFTER_PAD_REM)
257
258    // Store values in bounds
259    STORE_VECTOR_SELECT(src_vals, DATA_TYPE, dst.ptr, VEC_SIZE, VEC_SIZE_LEFTOVER_WRITE, get_global_id(0) == (get_global_size(0) - 1));
260#endif // SRC_WIDTH == 1
261}
262#endif // defined(IS_REFLECT) && defined(PAD_X_AFTER_REMAINDER) && defined(PAD_X_BEFORE_REMAINDER_REFL) && defined(PAD_X_AFTER_REMAINDER_REFL) && defined(AFTER_PAD_FACT_X)
263#endif // defined(DATA_TYPE) && defined(VEC_SIZE) && defined(PAD_X_BEFORE) && defined(SRC_WIDTH) && defined(PAD_X_BEFORE_REMAINDER) && defined(VEC_SIZE_LEFTOVER_WRITE)
264