blob: 77b9b649451ff96bc45ce2b77d408e6d2a0d8561 [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
Chunosov5124be52017-11-22 20:42:13 +0700100#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(PAD_VALUE)
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
Chunosov5124be52017-11-22 20:42:13 +0700104 * @note The value to use for the paddings must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 * @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.
106 *
Chunosov5124be52017-11-22 20:42:13 +0700107 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
109 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
110 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
111 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
112 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
113 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
114 * @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 +0100115 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
117 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
118 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
119 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
120 * @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 +0100121 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
122 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 */
124__kernel void im2col_generic(
125 TENSOR3D_DECLARATION(src),
steniu01868e5412017-07-17 23:16:00 +0100126 IMAGE_DECLARATION(dst),
steniu01868e5412017-07-17 23:16:00 +0100127 uint src_stride_w,
128 uint dst_stride_w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129{
steniu01868e5412017-07-17 23:16:00 +0100130 const int xc = get_global_id(0); // x coordinate in the convolved tensor
131 const int yc = get_global_id(1); // y coordinate in the convolved tensor
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000132 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
133 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000135 // Calculate input indices
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100136 const int xi = xc * STRIDE_X - PAD_LEFT;
137 const int yi = yc * STRIDE_Y - PAD_TOP;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000139 // Calculate output indices
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100140 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
141 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
142
steniu01868e5412017-07-17 23:16:00 +0100143 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w;
144 __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 +0100145
146 // Linearize convolution elements
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100147 for(int y = yi, y_e = yi + KERNEL_HEIGHT; y < y_e; ++y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 {
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100149 for(int x = xi, x_e = xi + KERNEL_WIDTH; x < x_e; ++x, ++output_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100151#if PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100152 *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
Chunosov5124be52017-11-22 20:42:13 +0700153#else // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100154 if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 {
Chunosov5124be52017-11-22 20:42:13 +0700156 *output_ptr = PAD_VALUE;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 }
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100158 else
159 {
160 *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
161 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100162#endif // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
164 }
165
Gian Marco Iodice368da832017-07-03 12:33:49 +0100166#ifdef HAS_BIAS
steniu01868e5412017-07-17 23:16:00 +0100167 if(ch == (KERNEL_DEPTH - 1))
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100168 {
Gian Marco Iodice368da832017-07-03 12:33:49 +0100169#ifdef FIXED_POINT_POSITION
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100170 *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100171#else // FIXED_POINT_POSITION
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100172 *output_ptr = 1.0f;
Gian Marco Iodice368da832017-07-03 12:33:49 +0100173#endif // FIXED_POINT_POSITION
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100174 }
175#endif // HAS_BIAS
176}
177
178/** 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
179 *
180 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
181 * @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.
182 *
Chunosov5124be52017-11-22 20:42:13 +0700183 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100184 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
185 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
186 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
187 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
188 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
189 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
190 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
191 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
192 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
193 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
194 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
195 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
196 * @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 +0100197 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
198 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100199 */
200__kernel void im2col_kernel3x3_padx0_pady0(
201 TENSOR3D_DECLARATION(src),
steniu01868e5412017-07-17 23:16:00 +0100202 IMAGE_DECLARATION(dst),
steniu01868e5412017-07-17 23:16:00 +0100203 uint src_stride_w,
204 uint dst_stride_w)
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100205{
steniu01868e5412017-07-17 23:16:00 +0100206 const int xc = get_global_id(0); // x coordinate in the convolved tensor
207 const int yc = get_global_id(1); // y coordinate in the convolved tensor
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000208 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
209 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100210
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000211 // Calculate input indices
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100212 const int xi = xc * STRIDE_X;
213 const int yi = yc * STRIDE_Y;
214
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000215 // Calculate output indices
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100216 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
217 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
218
219 // Get input and output address
steniu01868e5412017-07-17 23:16:00 +0100220 __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;
221
222 __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 +0100223
224 VEC_DATA_TYPE(DATA_TYPE, 3)
225 row0 = vload3(0, (__global DATA_TYPE *)(input_ptr + 0 * src_stride_y));
226 VEC_DATA_TYPE(DATA_TYPE, 3)
227 row1 = vload3(0, (__global DATA_TYPE *)(input_ptr + 1 * src_stride_y));
228 VEC_DATA_TYPE(DATA_TYPE, 3)
229 row2 = vload3(0, (__global DATA_TYPE *)(input_ptr + 2 * src_stride_y));
230
231 vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row0.s012, row1.s012, row2.s01), 0, output_ptr);
232 *(output_ptr + 8) = row2.s2;
233
234#ifdef HAS_BIAS
steniu01868e5412017-07-17 23:16:00 +0100235 if(ch == (KERNEL_DEPTH - 1))
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100236 {
237#ifdef FIXED_POINT_POSITION
238 *(output_ptr + 9) = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
239#else // FIXED_POINT_POSITION
240 *(output_ptr + 9) = 1.0f;
241#endif // FIXED_POINT_POSITION
242 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100243#endif // HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244}
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100245#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 +0100246
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100247#if defined(WIDTH_OUTPUT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248/** This kernel performs a reshaping of the output of the convolution layer.
249 *
250 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
251 *
Chunosov5124be52017-11-22 20:42:13 +0700252 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
254 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
255 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
256 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
steniu01cfc6fe82017-07-27 15:42:44 +0100257 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
258 * @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 +0100259 * @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 +0100260 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
262 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
263 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
264 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
265 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
266 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
267 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
steniu01cfc6fe82017-07-27 15:42:44 +0100268 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100269 */
270__kernel void col2im(
steniu01cfc6fe82017-07-27 15:42:44 +0100271 TENSOR3D_DECLARATION(src),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 TENSOR3D_DECLARATION(dst),
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100273 uint dst_stride_w)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274{
steniu01cfc6fe82017-07-27 15:42:44 +0100275 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(dst);
277
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100278 // Compute output offset
279 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 +0100280
Gian Marco Iodice2eac5bd2017-08-14 14:22:23 +0100281 // Store value
282 *((__global DATA_TYPE *)(dst.ptr + idx)) = *((__global DATA_TYPE *)(src.ptr));
283}
284#endif // defined(WIDTH_OUTPUT)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100285
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286/** This kernel reshapes the tensor's low three dimensions to single row for GEMM operation
287 *
288 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
289 * @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.
290 *
Chunosov5124be52017-11-22 20:42:13 +0700291 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QASYMM8/QS16/F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
293 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
294 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
295 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
296 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
297 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
298 * @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 +0100299 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
301 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
302 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
303 * @param[in] width The width of the input tensor
304 * @param[in] height The height of the input tensor
305 */
306__kernel void im2col_reduced(
307 TENSOR3D_DECLARATION(src),
308 VECTOR_DECLARATION(dst),
309 uint width, uint height)
310{
311 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
312
313 const uint image_size = width * height;
314
315 __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;
316
317 *((__global DATA_TYPE *)tmp_out_ptr) = *((__global DATA_TYPE *)src.ptr);
318
Anthony Barbierac69aa12017-07-03 17:39:37 +0100319#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 // If it is the last thread in the 3 dimensional workgroup
321 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))
322 {
323 tmp_out_ptr += dst_stride_x;
Gian Marco Iodice368da832017-07-03 12:33:49 +0100324#ifdef FIXED_POINT_POSITION
325 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
326#else // FIXED_POINT_POSITION
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)1;
Gian Marco Iodice368da832017-07-03 12:33:49 +0100328#endif // FIXED_POINT_POSITION
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100330#endif // HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331}
Anthony Barbierfcd52fb2017-11-28 10:31:43 +0000332
333#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)
334/** This kernel reshapes the input tensor to a tensor used to perform convolution using GEMM when
335 * the kernel width is greater than 1 (except when the kernel size is 3x3) and pad_x == pad_y == 0.
336 *
337 * @note The data type must be passed at compile time using -DDATA_TYPE e.g. -DDATA_TYPE=float.
338 * @note The vector size must be passed at compile time using -DVECTOR_SIZE e.g. -DVECTOR_SIZE=4.
339 * @note The width modulo vector size must be passed at compile time using -DWIDTH_MOD_VECTOR_SIZE e.g. -DWIDTH_MOD_VECTOR_SIZE=3.
340 * @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.
341 *
342 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32
343 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
344 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
345 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
346 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
347 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
348 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
349 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
350 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
351 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
352 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
353 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
354 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
355 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
356 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
357 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
358 */
359__kernel void im2col_generic_padx0_pady0(
360 TENSOR3D_DECLARATION(src),
361 IMAGE_DECLARATION(dst),
362 uint src_stride_w,
363 uint dst_stride_w)
364{
365 const int xc = get_global_id(0); // x coordinate in the convolved tensor
366 const int yc = get_global_id(1); // y coordinate in the convolved tensor
367 const int ch = get_global_id(2) % KERNEL_DEPTH; // input feature map
368 const int batch = get_global_id(2) / KERNEL_DEPTH; // batch size
369
370 // Calculate input indices
371 const int xi = xc * STRIDE_X;
372 const int yi = yc * STRIDE_Y;
373 // Calculate output indices
374 const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
375 const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
376 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w;
377 __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;
378 // Linearize convolution elements
379 for(int y = yi, y_e = yi + KERNEL_HEIGHT; y < y_e; ++y)
380 {
381 int last_x = 0;
382 for(int x = xi, x_e = xi + KERNEL_WIDTH; x + VECTOR_SIZE <= x_e; x += VECTOR_SIZE, output_ptr += VECTOR_SIZE)
383 {
384 VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE)
385 row = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y));
386 VSTORE(VECTOR_SIZE)
387 (row, 0, output_ptr);
388 last_x = x;
389 }
390 // Copy the remainder of the row by doing VLOAD(WIDTH_MOD_VECTOR_SIZE) and VSTORE(WIDTH_MOD_VECTOR_SIZE).
391 // Note that x and output_ptr have already been incremented by VECTOR_SIZE by the loop just before exit.
392#if WIDTH_MOD_VECTOR_SIZE == 1
393 *output_ptr = *((__global DATA_TYPE *)(input_ptr + (last_x + VECTOR_SIZE) * src_stride_x + y * src_stride_y));
394#elif WIDTH_MOD_VECTOR_SIZE > 1
395 VEC_DATA_TYPE(DATA_TYPE, WIDTH_MOD_VECTOR_SIZE)
396 row = VLOAD(WIDTH_MOD_VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + (last_x + VECTOR_SIZE) * src_stride_x + y * src_stride_y));
397 VSTORE(WIDTH_MOD_VECTOR_SIZE)
398 (row, 0, output_ptr);
399#endif /* WIDTH_MOD_VECTOR_SIZE */
400 output_ptr += WIDTH_MOD_VECTOR_SIZE;
401 } /* End of loop over KERNEL_HEIGHT */
402
403#ifdef HAS_BIAS
404 if(ch == (KERNEL_DEPTH - 1))
405 {
406#ifdef FIXED_POINT_POSITION
407 *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION);
408#else // FIXED_POINT_POSITION
409 *output_ptr = 1.0f;
410#endif // FIXED_POINT_POSITION
411 }
412#endif // HAS_BIAS
413}
414#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)