steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 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 | |
Moritz Pflanzer | 54f366a | 2017-09-25 15:36:14 +0100 | [diff] [blame] | 26 | #undef CONVERT_SAT |
| 27 | |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 28 | #define ADD_OP(a, b) ((a) + (b)) |
| 29 | #define MUL_OP(a, b) ((a) * (b)) |
| 30 | #define CONVERT_SAT(a, b) ((a)) |
| 31 | |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 32 | #if defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) |
| 33 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 34 | #if STRIDE_X == 1 |
| 35 | #define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) |
| 36 | #elif STRIDE_X == 2 /* STRIDE_X == 1 */ |
| 37 | #define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 38 | #else /* STRIDE_X not equals 1 or 2 */ |
| 39 | #error "STRIDE_X larger than 2 is not supported" |
| 40 | #endif /* STRIDE_X == 2 */ |
| 41 | |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 42 | #define CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) \ |
| 43 | ({ \ |
steniu01 | db00668 | 2017-08-09 16:26:22 +0100 | [diff] [blame] | 44 | VEC_DATA_TYPE(DATA_TYPE, 3) \ |
| 45 | weights_values0 = vload3(0, weights_row_ptr); \ |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 46 | VEC_DATA_TYPE(DATA_TYPE, 8) \ |
| 47 | src0 = vload8(0, src_row_ptr); \ |
| 48 | VEC_DATA_TYPE(DATA_TYPE, 2) \ |
| 49 | src1 = vload2(0, src_row_ptr + 8); \ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 50 | \ |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 51 | acc = ADD_OP(acc, MUL_OP(src0, (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0)); \ |
| 52 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1234, src0.s567, src1.s0), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s1)); \ |
| 53 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s234, src0.s567, src1.s01), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s2)); \ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 54 | }) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 55 | |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 56 | #define CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) \ |
| 57 | ({ \ |
steniu01 | db00668 | 2017-08-09 16:26:22 +0100 | [diff] [blame] | 58 | VEC_DATA_TYPE(DATA_TYPE, 3) \ |
| 59 | weights_values0 = vload3(0, weights_row_ptr); \ |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 60 | VEC_DATA_TYPE(DATA_TYPE, 16) \ |
| 61 | src0 = vload16(0, src_row_ptr); \ |
| 62 | DATA_TYPE src1 = *(src_row_ptr + 16); \ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 63 | \ |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 64 | acc = ADD_OP(acc, MUL_OP(src0.even, (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0)); \ |
| 65 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1357, src0.s9BDF), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s1)); \ |
| 66 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s2468, src0.sACE, src1), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s2)); \ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 67 | }) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 68 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 69 | #if defined(DATA_LAYOUT_NHWC) |
| 70 | |
| 71 | #define PTR_TO_VALUE(PTR, DATA_TYPE) *((__global DATA_TYPE *)(PTR)) |
| 72 | |
| 73 | #if STRIDE_X == 1 |
| 74 | #define CONVOLUTION1x3_NHWC(acc, row_ptr, weights_ptr) CONVOLUTION1x3_STRIDE_NHWC_STRIDE1(acc, row_ptr, weights_ptr) |
| 75 | #elif STRIDE_X == 2 /* STRIDE_X == 1 */ |
| 76 | #define CONVOLUTION1x3_NHWC(acc, row_ptr, weights_ptr) CONVOLUTION1x3_STRIDE_NHWC_STRIDE2(acc, row_ptr, weights_ptr) |
| 77 | #else /* STRIDE_X not equals 1 or 2 */ |
| 78 | #error "STRIDE_X larger than 2 is not supported" |
| 79 | #endif /* STRIDE_X == 2 */ |
| 80 | |
| 81 | #define CONVOLUTION1x3_STRIDE_NHWC_STRIDE1(acc, row_ptr, weights_ptr) \ |
| 82 | { \ |
| 83 | VEC_DATA_TYPE(DATA_TYPE, 8) \ |
| 84 | src0 = (VEC_DATA_TYPE(DATA_TYPE, 8))( \ |
| 85 | PTR_TO_VALUE(row_ptr + 0 * src_stride_y, DATA_TYPE), \ |
| 86 | PTR_TO_VALUE(row_ptr + 1 * src_stride_y, DATA_TYPE), \ |
| 87 | PTR_TO_VALUE(row_ptr + 2 * src_stride_y, DATA_TYPE), \ |
| 88 | PTR_TO_VALUE(row_ptr + 3 * src_stride_y, DATA_TYPE), \ |
| 89 | PTR_TO_VALUE(row_ptr + 4 * src_stride_y, DATA_TYPE), \ |
| 90 | PTR_TO_VALUE(row_ptr + 5 * src_stride_y, DATA_TYPE), \ |
| 91 | PTR_TO_VALUE(row_ptr + 6 * src_stride_y, DATA_TYPE), \ |
| 92 | PTR_TO_VALUE(row_ptr + 7 * src_stride_y, DATA_TYPE)); \ |
| 93 | VEC_DATA_TYPE(DATA_TYPE, 2) \ |
| 94 | src1 = (VEC_DATA_TYPE(DATA_TYPE, 2))( \ |
| 95 | PTR_TO_VALUE(row_ptr + 8 * src_stride_y, DATA_TYPE), \ |
| 96 | PTR_TO_VALUE(row_ptr + 9 * src_stride_y, DATA_TYPE)); \ |
| 97 | VEC_DATA_TYPE(DATA_TYPE, 3) \ |
| 98 | weights = (VEC_DATA_TYPE(DATA_TYPE, 3))( \ |
| 99 | PTR_TO_VALUE((weights_ptr) + 0 * weights_stride_y, DATA_TYPE), \ |
| 100 | PTR_TO_VALUE((weights_ptr) + 1 * weights_stride_y, DATA_TYPE), \ |
| 101 | PTR_TO_VALUE((weights_ptr) + 2 * weights_stride_y, DATA_TYPE)); \ |
| 102 | acc = ADD_OP(acc, MUL_OP(src0, (VEC_DATA_TYPE(DATA_TYPE, 8))weights.s0)); \ |
| 103 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1234, src0.s567, src1.s0), (VEC_DATA_TYPE(DATA_TYPE, 8))weights.s1)); \ |
| 104 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s234, src0.s567, src1.s01), (VEC_DATA_TYPE(DATA_TYPE, 8))weights.s2)); \ |
| 105 | } |
| 106 | |
| 107 | #define CONVOLUTION1x3_STRIDE_NHWC_STRIDE2(acc, row_ptr, weights_ptr) \ |
| 108 | { \ |
| 109 | VEC_DATA_TYPE(DATA_TYPE, 16) \ |
| 110 | src0 = (VEC_DATA_TYPE(DATA_TYPE, 16))( \ |
| 111 | PTR_TO_VALUE(row_ptr + 0 * src_stride_y, DATA_TYPE), \ |
| 112 | PTR_TO_VALUE(row_ptr + 1 * src_stride_y, DATA_TYPE), \ |
| 113 | PTR_TO_VALUE(row_ptr + 2 * src_stride_y, DATA_TYPE), \ |
| 114 | PTR_TO_VALUE(row_ptr + 3 * src_stride_y, DATA_TYPE), \ |
| 115 | PTR_TO_VALUE(row_ptr + 4 * src_stride_y, DATA_TYPE), \ |
| 116 | PTR_TO_VALUE(row_ptr + 5 * src_stride_y, DATA_TYPE), \ |
| 117 | PTR_TO_VALUE(row_ptr + 6 * src_stride_y, DATA_TYPE), \ |
| 118 | PTR_TO_VALUE(row_ptr + 7 * src_stride_y, DATA_TYPE), \ |
| 119 | PTR_TO_VALUE(row_ptr + 8 * src_stride_y, DATA_TYPE), \ |
| 120 | PTR_TO_VALUE(row_ptr + 9 * src_stride_y, DATA_TYPE), \ |
| 121 | PTR_TO_VALUE(row_ptr + 10 * src_stride_y, DATA_TYPE), \ |
| 122 | PTR_TO_VALUE(row_ptr + 11 * src_stride_y, DATA_TYPE), \ |
| 123 | PTR_TO_VALUE(row_ptr + 12 * src_stride_y, DATA_TYPE), \ |
| 124 | PTR_TO_VALUE(row_ptr + 13 * src_stride_y, DATA_TYPE), \ |
| 125 | PTR_TO_VALUE(row_ptr + 14 * src_stride_y, DATA_TYPE), \ |
| 126 | PTR_TO_VALUE(row_ptr + 15 * src_stride_y, DATA_TYPE)); \ |
| 127 | DATA_TYPE src1 = PTR_TO_VALUE(row_ptr + 16 * src_stride_y, DATA_TYPE); \ |
| 128 | VEC_DATA_TYPE(DATA_TYPE, 3) \ |
| 129 | weights = (VEC_DATA_TYPE(DATA_TYPE, 3))( \ |
| 130 | PTR_TO_VALUE((weights_ptr) + 0 * weights_stride_y, DATA_TYPE), \ |
| 131 | PTR_TO_VALUE((weights_ptr) + 1 * weights_stride_y, DATA_TYPE), \ |
| 132 | PTR_TO_VALUE((weights_ptr) + 2 * weights_stride_y, DATA_TYPE)); \ |
| 133 | \ |
| 134 | acc = ADD_OP(acc, MUL_OP(src0.s02468ACE, (VEC_DATA_TYPE(DATA_TYPE, 8))weights.s0)); \ |
| 135 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1357, src0.s9BDF), (VEC_DATA_TYPE(DATA_TYPE, 8))weights.s1)); \ |
| 136 | acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s2468, src0.sACE, src1), (VEC_DATA_TYPE(DATA_TYPE, 8))weights.s2)); \ |
| 137 | } |
| 138 | |
| 139 | /** This kernel performs a direct convolution to convolve the low three dimensions. |
| 140 | * |
| 141 | * @note This OpenCL kernel works with stride_x = 1 and 2 |
| 142 | * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float |
| 143 | * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH |
| 144 | * @note If biases are used then -DHAS_BIAS has to be passed at compile time |
| 145 | * |
| 146 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: QS8/QS16/F16/F32 |
| 147 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 148 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 149 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 150 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 151 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 152 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 153 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 154 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 155 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 156 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 157 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 158 | * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes) |
| 159 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 160 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 161 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 162 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr |
| 163 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 164 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 165 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 166 | * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes) |
| 167 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 168 | * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes) |
| 169 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 170 | * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr |
| 171 | * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes) |
| 172 | * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 173 | * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor |
| 174 | * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension |
| 175 | */ |
| 176 | __kernel void direct_convolution3x3_nhwc( |
| 177 | TENSOR3D_DECLARATION(src), |
| 178 | TENSOR3D_DECLARATION(dst), |
| 179 | TENSOR3D_DECLARATION(weights), |
| 180 | #ifdef HAS_BIAS |
| 181 | VECTOR_DECLARATION(biases), |
| 182 | #endif /* defined(HAS_BIAS) */ |
| 183 | unsigned int weights_stride_w) |
| 184 | { |
| 185 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 186 | Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights); |
| 187 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 188 | |
| 189 | VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8) |
| 190 | values0 = 0; |
| 191 | const int id0 = get_global_id(0); |
| 192 | const int id1 = get_global_id(1); |
| 193 | const int id2 = get_global_id(2); |
| 194 | |
| 195 | __global uchar *weights_addr = (__global uchar *)tensor3D_offset(&weights, 0, 0, 0); |
| 196 | __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0) - src_stride_x * id0 + ((id2 * STRIDE_Y) - PAD_TOP) * (int)src_stride_z; |
| 197 | |
| 198 | weights_addr += id0 * weights_stride_w; |
| 199 | |
| 200 | const int coordy = ((id2 * STRIDE_Y) - PAD_TOP); |
| 201 | for(volatile int d = 0; d < WEIGHTS_DEPTH; ++d) |
| 202 | { |
| 203 | #if PAD_TOP > 0 |
| 204 | if(coordy < 0) // special case Z = -1 doesn't exists |
| 205 | { |
| 206 | //skip first row and load the two next ones |
| 207 | CONVOLUTION1x3_NHWC(values0, src_addr + 1 * (int)src_stride_z, (weights_addr + 1 * (int)weights_stride_z)); |
| 208 | CONVOLUTION1x3_NHWC(values0, src_addr + 2 * (int)src_stride_z, (weights_addr + 2 * (int)weights_stride_z)); |
| 209 | } |
| 210 | else if(coordy == (SRC_HEIGHT - PAD_TOP - 1)) |
| 211 | { |
| 212 | // special case when computing the last row of the output we must read the last three rows from the input buffer (including padding) but the |
| 213 | // Z axis has no padding at all. |
| 214 | CONVOLUTION1x3_NHWC(values0, src_addr, (weights_addr + 0 * (int)weights_stride_z)); |
| 215 | CONVOLUTION1x3_NHWC(values0, src_addr + 1 * (int)src_stride_z, (weights_addr + 1 * (int)weights_stride_z)); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | CONVOLUTION1x3_NHWC(values0, src_addr, (weights_addr + 0 * (int)weights_stride_z)); |
| 220 | CONVOLUTION1x3_NHWC(values0, src_addr + 1 * (int)src_stride_z, (weights_addr + 1 * (int)weights_stride_z)); |
| 221 | CONVOLUTION1x3_NHWC(values0, src_addr + 2 * (int)src_stride_z, (weights_addr + 2 * (int)weights_stride_z)); |
| 222 | } |
| 223 | #else // PAD_TOP > 0 |
| 224 | CONVOLUTION1x3_NHWC(values0, src_addr, (weights_addr + 0 * (int)weights_stride_z)); |
| 225 | CONVOLUTION1x3_NHWC(values0, src_addr + 1 * (int)src_stride_z, (weights_addr + 1 * (int)weights_stride_z)); |
| 226 | CONVOLUTION1x3_NHWC(values0, src_addr + 2 * (int)src_stride_z, (weights_addr + 2 * (int)weights_stride_z)); |
| 227 | #endif // PAD_TOP > 0 |
| 228 | src_addr += src_stride_x; |
| 229 | weights_addr += weights_stride_x; |
| 230 | } |
| 231 | |
| 232 | #ifdef HAS_BIAS |
| 233 | Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); |
| 234 | values0 = ADD_OP(values0, (VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)) * ((__global DATA_TYPE *)(vector_offset(&biases, id0)))); |
| 235 | #endif /* defined(HAS_BIAS) */ |
| 236 | |
| 237 | *((__global DATA_TYPE *)(dst.ptr + 0 * dst_stride_y)) = values0.s0; |
| 238 | *((__global DATA_TYPE *)(dst.ptr + 1 * dst_stride_y)) = values0.s1; |
| 239 | *((__global DATA_TYPE *)(dst.ptr + 2 * dst_stride_y)) = values0.s2; |
| 240 | *((__global DATA_TYPE *)(dst.ptr + 3 * dst_stride_y)) = values0.s3; |
| 241 | *((__global DATA_TYPE *)(dst.ptr + 4 * dst_stride_y)) = values0.s4; |
| 242 | *((__global DATA_TYPE *)(dst.ptr + 5 * dst_stride_y)) = values0.s5; |
| 243 | *((__global DATA_TYPE *)(dst.ptr + 6 * dst_stride_y)) = values0.s6; |
| 244 | *((__global DATA_TYPE *)(dst.ptr + 7 * dst_stride_y)) = values0.s7; |
| 245 | } |
| 246 | #endif // defined(DATA_LAYOUT_NHWC) |
| 247 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 248 | /** This kernel performs a direct convolution to convolve the low three dimensions. |
| 249 | * |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 250 | * @note This OpenCL kernel works with stride_x = 1 and 2 |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 251 | * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 252 | * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 253 | * @note If biases are used then -DHAS_BIAS has to be passed at compile time |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 254 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 255 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32 |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 256 | * @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) |
| 260 | * @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) |
| 262 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 263 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 264 | * @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 Z 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 |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 271 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 272 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 273 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 274 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 275 | * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes) |
| 276 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 277 | * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes) |
| 278 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 279 | * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr |
| 280 | * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes) |
| 281 | * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 282 | * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 283 | * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 284 | */ |
| 285 | __kernel void direct_convolution3x3( |
| 286 | TENSOR3D_DECLARATION(src), |
| 287 | TENSOR3D_DECLARATION(dst), |
| 288 | TENSOR3D_DECLARATION(weights), |
| 289 | #ifdef HAS_BIAS |
| 290 | VECTOR_DECLARATION(biases), |
| 291 | #endif /* defined(HAS_BIAS) */ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 292 | unsigned int weights_stride_w) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 293 | { |
| 294 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 295 | Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights); |
| 296 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 297 | |
Michalis Spyrou | def665a | 2017-08-14 11:26:37 +0100 | [diff] [blame] | 298 | VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8) |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 299 | values0 = 0; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 300 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 301 | __global uchar *weights_addr = (__global uchar *)tensor3D_offset(&weights, 0, 0, 0); |
| 302 | __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 303 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 304 | const int kernel_index = get_global_id(2); |
| 305 | weights_addr += kernel_index * weights_stride_w; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 306 | |
Gian Marco Iodice | 744b5ed | 2017-10-06 15:44:27 +0100 | [diff] [blame] | 307 | for(volatile int d = 0; d < WEIGHTS_DEPTH; ++d) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 308 | { |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 309 | CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_y)); |
| 310 | CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y)); |
| 311 | CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_y)); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 312 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 313 | src_addr += src_stride_z; |
| 314 | weights_addr += weights_stride_z; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | #ifdef HAS_BIAS |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame] | 318 | Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); |
| 319 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 320 | values0 = ADD_OP(values0, (VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)) * ((__global DATA_TYPE *)(vector_offset(&biases, kernel_index)))); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 321 | #endif /* defined(HAS_BIAS) */ |
| 322 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 323 | vstore8(CONVERT_SAT(values0, VEC_DATA_TYPE(DATA_TYPE, 8)), 0, (__global DATA_TYPE *)dst.ptr); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 324 | } |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 325 | #endif //defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) |
| 326 | |
| 327 | #if defined(WEIGHTS_DEPTH) |
| 328 | |
| 329 | #define CONVOLUTION1x3_BIFROST(acc, src0, src1, weights_row0) \ |
| 330 | ({ \ |
| 331 | acc.s0 = mad(src0.s0, weights_row0.s0, acc.s0); \ |
| 332 | acc.s1 = mad(src0.s1, weights_row0.s0, acc.s1); \ |
| 333 | acc.s2 = mad(src0.s2, weights_row0.s0, acc.s2); \ |
| 334 | acc.s3 = mad(src0.s3, weights_row0.s0, acc.s3); \ |
| 335 | acc.s0 = mad(src0.s1, weights_row0.s1, acc.s0); \ |
| 336 | acc.s1 = mad(src0.s2, weights_row0.s1, acc.s1); \ |
| 337 | acc.s2 = mad(src0.s3, weights_row0.s1, acc.s2); \ |
| 338 | acc.s3 = mad(src1.s0, weights_row0.s1, acc.s3); \ |
| 339 | acc.s0 = mad(src0.s2, weights_row0.s2, acc.s0); \ |
| 340 | acc.s1 = mad(src0.s3, weights_row0.s2, acc.s1); \ |
| 341 | acc.s2 = mad(src1.s0, weights_row0.s2, acc.s2); \ |
| 342 | acc.s3 = mad(src1.s1, weights_row0.s2, acc.s3); \ |
| 343 | }) |
| 344 | |
| 345 | /** An optimized direct convolution 3x3 OpenCL kernel for Bifrost architectures when the data type is F32 |
| 346 | * |
| 347 | * @note This OpenCL kernel works only with stride_x and stride_y equal to 1 |
| 348 | * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH |
| 349 | * @note In case biases, -DHAS_BIAS must to be passed at compile |
| 350 | * |
| 351 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32 |
| 352 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 353 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 354 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 355 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 356 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 357 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 358 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 359 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 360 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 361 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 362 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 363 | * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes) |
| 364 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 365 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 366 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 367 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 368 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 369 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 370 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 371 | * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes) |
| 372 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 373 | * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes) |
| 374 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 375 | * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr |
| 376 | * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes) |
| 377 | * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 378 | * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor |
| 379 | * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension |
| 380 | */ |
| 381 | __kernel void direct_convolution3x3_f32_bifrost( |
| 382 | TENSOR3D_DECLARATION(src), |
| 383 | TENSOR3D_DECLARATION(dst), |
| 384 | TENSOR3D_DECLARATION(weights), |
| 385 | #ifdef HAS_BIAS |
| 386 | VECTOR_DECLARATION(biases), |
| 387 | #endif /* defined(HAS_BIAS) */ |
| 388 | unsigned int weights_stride_w) |
| 389 | { |
| 390 | // Get the kernel index |
| 391 | const int kernel_index = get_global_id(2); |
| 392 | |
| 393 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 394 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 395 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 396 | float4 values0 = 0; |
| 397 | float4 values1 = 0; |
| 398 | float4 values2 = 0; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 399 | |
| 400 | __global uchar *weights_addr = (__global uchar *)(weights_ptr + weights_offset_first_element_in_bytes + kernel_index * weights_stride_w); |
| 401 | __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0); |
| 402 | |
| 403 | // Note: Since each work-item computes 4x3 elements, we need to load 5 rows from the input tensor |
| 404 | |
| 405 | for(ushort d = 0; d < (ushort)WEIGHTS_DEPTH; ++d) |
| 406 | { |
| 407 | // Load the weights |
| 408 | float3 weights_row0 = vload3(0, (__global float *)(weights_addr + 0 * weights_stride_y)); |
| 409 | float3 weights_row1 = vload3(0, (__global float *)(weights_addr + 1 * weights_stride_y)); |
| 410 | float3 weights_row2 = vload3(0, (__global float *)(weights_addr + 2 * weights_stride_y)); |
| 411 | float4 src0; |
| 412 | float2 src1; |
| 413 | |
| 414 | // Load values from row0 of input tensor |
| 415 | src0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); |
| 416 | src1 = vload2(0, (__global float *)(src_addr + 0 * src_stride_y) + 4); |
| 417 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 418 | CONVOLUTION1x3_BIFROST(values0, src0, src1, weights_row0); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 419 | |
| 420 | // Load values from row1 of input tensor |
| 421 | src0 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); |
| 422 | src1 = vload2(0, (__global float *)(src_addr + 1 * src_stride_y) + 4); |
| 423 | |
| 424 | // Accumulate |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 425 | CONVOLUTION1x3_BIFROST(values0, src0, src1, weights_row1); |
| 426 | CONVOLUTION1x3_BIFROST(values1, src0, src1, weights_row0); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 427 | |
| 428 | // Load values from row2 of input tensor |
| 429 | src0 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); |
| 430 | src1 = vload2(0, (__global float *)(src_addr + 2 * src_stride_y) + 4); |
| 431 | |
| 432 | // Accumulate |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 433 | CONVOLUTION1x3_BIFROST(values0, src0, src1, weights_row2); |
| 434 | CONVOLUTION1x3_BIFROST(values1, src0, src1, weights_row1); |
| 435 | CONVOLUTION1x3_BIFROST(values2, src0, src1, weights_row0); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 436 | |
| 437 | // Load values from row3 of input tensor |
| 438 | src0 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); |
| 439 | src1 = vload2(0, (__global float *)(src_addr + 3 * src_stride_y) + 4); |
| 440 | |
| 441 | // Accumulate |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 442 | CONVOLUTION1x3_BIFROST(values1, src0, src1, weights_row2); |
| 443 | CONVOLUTION1x3_BIFROST(values2, src0, src1, weights_row1); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 444 | |
| 445 | // Row4 |
| 446 | src0 = vload4(0, (__global float *)(src_addr + 4 * src_stride_y)); |
| 447 | src1 = vload2(0, (__global float *)(src_addr + 4 * src_stride_y) + 4); |
| 448 | |
| 449 | // Accumulate |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 450 | CONVOLUTION1x3_BIFROST(values2, src0, src1, weights_row2); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 451 | |
| 452 | src_addr += src_stride_z; |
| 453 | weights_addr += weights_stride_z; |
| 454 | } |
| 455 | |
| 456 | #ifdef HAS_BIAS |
| 457 | Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); |
| 458 | |
Gian Marco Iodice | 1c8409d | 2017-09-06 17:24:25 +0100 | [diff] [blame] | 459 | float bias = (float) * ((__global float *)(vector_offset(&biases, kernel_index))); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 460 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 461 | values0 += (float4)bias; |
| 462 | values1 += (float4)bias; |
| 463 | values2 += (float4)bias; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 464 | #endif /* defined(HAS_BIAS) */ |
| 465 | |
Pablo Tello | 3d31946 | 2018-06-21 15:13:17 +0100 | [diff] [blame] | 466 | vstore4(values0, 0, (__global float *)(dst.ptr + 0 * dst_stride_y)); |
| 467 | vstore4(values1, 0, (__global float *)(dst.ptr + 1 * dst_stride_y)); |
| 468 | vstore4(values2, 0, (__global float *)(dst.ptr + 2 * dst_stride_y)); |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 469 | } |
| 470 | #endif // defined(WEIGHTS_DEPTH) |