Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [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 | layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in; |
| 25 | |
| 26 | #include "helpers_cs.h" |
| 27 | |
| 28 | #if defined(DATA_TYPE_FP16) |
| 29 | precision mediump float; |
| 30 | #endif // DATA_TYPE_FP16 |
| 31 | |
| 32 | /** This kernel performs a depthwise convolution. |
| 33 | * |
| 34 | * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32" |
| 35 | * @note This kernel has multiple optimized depthwise convolution options for FP16. |
| 36 | * The depthwise convolution option must be passed at compile time using "#define PROCESS_nX_nY_nZ" e.g. "#define PROCESS_8X_1Y_1Z" |
| 37 | * @note The convolution stride x must be passed at compile time using "#define STRIDE_X n" e.g. "#define STRIDE_X 1" |
| 38 | * @note In case biases will be added to the convolution "#define HAS_BIAS" has to be passed to append the final matrix with 1 in each row. |
| 39 | * |
| 40 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16 |
| 41 | * @param[in] src_attrs The attributes of the source tensor |
| 42 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 43 | * @param[in] dst_attrs The attributes of the destination tensor |
| 44 | * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr |
| 45 | * @param[in] weights_attrs The attributes of the weights tensor |
| 46 | * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr |
| 47 | * @param[in] biases_attrs The attributes of the weights tensor |
| 48 | */ |
| 49 | SHADER_PARAMS_DECLARATION |
| 50 | { |
| 51 | Tensor3DAttributes src_attrs; |
| 52 | Tensor3DAttributes dst_attrs; |
| 53 | Tensor3DAttributes weights_attrs; |
| 54 | #ifdef BIAS |
| 55 | VectorAttributes biases_attrs; |
| 56 | #endif /* BIAS */ |
| 57 | }; |
| 58 | |
| 59 | #if defined(DATA_TYPE_FP16) |
| 60 | #if defined(PROCESS_4X_3Y_1Z) |
| 61 | TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly); |
| 62 | TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly); |
| 63 | TENSOR_DECLARATION(3, weightsBuffer, uvec2, weights_ptr, weights_shift, 3, readonly); |
| 64 | #ifdef BIAS |
| 65 | TENSOR_DECLARATION(4, biasesBuffer, uint, biases_ptr, biases_shift, 2, readonly); |
| 66 | #endif /* BIAS */ |
| 67 | |
| 68 | #define LOAD_UNPACK_SWIZZLE(offset) load_unpack_swizzle_stride1(offset) |
| 69 | |
| 70 | vec4 convolve1x3(vec4 s[3], vec4 w) |
| 71 | { |
| 72 | vec4 r; |
| 73 | |
| 74 | r = s[0] * w[0] + s[1] * w[1] + s[2] * w[2]; |
| 75 | |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | vec4[3] load_unpack_swizzle_stride1(uint offset) |
| 80 | { |
| 81 | vec4 s[2]; |
| 82 | s = VLOAD2_UNPACK8_HALF(src_ptr, offset); |
| 83 | |
| 84 | vec4 r[3]; |
| 85 | r[0] = s[0]; |
| 86 | r[1] = vec4(s[0].yzw, s[1].x); |
| 87 | r[2] = vec4(s[0].zw, s[1].xy); |
| 88 | |
| 89 | return r; |
| 90 | } |
| 91 | |
| 92 | void main() |
| 93 | { |
| 94 | Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift); |
| 95 | Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift); |
| 96 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
| 97 | |
| 98 | #ifdef BIAS |
| 99 | VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift); |
| 100 | #endif /* BIAS */ |
| 101 | |
| 102 | vec4 pixels[3]; |
| 103 | for(int i = 0; i < 3; i++) |
| 104 | { |
| 105 | pixels[i] = vec4(0); |
| 106 | } |
| 107 | |
| 108 | uint z_index = gl_GlobalInvocationID.z; |
| 109 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_attrs.stride_z); |
| 110 | |
| 111 | vec4 w[3]; |
| 112 | w[0] = LOAD_UNPACK4_CURRENT_ITEM_HALF(weights_ptr, weights_iter); |
| 113 | w[1] = LOAD_UNPACK4_HALF(weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 1, 0)); |
| 114 | w[2] = LOAD_UNPACK4_HALF(weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 2, 0)); |
| 115 | |
| 116 | vec4 s[3]; |
| 117 | vec4 r; |
| 118 | // first line |
| 119 | s = LOAD_UNPACK_SWIZZLE(CURRENT_ITEM_OFFSET(src_iter)); |
| 120 | |
| 121 | r = convolve1x3(s, w[0]); |
| 122 | pixels[0] += r; |
| 123 | |
| 124 | // second line |
| 125 | s = LOAD_UNPACK_SWIZZLE(TENSOR3D_OFFSET(src_iter, 0, 1, 0)); |
| 126 | |
| 127 | r = convolve1x3(s, w[1]); |
| 128 | pixels[0] += r; |
| 129 | r = convolve1x3(s, w[0]); |
| 130 | pixels[1] += r; |
| 131 | |
| 132 | // third line |
| 133 | s = LOAD_UNPACK_SWIZZLE(TENSOR3D_OFFSET(src_iter, 0, 2, 0)); |
| 134 | |
| 135 | r = convolve1x3(s, w[2]); |
| 136 | pixels[0] += r; |
| 137 | r = convolve1x3(s, w[1]); |
| 138 | pixels[1] += r; |
| 139 | r = convolve1x3(s, w[0]); |
| 140 | pixels[2] += r; |
| 141 | |
| 142 | // forth line |
| 143 | s = LOAD_UNPACK_SWIZZLE(TENSOR3D_OFFSET(src_iter, 0, 3, 0)); |
| 144 | |
| 145 | r = convolve1x3(s, w[2]); |
| 146 | pixels[1] += r; |
| 147 | r = convolve1x3(s, w[1]); |
| 148 | pixels[2] += r; |
| 149 | |
| 150 | // fifth line |
| 151 | s = LOAD_UNPACK_SWIZZLE(TENSOR3D_OFFSET(src_iter, 0, 4, 0)); |
| 152 | |
| 153 | r = convolve1x3(s, w[2]); |
| 154 | pixels[2] += r; |
| 155 | |
| 156 | #ifdef BIAS |
| 157 | vec2 vec2_b; |
| 158 | float b; |
| 159 | |
| 160 | vec2_b = LOAD_UNPACK2_HALF(biases_ptr, VECTOR_OFFSET(biases_iter, z_index)); |
| 161 | |
| 162 | if(z_index % uint(2) == uint(0)) |
| 163 | { |
| 164 | b = vec2_b.x; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | b = vec2_b.y; |
| 169 | } |
| 170 | |
| 171 | for(int i = 0; i < 3; i++) |
| 172 | { |
| 173 | pixels[i] += vec4(b); |
| 174 | } |
| 175 | #endif /* BIAS */ |
| 176 | |
| 177 | STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, pixels[0]); |
| 178 | STORE_PACK4_HALF(dst_ptr, TENSOR3D_OFFSET(dst_iter, 0, 1, 0), pixels[1]); |
| 179 | STORE_PACK4_HALF(dst_ptr, TENSOR3D_OFFSET(dst_iter, 0, 2, 0), pixels[2]); |
| 180 | } |
| 181 | #elif defined(PROCESS_4X_1Y_1Z) |
| 182 | TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly); |
| 183 | TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly); |
| 184 | TENSOR_DECLARATION(3, weightsBuffer, uvec2, weights_ptr, weights_shift, 3, readonly); |
| 185 | #ifdef BIAS |
| 186 | TENSOR_DECLARATION(4, biasesBuffer, uint, biases_ptr, biases_shift, 2, readonly); |
| 187 | #endif /* BIAS */ |
| 188 | |
| 189 | #if STRIDE_X == 3 |
| 190 | #define LOAD_UNPACK_SWIZZLE(offset) load_unpack_swizzle_stride3(offset) |
| 191 | #elif STRIDE_X == 2 |
| 192 | #define LOAD_UNPACK_SWIZZLE(offset) load_unpack_swizzle_stride2(offset) |
| 193 | #elif STRIDE_X == 1 /* STRIDE_X == 1 */ |
| 194 | #define LOAD_UNPACK_SWIZZLE(offset) load_unpack_swizzle_stride1(offset) |
| 195 | #else /* STRIDE_X not equals 1 or 2 */ |
| 196 | #error STRIDE_X larger than 2 is not supported |
| 197 | #endif /* STRIDE_X == 2 */ |
| 198 | |
| 199 | vec4 convolve1x3(vec4 s[3], vec4 w) |
| 200 | { |
| 201 | vec4 r; |
| 202 | |
| 203 | r = s[0] * w[0] + s[1] * w[1] + s[2] * w[2]; |
| 204 | |
| 205 | return r; |
| 206 | } |
| 207 | |
| 208 | vec4[3] load_unpack_swizzle_stride1(uint offset) |
| 209 | { |
| 210 | vec4 s[2]; |
| 211 | s = VLOAD2_UNPACK8_HALF(src_ptr, offset); |
| 212 | |
| 213 | vec4 r[3]; |
| 214 | r[0] = s[0]; |
| 215 | r[1] = vec4(s[0].yzw, s[1].x); |
| 216 | r[2] = vec4(s[0].zw, s[1].xy); |
| 217 | |
| 218 | return r; |
| 219 | } |
| 220 | |
| 221 | vec4[3] load_unpack_swizzle_stride2(uint offset) |
| 222 | { |
| 223 | vec4 s[3]; |
| 224 | s[0] = LOAD_UNPACK4_HALF(src_ptr, offset); |
| 225 | s[1] = LOAD_UNPACK4_HALF(src_ptr, offset + uint(1)); |
| 226 | s[2] = LOAD_UNPACK4_HALF(src_ptr, offset + uint(2)); |
| 227 | |
| 228 | vec4 r[3]; |
| 229 | r[0] = vec4(s[0].xz, s[1].xz); |
| 230 | r[1] = vec4(s[0].yw, s[1].yw); |
| 231 | r[2] = vec4(s[0].z, s[1].xz, s[2].x); |
| 232 | |
| 233 | return r; |
| 234 | } |
| 235 | |
| 236 | vec4[3] load_unpack_swizzle_stride3(uint offset) |
| 237 | { |
| 238 | vec4 s[3]; |
| 239 | s[0] = LOAD_UNPACK4_HALF(src_ptr, offset); |
| 240 | s[1] = LOAD_UNPACK4_HALF(src_ptr, offset + uint(1)); |
| 241 | s[2] = LOAD_UNPACK4_HALF(src_ptr, offset + uint(2)); |
| 242 | |
| 243 | vec4 r[3]; |
| 244 | r[0] = vec4(s[0].xw, s[1].z, s[2].y); |
| 245 | r[1] = vec4(s[0].y, s[1].xw, s[2].z); |
| 246 | r[2] = vec4(s[0].z, s[1].y, s[2].xw); |
| 247 | |
| 248 | return r; |
| 249 | } |
| 250 | |
| 251 | void main() |
| 252 | { |
| 253 | Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift); |
| 254 | Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift); |
| 255 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
| 256 | |
| 257 | #ifdef BIAS |
| 258 | VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift); |
| 259 | #endif /* BIAS */ |
| 260 | |
| 261 | vec4 pixels = vec4(0.f); |
| 262 | |
| 263 | uint z_index = gl_GlobalInvocationID.z; |
| 264 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_attrs.stride_z); |
| 265 | |
| 266 | vec4 w[3]; |
| 267 | w[0] = LOAD_UNPACK4_CURRENT_ITEM_HALF(weights_ptr, weights_iter); |
| 268 | w[1] = LOAD_UNPACK4_HALF(weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 1, 0)); |
| 269 | w[2] = LOAD_UNPACK4_HALF(weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 2, 0)); |
| 270 | |
| 271 | vec4 s[3]; |
| 272 | vec4 r; |
| 273 | // first line |
| 274 | s = LOAD_UNPACK_SWIZZLE(CURRENT_ITEM_OFFSET(src_iter)); |
| 275 | |
| 276 | r = convolve1x3(s, w[0]); |
| 277 | pixels += r; |
| 278 | |
| 279 | // second line |
| 280 | s = LOAD_UNPACK_SWIZZLE(TENSOR3D_OFFSET(src_iter, 0, 1, 0)); |
| 281 | |
| 282 | r = convolve1x3(s, w[1]); |
| 283 | pixels += r; |
| 284 | |
| 285 | // third line |
| 286 | s = LOAD_UNPACK_SWIZZLE(TENSOR3D_OFFSET(src_iter, 0, 2, 0)); |
| 287 | |
| 288 | r = convolve1x3(s, w[2]); |
| 289 | pixels += r; |
| 290 | |
| 291 | #ifdef BIAS |
| 292 | vec2 vec2_b; |
| 293 | float b; |
| 294 | |
| 295 | vec2_b = LOAD_UNPACK2_HALF(biases_ptr, VECTOR_OFFSET(biases_iter, z_index)); |
| 296 | |
| 297 | if(z_index % uint(2) == uint(0)) |
| 298 | { |
| 299 | b = vec2_b.x; |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | b = vec2_b.y; |
| 304 | } |
| 305 | |
| 306 | pixels += vec4(b); |
| 307 | #endif /* BIAS */ |
| 308 | |
| 309 | STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, pixels); |
| 310 | } |
| 311 | #endif /* PROCESS_4X_3Y_1Z */ |
| 312 | #endif /* DATA_TYPE_FP16 */ |