1/* 2 * Copyright (c) 2018-2021 Arm Limited. 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24#include "helpers.h" 25 26#define VECTOR_N VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) 27#define COND_N SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, VECTOR_SIZE) 28 29#if defined(IM2COL_3X3) || defined(IM2COL_9X9) 30/** Store a 1x9 row or a 3x3 block in a boundary-aware manner to avoid paddings in the channel dimension 31 * @name IM2COL1X9_NHWC_STORE 32 * 33 * @note To use this macro for a 3x3 block, @p ROW has to be 0 34 * 35 * @param[in] VECTOR_SIZE The non-boundary vector width of @p DATA. Supported: 1(scalar), 2, 3, 4, 8, 16 36 * @param[in] BOUNDARY_VECTOR_SIZE The boundary vector width of @p DATA. Supported: 1-16, but has to be <= @p size 37 * @param[in] DATA_TYPE Data type of @p DATA 38 * @param[in] SRC_DEPTH Input channel size / depth 39 * @param[in] DATA Value variable base name 40 * @param[in] ROW The row number to store. Supported: 0-8 41 * @param[in] OUTPUT_PTR Output pointer 42 * @{ 43 */ 44#if defined(VECTOR_SIZE) && defined(BOUNDARY_VECTOR_SIZE) && BOUNDARY_VECTOR_SIZE < VECTOR_SIZE 45#define IM2COL1X9_NHWC_STORE(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) \ 46 const bool at_channel_boundary = get_global_id(0) == 0; \ 47 if(at_channel_boundary) \ 48 { \ 49 IM2COL1X9_NHWC_STORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) \ 50 } \ 51 else \ 52 { \ 53 IM2COL1X9_NHWC_STORE_NONPARTIAL(VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) \ 54 } 55#else // defined(VECTOR_SIZE) && defined(BOUNDARY_VECTOR_SIZE) && BOUNDARY_VECTOR_SIZE < VECTOR_SIZE 56#define IM2COL1X9_NHWC_STORE(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) \ 57 IM2COL1X9_NHWC_STORE_NONPARTIAL(VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) 58#endif // defined(VECTOR_SIZE) && defined(BOUNDARY_VECTOR_SIZE) && BOUNDARY_VECTOR_SIZE < VECTOR_SIZE 59 60#define IM2COL1X9_NHWC_STORE_NONPARTIAL(VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) \ 61 VSTORE(VECTOR_SIZE) \ 62 (DATA##0, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (0 + ROW * 9) * SRC_DEPTH); \ 63 VSTORE(VECTOR_SIZE) \ 64 (DATA##1, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (1 + ROW * 9) * SRC_DEPTH); \ 65 VSTORE(VECTOR_SIZE) \ 66 (DATA##2, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (2 + ROW * 9) * SRC_DEPTH); \ 67 VSTORE(VECTOR_SIZE) \ 68 (DATA##3, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (3 + ROW * 9) * SRC_DEPTH); \ 69 VSTORE(VECTOR_SIZE) \ 70 (DATA##4, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (4 + ROW * 9) * SRC_DEPTH); \ 71 VSTORE(VECTOR_SIZE) \ 72 (DATA##5, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (5 + ROW * 9) * SRC_DEPTH); \ 73 VSTORE(VECTOR_SIZE) \ 74 (DATA##6, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (6 + ROW * 9) * SRC_DEPTH); \ 75 VSTORE(VECTOR_SIZE) \ 76 (DATA##7, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (7 + ROW * 9) * SRC_DEPTH); \ 77 VSTORE(VECTOR_SIZE) \ 78 (DATA##8, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (8 + ROW * 9) * SRC_DEPTH); 79 80#define IM2COL1X9_NHWC_STORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, DATA, ROW, OUTPUT_PTR) \ 81 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 82 (DATA##0, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (0 + ROW * 9) * SRC_DEPTH); \ 83 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 84 (DATA##1, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (1 + ROW * 9) * SRC_DEPTH); \ 85 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 86 (DATA##2, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (2 + ROW * 9) * SRC_DEPTH); \ 87 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 88 (DATA##3, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (3 + ROW * 9) * SRC_DEPTH); \ 89 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 90 (DATA##4, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (4 + ROW * 9) * SRC_DEPTH); \ 91 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 92 (DATA##5, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (5 + ROW * 9) * SRC_DEPTH); \ 93 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 94 (DATA##6, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (6 + ROW * 9) * SRC_DEPTH); \ 95 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 96 (DATA##7, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (7 + ROW * 9) * SRC_DEPTH); \ 97 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) \ 98 (DATA##8, 0, (__global DATA_TYPE *)(OUTPUT_PTR) + (8 + ROW * 9) * SRC_DEPTH); 99/** @}*/ 100#endif // defined(IM2COL_3X3) || defined(IM2COL_9X9) 101 102#if defined(IM2COL_3X3) 103/** This kernel performs im2col when the kernel size is 3x3 and the data layout is NHWC 104 * 105 * @note This kernel computes VECTOR_SIZE elements 106 * @note This kernel stores VECTOR_SIZE or BOUNDARY_VECTOR_SIZE (if at boundary) elements 107 * @note The vector size must be passed at compile time using -DVECTOR_SIZE: e.g. -DVECTOR_SIZE=2 108 * @note The boundary vector size must be passed at compile time using -DBOUNDARY_VECTOR_SIZE: e.g. -DBOUNDARY_VECTOR_SIZE=1 109 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float 110 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34 111 * @note The kernel depth must be passed at compile time using -DSRC_DEPTH: e.g. -DSRC_DEPTH=3 112 * @note The stride along the Y direction must be passed at compile time using -DSTRIDE_Y: e.g. -DSTRIDE_Y=1 113 * @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. 114 * 115 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8_SIGNED/QASYMM8/F16/F32 116 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) 117 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) 118 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) 119 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) 120 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) 121 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) 122 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor 123 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr 124 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) 125 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) 126 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) 127 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) 128 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor 129 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes). 130 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes). 131 */ 132__kernel void im2col3x3_nhwc( 133 TENSOR3D_DECLARATION(src), 134 IMAGE_DECLARATION(dst), 135 uint src_stride_w, 136 uint dst_stride_w) 137{ 138 // input feature map, boundary-corrected (shift all non-boundary vectors by shift_amount) to avoid padding 139 const int shift_amount = (int)VECTOR_SIZE - (int)BOUNDARY_VECTOR_SIZE; 140 const int ch = max((int)(get_global_id(0) * VECTOR_SIZE) - shift_amount, 0); 141 const int yo = get_global_id(1); 142 const int batch = get_global_id(2); // batch size 143 144 // Calculate input indices 145 const int xi = (get_global_id(1) % CONVOLVED_WIDTH) * STRIDE_X; 146 const int yi = (get_global_id(1) / (int)CONVOLVED_WIDTH) * STRIDE_Y; 147 148 // Get input and output address 149 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * sizeof(DATA_TYPE) + batch * (int)src_stride_w; 150 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + ch * sizeof(DATA_TYPE) + yo * (int)dst_stride_y + batch * (int)dst_stride_w; 151 152 int yi_coord = 0; 153 int3 offset = 0; 154 155 // Clamp xi 156 int3 xi_offset = ((int3)xi + (int3)(0, 1, 2) * DILATION_X - (int3)PAD_LEFT); 157#if PAD_LEFT != 0 || PAD_RIGHT != 0 158#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val) 159 xi_offset = CLAMP(xi_offset, (int3)0, (int3)(SRC_WIDTH - 1)); 160#endif // PAD_LEFT != 0 || PAD_RIGHT != 0 161 // Multiply by src_stride_y as the width (X) dimension here is the second (y) dimension in src NHWC tensor 162 xi_offset *= (int3)src_stride_y; 163 164 // Out-of-bound condition for X 165 int3 x_cond = (((int3)xi + (int3)(0, 1, 2) * DILATION_X - (int3)PAD_LEFT) < (int3)0) || (((int3)xi + (int3)(0, 1, 2) * DILATION_X - (int3)PAD_LEFT) >= (int3)SRC_WIDTH); 166 167 // yi == 0 168 // Clamp yi 169 // yi_coord is casted to unsigned int in order to use just a min() operation 170 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295 171 // This is a trick so that the values loaded in the padding areas are always from the last row (SRC_HEIGHT - 1), 172 // because of the negative yi_coord wrap-around, but it gets overwritten by PAD_VALUE immediately as the wrap-around 173 // also causes y_cond (y padding condition) to be satisfied 174 yi_coord = yi - (int)PAD_TOP; 175 176 // Clamp only if PAD_TOP or PAD_BOTTOM is not equal to 0 177#if PAD_TOP != 0 || PAD_BOTTOM != 0 178 yi_coord = min((uint)yi_coord, (uint)(SRC_HEIGHT - 1)); 179#endif // PAD_TOP != 0 || PAD_BOTTOM != 0 180 181 // Compute offset 182 offset = xi_offset + (yi_coord * (int)src_stride_z); 183 184 // Load input values 185 VECTOR_N values0 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s0)); 186 VECTOR_N values1 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s1)); 187 VECTOR_N values2 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s2)); 188 189#if PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 190 // Replace invalid values with PAD_VALUE 191 int y_cond = (int)((uint)(yi - (int)PAD_TOP) >= (uint)(SRC_HEIGHT)); 192 values0 = select(values0, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s0))); 193 values1 = select(values1, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s1))); 194 values2 = select(values2, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s2))); 195#endif // PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 196 197 // yi == 1 198 // Clamp yi_coord (it can be negative if PAD_TOP > 1) 199 yi_coord = yi - (int)PAD_TOP + 1 * DILATION_Y; 200 201 // Clamp only if PAD_TOP or PAD_BOTTOM is not equal to 0 202#if PAD_TOP != 0 || PAD_BOTTOM != 0 203 yi_coord = min((uint)yi_coord, (uint)(SRC_HEIGHT - 1)); 204#endif // PAD_TOP != 0 || PAD_BOTTOM != 0 205 206 // Compute offset 207 offset = xi_offset + (yi_coord * (int)src_stride_z); 208 209 // Load input values 210 VECTOR_N values3 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s0)); 211 VECTOR_N values4 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s1)); 212 VECTOR_N values5 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s2)); 213 214#if PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 215 // Replace invalid values with zeros 216 y_cond = (int)((uint)(yi - (int)PAD_TOP + 1 * DILATION_Y) >= (uint)(SRC_HEIGHT)); 217 values3 = select(values3, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s0))); 218 values4 = select(values4, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s1))); 219 values5 = select(values5, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s2))); 220#endif // PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 221 222 // yi == 2 223 // Clamp yi_coord 224 yi_coord = yi - (int)PAD_TOP + 2 * DILATION_Y; 225 226 // Clamp only if PAD_TOP or PAD_BOTTOM is not equal to 0 227#if PAD_TOP != 0 || PAD_BOTTOM != 0 228 yi_coord = min((uint)yi_coord, (uint)(SRC_HEIGHT - 1)); 229#endif // PAD_TOP != 0 || PAD_BOTTOM != 0 230 231 // Compute offset 232 offset = xi_offset + (yi_coord * (int)src_stride_z); 233 234 // Load input values 235 VECTOR_N values6 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s0)); 236 VECTOR_N values7 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s1)); 237 VECTOR_N values8 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset.s2)); 238 239#if PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 240 // Replace invalid values with PAD_VALUE 241 y_cond = (int)((uint)(yi - (int)PAD_TOP + 2 * DILATION_Y) >= (uint)(SRC_HEIGHT)); 242 values6 = select(values6, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s0))); 243 values7 = select(values7, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s1))); 244 values8 = select(values8, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond.s2))); 245#endif // PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 246 247 // Store in a boundary-aware way to avoid padding 248 IM2COL1X9_NHWC_STORE(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, values, 0, output_ptr) 249 250#ifdef HAS_BIAS 251 // We can use VECTOR_SIZE instead of BOUNDARY_VECTOR_SIZE even if it's at the boundary. This is because the bias is 252 // added at the end of the channel, while the boundary vec is at the beginning of the channel. 253 // The only case where the boundary vec is at the end of the channel is when there's only a single boundary vec in 254 // the whole channel dimension, but in that case VECTOR_SIZE is also equal to BOUNDARY_VECTOR_SIZE 255 // See the value of num_elems_processed_per_iteration in configure_opencl_kernel method in CLIm2ColKernel.cpp 256 if((ch + VECTOR_SIZE) >= SRC_DEPTH) 257 { 258 *((__global DATA_TYPE *)(output_ptr) - ch + SRC_DEPTH * 9) = 1.0f; 259 } 260#endif // HAS_BIAS 261} 262#endif // defined(IM2COL_3X3) 263 264#if defined(IM2COL_9X9) 265#if PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 266#define IM2COL1x9(i) \ 267 ({ \ 268 yi_coord = yi - (int)PAD_TOP + i * DILATION_Y; \ 269 yi_coord = min((uint)yi_coord, (uint)(SRC_HEIGHT - 1)); \ 270 \ 271 offset0 = xi_offset0 + (yi_coord * (int)src_stride_z); \ 272 offset1 = xi_offset1 + (yi_coord * (int)src_stride_z); \ 273 \ 274 VECTOR_N values0 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s0)); \ 275 VECTOR_N values1 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s1)); \ 276 VECTOR_N values2 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s2)); \ 277 VECTOR_N values3 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s3)); \ 278 VECTOR_N values4 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s4)); \ 279 VECTOR_N values5 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s5)); \ 280 VECTOR_N values6 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s6)); \ 281 VECTOR_N values7 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s7)); \ 282 VECTOR_N values8 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset1)); \ 283 \ 284 int y_cond = (int)((uint)(yi - (int)PAD_TOP + i * DILATION_Y) >= (uint)(SRC_HEIGHT)); \ 285 values0 = select(values0, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s0))); \ 286 values1 = select(values1, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s1))); \ 287 values2 = select(values2, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s2))); \ 288 values3 = select(values3, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s3))); \ 289 values4 = select(values4, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s4))); \ 290 values5 = select(values5, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s5))); \ 291 values6 = select(values6, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s6))); \ 292 values7 = select(values7, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond0.s7))); \ 293 values8 = select(values8, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)y_cond || (COND_N)(x_cond1))); \ 294 \ 295 IM2COL1X9_NHWC_STORE(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, values, i, output_ptr) \ 296 }) 297#else // PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 298#define IM2COL1x9(i) \ 299 ({ \ 300 yi_coord = yi - (int)PAD_TOP + i * DILATION_Y; \ 301 yi_coord = min((uint)yi_coord, (uint)(SRC_HEIGHT - 1)); \ 302 \ 303 offset0 = xi_offset0 + (yi_coord * (int)src_stride_z); \ 304 offset1 = xi_offset1 + (yi_coord * (int)src_stride_z); \ 305 \ 306 VECTOR_N values0 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s0)); \ 307 VECTOR_N values1 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s1)); \ 308 VECTOR_N values2 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s2)); \ 309 VECTOR_N values3 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s3)); \ 310 VECTOR_N values4 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s4)); \ 311 VECTOR_N values5 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s5)); \ 312 VECTOR_N values6 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s6)); \ 313 VECTOR_N values7 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset0.s7)); \ 314 VECTOR_N values8 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset1)); \ 315 \ 316 IM2COL1X9_NHWC_STORE(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE, DATA_TYPE, SRC_DEPTH, values, i, output_ptr) \ 317 }) 318#endif // PAD_TOP != 0 || PAD_LEFT != 0 || PAD_BOTTOM != 0 || PAD_RIGHT != 0 319 320/** This kernel performs im2col when the kernel size is 9x9 and the data layout is NHWC 321 * 322 * @note This kernel computes VECTOR_SIZE elements 323 * @note This kernel stores VECTOR_SIZE or BOUNDARY_VECTOR_SIZE (if at boundary) elements 324 * @note The vector size must be passed at compile time using -DVECTOR_SIZE: e.g. -DVECTOR_SIZE=2 325 * @note The boundary vector size must be passed at compile time using -DBOUNDARY_VECTOR_SIZE: e.g. -DBOUNDARY_VECTOR_SIZE=1 326 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float 327 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34 328 * @note The kernel depth must be passed at compile time using -DSRC_DEPTH: e.g. -DSRC_DEPTH=3 329 * @note The stride along the Y direction must be passed at compile time using -DSTRIDE_Y: e.g. -DSTRIDE_Y=1 330 * @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. 331 * 332 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8_SIGNED/QASYMM8/F16/F32 333 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) 334 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) 335 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) 336 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) 337 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) 338 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) 339 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor 340 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr 341 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) 342 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) 343 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) 344 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) 345 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor 346 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes). 347 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes). 348 */ 349__kernel void im2col9x9_nhwc( 350 TENSOR3D_DECLARATION(src), 351 IMAGE_DECLARATION(dst), 352 uint src_stride_w, 353 uint dst_stride_w) 354{ 355 // input feature map, boundary-corrected (shift all non-boundary vectors by shift_amount) to avoid padding 356 const int shift_amount = (int)VECTOR_SIZE - (int)BOUNDARY_VECTOR_SIZE; 357 const int ch = max((int)(get_global_id(0) * VECTOR_SIZE) - shift_amount, 0); 358 const int yo = get_global_id(1); 359 const int batch = get_global_id(2); // batch size 360 361 // Calculate input indices 362 const int xi = (get_global_id(1) % CONVOLVED_WIDTH) * STRIDE_X; 363 const int yi = (get_global_id(1) / (int)CONVOLVED_WIDTH) * STRIDE_Y; 364 365 // Get input and output address 366 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + ch * sizeof(DATA_TYPE) + batch * (int)src_stride_w; 367 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + ch * sizeof(DATA_TYPE) + yo * (int)dst_stride_y + batch * (int)dst_stride_w; 368 369 int yi_coord = 0; 370 int8 offset0 = 0; 371 int offset1 = 0; 372 373 // Clamp xi 374 int8 xi_offset0 = ((int8)xi + (int8)(0, 1, 2, 3, 4, 5, 6, 7) * DILATION_X - (int8)PAD_LEFT); 375 int xi_offset1 = ((int)xi + (int)(8) * DILATION_X - (int)PAD_LEFT); 376 377#if PAD_LEFT != 0 || PAD_RIGHT != 0 378#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val) 379 xi_offset0 = CLAMP(xi_offset0, (int8)0, (int8)(SRC_WIDTH - 1)); 380 xi_offset1 = CLAMP(xi_offset1, (int)0, (int)(SRC_WIDTH - 1)); 381#endif // PAD_LEFT != 0 || PAD_RIGHT != 0 382 xi_offset0 *= (int8)src_stride_y; 383 xi_offset1 *= (int)src_stride_y; 384 385 // Out-of-bound condition for X 386 int8 x_cond0 = (((int8)xi + (int8)(0, 1, 2, 3, 4, 5, 6, 7) * DILATION_X - (int8)PAD_LEFT) < (int8)0) || (((int8)xi + (int8)(0, 1, 2, 3, 4, 5, 6, 7) * DILATION_X - (int8)PAD_LEFT) >= (int8)SRC_WIDTH); 387 int x_cond1 = (((int)xi + (int)(8) * DILATION_X - (int)PAD_LEFT) < (int)0) || (((int)xi + (int)(8) * DILATION_X - (int)PAD_LEFT) >= (int)SRC_WIDTH); 388 389 IM2COL1x9(0); 390 IM2COL1x9(1); 391 IM2COL1x9(2); 392 IM2COL1x9(3); 393 IM2COL1x9(4); 394 IM2COL1x9(5); 395 IM2COL1x9(6); 396 IM2COL1x9(7); 397 IM2COL1x9(8); 398 399#ifdef HAS_BIAS 400 // We can use VECTOR_SIZE instead of BOUNDARY_VECTOR_SIZE even if it's at the boundary. This is because the bias is 401 // added at the end of the channel, while the boundary vec is at the beginning of the channel. 402 // The only case where the boundary vec is at the end of the channel is when there's only a single boundary vec in 403 // the whole channel dimension, but in that case VECTOR_SIZE is also equal to BOUNDARY_VECTOR_SIZE 404 // See the value of num_elems_processed_per_iteration in configure_opencl_kernel method in CLIm2ColKernel.cpp 405 if((ch + VECTOR_SIZE) >= SRC_DEPTH) 406 { 407 *((__global DATA_TYPE *)(output_ptr) - ch + SRC_DEPTH * 81) = 1.0f; 408 } 409#endif // HAS_BIAS 410} 411#endif // defined(IM2COL_9X9) 412 413#if defined(IM2COL_GENERIC) 414/** This opencl kernel performs a generic im2col implementation when the data layout is NHWC 415 * 416 * @note This kernel computes VECTOR_SIZE elements 417 * @note This kernel stores VECTOR_SIZE or BOUNDARY_VECTOR_SIZE (if at boundary) elements 418 * @note The vector size must be passed at compile time using -DVECTOR_SIZE: e.g. -DVECTOR_SIZE=2 419 * @note The boundary vector size must be passed at compile time using -DBOUNDARY_VECTOR_SIZE: e.g. -DBOUNDARY_VECTOR_SIZE=1 420 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float 421 * @note The width and height of the input tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT: e.g. -DSRC_WIDTH=128 and -DSRC_HEIGHT=128 422 * @note The width of output tensor after matrix multiplication must be passed at compile time using -DCONVOLVED_WIDTH: e.g. -DCONVOLVED_WIDTH=34 423 * @note The kernel width, height and depth must be passed at compile time using -DKERNEL_WIDTH, -DKERNEL_HEIGHT and -DSRC_DEPTH: e.g. -DKERNEL_WIDTH=3, -DKERNEL_HEIGHT=3 and -DSRC_DEPTH=64 424 * @note The pad_left, pad_right, pad_top and pad_bottom must be passed at compile time using -DPAD_LEFT, -DPAD_RIGHT, -DPAD_TOP and -DPAD_BOTTOM: e.g. -DPAD_LEFT=1, -DPAD_RIGHT=2, -DPAD_TOP=3 and -DPAD_BOTTOM=2 425 * @note The zero value to store in case we load values out-of-bounds must be passed at compile time using -DPAD_VALUE: e.g. -DPAD_VALUE=0.0 426 * @note The stride along the X and Y directions must be passed at compile time using -DSTRIDE_X and -DSTRIDE_Y: e.g. -DSTRIDE_X=1 and -DSTRIDE_Y=1 427 * @note The dilation_x and dilation_y must be passed at compile time using -DDILATION_X and -DDILATION_Y: e.g. -DDILATION_X=1, -DDILATION_Y=1 428 * @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. 429 * 430 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8_SIGNED/QASYMM8/F16/F32 431 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) 432 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) 433 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) 434 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) 435 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) 436 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) 437 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor 438 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr 439 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) 440 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) 441 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) 442 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) 443 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor 444 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes). 445 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes). 446 */ 447__kernel void im2col_generic_nhwc( 448 TENSOR3D_DECLARATION(src), 449 IMAGE_DECLARATION(dst), 450 uint src_stride_w, 451 uint dst_stride_w) 452{ 453 // input feature map, boundary-corrected (shift all non-boundary vectors by shift_amount) to avoid padding 454 const int shift_amount = (int)VECTOR_SIZE - (int)BOUNDARY_VECTOR_SIZE; 455 const int ch = max((int)(get_global_id(0) * VECTOR_SIZE) - shift_amount, 0); 456 const int yo = get_global_id(1); 457 const int batch = get_global_id(2); // batch size 458 459 // Calculate input indices 460 const int xi = (yo % CONVOLVED_WIDTH) * STRIDE_X; 461 const int yi = (yo / (int)CONVOLVED_WIDTH) * STRIDE_Y; 462 463 // Get input and output address 464 const int stride_x = ch * sizeof(DATA_TYPE); 465 __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + stride_x + batch * (int)src_stride_w; 466 __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + stride_x + yo * (int)dst_stride_y + batch * (int)dst_stride_w; 467 468 int i = 0; 469 for(int yk = 0; yk < KERNEL_HEIGHT; ++yk) 470 { 471 // Clamp yi_coord 472 int yi_coord = yi + yk * DILATION_Y - (int)PAD_TOP; 473 yi_coord = clamp(yi_coord, (int)0, (int)(SRC_HEIGHT - 1)); 474 475 // Out-of-bound condition for Y 476 int y_border_condition = ((yi + yk * DILATION_Y - (int)PAD_TOP) < (int)0) || ((yi + yk * DILATION_Y - (int)PAD_TOP) >= (int)SRC_HEIGHT); 477 478 for(int xk = 0; xk < KERNEL_WIDTH; ++xk) 479 { 480 // Clamp xi_coord 481 int xi_coord = (xi + xk * DILATION_X - (int)PAD_LEFT); 482 xi_coord = clamp(xi_coord, (int)0, (int)(SRC_WIDTH - 1)); 483 484 // Out-of-bound condition for X 485 int x_border_condition = ((xi + xk * DILATION_X - (int)PAD_LEFT) < (int)0) || ((xi + xk * DILATION_X - (int)PAD_LEFT) >= (int)SRC_WIDTH); 486 487 int offset = xi_coord * (int)src_stride_y + (yi_coord * (int)src_stride_z); 488 489 VECTOR_N values0 = VLOAD(VECTOR_SIZE)(0, (__global DATA_TYPE *)(input_ptr + offset)); 490 491#if PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0 492 // Replace with PAD_VALUE if the value is out-of-bound 493 values0 = select(values0, (VECTOR_N)PAD_VALUE, (COND_N)((COND_N)x_border_condition || (COND_N)(y_border_condition))); 494#endif // PAD_LEFT != 0 || PAD_TOP != 0 || PAD_RIGHT != 0 || PAD_BOTTOM != 0 495 496 // Store in a boundary-aware way to avoid padding 497#if BOUNDARY_VECTOR_SIZE != VECTOR_SIZE 498 const bool at_channel_boundary = get_global_id(0) == 0; 499 if(at_channel_boundary) 500 { 501 VSTORE_PARTIAL(VECTOR_SIZE, BOUNDARY_VECTOR_SIZE) 502 (values0, 0, (__global DATA_TYPE *)(output_ptr) + i * (int)SRC_DEPTH); 503 } 504 else // at_channel_boundary 505#endif // BOUNDARY_VECTOR_SIZE != VECTOR_SIZE 506 { 507 VSTORE(VECTOR_SIZE) 508 (values0, 0, (__global DATA_TYPE *)(output_ptr) + i * (int)SRC_DEPTH); 509 } 510 i++; 511 } 512 } 513 514#ifdef HAS_BIAS 515 // We can use VECTOR_SIZE instead of BOUNDARY_VECTOR_SIZE even if it's at the boundary. This is because the bias is 516 // added at the end of the channel, while the boundary vec is at the beginning of the channel. 517 // The only case where the boundary vec is at the end of the channel is when there's only a single boundary vec in 518 // the whole channel dimension, but in that case VECTOR_SIZE is also equal to BOUNDARY_VECTOR_SIZE 519 // See the value of num_elems_processed_per_iteration in configure_opencl_kernel method in CLIm2ColKernel.cpp 520 if((ch + VECTOR_SIZE) >= SRC_DEPTH) 521 { 522 *((__global DATA_TYPE *)(output_ptr) - ch + SRC_DEPTH * KERNEL_WIDTH * KERNEL_HEIGHT) = 1.0f; 523 } 524#endif // HAS_BIAS 525} 526#endif // defined(IM2COL_GENERIC)