blob: ce0849bf7abc59475eafae317a268a4b4ab26b50 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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
Gian Marco Iodice3a623242017-07-25 10:25:53 +010026#if defined(FIXED_POINT_POSITION)
27#include "fixed_point.h"
28#endif // FIXED_POINT_POSITION
29
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030/** This kernel reshapes the tensor's low three dimensions to single column
31 *
32 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
33 *
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010034 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
36 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
37 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
38 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
39 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
40 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
41 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010042 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
44 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
45 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
46 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
47 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010048 * @param[in] bias_ptr Pointer to the bias tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 * @param[in] bias_stride_x Stride of the bias tensor in X dimension (in bytes)
50 * @param[in] bias_step_x bias_stride_x * number of elements along X processed per workitem(in bytes)
51 * @param[in] bias_offset_first_element_in_bytes The offset of the first element in the source tensor
52 * @param[in] width The width of the input tensor
53 * @param[in] height The height of the input tensor
54 * @param[in] depth The depth of the input tensor
55 * @param[in] total_filters Total number of filters. 4th dimension of the weights matrix
56 */
57__kernel void reshape_to_columns(
58 TENSOR3D_DECLARATION(src),
59 IMAGE_DECLARATION(dst),
Anthony Barbierac69aa12017-07-03 17:39:37 +010060#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 VECTOR_DECLARATION(bias),
Anthony Barbierac69aa12017-07-03 17:39:37 +010062#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 uint width, uint height, uint depth, uint total_filters)
64{
65 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
66 bool is_last_thread = (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));
67
68 __global uchar *tmp_src_ptr = src.ptr;
69 __global uchar *tmp_dst_ptr = dst_ptr + dst_offset_first_element_in_bytes + get_global_id(0) * dst_stride_y + get_global_id(1) * width * dst_stride_y + get_global_id(
70 2) * width * height * dst_stride_y;
Anthony Barbierac69aa12017-07-03 17:39:37 +010071#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 __global uchar *tmp_bias_ptr = bias_ptr + bias_offset_first_element_in_bytes;
Anthony Barbierac69aa12017-07-03 17:39:37 +010073#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
75 if(is_last_thread)
76 {
77 for(uint i = 0; i < total_filters; ++i)
78 {
79 *((__global DATA_TYPE *)tmp_dst_ptr) = *((__global DATA_TYPE *)tmp_src_ptr);
80
Anthony Barbierac69aa12017-07-03 17:39:37 +010081#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 *((__global DATA_TYPE *)(tmp_dst_ptr + dst_stride_y)) = *((__global DATA_TYPE *)(tmp_bias_ptr));
83 tmp_bias_ptr += bias_stride_x;
Anthony Barbierac69aa12017-07-03 17:39:37 +010084#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 tmp_src_ptr += depth * src_stride_z;
86 tmp_dst_ptr += dst_stride_x;
87 }
88 }
89 else
90 {
91 for(uint i = 0; i < total_filters; ++i)
92 {
93 *((__global DATA_TYPE *)tmp_dst_ptr) = *((__global DATA_TYPE *)tmp_src_ptr);
94 tmp_src_ptr += depth * src_stride_z;
95 tmp_dst_ptr += dst_stride_x;
96 }
97 }
98}
99
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100100#if 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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM.
102 *
103 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
104 * @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.
105 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100106 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
108 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
109 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
110 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
111 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
112 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
113 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100114 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
116 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
117 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
118 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
119 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Giorgio Arena9fe41442017-08-23 16:36:24 +0100120 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
121 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 */
123__kernel void im2col_generic(
124 TENSOR3D_DECLARATION(src),
steniu01868e5412017-07-17 23:16:00 +0100125 IMAGE_DECLARATION(dst),
steniu01868e5412017-07-17 23:16:00 +0100126 uint src_stride_w,
127 uint dst_stride_w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
steniu01868e5412017-07-17 23:16:00 +0100129 const int xc = get_global_id(0); // x coordinate in the convolved tensor
130 const int yc = get_global_id(1); // y coordinate in the convolved tensor
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000131 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
132 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000134 // Calculate input indices
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100135 const int xi = xc * STRIDE_X - PAD_LEFT;
136 const int yi = yc * STRIDE_Y - PAD_TOP;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000138 // Calculate output indices
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100139 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
140 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
141
steniu01868e5412017-07-17 23:16:00 +0100142 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w;
143 __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;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
145 // Linearize convolution elements
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100146 for(int y = yi, y_e = yi + KERNEL_HEIGHT; y < y_e; ++y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 {
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100148 for(int x = xi, x_e = xi + KERNEL_WIDTH; x < x_e; ++x, ++output_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100150#if PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100151 *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000152#else // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100153 if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000155#if defined(OFFSET)
156 *output_ptr = OFFSET;
157#else /* OFFSET */
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100158 *output_ptr = 0;
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000159#endif /* OFFSET */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 }
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100161 else
162 {
163 *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
164 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100165#endif // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 }
167 }
168
Gian Marco Iodice368da832017-07-03 12:33:49 +0100169#ifdef HAS_BIAS
steniu01868e5412017-07-17 23:16:00 +0100170 if(ch == (KERNEL_DEPTH - 1))
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100171 {
Gian Marco Iodice368da832017-07-03 12:33:49 +0100172#ifdef FIXED_POINT_POSITION
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100173 *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100174#else // FIXED_POINT_POSITION
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100175 *output_ptr = 1.0f;
Gian Marco Iodice368da832017-07-03 12:33:49 +0100176#endif // FIXED_POINT_POSITION
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100177 }
178#endif // HAS_BIAS
179}
180
181/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM when the kernel size is 3x3 and pad_x = pad_y = 0
182 *
183 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
184 * @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.
185 *
186 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32
187 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
188 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
189 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
190 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
191 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
192 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
193 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
194 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
195 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
196 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
197 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
198 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
199 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Giorgio Arena9fe41442017-08-23 16:36:24 +0100200 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
201 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100202 */
203__kernel void im2col_kernel3x3_padx0_pady0(
204 TENSOR3D_DECLARATION(src),
steniu01868e5412017-07-17 23:16:00 +0100205 IMAGE_DECLARATION(dst),
steniu01868e5412017-07-17 23:16:00 +0100206 uint src_stride_w,
207 uint dst_stride_w)
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100208{
steniu01868e5412017-07-17 23:16:00 +0100209 const int xc = get_global_id(0); // x coordinate in the convolved tensor
210 const int yc = get_global_id(1); // y coordinate in the convolved tensor
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000211 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
212 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100213
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000214 // Calculate input indices
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100215 const int xi = xc * STRIDE_X;
216 const int yi = yc * STRIDE_Y;
217
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000218 // Calculate output indices
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100219 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
220 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
221
222 // Get input and output address
steniu01868e5412017-07-17 23:16:00 +0100223 __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;
224
225 __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;
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100226
227 VEC_DATA_TYPE(DATA_TYPE, 3)
228 row0 = vload3(0, (__global DATA_TYPE *)(input_ptr + 0 * src_stride_y));
229 VEC_DATA_TYPE(DATA_TYPE, 3)
230 row1 = vload3(0, (__global DATA_TYPE *)(input_ptr + 1 * src_stride_y));
231 VEC_DATA_TYPE(DATA_TYPE, 3)
232 row2 = vload3(0, (__global DATA_TYPE *)(input_ptr + 2 * src_stride_y));
233
234 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row0.s012, row1.s012, row2.s01), 0, output_ptr);
235 *(output_ptr + 8) = row2.s2;
236
237#ifdef HAS_BIAS
steniu01868e5412017-07-17 23:16:00 +0100238 if(ch == (KERNEL_DEPTH - 1))
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100239 {
240#ifdef FIXED_POINT_POSITION
241 *(output_ptr + 9) = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
242#else // FIXED_POINT_POSITION
243 *(output_ptr + 9) = 1.0f;
244#endif // FIXED_POINT_POSITION
245 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100246#endif // HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247}
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100248#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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100250#if defined(WIDTH_OUTPUT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251/** This kernel performs a reshaping of the output of the convolution layer.
252 *
253 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
254 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100255 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
257 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
258 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
259 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
steniu01cfc6fe82017-07-27 15:42:44 +0100260 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
261 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100263 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
265 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
266 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
267 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
268 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
269 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
270 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
steniu01cfc6fe82017-07-27 15:42:44 +0100271 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 */
273__kernel void col2im(
steniu01cfc6fe82017-07-27 15:42:44 +0100274 TENSOR3D_DECLARATION(src),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275 TENSOR3D_DECLARATION(dst),
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100276 uint dst_stride_w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277{
steniu01cfc6fe82017-07-27 15:42:44 +0100278 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(dst);
280
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100281 // Compute output offset
282 int idx = get_global_id(0) * dst.stride_z + (get_global_id(1) / WIDTH_OUTPUT) * dst_stride_y + (get_global_id(1) % WIDTH_OUTPUT) * dst_stride_x + get_global_id(2) * dst_stride_w;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100284 // Store value
285 *((__global DATA_TYPE *)(dst.ptr + idx)) = *((__global DATA_TYPE *)(src.ptr));
286}
287#endif // defined(WIDTH_OUTPUT)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100288
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289/** This kernel reshapes the tensor's low three dimensions to single row for GEMM operation
290 *
291 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
292 * @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.
293 *
Gian Marco Iodice368da832017-07-03 12:33:49 +0100294 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
296 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
297 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
298 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
299 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
300 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
301 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100302 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
304 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
305 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
306 * @param[in] width The width of the input tensor
307 * @param[in] height The height of the input tensor
308 */
309__kernel void im2col_reduced(
310 TENSOR3D_DECLARATION(src),
311 VECTOR_DECLARATION(dst),
312 uint width, uint height)
313{
314 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
315
316 const uint image_size = width * height;
317
318 __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;
319
320 *((__global DATA_TYPE *)tmp_out_ptr) = *((__global DATA_TYPE *)src.ptr);
321
Anthony Barbierac69aa12017-07-03 17:39:37 +0100322#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323 // If it is the last thread in the 3 dimensional workgroup
324 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))
325 {
326 tmp_out_ptr += dst_stride_x;
Gian Marco Iodice368da832017-07-03 12:33:49 +0100327#ifdef FIXED_POINT_POSITION
328 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
329#else // FIXED_POINT_POSITION
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)1;
Gian Marco Iodice368da832017-07-03 12:33:49 +0100331#endif // FIXED_POINT_POSITION
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100333#endif // HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334}
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000335
336#if 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)
337/** This kernel reshapes the input tensor to a tensor used to perform convolution using GEMM when
338 * the kernel width is greater than 1 (except when the kernel size is 3x3) and pad_x == pad_y == 0.
339 *
340 * @note The data type must be passed at compile time using -DDATA_TYPE e.g. -DDATA_TYPE=float.
341 * @note The vector size must be passed at compile time using -DVECTOR_SIZE e.g. -DVECTOR_SIZE=4.
342 * @note The width modulo vector size must be passed at compile time using -DWIDTH_MOD_VECTOR_SIZE e.g. -DWIDTH_MOD_VECTOR_SIZE=3.
343 * @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.
344 *
345 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32
346 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
347 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
348 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
349 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
350 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
351 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
352 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
353 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
354 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
355 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
356 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
357 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
358 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
359 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
360 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
361 */
362__kernel void im2col_generic_padx0_pady0(
363 TENSOR3D_DECLARATION(src),
364 IMAGE_DECLARATION(dst),
365 uint src_stride_w,
366 uint dst_stride_w)
367{
368 const int xc = get_global_id(0); // x coordinate in the convolved tensor
369 const int yc = get_global_id(1); // y coordinate in the convolved tensor
370 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
371 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
372
373 // Calculate input indices
374 const int xi = xc * STRIDE_X;
375 const int yi = yc * STRIDE_Y;
376 // Calculate output indices
377 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
378 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
379 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w;
380 __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;
381 // Linearize convolution elements
382 for(int y = yi, y_e = yi + KERNEL_HEIGHT; y < y_e; ++y)
383 {
384 int last_x = 0;
385 for(int x = xi, x_e = xi + KERNEL_WIDTH; x + VECTOR_SIZE <= x_e; x += VECTOR_SIZE, output_ptr += VECTOR_SIZE)
386 {
387 VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE)
388 row = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
389 VSTORE(VECTOR_SIZE)
390 (row, 0, output_ptr);
391 last_x = x;
392 }
393 // Copy the remainder of the row by doing VLOAD(WIDTH_MOD_VECTOR_SIZE) and VSTORE(WIDTH_MOD_VECTOR_SIZE).
394 // Note that x and output_ptr have already been incremented by VECTOR_SIZE by the loop just before exit.
395#if WIDTH_MOD_VECTOR_SIZE == 1
396 *output_ptr = *((__global DATA_TYPE *)(input_ptr + (last_x + VECTOR_SIZE) * src_stride_x + y * src_stride_y));
397#elif WIDTH_MOD_VECTOR_SIZE > 1
398 VEC_DATA_TYPE(DATA_TYPE, WIDTH_MOD_VECTOR_SIZE)
399 row = VLOAD(WIDTH_MOD_VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + (last_x + VECTOR_SIZE) * src_stride_x + y * src_stride_y));
400 VSTORE(WIDTH_MOD_VECTOR_SIZE)
401 (row, 0, output_ptr);
402#endif /* WIDTH_MOD_VECTOR_SIZE */
403 output_ptr += WIDTH_MOD_VECTOR_SIZE;
404 } /* End of loop over KERNEL_HEIGHT */
405
406#ifdef HAS_BIAS
407 if(ch == (KERNEL_DEPTH - 1))
408 {
409#ifdef FIXED_POINT_POSITION
410 *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
411#else // FIXED_POINT_POSITION
412 *output_ptr = 1.0f;
413#endif // FIXED_POINT_POSITION
414 }
415#endif // HAS_BIAS
416}
417#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)