Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 26 | #if defined(FIXED_POINT_POSITION) |
| 27 | #include "fixed_point.h" |
| 28 | #endif // FIXED_POINT_POSITION |
| 29 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | /** 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 Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 34 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | * @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 Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 42 | * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | * @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 Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 48 | * @param[in] bias_ptr Pointer to the bias tensor. Same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | * @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 Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 60 | #ifdef HAS_BIAS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | VECTOR_DECLARATION(bias), |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 62 | #endif /* HAS_BIAS */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | 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 Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 71 | #ifdef HAS_BIAS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | __global uchar *tmp_bias_ptr = bias_ptr + bias_offset_first_element_in_bytes; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 73 | #endif /* HAS_BIAS */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
| 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 Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 81 | #ifdef HAS_BIAS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | *((__global DATA_TYPE *)(tmp_dst_ptr + dst_stride_y)) = *((__global DATA_TYPE *)(tmp_bias_ptr)); |
| 83 | tmp_bias_ptr += bias_stride_x; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 84 | #endif /* HAS_BIAS */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | 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 Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 100 | #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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | /** 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 Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 106 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | * @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 Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 114 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | * @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 Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 120 | * @param[in] filter_depth The depth of the used filter |
| 121 | * @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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | */ |
| 124 | __kernel void im2col_generic( |
| 125 | TENSOR3D_DECLARATION(src), |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 126 | IMAGE_DECLARATION(dst), |
| 127 | uint filter_depth, |
| 128 | uint src_stride_w, |
| 129 | uint dst_stride_w) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | { |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 131 | const int xc = get_global_id(0); // x coordinate in the convolved tensor |
| 132 | const int yc = get_global_id(1); // y coordinate in the convolved tensor |
| 133 | const int ch = get_global_id(2) % filter_depth; // input feature map |
| 134 | const int batch = get_global_id(2) / filter_depth; // the batch |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 136 | // Calculate input indeces |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 137 | const int xi = xc * STRIDE_X - PAD_LEFT; |
| 138 | const int yi = yc * STRIDE_Y - PAD_TOP; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 140 | // Calculate output indeces |
| 141 | const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT; |
| 142 | const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution |
| 143 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 144 | __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * src_stride_z + batch * src_stride_w; |
| 145 | __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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | |
| 147 | // Linearize convolution elements |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 148 | for(int y = yi, y_e = yi + KERNEL_HEIGHT; y < y_e; ++y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | { |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 150 | for(int x = xi, x_e = xi + KERNEL_WIDTH; x < x_e; ++x, ++output_ptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 152 | #if PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0 |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 153 | *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y)); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame^] | 154 | #else // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0 |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 155 | if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame^] | 157 | #if defined(OFFSET) |
| 158 | *output_ptr = OFFSET; |
| 159 | #else /* OFFSET */ |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 160 | *output_ptr = 0; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame^] | 161 | #endif /* OFFSET */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | } |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 163 | else |
| 164 | { |
| 165 | *output_ptr = *((__global DATA_TYPE *)(input_ptr + x * src_stride_x + y * src_stride_y)); |
| 166 | } |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 167 | #endif // PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 171 | #ifdef HAS_BIAS |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 172 | if(ch == (KERNEL_DEPTH - 1)) |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 173 | { |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 174 | #ifdef FIXED_POINT_POSITION |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 175 | *output_ptr = (DATA_TYPE)(1 << FIXED_POINT_POSITION); |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 176 | #else // FIXED_POINT_POSITION |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 177 | *output_ptr = 1.0f; |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 178 | #endif // FIXED_POINT_POSITION |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 179 | } |
| 180 | #endif // HAS_BIAS |
| 181 | } |
| 182 | |
| 183 | /** 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 |
| 184 | * |
| 185 | * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float |
| 186 | * @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. |
| 187 | * |
| 188 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32 |
| 189 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 190 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 191 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 192 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 193 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 194 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 195 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 196 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 197 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 198 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 199 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 200 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 201 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 202 | * @param[in] filter_depth The depth of the used filter |
| 203 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes). |
| 204 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes). |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 205 | */ |
| 206 | __kernel void im2col_kernel3x3_padx0_pady0( |
| 207 | TENSOR3D_DECLARATION(src), |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 208 | IMAGE_DECLARATION(dst), |
| 209 | uint filter_depth, |
| 210 | uint src_stride_w, |
| 211 | uint dst_stride_w) |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 212 | { |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 213 | const int xc = get_global_id(0); // x coordinate in the convolved tensor |
| 214 | const int yc = get_global_id(1); // y coordinate in the convolved tensor |
| 215 | const int ch = get_global_id(2) % filter_depth; // input feature map |
| 216 | const int batch = get_global_id(2) / filter_depth; // the batch |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 217 | |
| 218 | // Calculate input indeces |
| 219 | const int xi = xc * STRIDE_X; |
| 220 | const int yi = yc * STRIDE_Y; |
| 221 | |
| 222 | // Calculate output indeces |
| 223 | const int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT; |
| 224 | const int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution |
| 225 | |
| 226 | // Get input and output address |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 227 | __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; |
| 228 | |
| 229 | __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 Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 230 | |
| 231 | VEC_DATA_TYPE(DATA_TYPE, 3) |
| 232 | row0 = vload3(0, (__global DATA_TYPE *)(input_ptr + 0 * src_stride_y)); |
| 233 | VEC_DATA_TYPE(DATA_TYPE, 3) |
| 234 | row1 = vload3(0, (__global DATA_TYPE *)(input_ptr + 1 * src_stride_y)); |
| 235 | VEC_DATA_TYPE(DATA_TYPE, 3) |
| 236 | row2 = vload3(0, (__global DATA_TYPE *)(input_ptr + 2 * src_stride_y)); |
| 237 | |
| 238 | vstore8((VEC_DATA_TYPE(DATA_TYPE, 8))(row0.s012, row1.s012, row2.s01), 0, output_ptr); |
| 239 | *(output_ptr + 8) = row2.s2; |
| 240 | |
| 241 | #ifdef HAS_BIAS |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 242 | if(ch == (KERNEL_DEPTH - 1)) |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 243 | { |
| 244 | #ifdef FIXED_POINT_POSITION |
| 245 | *(output_ptr + 9) = (DATA_TYPE)(1 << FIXED_POINT_POSITION); |
| 246 | #else // FIXED_POINT_POSITION |
| 247 | *(output_ptr + 9) = 1.0f; |
| 248 | #endif // FIXED_POINT_POSITION |
| 249 | } |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 250 | #endif // HAS_BIAS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 251 | } |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 252 | #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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 253 | |
Gian Marco Iodice | 2eac5bd | 2017-08-14 14:22:23 +0100 | [diff] [blame] | 254 | #if defined(WIDTH_OUTPUT) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | /** This kernel performs a reshaping of the output of the convolution layer. |
| 256 | * |
| 257 | * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float |
| 258 | * |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 259 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 260 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 261 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 262 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 263 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
steniu01 | cfc6fe8 | 2017-07-27 15:42:44 +0100 | [diff] [blame] | 264 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 265 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 266 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 267 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 269 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 270 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 271 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 272 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 273 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 274 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
steniu01 | cfc6fe8 | 2017-07-27 15:42:44 +0100 | [diff] [blame] | 275 | * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 276 | */ |
| 277 | __kernel void col2im( |
steniu01 | cfc6fe8 | 2017-07-27 15:42:44 +0100 | [diff] [blame] | 278 | TENSOR3D_DECLARATION(src), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 279 | TENSOR3D_DECLARATION(dst), |
Gian Marco Iodice | 2eac5bd | 2017-08-14 14:22:23 +0100 | [diff] [blame] | 280 | uint dst_stride_w) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | { |
steniu01 | cfc6fe8 | 2017-07-27 15:42:44 +0100 | [diff] [blame] | 282 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 283 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(dst); |
| 284 | |
Gian Marco Iodice | 2eac5bd | 2017-08-14 14:22:23 +0100 | [diff] [blame] | 285 | // Compute output offset |
| 286 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 287 | |
Gian Marco Iodice | 2eac5bd | 2017-08-14 14:22:23 +0100 | [diff] [blame] | 288 | // Store value |
| 289 | *((__global DATA_TYPE *)(dst.ptr + idx)) = *((__global DATA_TYPE *)(src.ptr)); |
| 290 | } |
| 291 | #endif // defined(WIDTH_OUTPUT) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 292 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 293 | /** This kernel reshapes the tensor's low three dimensions to single row for GEMM operation |
| 294 | * |
| 295 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float |
| 296 | * @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. |
| 297 | * |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 298 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 299 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 300 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 301 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 302 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 303 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 304 | * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes) |
| 305 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 306 | * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 307 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 308 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 309 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 310 | * @param[in] width The width of the input tensor |
| 311 | * @param[in] height The height of the input tensor |
| 312 | */ |
| 313 | __kernel void im2col_reduced( |
| 314 | TENSOR3D_DECLARATION(src), |
| 315 | VECTOR_DECLARATION(dst), |
| 316 | uint width, uint height) |
| 317 | { |
| 318 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
| 319 | |
| 320 | const uint image_size = width * height; |
| 321 | |
| 322 | __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; |
| 323 | |
| 324 | *((__global DATA_TYPE *)tmp_out_ptr) = *((__global DATA_TYPE *)src.ptr); |
| 325 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 326 | #ifdef HAS_BIAS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 327 | // If it is the last thread in the 3 dimensional workgroup |
| 328 | 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)) |
| 329 | { |
| 330 | tmp_out_ptr += dst_stride_x; |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 331 | #ifdef FIXED_POINT_POSITION |
| 332 | *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)(1 << FIXED_POINT_POSITION); |
| 333 | #else // FIXED_POINT_POSITION |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 334 | *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)1; |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 335 | #endif // FIXED_POINT_POSITION |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 336 | } |
Gian Marco Iodice | 368da83 | 2017-07-03 12:33:49 +0100 | [diff] [blame] | 337 | #endif // HAS_BIAS |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 338 | } |