Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #if defined(NUM_TILES_X) |
| 27 | |
| 28 | /** This OpenCL kernel computes the input transform when the kernel size is 3x3 and the output tile is 2x2 |
| 29 | * |
| 30 | * @note The number of tiles in the x axis must be passed at compile time using -DNUM_TILES_X (i.e.-DNUM_TILES_X=5). |
| 31 | * @note The pad left and pad top must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (i.e.-DPAD_LEFT=1 and -DPAD_TOP=0). |
| 32 | * |
| 33 | * @param[in] src_ptr Pointer to the source image. Supported data types: F32 |
| 34 | * @param[in] src_stride_x Stride of the source image in X dimension (in bytes) |
| 35 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 36 | * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes) |
| 37 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 38 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image |
| 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] dst_ptr Pointer to the destination tensor. Supported data types: as @p src_ptr |
| 42 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 43 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 44 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 45 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 46 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 47 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 48 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 49 | */ |
| 50 | __kernel void winograd_input_transform_2x2_3x3_stepz1_nchw( |
| 51 | TENSOR3D_DECLARATION(src), |
| 52 | TENSOR3D_DECLARATION(dst)) |
| 53 | { |
| 54 | int x = get_global_id(0); |
| 55 | int y = get_global_id(1); |
| 56 | int z = get_global_id(2); |
| 57 | |
| 58 | // Compute input address |
| 59 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * 2 * src_stride_x + y * 2 * src_stride_y + z * src_stride_z; |
| 60 | |
| 61 | src_addr = src_addr - ((int)PAD_LEFT * src_stride_x) - ((int)PAD_TOP * src_stride_y); |
| 62 | |
| 63 | float4 in_row0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); |
| 64 | float4 in_row1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); |
| 65 | float4 in_row2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); |
| 66 | float4 in_row3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); |
| 67 | |
| 68 | float4 tmp0 = in_row0 - in_row2; |
| 69 | float4 tmp1 = in_row1 + in_row2; |
| 70 | float4 tmp2 = in_row2 - in_row1; |
| 71 | float4 tmp3 = in_row1 - in_row3; |
| 72 | |
| 73 | float out00 = tmp0.s0 - tmp0.s2; |
| 74 | float out01 = tmp0.s1 + tmp0.s2; |
| 75 | float out02 = tmp0.s2 - tmp0.s1; |
| 76 | float out03 = tmp0.s1 - tmp0.s3; |
| 77 | |
| 78 | float out10 = tmp1.s0 - tmp1.s2; |
| 79 | float out11 = tmp1.s1 + tmp1.s2; |
| 80 | float out12 = tmp1.s2 - tmp1.s1; |
| 81 | float out13 = tmp1.s1 - tmp1.s3; |
| 82 | |
| 83 | float out20 = tmp2.s0 - tmp2.s2; |
| 84 | float out21 = tmp2.s1 + tmp2.s2; |
| 85 | float out22 = tmp2.s2 - tmp2.s1; |
| 86 | float out23 = tmp2.s1 - tmp2.s3; |
| 87 | |
| 88 | float out30 = tmp3.s0 - tmp3.s2; |
| 89 | float out31 = tmp3.s1 + tmp3.s2; |
| 90 | float out32 = tmp3.s2 - tmp3.s1; |
| 91 | float out33 = tmp3.s1 - tmp3.s3; |
| 92 | |
| 93 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + z * dst_stride_x + (x + y * (int)NUM_TILES_X) * dst_stride_y; |
| 94 | |
| 95 | *((__global float *)(dst_addr + 0 * dst_stride_z)) = out00; |
| 96 | *((__global float *)(dst_addr + 1 * dst_stride_z)) = out01; |
| 97 | *((__global float *)(dst_addr + 2 * dst_stride_z)) = out02; |
| 98 | *((__global float *)(dst_addr + 3 * dst_stride_z)) = out03; |
| 99 | *((__global float *)(dst_addr + 4 * dst_stride_z)) = out10; |
| 100 | *((__global float *)(dst_addr + 5 * dst_stride_z)) = out11; |
| 101 | *((__global float *)(dst_addr + 6 * dst_stride_z)) = out12; |
| 102 | *((__global float *)(dst_addr + 7 * dst_stride_z)) = out13; |
| 103 | *((__global float *)(dst_addr + 8 * dst_stride_z)) = out20; |
| 104 | *((__global float *)(dst_addr + 9 * dst_stride_z)) = out21; |
| 105 | *((__global float *)(dst_addr + 10 * dst_stride_z)) = out22; |
| 106 | *((__global float *)(dst_addr + 11 * dst_stride_z)) = out23; |
| 107 | *((__global float *)(dst_addr + 12 * dst_stride_z)) = out30; |
| 108 | *((__global float *)(dst_addr + 13 * dst_stride_z)) = out31; |
| 109 | *((__global float *)(dst_addr + 14 * dst_stride_z)) = out32; |
| 110 | *((__global float *)(dst_addr + 15 * dst_stride_z)) = out33; |
| 111 | } |
| 112 | |
| 113 | /** This OpenCL kernel computes the input transform when the kernel size is 3x3, the output tile is 2x2 and the number of channels is multiple of 2 |
| 114 | * |
| 115 | * @note The number of tiles in the x axis must be passed at compile time using -DNUM_TILES_X (i.e.-DNUM_TILES_X=5). |
| 116 | * @note The pad left and pad top must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (i.e.-DPAD_LEFT=1 and -DPAD_TOP=0). |
| 117 | * |
| 118 | * @param[in] src_ptr Pointer to the source image. Supported data types: F32 |
| 119 | * @param[in] src_stride_x Stride of the source image in X dimension (in bytes) |
| 120 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 121 | * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes) |
| 122 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 123 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image |
| 124 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 125 | * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes) |
| 126 | * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: as @p src_ptr |
| 127 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 128 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 129 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 130 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 131 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 132 | * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes) |
| 133 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 134 | */ |
| 135 | __kernel void winograd_input_transform_2x2_3x3_stepz2_nchw( |
| 136 | TENSOR3D_DECLARATION(src), |
| 137 | TENSOR3D_DECLARATION(dst)) |
| 138 | { |
| 139 | int x = get_global_id(0); |
| 140 | int y = get_global_id(1); |
| 141 | int z = get_global_id(2) * 2; |
| 142 | |
| 143 | // Compute input address |
| 144 | __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * 2 * src_stride_x + y * 2 * src_stride_y + z * src_stride_z; |
| 145 | |
| 146 | src_addr = src_addr - ((int)PAD_LEFT * src_stride_x) - ((int)PAD_TOP * src_stride_y); |
| 147 | |
| 148 | float4 in_row0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); |
| 149 | float4 in_row1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); |
| 150 | float4 in_row2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); |
| 151 | float4 in_row3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); |
| 152 | |
| 153 | src_addr += src_stride_z; |
| 154 | float4 in_row4 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y)); |
| 155 | float4 in_row5 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y)); |
| 156 | float4 in_row6 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y)); |
| 157 | float4 in_row7 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y)); |
| 158 | |
| 159 | float4 tmp0 = in_row0 - in_row2; |
| 160 | float4 tmp1 = in_row1 + in_row2; |
| 161 | float4 tmp2 = in_row2 - in_row1; |
| 162 | float4 tmp3 = in_row1 - in_row3; |
| 163 | |
| 164 | float4 tmp4 = in_row4 - in_row6; |
| 165 | float4 tmp5 = in_row5 + in_row6; |
| 166 | float4 tmp6 = in_row6 - in_row5; |
| 167 | float4 tmp7 = in_row5 - in_row7; |
| 168 | |
| 169 | float2 out00 = (float2)(tmp0.s0 - tmp0.s2, tmp4.s0 - tmp4.s2); |
| 170 | float2 out01 = (float2)(tmp0.s1 + tmp0.s2, tmp4.s1 + tmp4.s2); |
| 171 | float2 out02 = (float2)(tmp0.s2 - tmp0.s1, tmp4.s2 - tmp4.s1); |
| 172 | float2 out03 = (float2)(tmp0.s1 - tmp0.s3, tmp4.s1 - tmp4.s3); |
| 173 | |
| 174 | float2 out10 = (float2)(tmp1.s0 - tmp1.s2, tmp5.s0 - tmp5.s2); |
| 175 | float2 out11 = (float2)(tmp1.s1 + tmp1.s2, tmp5.s1 + tmp5.s2); |
| 176 | float2 out12 = (float2)(tmp1.s2 - tmp1.s1, tmp5.s2 - tmp5.s1); |
| 177 | float2 out13 = (float2)(tmp1.s1 - tmp1.s3, tmp5.s1 - tmp5.s3); |
| 178 | |
| 179 | float2 out20 = (float2)(tmp2.s0 - tmp2.s2, tmp6.s0 - tmp6.s2); |
| 180 | float2 out21 = (float2)(tmp2.s1 + tmp2.s2, tmp6.s1 + tmp6.s2); |
| 181 | float2 out22 = (float2)(tmp2.s2 - tmp2.s1, tmp6.s2 - tmp6.s1); |
| 182 | float2 out23 = (float2)(tmp2.s1 - tmp2.s3, tmp6.s1 - tmp6.s3); |
| 183 | |
| 184 | float2 out30 = (float2)(tmp3.s0 - tmp3.s2, tmp7.s0 - tmp7.s2); |
| 185 | float2 out31 = (float2)(tmp3.s1 + tmp3.s2, tmp7.s1 + tmp7.s2); |
| 186 | float2 out32 = (float2)(tmp3.s2 - tmp3.s1, tmp7.s2 - tmp7.s1); |
| 187 | float2 out33 = (float2)(tmp3.s1 - tmp3.s3, tmp7.s1 - tmp7.s3); |
| 188 | |
| 189 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + z * dst_stride_x + (x + y * (int)NUM_TILES_X) * dst_stride_y; |
| 190 | |
| 191 | vstore2(out00, 0, (__global float *)(dst_addr + 0 * dst_stride_z)); |
| 192 | vstore2(out01, 0, (__global float *)(dst_addr + 1 * dst_stride_z)); |
| 193 | vstore2(out02, 0, (__global float *)(dst_addr + 2 * dst_stride_z)); |
| 194 | vstore2(out03, 0, (__global float *)(dst_addr + 3 * dst_stride_z)); |
| 195 | vstore2(out10, 0, (__global float *)(dst_addr + 4 * dst_stride_z)); |
| 196 | vstore2(out11, 0, (__global float *)(dst_addr + 5 * dst_stride_z)); |
| 197 | vstore2(out12, 0, (__global float *)(dst_addr + 6 * dst_stride_z)); |
| 198 | vstore2(out13, 0, (__global float *)(dst_addr + 7 * dst_stride_z)); |
| 199 | vstore2(out20, 0, (__global float *)(dst_addr + 8 * dst_stride_z)); |
| 200 | vstore2(out21, 0, (__global float *)(dst_addr + 9 * dst_stride_z)); |
| 201 | vstore2(out22, 0, (__global float *)(dst_addr + 10 * dst_stride_z)); |
| 202 | vstore2(out23, 0, (__global float *)(dst_addr + 11 * dst_stride_z)); |
| 203 | vstore2(out30, 0, (__global float *)(dst_addr + 12 * dst_stride_z)); |
| 204 | vstore2(out31, 0, (__global float *)(dst_addr + 13 * dst_stride_z)); |
| 205 | vstore2(out32, 0, (__global float *)(dst_addr + 14 * dst_stride_z)); |
| 206 | vstore2(out33, 0, (__global float *)(dst_addr + 15 * dst_stride_z)); |
| 207 | } |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame^] | 208 | #endif //defined(NUM_TILES_X) |
| 209 | |
| 210 | #if defined(NUM_CHANNELS) |
| 211 | |
| 212 | /** This OpenCL kernel performs Winograd filter transform 3x3 when the data format is NCHW and the output tile is 2x2 |
| 213 | * |
| 214 | * @note The number of channels must be passed at compile time using -DNUM_CHANNELS: e.g. -DNUM_CHANNELS=64 |
| 215 | * |
| 216 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32 |
| 217 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 218 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 219 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 220 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 221 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 222 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 223 | * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes) |
| 224 | * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes) |
| 225 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 226 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 227 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 228 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 229 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 230 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 231 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 232 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 233 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 234 | */ |
| 235 | __kernel void winograd_filter_transform_2x2_3x3_nchw( |
| 236 | TENSOR4D_DECLARATION(src), |
| 237 | TENSOR3D_DECLARATION(dst)) |
| 238 | { |
| 239 | Tensor4D src = CONVERT_TO_TENSOR4D_STRUCT(src, NUM_CHANNELS); |
| 240 | |
| 241 | const __global uchar *src_addr = tensor4D_offset(&src, 0, 0, 0, 0); |
| 242 | |
| 243 | // Load the values from the input tensor |
| 244 | float3 w0 = vload3(0, (__global float *)(src_addr + 0 * src_stride_y)); |
| 245 | float3 w1 = vload3(0, (__global float *)(src_addr + 1 * src_stride_y)); |
| 246 | float3 w2 = vload3(0, (__global float *)(src_addr + 2 * src_stride_y)); |
| 247 | |
| 248 | // Transform the 3x3 tile in a 4x4 tile |
| 249 | float4 out0 = 0.0f; |
| 250 | float4 out1 = 0.0f; |
| 251 | float4 out2 = 0.0f; |
| 252 | float4 out3 = 0.0f; |
| 253 | |
| 254 | // Row 0 |
| 255 | out0.s0 = (w0.s0); |
| 256 | out0.s1 = (w0.s0 + w0.s1 + w0.s2) * 0.5f; |
| 257 | out0.s2 = (w0.s0 + w0.s2 - w0.s1) * 0.5f; |
| 258 | out0.s3 = (w0.s2); |
| 259 | |
| 260 | // Row 1 |
| 261 | out1.s0 = (w0.s0 + w1.s0 + w2.s0) * 0.5f; |
| 262 | out1.s1 = (w0.s0 + w1.s0 + w2.s0 + w0.s1 + w1.s1 + w2.s1 + w0.s2 + w1.s2 + w2.s2) * 0.25f; |
| 263 | out1.s2 = (w0.s0 + w1.s0 + w2.s0 + w0.s2 + w1.s2 + w2.s2 - w0.s1 - w1.s1 - w2.s1) * 0.25f; |
| 264 | out1.s3 = (w0.s2 + w1.s2 + w2.s2) * 0.5f; |
| 265 | |
| 266 | // Row 2 |
| 267 | out2.s0 = (w0.s0 + w2.s0 - w1.s0) * 0.5f; |
| 268 | out2.s1 = (w0.s0 + w2.s0 + w0.s1 + w2.s1 + w0.s2 + w2.s2 - w1.s0 - w1.s1 - w1.s2) * 0.25f; |
| 269 | out2.s2 = (w0.s0 + w2.s0 + w1.s1 + w0.s2 + w2.s2 - w1.s0 - w0.s1 - w2.s1 - w1.s2) * 0.25f; |
| 270 | out2.s3 = (w0.s2 + w2.s2 - w1.s2) * 0.5f; |
| 271 | |
| 272 | // Row 3 |
| 273 | out3.s0 = (w2.s0); |
| 274 | out3.s1 = (w2.s0 + w2.s1 + w2.s2) * 0.5f; |
| 275 | out3.s2 = (w2.s0 + w2.s2 - w2.s1) * 0.5f; |
| 276 | out3.s3 = (w2.s2); |
| 277 | |
| 278 | int z = get_global_id(2); |
| 279 | int x0 = z / NUM_CHANNELS; // idx filter |
| 280 | int y0 = z % NUM_CHANNELS; // idx channel |
| 281 | |
| 282 | // Get output address |
| 283 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x0 * dst_stride_x + y0 * dst_stride_y; |
| 284 | |
| 285 | // Store the 16 values across the 16 channels |
| 286 | *(__global float *)(dst_addr + 0 * dst_stride_z) = out0.s0; |
| 287 | *(__global float *)(dst_addr + 1 * dst_stride_z) = out0.s1; |
| 288 | *(__global float *)(dst_addr + 2 * dst_stride_z) = out0.s2; |
| 289 | *(__global float *)(dst_addr + 3 * dst_stride_z) = out0.s3; |
| 290 | *(__global float *)(dst_addr + 4 * dst_stride_z) = out1.s0; |
| 291 | *(__global float *)(dst_addr + 5 * dst_stride_z) = out1.s1; |
| 292 | *(__global float *)(dst_addr + 6 * dst_stride_z) = out1.s2; |
| 293 | *(__global float *)(dst_addr + 7 * dst_stride_z) = out1.s3; |
| 294 | *(__global float *)(dst_addr + 8 * dst_stride_z) = out2.s0; |
| 295 | *(__global float *)(dst_addr + 9 * dst_stride_z) = out2.s1; |
| 296 | *(__global float *)(dst_addr + 10 * dst_stride_z) = out2.s2; |
| 297 | *(__global float *)(dst_addr + 11 * dst_stride_z) = out2.s3; |
| 298 | *(__global float *)(dst_addr + 12 * dst_stride_z) = out3.s0; |
| 299 | *(__global float *)(dst_addr + 13 * dst_stride_z) = out3.s1; |
| 300 | *(__global float *)(dst_addr + 14 * dst_stride_z) = out3.s2; |
| 301 | *(__global float *)(dst_addr + 15 * dst_stride_z) = out3.s3; |
| 302 | } |
| 303 | #endif // defined(NUM_CHANNELS) |