blob: fe71b5d1193b420aa2aef10fb9616dde8c72a22b [file] [log] [blame]
Giorgio Arena205eed82019-08-14 10:13:50 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Giorgio Arena205eed82019-08-14 10:13:50 +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"
25
Giorgio Arenad056e572020-10-12 11:53:51 +010026#if defined(DATA_TYPE) && defined(VEC_SIZE) && defined(PAD_X_BEFORE) && defined(SRC_WIDTH)
Giorgio Arena205eed82019-08-14 10:13:50 +010027
28#define VEC_TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
29#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
Giorgio Arena2d1a8352020-10-26 15:04:08 +000030#define VEC_SELECT SELECT_VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
31#define OFFSETS VEC_OFFS(SELECT_DATA_TYPE(DATA_TYPE), VEC_SIZE)
Giorgio Arena205eed82019-08-14 10:13:50 +010032
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010033#if defined(CONST_VAL)
34/** Perform a pad operation when PaddingMode is CONSTANT
Giorgio Arena205eed82019-08-14 10:13:50 +010035 *
36 * @note Data type can be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float
37 * @note Vector size must be passed using the -DVEC_SIZE compile flag, e.g. -DVEC_SIZE=4
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010038 * @note Constant value used to fill the pads must be passed using the -DCONST_VAL compile flag, e.g. -DCONST_VAL=1.27
39 * @note Pad to add to the left must be passed using the -DPAD_X_BEFORE compile flag, e.g. -DPAD_X_BEFORE=5
Giorgio Arena205eed82019-08-14 10:13:50 +010040 * @note Input tensor's width must be passed using the -DSRC_WIDTH compile flag, e.g. -DSRC_WIDTH=224
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010041 * @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
42 * -DNUM_THREADS_TO_SKIP_X compile flag, e.g. -DNUM_THREADS_TO_SKIP_X=1. This is defined as (PAD_X_BEFORE / VEC_SIZE)
Matthew Jacksonc2a60592019-08-30 15:19:42 +010043 * @note If pad also needs to be added to the top of the tensor, the following compile flags must be passed at compile time:
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010044 * -# -DPAD_Y_BEFORE: Pad to add to the top of the input tensor (e.g. -DPAD_Y_BEFORE=3)
Giorgio Arena205eed82019-08-14 10:13:50 +010045 * -# -DSRC_HEIGHT: Input tensor's height (e.g. -DSRC_HEIGHT=127)
Matthew Jacksonc2a60592019-08-30 15:19:42 +010046 * @note If pad also needs to be added to the depth of the tensor, the following compile flags must be passed at compile time:
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010047 * -# -DPAD_Z_BEFORE: Pad to add before the first plane of the input tensor (e.g. -DPAD_Z_BEFORE=3)
Giorgio Arena205eed82019-08-14 10:13:50 +010048 * -# -DSRC_DEPTH: Input tensor's depth (e.g. -DSRC_DEPTH=32)
Matthew Jacksonc2a60592019-08-30 15:19:42 +010049 * @note If pad also needs to be added to the batch of the tensor, the following compile flags must be passed at compile time:
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010050 * -# -DPAD_W_BEFORE: Pad to add before the first batch of the input tensor (e.g. -DPAD_W_BEFORE=3)
Matthew Jacksonc2a60592019-08-30 15:19:42 +010051 * -# -DSRC_BATCH: Input tensor's batch size (e.g. -DSRC_BATCH=4)
Giorgio Arena205eed82019-08-14 10:13:50 +010052 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010053 * @param[in] src_ptr Pointer to the source image. Supported data types: All
Giorgio Arena205eed82019-08-14 10:13:50 +010054 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
55 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
56 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
57 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
58 * @param[in] src_stride_z Stride of the source image in Z dimension (in bytes)
59 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
60 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
61 * @param[out] dst_ptr Pointer to the destination image. Supported data types: same as @p src_ptr
62 * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes)
63 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
64 * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes)
65 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
66 * @param[in] dst_stride_z Stride of the destination image in Z dimension (in bytes)
67 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
68 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010069 * @param[in] batch (Optional) Batch index if 4D pad must be applied
Giorgio Arena205eed82019-08-14 10:13:50 +010070 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010071__kernel void pad_layer_constant(TENSOR3D_DECLARATION(src),
72 TENSOR3D_DECLARATION(dst)
73#if defined(PAD_W_BEFORE)
74 ,
75 uint batch
76#endif // defined(PAD_W_BEFORE)
77 )
Giorgio Arena205eed82019-08-14 10:13:50 +010078{
79 const int x = get_global_id(0);
80 const int y = get_global_id(1);
81 const int z = get_global_id(2);
82
Matthew Jacksonc2a60592019-08-30 15:19:42 +010083 uint cond = 0;
Matthew Jacksonc2a60592019-08-30 15:19:42 +010084
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010085#if defined(PAD_W_BEFORE)
86 cond |= batch < PAD_W_BEFORE || batch >= (SRC_BATCH + PAD_W_BEFORE);
87#endif // defined(PAD_W_BEFORE)
88#if defined(PAD_Z_BEFORE)
89 cond |= z < PAD_Z_BEFORE || z >= (SRC_DEPTH + PAD_Z_BEFORE);
90#endif // defined(PAD_Z_BEFORE)
91
Matthew Jacksonc2a60592019-08-30 15:19:42 +010092 if(cond)
Giorgio Arena205eed82019-08-14 10:13:50 +010093 {
94 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010095 VSTORE(VEC_SIZE)
96 ((VEC_TYPE)CONST_VAL, 0, (__global DATA_TYPE *)dst.ptr);
Giorgio Arena205eed82019-08-14 10:13:50 +010097 }
98 else
99 {
Giorgio Arena205eed82019-08-14 10:13:50 +0100100 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
101 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
102
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100103#if defined(NUM_THREADS_TO_SKIP_X)
Giorgio Arena205eed82019-08-14 10:13:50 +0100104 /* In case the pad left is greater than the vector size, and we are past the threads operating solely on pad values,
105 * the input pointer must be brought back along the X axis to start from the first non-pad values.
106 *
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100107 * E.g. with VEC_SIZE=2, PAD_X_BEFORE=5, CONST_VAL=0 and 1D input |1 2 3 4 5 6|:
108 * -# The first thread will compute the output values |0 0| since it detects (x_outs == (0, 1)) < PAD_X_BEFORE
109 * -# The second thread will compute the output values |0 0| since it detects (x_outs == (2, 3)) < PAD_X_BEFORE
Giorgio Arena205eed82019-08-14 10:13:50 +0100110 * -# The third thread should compute |0 1|, however the input pointer is now ahead of ((x * VEC_SIZE) == 4) values, reading |4 5|
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100111 * -# To detect this, we use ((PAD_X_BEFORE / VEC_SIZE) == NUM_THREADS_TO_SKIP_X == 2) and check that it is >= to the current x
112 * -# So, we bring the pointer back of NUM_THREADS_TO_SKIP_X threads, which means multiplying this constant by the input's step along the X axis
113 * -# Now that the pointer is back of ((NUM_THREADS_TO_SKIP_X * src_step_x) == 4) values, it will read the desired values |0 1|
Giorgio Arena205eed82019-08-14 10:13:50 +0100114 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100115 src.ptr -= select(0u, NUM_THREADS_TO_SKIP_X * src_step_x, x >= NUM_THREADS_TO_SKIP_X);
116#endif // defined(NUM_THREADS_TO_SKIP_X)
117#if defined(PAD_Z_BEFORE)
118 src.ptr -= PAD_Z_BEFORE * src_step_z;
119#endif // defined(PAD_Z_BEFORE)
120#if defined(PAD_W_BEFORE)
121 src.ptr -= PAD_W_BEFORE * SRC_DEPTH * src_step_z;
122#endif // defined(PAD_W_BEFORE)
Giorgio Arena205eed82019-08-14 10:13:50 +0100123
124 VEC_TYPE src_vals = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src.ptr);
125
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100126 VEC_INT xs_out = (VEC_INT)(x * VEC_SIZE) + CONVERT(OFFSETS, VEC_INT);
127 VEC_INT cond = xs_out < (VEC_INT)PAD_X_BEFORE || xs_out >= (VEC_INT)(SRC_WIDTH + PAD_X_BEFORE);
128#if defined(PAD_Y_BEFORE)
129 cond |= (VEC_INT)y < (VEC_INT)PAD_Y_BEFORE || (VEC_INT)y >= (VEC_INT)(SRC_HEIGHT + PAD_Y_BEFORE);
130#endif // defined(PAD_Y_BEFORE)
Giorgio Arena205eed82019-08-14 10:13:50 +0100131 VSTORE(VEC_SIZE)
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100132 (select(src_vals, (VEC_TYPE)CONST_VAL, CONVERT(cond, VEC_SELECT)), 0, (__global DATA_TYPE *)dst.ptr);
Giorgio Arena205eed82019-08-14 10:13:50 +0100133 }
Giorgio Arena205eed82019-08-14 10:13:50 +0100134}
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100135#endif // defined(CONST_VAL)
136
137#if defined(PAD_X_BEFORE_REMAINDER) && defined(PAD_X_AFTER_REMAINDER) && defined(PAD_X_BEFORE_REMAINDER_REFL) && defined(PAD_X_AFTER_REMAINDER_REFL) && defined(AFTER_PAD_FACT_X)
138
139#define SCALAR_COND(x) (VEC_SELECT) x == (VEC_SELECT)1
140#define ROTATE_REVERSE(x, n) ROTATE(REVERSE(x, VEC_SIZE), VEC_SIZE, n)
141#define SYMM_REFL_LEFT(x, n0, n1) select(ROTATE_REVERSE(x, n1), ROTATE(x, VEC_SIZE, n0), OFFSETS >= (VEC_SELECT)n0)
142#define SYMM_REFL_RIGHT(x, n0, n1) select(ROTATE(x, VEC_SIZE, n0), ROTATE_REVERSE(x, n1), OFFSETS >= (VEC_SELECT)n0)
143
144/** Perform a pad operation when PaddingMode is SYMMETRIC
145 *
146 * @note Data type can be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float
147 * @note Vector size must be passed using the -DVEC_SIZE compile flag, e.g. -DVEC_SIZE=4
148 * @note Constant value must be passed using the -DCONST_VAL compile flag, e.g. -DCONST_VAL=1.27
149 * @note Pad to add to the left must be passed using the -DPAD_X_BEFORE compile flag, e.g. -DPAD_X_BEFORE=5
150 * @note Input tensor's width must be passed using the -DSRC_WIDTH compile flag, e.g. -DSRC_WIDTH=224
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100151 * @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
152 * @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
153 * @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
154 * @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
155 * @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
156 * @note If padding mode is REFLECT, the -DIS_REFLECT compile flag must be set to 1, else it must be set to 0
157 * @note If pad also needs to be added to the top of the tensor, the following compile flags must be passed at compile time:
158 * -# -DPAD_Y_BEFORE: Pad to add to the top of the input tensor (e.g. -DPAD_Y_BEFORE=3)
159 * -# -DSRC_HEIGHT: Input tensor's height (e.g. -DSRC_HEIGHT=127)
160 * @note If pad also needs to be added to the depth of the tensor, the following compile flags must be passed at compile time:
161 * -# -DPAD_Z_BEFORE: Pad to add before the first plane of the input tensor (e.g. -DPAD_Z_BEFORE=3)
162 * -# -DSRC_DEPTH: Input tensor's depth (e.g. -DSRC_DEPTH=32)
163 * @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:
164 * -# -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)
165 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100166 * @param[in] src_ptr Pointer to the source image. Supported data types: All
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100167 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
168 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
169 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
170 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
171 * @param[in] src_stride_z Stride of the source image in Z dimension (in bytes)
172 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
173 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
174 * @param[out] dst_ptr Pointer to the destination image. Supported data types: same as @p src_ptr
175 * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes)
176 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
177 * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes)
178 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
179 * @param[in] dst_stride_z Stride of the destination image in Z dimension (in bytes)
180 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
181 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image
182 */
183__kernel void pad_layer_symmetric_reflect(TENSOR3D_DECLARATION(src),
184 TENSOR3D_DECLARATION(dst))
185{
186 // Get current thread position
187 const int x = get_global_id(0);
188 const int y = get_global_id(1);
189 const int z = get_global_id(2);
190
191 // Define conditions based on the thread X position w.r.t. pad left and right
192 const int x_out_first = x * VEC_SIZE;
193 const int x_out_last = x_out_first + VEC_SIZE;
194 const int is_before_pad_left = (x_out_last <= PAD_X_BEFORE);
195 const int is_across_pad_left = (x_out_first < PAD_X_BEFORE) && (x_out_last > PAD_X_BEFORE);
196 const int is_inside_input = (x_out_first >= PAD_X_BEFORE) && (x_out_last <= (SRC_WIDTH + PAD_X_BEFORE));
197 const int is_across_pad_right = (x_out_first < (SRC_WIDTH + PAD_X_BEFORE)) && (x_out_last > (SRC_WIDTH + PAD_X_BEFORE));
198 const int is_after_pad_right = (x_out_first >= (SRC_WIDTH + PAD_X_BEFORE));
199
200 // Calculate base pointers
201 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes;
202 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
203
204 // Calculate input tensor's offset based on the defined conditions
205 int x_offset = 0;
206 x_offset = select(x_offset, PAD_X_BEFORE - x_out_last + IS_REFLECT, is_before_pad_left);
207 x_offset = select(x_offset, x_out_first - PAD_X_BEFORE, is_inside_input);
208 x_offset = select(x_offset, SRC_WIDTH - VEC_SIZE, is_across_pad_right);
209 x_offset = select(x_offset, AFTER_PAD_FACT_X - x_out_last, is_after_pad_right);
210
211#if defined(AFTER_PAD_REM)
212 int neg_offs = x_offset < 0;
213 x_offset = max(x_offset, 0);
214#endif // defined(AFTER_PAD_REM)
215
216 // Load input values from the computed offset
217 int y_in = y;
218 int z_in = z;
219#if defined(PAD_Y_BEFORE)
220 y_in = select(y - PAD_Y_BEFORE, PAD_Y_BEFORE - y + IS_REFLECT - 1, y < PAD_Y_BEFORE);
221 y_in = select(y_in, 2 * SRC_HEIGHT + PAD_Y_BEFORE - y - IS_REFLECT - 1, y >= (SRC_HEIGHT + PAD_Y_BEFORE));
222#endif // defined(PAD_Y_BEFORE)
223#if defined(PAD_Z_BEFORE)
224 z_in = select(z - PAD_Z_BEFORE, PAD_Z_BEFORE - z + IS_REFLECT - 1, z < PAD_Z_BEFORE);
225 z_in = select(z_in, 2 * SRC_DEPTH + PAD_Z_BEFORE - z - IS_REFLECT - 1, z >= (SRC_DEPTH + PAD_Z_BEFORE));
226#endif // defined(PAD_Y_BEFORE)
227
228 src_addr += x_offset * src_stride_x + y_in * src_step_y + z_in * src_step_z;
229
230#if SRC_WIDTH == 1
231 VSTORE(VEC_SIZE)
232 ((VEC_TYPE)(*(__global DATA_TYPE *)src_addr), 0, (__global DATA_TYPE *)dst.ptr);
233#else // SRC_WIDTH == 1
234
235 VEC_TYPE src_vals = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src_addr);
236
237 // Choose rearrangement policy based on the defined conditions
238 src_vals = select(src_vals, SYMM_REFL_LEFT(src_vals, PAD_X_BEFORE_REMAINDER, PAD_X_BEFORE_REMAINDER_REFL), SCALAR_COND(is_across_pad_left));
239 src_vals = select(src_vals, SYMM_REFL_RIGHT(src_vals, PAD_X_AFTER_REMAINDER, PAD_X_AFTER_REMAINDER_REFL), SCALAR_COND(is_across_pad_right));
240 src_vals = select(src_vals, REVERSE(src_vals, VEC_SIZE), SCALAR_COND((is_before_pad_left || is_after_pad_right)));
241#if defined(AFTER_PAD_REM)
242 src_vals = select(src_vals, ROTATE(src_vals, VEC_SIZE, AFTER_PAD_REM), SCALAR_COND(neg_offs));
243#endif // defined(AFTER_PAD_REM)
244
245 // Store
246 VSTORE(VEC_SIZE)
247 (src_vals, 0, (__global DATA_TYPE *)dst.ptr);
248#endif // SRC_WIDTH == 1
249}
250#endif // defined(PAD_X_BEFORE_REMAINDER) && defined(PAD_X_AFTER_REMAINDER) && defined(PAD_X_BEFORE_REMAINDER_REFL) && defined(PAD_X_AFTER_REMAINDER_REFL) && defined(AFTER_PAD_FACT_X)
Giorgio Arenad056e572020-10-12 11:53:51 +0100251#endif // defined(DATA_TYPE) && defined(VEC_SIZE) && defined(PAD_X_BEFORE) && defined(SRC_WIDTH)