Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017, 2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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 | |
| 25 | layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in; |
| 26 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 27 | #include "helpers_cs.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 28 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 29 | #if defined(DATA_TYPE_FP16) |
| 30 | precision mediump float; |
| 31 | #endif // DATA_TYPE_FP16 |
ASIAPAC\steli01 | 23ac91b | 2017-11-07 16:14:44 +0800 | [diff] [blame] | 32 | |
| 33 | /** This kernel performs a direct convolution to convolve the low three dimensions |
| 34 | * |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 35 | * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32" |
| 36 | * @note This kernel has multiple optimized direct convolution options for FP16. |
| 37 | * The direct convolution option must be passed at compile time using "#define PROCESS_nX_nY_nZ" e.g. "#define PROCESS_8X_1Y_1Z" |
| 38 | * @note The convolution stride x must be passed at compile time using "#define STRIDE_X n" e.g. "#define STRIDE_X 1" |
| 39 | * This OpenGL ES shader works with stride_x = 1 and 2 |
ASIAPAC\steli01 | 23ac91b | 2017-11-07 16:14:44 +0800 | [diff] [blame] | 40 | * @note If biases are used then "define HAS_BIAS" has to be passed at compile time |
| 41 | * |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 42 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 43 | * @param[in] src_attrs The attributes of the source tensor |
| 44 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 45 | * @param[in] dst_attrs The attributes of the destination tensor |
| 46 | * @param[out] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr |
| 47 | * @param[in] weights_attrs The attributes of the weights tensor |
| 48 | * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr |
| 49 | * @param[in] biases_attrs The attributes of the weights tensor |
| 50 | * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension |
| 51 | * @param[in] weights_depth The third dimensions of the weights tensors |
ASIAPAC\steli01 | 23ac91b | 2017-11-07 16:14:44 +0800 | [diff] [blame] | 52 | */ |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 53 | SHADER_PARAMS_DECLARATION |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 54 | { |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 55 | Tensor3DAttributes src_attrs; |
| 56 | Tensor3DAttributes dst_attrs; |
| 57 | Tensor3DAttributes weights_attrs; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 58 | #ifdef BIAS |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 59 | VectorAttributes biases_attrs; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 60 | #endif /* BIAS */ |
| 61 | uint weights_stride_w; |
| 62 | uint weights_depth; |
| 63 | }; |
| 64 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 65 | #ifdef DATA_TYPE_FP32 |
| 66 | TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly); |
| 67 | TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly); |
| 68 | TENSOR_DECLARATION(3, weightsBuffer, float, weights_ptr, weights_shift, 2, readonly); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 69 | #ifdef BIAS |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 70 | TENSOR_DECLARATION(4, biasesBuffer, float, biases_ptr, biases_shift, 2, readonly); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 71 | #endif /* BIAS */ |
| 72 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 73 | void main() |
| 74 | { |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 75 | ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift); |
| 76 | Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift); |
| 77 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 78 | |
| 79 | #ifdef BIAS |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 80 | VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 81 | #endif /* BIAS */ |
| 82 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 83 | float pixels = 0.f; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 84 | uint z_index = gl_GlobalInvocationID.z; |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 85 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_stride_w); |
| 86 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 87 | float temp[5]; |
| 88 | float temp_weight[5]; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 89 | for(int d = 0; d < int(weights_depth); ++d) |
| 90 | { |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 91 | temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 0)); |
| 92 | temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 0, 0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 93 | pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4]; |
| 94 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 95 | temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 1)); |
| 96 | temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 1, 0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 97 | pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4]; |
| 98 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 99 | temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 2)); |
| 100 | temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 2, 0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 101 | pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4]; |
| 102 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 103 | temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 3)); |
| 104 | temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 3, 0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 105 | pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4]; |
| 106 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 107 | temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 4)); |
| 108 | temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 4, 0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 109 | pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4]; |
| 110 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 111 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, src_attrs.stride_z); |
| 112 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, weights_attrs.stride_z); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #ifdef BIAS |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 116 | pixels += LOAD(biases_ptr, VECTOR_OFFSET(biases_iter, z_index)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 117 | #endif /* BIAS */ |
| 118 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 119 | STORE_CURRENT_ITEM(dst_ptr, dst_iter, pixels); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 120 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 121 | #elif defined(DATA_TYPE_FP16) |
| 122 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 123 | // Common definitions for DATA_TYPE_FP16 |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 124 | #if STRIDE_X == 1 |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 125 | #define LOAD_SRC_AT_ROW(row) VLOAD2_UNPACK8_HALF(src_ptr, IMAGE_OFFSET(src_iter, 0, row)) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 126 | #define CONVOLVE1x5(src, weight) convolve1x5_stride1(src, weight) |
| 127 | #elif STRIDE_X == 2 /* STRIDE_X == 1 */ |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 128 | #define LOAD_SRC_AT_ROW(row) VLOAD3_UNPACK12_HALF(src_ptr, IMAGE_OFFSET(src_iter, 0, row)) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 129 | #define CONVOLVE1x5(src, weight) convolve1x5_stride2(src, weight) |
| 130 | #else /* STRDIDE_X == 1 */ |
| 131 | #error STRIDE_X larger than 2 is not supported |
| 132 | #endif /* STRIDE_X == 1 */ |
| 133 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 134 | #define LOAD_WEIGHT_AT_ROW(row) VLOAD3_UNPACK6_HALF(weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, row, 0)) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 135 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 136 | vec4 convolve1x5_stride1(vec4 tmp[2], vec2 w[3]) |
| 137 | { |
| 138 | vec4 src0 = tmp[0]; |
| 139 | vec4 src1 = vec4(tmp[0].yzw, tmp[1].x); |
| 140 | vec4 src2 = vec4(tmp[0].zw, tmp[1].xy); |
| 141 | vec4 src3 = vec4(tmp[0].w, tmp[1].xyz); |
| 142 | vec4 src4 = tmp[1]; |
| 143 | vec4 ret = src0 * w[0].x + src1 * w[0].y + src2 * w[1].x + src3 * w[1].y + src4 * w[2].x; |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | vec4 convolve1x5_stride2(vec4 tmp[3], vec2 w[3]) |
| 149 | { |
| 150 | vec4 src0 = vec4(tmp[0].xz, tmp[1].xz); |
| 151 | vec4 src1 = vec4(tmp[0].yw, tmp[1].yw); |
| 152 | vec4 src2 = vec4(tmp[0].z, tmp[1].xz, tmp[2].x); |
| 153 | vec4 src3 = vec4(tmp[0].w, tmp[1].yw, tmp[2].y); |
| 154 | vec4 src4 = vec4(tmp[1].x, tmp[1].z, tmp[2].xz); |
| 155 | vec4 ret = src0 * w[0].x + src1 * w[0].y + src2 * w[1].x + src3 * w[1].y + src4 * w[2].x; |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 160 | #if defined(PROCESS_4X_1Y_1Z) |
| 161 | TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly); |
| 162 | TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly); |
| 163 | TENSOR_DECLARATION(3, weightsBuffer, uint, weights_ptr, weights_shift, 2, readonly); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 164 | #ifdef BIAS |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 165 | TENSOR_DECLARATION(4, biasesBuffer, uint, biases_ptr, biases_shift, 2, readonly); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 166 | #endif /* BIAS */ |
| 167 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 168 | void main() |
| 169 | { |
| 170 | ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift); |
| 171 | Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift); |
| 172 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 173 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 174 | #ifdef BIAS |
| 175 | VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift); |
| 176 | #endif /* BIAS */ |
| 177 | |
| 178 | vec4 res = vec4(0); |
| 179 | vec2 w[3]; |
| 180 | vec4 s[STRIDE_X + 1]; |
| 181 | |
| 182 | uint z_index = gl_GlobalInvocationID.z; |
| 183 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_stride_w); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 184 | |
| 185 | for(int d = 0; d < int(weights_depth); ++d) |
| 186 | { |
| 187 | for(int row = 0; row < 5; row++) |
| 188 | { |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 189 | w = LOAD_WEIGHT_AT_ROW(row); |
| 190 | s = LOAD_SRC_AT_ROW(row); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 191 | res += CONVOLVE1x5(s, w); |
| 192 | } |
| 193 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 194 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, src_attrs.stride_z); |
| 195 | TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, weights_attrs.stride_z); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #ifdef BIAS |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 199 | vec2 vec2_b; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 200 | float b; |
| 201 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 202 | vec2_b = LOAD_UNPACK2_HALF(biases_ptr, VECTOR_OFFSET(biases_iter, z_index)); |
| 203 | b = (z_index % uint(2) == uint(0)) ? vec2_b.x : vec2_b.y; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 204 | res += vec4(b); |
| 205 | #endif /* BIAS */ |
| 206 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 207 | STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, res); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 210 | #endif /* PROCESS_nX_nY_nZ */ |
| 211 | #else /* DATA_TYPE_FP32 */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 212 | #error Data type not supported |
Joel Liang | 09849a0 | 2018-01-05 15:12:53 +0800 | [diff] [blame] | 213 | #endif /* DATA_TYPE_FP32 */ |