| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #version 450 |
| 16 | |
| 17 | #if NCNN_fp16_storage |
| 18 | #extension GL_EXT_shader_16bit_storage: require |
| 19 | #endif |
| 20 | #if NCNN_fp16_arithmetic |
| 21 | #extension GL_EXT_shader_explicit_arithmetic_types_float16: require |
| 22 | #endif |
| 23 | |
| 24 | #extension GL_GOOGLE_include_directive: enable |
| 25 | #include "vulkan_activation.comp" |
| 26 | |
| 27 | #define LOCAL_MEMORY_UNROLL_INCH 8 |
| 28 | |
| 29 | layout (constant_id = 0) const int bias_term = 0; |
| 30 | layout (constant_id = 1) const int activation_type = 0; |
| 31 | layout (constant_id = 2) const float activation_param_0 = 0; |
| 32 | layout (constant_id = 3) const float activation_param_1 = 0; |
| 33 | |
| 34 | #define shape_constant_id_offset 4 |
| 35 | layout (constant_id = shape_constant_id_offset + 0) const int w = 0; |
| 36 | layout (constant_id = shape_constant_id_offset + 1) const int h = 0; |
| 37 | layout (constant_id = shape_constant_id_offset + 2) const int c = 0; |
| 38 | layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0; |
| 39 | |
| 40 | layout (constant_id = shape_constant_id_offset + 4) const int outw = 0; |
| 41 | layout (constant_id = shape_constant_id_offset + 5) const int outh = 0; |
| 42 | layout (constant_id = shape_constant_id_offset + 6) const int outc = 0; |
| 43 | layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0; |
| 44 | |
| 45 | #if NCNN_image_shader |
| 46 | layout (binding = 0) uniform unfp sampler3D bottom_blob; |
| 47 | layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob; |
| 48 | layout (binding = 2) uniform unfp sampler3D weight_blob; |
| 49 | layout (binding = 3) uniform unfp sampler3D bias_blob; |
| 50 | #else |
| 51 | layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; }; |
| 52 | layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; }; |
| 53 | layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; }; |
| 54 | layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; }; |
| 55 | #endif |
| 56 | |
| 57 | layout (push_constant) uniform parameter |
| 58 | { |
| 59 | int w; |
| 60 | int h; |
| 61 | int c; |
| 62 | int cstep; |
| 63 | |
| 64 | int outw; |
| 65 | int outh; |
| 66 | int outc; |
| 67 | int outcstep; |
| 68 | } p; |
| 69 | |
| 70 | #if NCNN_shader_local_memory |
| 71 | shared lfp tmp_v[8][LOCAL_MEMORY_UNROLL_INCH][4]; |
| 72 | shared lfpvec4 tmp_k[8][LOCAL_MEMORY_UNROLL_INCH]; |
| 73 | #endif |
| 74 | |
| 75 | void main() |
| 76 | { |
| 77 | int gx = int(gl_GlobalInvocationID.x) * 4; |
| 78 | int gy = int(gl_GlobalInvocationID.y); |
| 79 | |
| 80 | #if NCNN_image_shader |
| 81 | if (gx >= psc(outw) * psc(outh) || gy >= psc(outc)) |
| 82 | return; |
| 83 | #else |
| 84 | #if !NCNN_shader_local_memory |
| 85 | if (gx >= psc(outcstep) || gy >= psc(outc)) |
| 86 | return; |
| 87 | #endif |
| 88 | #endif |
| 89 | |
| 90 | afpvec4 sum0; |
| 91 | afpvec4 sum1; |
| 92 | afpvec4 sum2; |
| 93 | afpvec4 sum3; |
| 94 | |
| 95 | if (bias_term == 1) |
| 96 | { |
| 97 | #if NCNN_image_shader |
| 98 | afpvec4 b = image3d_ld4(bias_blob, ivec3(gy, 0, 0)); |
| 99 | #else |
| 100 | afpvec4 b = buffer_ld4(bias_data, gy); |
| 101 | #endif |
| 102 | sum0 = b; |
| 103 | sum1 = b; |
| 104 | sum2 = b; |
| 105 | sum3 = b; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | sum0 = afpvec4(0.f); |
| 110 | sum1 = afpvec4(0.f); |
| 111 | sum2 = afpvec4(0.f); |
| 112 | sum3 = afpvec4(0.f); |
| 113 | } |
| 114 | |
| 115 | #if NCNN_image_shader |
| 116 | ivec4 gx4 = gx + ivec4(0, 1, 2, 3); |
| 117 | |
| 118 | ivec4 sy4 = gx4 / psc(w); |
| 119 | ivec4 sx4 = gx4 % psc(w); |
| 120 | |
| 121 | for (int z = 0; z < psc(c); z++) |
| 122 | { |
| 123 | afp v0 = image3d_ld1(bottom_blob, ivec3(sx4.r, sy4.r, z)); |
| 124 | afp v1 = image3d_ld1(bottom_blob, ivec3(sx4.g, sy4.g, z)); |
| 125 | afp v2 = image3d_ld1(bottom_blob, ivec3(sx4.b, sy4.b, z)); |
| 126 | afp v3 = image3d_ld1(bottom_blob, ivec3(sx4.a, sy4.a, z)); |
| 127 | |
| 128 | afpvec4 k = image3d_ld4(weight_blob, ivec3(0, z, gy)); |
| 129 | |
| 130 | sum0 += v0 * k; |
| 131 | sum1 += v1 * k; |
| 132 | sum2 += v2 * k; |
| 133 | sum3 += v3 * k; |
| 134 | } |
| 135 | #else |
| 136 | int w_offset = gy * psc(c); |
| 137 | int v_offset = gx; |
| 138 | |
| 139 | #if NCNN_shader_local_memory |
| 140 | const int lx = int(gl_LocalInvocationID.x); |
| 141 | const int ly = int(gl_LocalInvocationID.y); |
| 142 | |
| 143 | int z = 0; |
| 144 | for (; z + (LOCAL_MEMORY_UNROLL_INCH - 1) < psc(c); z += LOCAL_MEMORY_UNROLL_INCH) |
| 145 | { |
| 146 | if (ly < 4) |
| 147 | { |
| 148 | for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) |
| 149 | { |
| 150 | tmp_v[lx][z4][ly] = sfp2lfp(bottom_blob_data[v_offset + z4 * psc(cstep) + ly]); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (lx == 0) |
| 155 | { |
| 156 | for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) |
| 157 | { |
| 158 | tmp_k[ly][z4] = sfp2lfpvec4(weight_data[w_offset + z4]); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | barrier(); |
| 163 | |
| 164 | for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) |
| 165 | { |
| 166 | afp v0 = lfp2afp(tmp_v[lx][z4][0]); |
| 167 | afp v1 = lfp2afp(tmp_v[lx][z4][1]); |
| 168 | afp v2 = lfp2afp(tmp_v[lx][z4][2]); |
| 169 | afp v3 = lfp2afp(tmp_v[lx][z4][3]); |
| 170 | |
| 171 | afpvec4 k = lfp2afpvec4(tmp_k[ly][z4]); |
| 172 | |
| 173 | sum0 += v0 * k; |
| 174 | sum1 += v1 * k; |
| 175 | sum2 += v2 * k; |
| 176 | sum3 += v3 * k; |
| 177 | } |
| 178 | |
| 179 | v_offset += LOCAL_MEMORY_UNROLL_INCH * psc(cstep); |
| 180 | w_offset += LOCAL_MEMORY_UNROLL_INCH; |
| 181 | |
| 182 | barrier(); |
| 183 | } |
| 184 | |
| 185 | if (z < psc(c)) |
| 186 | { |
| 187 | const int remain = psc(c) - z; |
| 188 | |
| 189 | if (ly < 4) |
| 190 | { |
| 191 | for (int z4 = 0; z4 < remain; z4++) |
| 192 | { |
| 193 | tmp_v[lx][z4][ly] = sfp2lfp(bottom_blob_data[v_offset + z4 * psc(cstep) + ly]); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (lx == 0) |
| 198 | { |
| 199 | for (int z4 = 0; z4 < remain; z4++) |
| 200 | { |
| 201 | tmp_k[ly][z4] = sfp2lfpvec4(weight_data[w_offset + z4]); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | barrier(); |
| 206 | |
| 207 | for (int z4 = 0; z4 < remain; z4++) |
| 208 | { |
| 209 | afp v0 = lfp2afp(tmp_v[lx][z4][0]); |
| 210 | afp v1 = lfp2afp(tmp_v[lx][z4][1]); |
| 211 | afp v2 = lfp2afp(tmp_v[lx][z4][2]); |
| 212 | afp v3 = lfp2afp(tmp_v[lx][z4][3]); |
| 213 | |
| 214 | afpvec4 k = lfp2afpvec4(tmp_k[ly][z4]); |
| 215 | |
| 216 | sum0 += v0 * k; |
| 217 | sum1 += v1 * k; |
| 218 | sum2 += v2 * k; |
| 219 | sum3 += v3 * k; |
| 220 | } |
| 221 | } |
| 222 | #else |
| 223 | for (int z = 0; z < psc(c); z++) |
| 224 | { |
| 225 | afp v0 = buffer_ld1(bottom_blob_data, v_offset + 0); |
| 226 | afp v1 = buffer_ld1(bottom_blob_data, v_offset + 1); |
| 227 | afp v2 = buffer_ld1(bottom_blob_data, v_offset + 2); |
| 228 | afp v3 = buffer_ld1(bottom_blob_data, v_offset + 3); |
| 229 | |
| 230 | afpvec4 k = buffer_ld4(weight_data, w_offset); |
| 231 | |
| 232 | sum0 += v0 * k; |
| 233 | sum1 += v1 * k; |
| 234 | sum2 += v2 * k; |
| 235 | sum3 += v3 * k; |
| 236 | |
| 237 | w_offset += 1; |
| 238 | v_offset += psc(cstep); |
| 239 | } |
| 240 | #endif |
| 241 | #endif |
| 242 | |
| 243 | sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1); |
| 244 | sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1); |
| 245 | sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1); |
| 246 | sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1); |
| 247 | |
| 248 | #if NCNN_image_shader |
| 249 | image3d_st4(top_blob, ivec3(sx4.r, sy4.r, gy), sum0); |
| 250 | image3d_st4(top_blob, ivec3(sx4.g, sy4.g, gy), sum1); |
| 251 | image3d_st4(top_blob, ivec3(sx4.b, sy4.b, gy), sum2); |
| 252 | image3d_st4(top_blob, ivec3(sx4.a, sy4.a, gy), sum3); |
| 253 | #else |
| 254 | #if NCNN_shader_local_memory |
| 255 | if (gx >= psc(outcstep) || gy >= psc(outc)) |
| 256 | return; |
| 257 | #endif |
| 258 | |
| 259 | int gi = gy * psc(outcstep) + gx; |
| 260 | |
| 261 | buffer_st4(top_blob_data, gi + 0, sum0); |
| 262 | if (gx + 1 < psc(outcstep)) buffer_st4(top_blob_data, gi + 1, sum1); |
| 263 | if (gx + 2 < psc(outcstep)) buffer_st4(top_blob_data, gi + 2, sum2); |
| 264 | if (gx + 3 < psc(outcstep)) buffer_st4(top_blob_data, gi + 3, sum3); |
| 265 | #endif |
| 266 | } |