blob: 6f25ad4b7a58d11a528810e36b0db0d9391ff3f2 [file] [log] [blame]
Gian Marco76faef82018-01-29 12:15:32 +00001/*
2 * Copyright (c) 2018 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(FIXED_POINT_POSITION)
27#include "fixed_point.h"
28#endif // FIXED_POINT_POSITION
29
30#if defined(DATA_TYPE) && defined(ELEMENT_SIZE)
31#if !defined(FIXED_POINT_POSITION)
32
33#if ELEMENT_SIZE == 1
34#define COND_DATA_TYPE char
35#elif ELEMENT_SIZE == 2
36#define COND_DATA_TYPE short
37#elif ELEMENT_SIZE == 4
38#define COND_DATA_TYPE int
39#else // ELEMENT_SIZE
40#error "Element size not support"
41#endif // ELEMENT_SIZE
42
43#if defined(CONVOLVED_WIDTH) && defined(STRIDE_Y) && defined(KERNEL_DEPTH)
44/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 1x1 and the stride_x = 1
45 *
46 * @note This kernel computes 4 elements
47 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
48 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
49 * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3
50 * @note The stride along the Y direction must be passed at compile time using -DSTRIDE_Y: e.g. -DSTRIDE_Y=1
51 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
52 *
53 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
54 * @param[in] src_stride_x Stride of the source tensor 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 tensor 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 tensor 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 tensor
61 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
62 * @param[in] dst_stride_x Stride of the destination tensor 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 tensor 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_offset_first_element_in_bytes The offset of the first element in the destination tensor
67 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
68 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
69 */
70__kernel void im2col1x1_stridex1_dchw(
71 TENSOR3D_DECLARATION(src),
72 IMAGE_DECLARATION(dst),
73 uint src_stride_w,
74 uint dst_stride_w)
75{
76 const uint xc = get_global_id(0) * 4; // x coordinate in the convolved tensor
77 const uint yc = get_global_id(1); // y coordinate in the convolved tensor
78 const uint ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
79 const uint batch = get_global_id(2) / KERNEL_DEPTH; // batch size
80
81 // Clamp xc
82 // The strategy clamps at "xc" as it will be a valid value for sure
83 uint4 xc_clamped = xc + (uint4)(0, 1, 2, 3);
84
85 // Check which values are valid
86 const VEC_DATA_TYPE(COND_DATA_TYPE, 4) cond0 = CONVERT((xc_clamped < SRC_WIDTH), VEC_DATA_TYPE(COND_DATA_TYPE, 4));
87
88 xc_clamped = select((uint4)xc, xc_clamped, convert_int4(cond0));
89
90 // Calculate input indices
91 const uint xi = xc;
92 const uint yi = yc * STRIDE_Y;
93
94 // Calculate output indices
95 const uint xo = ch;
96 const uint4 yo = xc_clamped + yc * CONVOLVED_WIDTH; // Index of the convolution
97
98 // Get input and output address
99 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * src_stride_x + yi * src_stride_y + ch * src_stride_z + batch * src_stride_w;
100
101 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + batch * dst_stride_w;
102
103 VEC_DATA_TYPE(DATA_TYPE, 4)
104 data = vload4(0, (__global DATA_TYPE *)input_ptr);
105
106 // If out-of-bound, overwrite with the first element
107 data = select((VEC_DATA_TYPE(DATA_TYPE, 4))data.s0, data, cond0);
108
109 *(__global DATA_TYPE *)(output_ptr + yo.s0 * dst_stride_y) = data.s0;
110 *(__global DATA_TYPE *)(output_ptr + yo.s1 * dst_stride_y) = data.s1;
111 *(__global DATA_TYPE *)(output_ptr + yo.s2 * dst_stride_y) = data.s2;
112 *(__global DATA_TYPE *)(output_ptr + yo.s3 * dst_stride_y) = data.s3;
113
114#ifdef HAS_BIAS
115 if(ch == (KERNEL_DEPTH - 1))
116 {
117 *((__global DATA_TYPE *)(output_ptr + yo.s0 * dst_stride_y) + 1) = 1.0f;
118 *((__global DATA_TYPE *)(output_ptr + yo.s1 * dst_stride_y) + 1) = 1.0f;
119 *((__global DATA_TYPE *)(output_ptr + yo.s2 * dst_stride_y) + 1) = 1.0f;
120 *((__global DATA_TYPE *)(output_ptr + yo.s3 * dst_stride_y) + 1) = 1.0f;
121 }
122#endif // HAS_BIAS
123}
124#endif // defined(CONVOLVED_WIDTH) && defined(STRIDE_Y) && defined(KERNEL_DEPTH)
125
Michele Di Giorgio14bd2cf2018-06-14 14:55:14 +0100126#define PTR_TO_VALUE(PTR, DATA_TYPE) *((__global DATA_TYPE *)(PTR))
Pablo Tello4a626a72018-04-04 10:01:14 +0100127
Gian Marco76faef82018-01-29 12:15:32 +0000128#if defined(CONVOLVED_WIDTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_DEPTH) && defined(PAD_LEFT) && defined(PAD_RIGHT) && defined(PAD_TOP) && defined(PAD_BOTTOM) && defined(PAD_VALUE)
Pablo Tello4a626a72018-04-04 10:01:14 +0100129
130/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 5x5
131 *
132 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
133 * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128
134 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
135 * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3
136 * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2
137 * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0
138 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100139 * @note The dilation_x and dilation_y must be passed at compile time using -DDILATION_X and -DDILATION_Y: e.g. -DDILATION_X=1, -DDILATION_Y=1
Pablo Tello4a626a72018-04-04 10:01:14 +0100140 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
141 *
142 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
143 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
144 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
145 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
146 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
147 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
148 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
149 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
150 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
151 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
152 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
153 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
154 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
155 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
156 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
157 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
158 */
159__kernel void im2col_generic_nhwc(
160 TENSOR3D_DECLARATION(src),
161 IMAGE_DECLARATION(dst),
162 uint src_stride_w,
163 uint dst_stride_w)
164{
165 const int src_stride_y_int = (int)src_stride_y;
166 const int src_stride_z_int = (int)src_stride_z;
167 const int xc = get_global_id(1); // x coordinate in the convolved tensor
168 const int yc = get_global_id(2) % CONVOLVED_HEIGHT; // y coordinate in the convolved tensor
169 const int ch = get_global_id(0); // input feature map
170 const int batch = get_global_id(2) / CONVOLVED_HEIGHT; // batch size
171
172 // Calculate input indices
173 const int xi = xc * STRIDE_X - PAD_LEFT;
174 const int yi = yc * STRIDE_Y - PAD_TOP;
175
176 // Calculate output indices
177 const int xo = ch * KERNEL_HEIGHT * KERNEL_WIDTH;
178 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
179
180 // Get input and output address
181 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * src_stride_y_int + yi * src_stride_z_int + ch * src_stride_x + batch * src_stride_w;
182 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w;
183
184 for(int yk = 0; yk < KERNEL_HEIGHT; ++yk)
185 {
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100186 const int dilated_offset_y = yk * DILATION_Y;
187 const int y0 = yi + dilated_offset_y;
Pablo Tello4a626a72018-04-04 10:01:14 +0100188 if(y0 >= 0 && y0 < SRC_HEIGHT)
189 {
190 int xk;
191 for(xk = 0; xk < KERNEL_WIDTH; xk++)
192 {
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100193 const int dilated_offset_x = xk * DILATION_X;
194 const int x0 = xi + dilated_offset_x;
Pablo Tello4a626a72018-04-04 10:01:14 +0100195 if(x0 >= 0 && x0 < SRC_WIDTH)
196 {
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100197 *((__global DATA_TYPE *)output_ptr) = PTR_TO_VALUE(input_ptr + dilated_offset_x * src_stride_y + dilated_offset_y * src_stride_z, DATA_TYPE);
Pablo Tello4a626a72018-04-04 10:01:14 +0100198 }
199 else
200 {
201 *((__global DATA_TYPE *)output_ptr) = PAD_VALUE;
202 }
203 output_ptr += 1 * sizeof(DATA_TYPE);
204 }
205 }
206 else
207 {
208 for(int xk = 0; xk < KERNEL_WIDTH; xk++)
209 {
210 *((__global DATA_TYPE *)output_ptr) = (DATA_TYPE)PAD_VALUE;
211 output_ptr += 1 * dst_stride_x;
212 }
213 }
214 }
215#ifdef HAS_BIAS
216 if(ch == (KERNEL_DEPTH - 1))
217 {
218 *((__global DATA_TYPE *)output_ptr) = 1.0f;
219 output_ptr += 1 * dst_stride_x;
220 }
221#endif // HAS_BIAS
222}
223
224/** This kernel performs a reshaping of the input tensor (with layout NHWC) to a tensor used to perform convolution using GEMM when the kernel size is 3x3
225 *
226 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
227 * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128
228 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
229 * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3
230 * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2
231 * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0
232 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1
233 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
234 *
235 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
236 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
237 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
238 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
239 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
240 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
241 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
242 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
243 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
244 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
245 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
246 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
247 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
248 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
249 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
250 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
251 */
252__kernel void im2col3x3_nhwc(
253 TENSOR3D_DECLARATION(src),
254 IMAGE_DECLARATION(dst),
255 uint src_stride_w,
256 uint dst_stride_w)
257{
258 const int src_stride_y_int = (int)src_stride_y;
259 const int src_stride_z_int = (int)src_stride_z;
260 const int xc = get_global_id(1); // x coordinate in the convolved tensor
261 const int yc = get_global_id(2) % CONVOLVED_HEIGHT; // y coordinate in the convolved tensor
262 const int ch = get_global_id(0); // input feature map
263 const int batch = get_global_id(2) / CONVOLVED_HEIGHT; // batch size
264
265 // Calculate input indices
266 const int xi = xc * STRIDE_X - PAD_LEFT;
267 const int yi = yc * STRIDE_Y - PAD_TOP;
268
269 // Calculate output indices
270 const int xo = ch * 9; // 3x3
271 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
272
273 // Get input and output address
274 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * src_stride_y_int + yi * src_stride_z_int + ch * src_stride_x + batch * src_stride_w;
275 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w;
276
277 VEC_DATA_TYPE(DATA_TYPE, 3)
278 row0 = (VEC_DATA_TYPE(DATA_TYPE, 3))(PAD_VALUE);
279 VEC_DATA_TYPE(DATA_TYPE, 3)
280 row1 = (VEC_DATA_TYPE(DATA_TYPE, 3))(PAD_VALUE);
281 VEC_DATA_TYPE(DATA_TYPE, 3)
282 row2 = (VEC_DATA_TYPE(DATA_TYPE, 3))(PAD_VALUE);
283
284 const int3 y = (int3)yi + (int3)(0, 1, 2);
285 // Guard against reading outside the input buffer, there is no padding in Z so we check if ry is inside the buffer.
286 if(y.s0 >= 0 && y.s0 < SRC_HEIGHT)
287 {
288 row0 = (VEC_DATA_TYPE(DATA_TYPE, 3))(
289 PTR_TO_VALUE(input_ptr + 0 * src_stride_y, DATA_TYPE),
290 PTR_TO_VALUE(input_ptr + 1 * src_stride_y, DATA_TYPE),
291 PTR_TO_VALUE(input_ptr + 2 * src_stride_y, DATA_TYPE));
292 }
293
294 if(y.s1 >= 0 && y.s1 < SRC_HEIGHT)
295 {
296 row1 = (VEC_DATA_TYPE(DATA_TYPE, 3))(
297 PTR_TO_VALUE(input_ptr + 0 * src_stride_y + 1 * src_stride_z, DATA_TYPE),
298 PTR_TO_VALUE(input_ptr + 1 * src_stride_y + 1 * src_stride_z, DATA_TYPE),
299 PTR_TO_VALUE(input_ptr + 2 * src_stride_y + 1 * src_stride_z, DATA_TYPE));
300 }
301
302 if(y.s2 >= 0 && y.s2 < SRC_HEIGHT)
303 {
304 row2 = (VEC_DATA_TYPE(DATA_TYPE, 3))(
305 PTR_TO_VALUE(input_ptr + 0 * src_stride_y + 2 * src_stride_z, DATA_TYPE),
306 PTR_TO_VALUE(input_ptr + 1 * src_stride_y + 2 * src_stride_z, DATA_TYPE),
307 PTR_TO_VALUE(input_ptr + 2 * src_stride_y + 2 * src_stride_z, DATA_TYPE));
308 }
309
310#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
311 // Put 0 if the value is out-of-bound
312 const int3 x = (int3)xi + (int3)(0, 1, 2);
313 VEC_DATA_TYPE(COND_DATA_TYPE, 3)
314 cond0 = CONVERT((x >= (int3)0 && x < (int3)SRC_WIDTH), VEC_DATA_TYPE(COND_DATA_TYPE, 3));
315 row0 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row0, cond0);
316 row1 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row1, cond0);
317 row2 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row2, cond0);
318#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
319 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row0.s012, row1.s012, row2.s01), 0, (__global DATA_TYPE *)output_ptr);
320 *((__global DATA_TYPE *)output_ptr + 8) = row2.s2;
321
322#ifdef HAS_BIAS
323 if(ch == (KERNEL_DEPTH - 1))
324 {
325 *((__global DATA_TYPE *)output_ptr + 9) = 1.0f;
326 }
327#endif // HAS_BIAS
328}
329
Gian Marco76faef82018-01-29 12:15:32 +0000330/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 3x3
331 *
332 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
333 * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128
334 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
335 * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3
336 * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2
337 * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0
338 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1
339 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
340 *
341 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
342 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
343 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
344 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
345 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
346 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
347 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
348 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
349 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
350 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
351 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
352 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
353 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
354 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
355 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
356 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
357 */
358__kernel void im2col3x3_dchw(
359 TENSOR3D_DECLARATION(src),
360 IMAGE_DECLARATION(dst),
361 uint src_stride_w,
362 uint dst_stride_w)
363{
364 const int xc = get_global_id(0); // x coordinate in the convolved tensor
365 const int yc = get_global_id(1); // y coordinate in the convolved tensor
366 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
367 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
368
369 // Calculate input indices
370 const int xi = xc * STRIDE_X - PAD_LEFT;
371 const int yi = yc * STRIDE_Y - PAD_TOP;
372
373 // Calculate output indices
374 const int xo = ch * 9; // 3x3
375 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
376
377 // Get input and output address
378 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * (int)src_stride_x + yi * (int)src_stride_y + ch * src_stride_z + batch * src_stride_w;
379
380 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w;
381
382 VEC_DATA_TYPE(DATA_TYPE, 3)
383 row0 = vload3(0, (__global DATA_TYPE *)(input_ptr + 0 * src_stride_y));
384 VEC_DATA_TYPE(DATA_TYPE, 3)
385 row1 = vload3(0, (__global DATA_TYPE *)(input_ptr + 1 * src_stride_y));
386 VEC_DATA_TYPE(DATA_TYPE, 3)
387 row2 = vload3(0, (__global DATA_TYPE *)(input_ptr + 2 * src_stride_y));
388
389#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
390 // Put 0 if the value is out-of-bound
391 int3 x = (int3)xi + (int3)(0, 1, 2);
392 int3 y = (int3)yi + (int3)(0, 1, 2);
393
394 VEC_DATA_TYPE(COND_DATA_TYPE, 3)
395 cond0 = CONVERT((x >= (int3)0 && x < (int3)SRC_WIDTH && (int3)(y.s0 >= 0 && y.s0 < SRC_HEIGHT)), VEC_DATA_TYPE(COND_DATA_TYPE, 3));
396 VEC_DATA_TYPE(COND_DATA_TYPE, 3)
397 cond1 = CONVERT((x >= (int3)0 && x < (int3)SRC_WIDTH && (int3)(y.s1 >= 0 && y.s1 < SRC_HEIGHT)), VEC_DATA_TYPE(COND_DATA_TYPE, 3));
398 VEC_DATA_TYPE(COND_DATA_TYPE, 3)
399 cond2 = CONVERT((x >= (int3)0 && x < (int3)SRC_WIDTH && (int3)(y.s2 >= 0 && y.s2 < SRC_HEIGHT)), VEC_DATA_TYPE(COND_DATA_TYPE, 3));
400
401 row0 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row0, cond0);
402 row1 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row1, cond1);
403 row2 = select((VEC_DATA_TYPE(DATA_TYPE, 3))PAD_VALUE, row2, cond2);
404#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
405
406 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row0.s012, row1.s012, row2.s01), 0, (__global DATA_TYPE *)output_ptr);
407 *((__global DATA_TYPE *)output_ptr + 8) = row2.s2;
408
409#ifdef HAS_BIAS
410 if(ch == (KERNEL_DEPTH - 1))
411 {
412 *((__global DATA_TYPE *)output_ptr + 9) = 1.0f;
413 }
414#endif // HAS_BIAS
415}
416
417/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 5x5
418 *
419 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
420 * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128
421 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
422 * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3
423 * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2
424 * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0
425 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1
426 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
427 *
428 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
429 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
430 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
431 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
432 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
433 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
434 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
435 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
436 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
437 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
438 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
439 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
440 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
441 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
442 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
443 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
444 */
445__kernel void im2col5x5_dchw(
446 TENSOR3D_DECLARATION(src),
447 IMAGE_DECLARATION(dst),
448 uint src_stride_w,
449 uint dst_stride_w)
450{
451 const int xc = get_global_id(0); // x coordinate in the convolved tensor
452 const int yc = get_global_id(1); // y coordinate in the convolved tensor
453 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
454 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
455
456 // Calculate input indices
457 const int xi = xc * STRIDE_X - PAD_LEFT;
458 const int yi = yc * STRIDE_Y - PAD_TOP;
459
460 // Calculate output indices
461 const int xo = ch * 25; // 5x5
462 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
463
464#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
465 // Put 0 if the value is out-of-bound
466 int4 x0 = (int4)xi + (int4)(0, 1, 2, 3);
467 int4 y0 = (int4)yi + (int4)(0, 1, 2, 3);
468 int x1 = xi + 4;
469 int y1 = yi + 4;
470
471 // Check if we could have out-of-bounds elements in the x direction
472 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
473 x0_condition = CONVERT((x0 >= (int4)0 && x0 < (int4)SRC_WIDTH), VEC_DATA_TYPE(COND_DATA_TYPE, 4));
474 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
475 y0_condition = CONVERT((y0 >= (int4)0 && y0 < (int4)SRC_HEIGHT), VEC_DATA_TYPE(COND_DATA_TYPE, 4));
476 COND_DATA_TYPE x1_condition = (COND_DATA_TYPE)(x1 >= 0 && x1 < SRC_WIDTH);
477 COND_DATA_TYPE y1_condition = (COND_DATA_TYPE)(y1 >= 0 && y1 < SRC_HEIGHT);
478#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
479
480 // Get input and output address
481 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * (int)src_stride_x + yi * (int)src_stride_y + ch * src_stride_z + batch * src_stride_w;
482
483 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w;
484
485 {
486 VEC_DATA_TYPE(DATA_TYPE, 4)
487 row00 = vload4(0, (__global DATA_TYPE *)input_ptr);
488 DATA_TYPE
489 row01 = *((__global DATA_TYPE *)input_ptr + 4);
490
491 input_ptr += src_stride_y;
492
493 VEC_DATA_TYPE(DATA_TYPE, 4)
494 row10 = vload4(0, (__global DATA_TYPE *)input_ptr);
495 DATA_TYPE
496 row11 = *((__global DATA_TYPE *)input_ptr + 4);
497
498#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
499 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
500 cond00 = x0_condition && (VEC_DATA_TYPE(COND_DATA_TYPE, 4))y0_condition.s0;
501 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
502 cond10 = x0_condition && (VEC_DATA_TYPE(COND_DATA_TYPE, 4))y0_condition.s1;
503 COND_DATA_TYPE cond01 = (COND_DATA_TYPE)(x1_condition && y0_condition.s0);
504 COND_DATA_TYPE cond11 = (COND_DATA_TYPE)(x1_condition && y0_condition.s1);
505
506 // Replace with 0 if the value is not valid
507 row00 = select((VEC_DATA_TYPE(DATA_TYPE, 4))PAD_VALUE, row00, cond00);
508 row10 = select((VEC_DATA_TYPE(DATA_TYPE, 4))PAD_VALUE, row10, cond10);
509 row01 = select((DATA_TYPE)PAD_VALUE, row01, cond01);
510 row11 = select((DATA_TYPE)PAD_VALUE, row11, cond11);
511#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
512
513 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s0123, row01,
514 row10.s012),
515 0, (__global DATA_TYPE *)output_ptr);
516 vstore2((VEC_DATA_TYPE(DATA_TYPE, 2))(row10.s3, row11), 0, (__global DATA_TYPE *)output_ptr + 8);
517
518 input_ptr += src_stride_y;
519 output_ptr += 10 * dst_stride_x;
520 }
521
522 {
523 VEC_DATA_TYPE(DATA_TYPE, 4)
524 row00 = vload4(0, (__global DATA_TYPE *)input_ptr);
525 DATA_TYPE
526 row01 = *((__global DATA_TYPE *)input_ptr + 4);
527
528 input_ptr += src_stride_y;
529
530 VEC_DATA_TYPE(DATA_TYPE, 4)
531 row10 = vload4(0, (__global DATA_TYPE *)input_ptr);
532 DATA_TYPE
533 row11 = *((__global DATA_TYPE *)input_ptr + 4);
534
535#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
536 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
537 cond00 = x0_condition && (VEC_DATA_TYPE(COND_DATA_TYPE, 4))y0_condition.s2;
538 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
539 cond10 = x0_condition && (VEC_DATA_TYPE(COND_DATA_TYPE, 4))y0_condition.s3;
540 COND_DATA_TYPE cond01 = (COND_DATA_TYPE)(x1_condition && y0_condition.s2);
541 COND_DATA_TYPE cond11 = (COND_DATA_TYPE)(x1_condition && y0_condition.s3);
542
543 // Replace with 0 if the value is not valid
544 row00 = select((VEC_DATA_TYPE(DATA_TYPE, 4))PAD_VALUE, row00, cond00);
545 row10 = select((VEC_DATA_TYPE(DATA_TYPE, 4))PAD_VALUE, row10, cond10);
546 row01 = select((DATA_TYPE)PAD_VALUE, row01, cond01);
547 row11 = select((DATA_TYPE)PAD_VALUE, row11, cond11);
548#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
549
550 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s0123, row01,
551 row10.s012),
552 0, (__global DATA_TYPE *)output_ptr);
553 vstore2((VEC_DATA_TYPE(DATA_TYPE, 2))(row10.s3, row11), 0, (__global DATA_TYPE *)output_ptr + 8);
554
555 input_ptr += src_stride_y;
556 output_ptr += 10 * dst_stride_x;
557 }
558
559 {
560 VEC_DATA_TYPE(DATA_TYPE, 4)
561 row00 = vload4(0, (__global DATA_TYPE *)input_ptr);
562 DATA_TYPE
563 row01 = *((__global DATA_TYPE *)input_ptr + 4);
564
565 input_ptr += src_stride_y;
566
567#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
568 VEC_DATA_TYPE(COND_DATA_TYPE, 4)
569 cond00 = x0_condition && (VEC_DATA_TYPE(COND_DATA_TYPE, 4))y1_condition;
570 COND_DATA_TYPE cond01 = (COND_DATA_TYPE)(x1_condition && y1_condition);
571
572 // Replace with 0 if the value is not valid
573 row00 = select((VEC_DATA_TYPE(DATA_TYPE, 4))PAD_VALUE, row00, cond00);
574 row01 = select((DATA_TYPE)PAD_VALUE, row01, cond01);
575#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0
576
577 vstore4(row00, 0, (__global DATA_TYPE *)output_ptr);
578 *((__global DATA_TYPE *)output_ptr + 4) = row01;
579
580 output_ptr += 5 * dst_stride_x;
581 }
582
583#ifdef HAS_BIAS
584 if(ch == (KERNEL_DEPTH - 1))
585 {
586 *((__global DATA_TYPE *)output_ptr) = 1.0f;
587 }
588#endif // HAS_BIAS
589}
590#endif // defined(CONVOLVED_WIDTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_DEPTH) && defined(PAD_LEFT) && defined(PAD_RIGHT) && defined(PAD_TOP) && defined(PAD_BOTTOM) && defined(PAD_VALUE)
591
592#if defined(CONVOLVED_WIDTH) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_DEPTH)
593/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 11x11
594 *
595 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
596 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
597 * @note The kernel depth must be passed at compile time using -DKERNEL_DEPTH: e.g. -DKERNEL_DEPTH=3
598 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1
599 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
600 *
601 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
602 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
603 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
604 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
605 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
606 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
607 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
608 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
609 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
610 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
611 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
612 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
613 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
614 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
615 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
616 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
617 */
618__kernel void im2col11x11_padx0_pady0_dchw(
619 TENSOR3D_DECLARATION(src),
620 IMAGE_DECLARATION(dst),
621 uint src_stride_w,
622 uint dst_stride_w)
623{
624 const int xc = get_global_id(0); // x coordinate in the convolved tensor
625 const int yc = get_global_id(1); // y coordinate in the convolved tensor
626 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
627 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
628
629 // Calculate input indices
630 const int xi = xc * STRIDE_X;
631 const int yi = yc * STRIDE_Y;
632
633 // Calculate output indices
634 const int xo = ch * 121; // 11x11
635 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
636
637 // Get input and output address
638 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + xi * src_stride_x + yi * src_stride_y + ch * src_stride_z + batch * src_stride_w;
639
640 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + xo * dst_stride_x + yo * dst_stride_y + batch * dst_stride_w;
641 {
642 VEC_DATA_TYPE(DATA_TYPE, 8)
643 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
644 VEC_DATA_TYPE(DATA_TYPE, 3)
645 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
646
647 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
648 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
649
650 input_ptr += src_stride_y;
651 output_ptr += 11 * src_stride_x;
652 }
653
654 {
655 VEC_DATA_TYPE(DATA_TYPE, 8)
656 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
657 VEC_DATA_TYPE(DATA_TYPE, 3)
658 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
659
660 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
661 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
662
663 input_ptr += src_stride_y;
664 output_ptr += 11 * src_stride_x;
665 }
666
667 {
668 VEC_DATA_TYPE(DATA_TYPE, 8)
669 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
670 VEC_DATA_TYPE(DATA_TYPE, 3)
671 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
672
673 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
674 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
675
676 input_ptr += src_stride_y;
677 output_ptr += 11 * src_stride_x;
678 }
679
680 {
681 VEC_DATA_TYPE(DATA_TYPE, 8)
682 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
683 VEC_DATA_TYPE(DATA_TYPE, 3)
684 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
685
686 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
687 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
688
689 input_ptr += src_stride_y;
690 output_ptr += 11 * src_stride_x;
691 }
692
693 {
694 VEC_DATA_TYPE(DATA_TYPE, 8)
695 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
696 VEC_DATA_TYPE(DATA_TYPE, 3)
697 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
698
699 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
700 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
701
702 input_ptr += src_stride_y;
703 output_ptr += 11 * src_stride_x;
704 }
705
706 {
707 VEC_DATA_TYPE(DATA_TYPE, 8)
708 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
709 VEC_DATA_TYPE(DATA_TYPE, 3)
710 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
711
712 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
713 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
714
715 input_ptr += src_stride_y;
716 output_ptr += 11 * src_stride_x;
717 }
718
719 {
720 VEC_DATA_TYPE(DATA_TYPE, 8)
721 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
722 VEC_DATA_TYPE(DATA_TYPE, 3)
723 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
724
725 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
726 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
727
728 input_ptr += src_stride_y;
729 output_ptr += 11 * src_stride_x;
730 }
731
732 {
733 VEC_DATA_TYPE(DATA_TYPE, 8)
734 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
735 VEC_DATA_TYPE(DATA_TYPE, 3)
736 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
737
738 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
739 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
740
741 input_ptr += src_stride_y;
742 output_ptr += 11 * src_stride_x;
743 }
744
745 {
746 VEC_DATA_TYPE(DATA_TYPE, 8)
747 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
748 VEC_DATA_TYPE(DATA_TYPE, 3)
749 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
750
751 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
752 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
753
754 input_ptr += src_stride_y;
755 output_ptr += 11 * src_stride_x;
756 }
757
758 {
759 VEC_DATA_TYPE(DATA_TYPE, 8)
760 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
761 VEC_DATA_TYPE(DATA_TYPE, 3)
762 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
763
764 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
765 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
766
767 input_ptr += src_stride_y;
768 output_ptr += 11 * src_stride_x;
769 }
770
771 {
772 VEC_DATA_TYPE(DATA_TYPE, 8)
773 row00 = vload8(0, (__global DATA_TYPE *)(input_ptr));
774 VEC_DATA_TYPE(DATA_TYPE, 3)
775 row01 = vload3(0, (__global DATA_TYPE *)(input_ptr) + 8);
776
777 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row00.s01234567), 0, (__global DATA_TYPE *)output_ptr);
778 vstore3((VEC_DATA_TYPE(DATA_TYPE, 3))(row01.s012), 0, (__global DATA_TYPE *)output_ptr + 8);
779
780 output_ptr += 11 * src_stride_x;
781 }
782
783#ifdef HAS_BIAS
784 if(ch == (KERNEL_DEPTH - 1))
785 {
786 *((__global DATA_TYPE *)output_ptr) = 1.0f;
787 }
788#endif // HAS_BIAS
789}
790#endif // defined(CONVOLVED_WIDTH) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_DEPTH)
791#endif // !defined(FIXED_POINT_POSITION)
792
793#if defined(CONVOLVED_WIDTH) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(KERNEL_DEPTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(VECTOR_SIZE) && defined(WIDTH_MOD_VECTOR_SIZE)
794/** This kernel reshapes the input tensor to a tensor used to perform convolution using GEMM when
795 * the kernel width is greater than 1 (except when the kernel size is 3x3) and pad_x == pad_y == 0.
796 *
797 * @note The data type must be passed at compile time using -DDATA_TYPE e.g. -DDATA_TYPE=float.
798 * @note The vector size must be passed at compile time using -DVECTOR_SIZE e.g. -DVECTOR_SIZE=4.
799 * @note The width modulo vector size must be passed at compile time using -DWIDTH_MOD_VECTOR_SIZE e.g. -DWIDTH_MOD_VECTOR_SIZE=3.
800 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
801 *
802 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32
803 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
804 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
805 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
806 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
807 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
808 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
809 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
810 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
811 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
812 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
813 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
814 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
815 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
816 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
817 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
818 */
819__kernel void im2col_generic_padx0_pady0_dchw(
820 TENSOR3D_DECLARATION(src),
821 IMAGE_DECLARATION(dst),
822 uint src_stride_w,
823 uint dst_stride_w)
824{
825 const int xc = get_global_id(0); // x coordinate in the convolved tensor
826 const int yc = get_global_id(1); // y coordinate in the convolved tensor
827 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
828 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
829
830 // Calculate input indices
831 const int xi = xc * STRIDE_X;
832 const int yi = yc * STRIDE_Y;
833 // Calculate output indices
834 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
835 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
836 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w;
837 __global DATA_TYPE *output_ptr = ((__global DATA_TYPE *)(dst_ptr + dst_offset_first_element_in_bytes + yo * dst_stride_y + batch * dst_stride_w)) + xo;
838 // Linearize convolution elements
839 for(int y = yi, y_e = yi + KERNEL_HEIGHT; y < y_e; ++y)
840 {
841 int last_x = 0;
842 for(int x = xi, x_e = xi + KERNEL_WIDTH; x + VECTOR_SIZE <= x_e; x += VECTOR_SIZE, output_ptr += VECTOR_SIZE)
843 {
844 VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE)
845 row = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
846 VSTORE(VECTOR_SIZE)
847 (row, 0, output_ptr);
848 last_x = x;
849 }
850 // Copy the remainder of the row by doing VLOAD(WIDTH_MOD_VECTOR_SIZE) and VSTORE(WIDTH_MOD_VECTOR_SIZE).
851 // Note that x and output_ptr have already been incremented by VECTOR_SIZE by the loop just before exit.
852#if WIDTH_MOD_VECTOR_SIZE == 1
853 *output_ptr = *((__global DATA_TYPE *)(input_ptr + (last_x + VECTOR_SIZE) * src_stride_x + y * src_stride_y));
854#elif WIDTH_MOD_VECTOR_SIZE > 1
855 VEC_DATA_TYPE(DATA_TYPE, WIDTH_MOD_VECTOR_SIZE)
856 row = VLOAD(WIDTH_MOD_VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + (last_x + VECTOR_SIZE) * src_stride_x + y * src_stride_y));
857 VSTORE(WIDTH_MOD_VECTOR_SIZE)
858 (row, 0, output_ptr);
859#endif /* WIDTH_MOD_VECTOR_SIZE */
860 output_ptr += WIDTH_MOD_VECTOR_SIZE;
861 } /* End of loop over KERNEL_HEIGHT */
862
863#ifdef HAS_BIAS
864 if(ch == (KERNEL_DEPTH - 1))
865 {
866#ifdef FIXED_POINT_POSITION
867 *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
868#else // FIXED_POINT_POSITION
869 *output_ptr = 1.0f;
870#endif // FIXED_POINT_POSITION
871 }
872#endif // HAS_BIAS
873}
874#endif //defined(CONVOLVED_WIDTH) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(PAD_LEFT) && defined(PAD_TOP) && defined(PAD_RIGHT) && defined(PAD_BOTTOM) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(KERNEL_DEPTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(VECTOR_SIZE) && defined(WIDTH_MOD_VECTOR_SIZE)
875
876#if defined(CONVOLVED_WIDTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(KERNEL_DEPTH) && defined(PAD_LEFT) && defined(PAD_RIGHT) && defined(PAD_TOP) && defined(PAD_BOTTOM) && defined(PAD_VALUE)
877/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM.
878 *
879 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
880 * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128
881 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34
882 * @note The kernel width, height and depth must be passed at compile time using -DKERNEL_WIDTH, -DKERNEL_HEIGHT and -DKERNEL_DEPTH: e.g. -DKERNEL_WIDTH=3, -DKERNEL_HEIGHT=3 and -DKERNEL_DEPTH=64
883 * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2
884 * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0
885 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1
Alex Gilday7da29b62018-03-23 14:16:00 +0000886 * @note The dilation_x and dilation_y must be passed at compile time using -DDILATION_X and -DDILATION_Y: e.g. -DDILATION_X=1, -DDILATION_Y=1
Gian Marco76faef82018-01-29 12:15:32 +0000887 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
888 *
889 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
890 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
891 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
892 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
893 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
894 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
895 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
896 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
897 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
898 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
899 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
900 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
901 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
902 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
903 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
904 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
905 */
906__kernel void im2col_generic_dchw(
907 TENSOR3D_DECLARATION(src),
908 IMAGE_DECLARATION(dst),
909 uint src_stride_w,
910 uint dst_stride_w)
911{
912 const int xc = get_global_id(0); // x coordinate in the convolved tensor
913 const int yc = get_global_id(1); // y coordinate in the convolved tensor
914 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
915 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
916
917 // Calculate input indices
918 const int xi = xc * STRIDE_X - PAD_LEFT;
919 const int yi = yc * STRIDE_Y - PAD_TOP;
920
921 // Calculate output indices
922 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
923 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
924
925 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w;
926 __global DATA_TYPE *output_ptr = ((__global DATA_TYPE *)(dst_ptr + dst_offset_first_element_in_bytes + yo * dst_stride_y + batch * dst_stride_w)) + xo;
927
928 // Linearize convolution elements
Alex Gilday7da29b62018-03-23 14:16:00 +0000929 for(int yk = 0; yk < KERNEL_HEIGHT; ++yk)
Gian Marco76faef82018-01-29 12:15:32 +0000930 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000931 int y = yi + yk * DILATION_Y;
932 for(int xk = 0; xk < KERNEL_WIDTH; ++xk, ++output_ptr)
Gian Marco76faef82018-01-29 12:15:32 +0000933 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000934 int x = xi + xk * DILATION_X;
Gian Marco76faef82018-01-29 12:15:32 +0000935#if PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
936 *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
937#else // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
938 if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT)
939 {
940 *output_ptr = PAD_VALUE;
941 }
942 else
943 {
944 *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
945 }
946#endif // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
947 }
948 }
949
950#ifdef HAS_BIAS
951 if(ch == (KERNEL_DEPTH - 1))
952 {
953#ifdef FIXED_POINT_POSITION
954 *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
955#else // FIXED_POINT_POSITION
956 *output_ptr = 1.0f;
957#endif // FIXED_POINT_POSITION
958 }
959#endif // HAS_BIAS
960}
961#endif // defined(CONVOLVED_WIDTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(KERNEL_DEPTH) && defined(PAD_LEFT) && defined(PAD_RIGHT) && defined(PAD_TOP) && defined(PAD_BOTTOM) && defined(PAD_VALUE)
962
963/**This kernel reshapes the input tensor to a tensor used to perform convolution using GEMM when
964 * the kernel width and height are the same of width and height of the input tensor
965 *
966 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
967 * @note In case biases will be added in late stage, -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
968 *
969 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
970 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
971 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
972 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
973 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
974 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
975 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
976 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
977 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
978 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
979 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
980 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
981 * @param[in] width The width of the input tensor
982 * @param[in] height The height of the input tensor
983 */
984__kernel void im2col_reduced_dchw(
985 TENSOR3D_DECLARATION(src),
986 VECTOR_DECLARATION(dst),
987 uint width, uint height)
988{
989 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
990
991 const uint image_size = width * height;
992
993 __global uchar *tmp_out_ptr = dst_ptr + dst_offset_first_element_in_bytes + (get_global_id(0) + get_global_id(1) * width + get_global_id(2) * image_size) * dst_stride_x;
994
995 *((__global DATA_TYPE *)tmp_out_ptr) = *((__global DATA_TYPE *)src.ptr);
996
997#ifdef HAS_BIAS
998 // If it is the last thread in the 3 dimensional workgroup
999 if(get_global_id(0) == (get_global_size(0) - 1) && get_global_id(1) == (get_global_size(1) - 1) && get_global_id(2) == (get_global_size(2) - 1))
1000 {
1001 tmp_out_ptr += dst_stride_x;
1002#ifdef FIXED_POINT_POSITION
1003 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
1004#else // FIXED_POINT_POSITION
1005 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)1.0f;
1006#endif // FIXED_POINT_POSITION
1007 }
1008#endif // HAS_BIAS
1009}
Pablo Tello4a626a72018-04-04 10:01:14 +01001010#endif // defined(DATA_TYPE) && defined(ELEMENT_SIZE)