| 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 | #define LOCAL_MEMORY_UNROLL_INCH 8 |
| 25 | |
| 26 | layout (constant_id = 0) const int batch = 1; |
| 27 | |
| 28 | #define shape_constant_id_offset 1 |
| 29 | layout (constant_id = shape_constant_id_offset + 0) const int c = 0; |
| 30 | layout (constant_id = shape_constant_id_offset + 1) const int cstep = 0; |
| 31 | |
| 32 | layout (constant_id = shape_constant_id_offset + 2) const int outw = 0; |
| 33 | layout (constant_id = shape_constant_id_offset + 3) const int outc = 0; |
| 34 | layout (constant_id = shape_constant_id_offset + 4) const int outcstep = 0; |
| 35 | |
| 36 | #if NCNN_image_shader |
| 37 | layout (binding = 0) uniform unfp sampler3D bottom_tm_blob; |
| 38 | layout (binding = 1, imfmtc1) writeonly uniform unfp image3D top_tm_blob; |
| 39 | layout (binding = 2) uniform unfp sampler3D weight_tm_blob; |
| 40 | #else |
| 41 | layout (binding = 0) readonly buffer bottom_tm_blob { sfp bottom_tm_blob_data[]; }; |
| 42 | layout (binding = 1) writeonly buffer top_tm_blob { sfp top_tm_blob_data[]; }; |
| 43 | layout (binding = 2) readonly buffer weight_tm_blob { sfp weight_tm_data[]; }; |
| 44 | #endif |
| 45 | |
| 46 | layout (push_constant) uniform parameter |
| 47 | { |
| 48 | int c; |
| 49 | int cstep; |
| 50 | |
| 51 | int outw; |
| 52 | int outc; |
| 53 | int outcstep; |
| 54 | } p; |
| 55 | |
| 56 | #if NCNN_shader_local_memory |
| 57 | shared lfp tmp_v[8][LOCAL_MEMORY_UNROLL_INCH][4]; |
| 58 | shared lfp tmp_k[8][LOCAL_MEMORY_UNROLL_INCH]; |
| 59 | #endif |
| 60 | |
| 61 | void main() |
| 62 | { |
| 63 | int gx = int(gl_GlobalInvocationID.x) * 4; |
| 64 | int gy = int(gl_GlobalInvocationID.y); |
| 65 | int gz = int(gl_GlobalInvocationID.z); |
| 66 | |
| 67 | #if !NCNN_shader_local_memory |
| 68 | if (gx >= psc(outw) || gy >= psc(outc) || gz >= batch) |
| 69 | return; |
| 70 | #endif |
| 71 | |
| 72 | afp sum0 = afp(0.f); |
| 73 | afp sum1 = afp(0.f); |
| 74 | afp sum2 = afp(0.f); |
| 75 | afp sum3 = afp(0.f); |
| 76 | |
| 77 | #if NCNN_image_shader |
| 78 | for (int z = 0; z < psc(c); z++) |
| 79 | { |
| 80 | afp v0 = image3d_ld1(bottom_tm_blob, ivec3(gx + 0, z, gz)); |
| 81 | afp v1 = image3d_ld1(bottom_tm_blob, ivec3(gx + 1, z, gz)); |
| 82 | afp v2 = image3d_ld1(bottom_tm_blob, ivec3(gx + 2, z, gz)); |
| 83 | afp v3 = image3d_ld1(bottom_tm_blob, ivec3(gx + 3, z, gz)); |
| 84 | |
| 85 | afp k = image3d_ld1(weight_tm_blob, ivec3(z, gy, gz)); |
| 86 | |
| 87 | sum0 += v0 * k; |
| 88 | sum1 += v1 * k; |
| 89 | sum2 += v2 * k; |
| 90 | sum3 += v3 * k; |
| 91 | } |
| 92 | #else |
| 93 | int v_offset = gz * psc(cstep) + gx; |
| 94 | int w_offset = gz * psc(c) * psc(outc) + gy * psc(c); |
| 95 | |
| 96 | #if NCNN_shader_local_memory |
| 97 | const int lx = int(gl_LocalInvocationID.x); |
| 98 | const int ly = int(gl_LocalInvocationID.y); |
| 99 | |
| 100 | int z = 0; |
| 101 | for (; z + (LOCAL_MEMORY_UNROLL_INCH - 1) < psc(c); z += LOCAL_MEMORY_UNROLL_INCH) |
| 102 | { |
| 103 | if (ly < 4) |
| 104 | { |
| 105 | for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) |
| 106 | { |
| 107 | tmp_v[lx][z4][ly] = sfp2lfp(bottom_tm_blob_data[v_offset + z4 * psc(outw) + ly]); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (lx == 0) |
| 112 | { |
| 113 | for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) |
| 114 | { |
| 115 | tmp_k[ly][z4] = sfp2lfp(weight_tm_data[w_offset + z4]); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | barrier(); |
| 120 | |
| 121 | for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) |
| 122 | { |
| 123 | afp v0 = lfp2afp(tmp_v[lx][z4][0]); |
| 124 | afp v1 = lfp2afp(tmp_v[lx][z4][1]); |
| 125 | afp v2 = lfp2afp(tmp_v[lx][z4][2]); |
| 126 | afp v3 = lfp2afp(tmp_v[lx][z4][3]); |
| 127 | |
| 128 | afp k = lfp2afp(tmp_k[ly][z4]); |
| 129 | |
| 130 | sum0 += v0 * k; |
| 131 | sum1 += v1 * k; |
| 132 | sum2 += v2 * k; |
| 133 | sum3 += v3 * k; |
| 134 | } |
| 135 | |
| 136 | v_offset += LOCAL_MEMORY_UNROLL_INCH * psc(outw); |
| 137 | w_offset += LOCAL_MEMORY_UNROLL_INCH; |
| 138 | |
| 139 | barrier(); |
| 140 | } |
| 141 | |
| 142 | if (z < psc(c)) |
| 143 | { |
| 144 | const int remain = psc(c) - z; |
| 145 | |
| 146 | if (ly < 4) |
| 147 | { |
| 148 | for (int z4 = 0; z4 < remain; z4++) |
| 149 | { |
| 150 | tmp_v[lx][z4][ly] = sfp2lfp(bottom_tm_blob_data[v_offset + z4 * psc(outw) + ly]); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (lx == 0) |
| 155 | { |
| 156 | for (int z4 = 0; z4 < remain; z4++) |
| 157 | { |
| 158 | tmp_k[ly][z4] = sfp2lfp(weight_tm_data[w_offset + z4]); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | barrier(); |
| 163 | |
| 164 | for (int z4 = 0; z4 < remain; 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 | afp k = lfp2afp(tmp_k[ly][z4]); |
| 172 | |
| 173 | sum0 += v0 * k; |
| 174 | sum1 += v1 * k; |
| 175 | sum2 += v2 * k; |
| 176 | sum3 += v3 * k; |
| 177 | } |
| 178 | } |
| 179 | #else |
| 180 | for (int z = 0; z < psc(c); z++) |
| 181 | { |
| 182 | afp v0 = buffer_ld1(bottom_tm_blob_data, v_offset + 0); |
| 183 | afp v1 = buffer_ld1(bottom_tm_blob_data, v_offset + 1); |
| 184 | afp v2 = buffer_ld1(bottom_tm_blob_data, v_offset + 2); |
| 185 | afp v3 = buffer_ld1(bottom_tm_blob_data, v_offset + 3); |
| 186 | |
| 187 | afp k = buffer_ld1(weight_tm_data, w_offset); |
| 188 | |
| 189 | sum0 += v0 * k; |
| 190 | sum1 += v1 * k; |
| 191 | sum2 += v2 * k; |
| 192 | sum3 += v3 * k; |
| 193 | |
| 194 | v_offset += psc(outw); |
| 195 | w_offset += 1; |
| 196 | } |
| 197 | #endif |
| 198 | #endif |
| 199 | |
| 200 | #if NCNN_shader_local_memory |
| 201 | if (gx >= psc(outw) || gy >= psc(outc) || gz >= batch) |
| 202 | return; |
| 203 | #endif |
| 204 | |
| 205 | #if NCNN_image_shader |
| 206 | image3d_st1(top_tm_blob, ivec3(gx + 0, gy, gz), sum0); |
| 207 | image3d_st1(top_tm_blob, ivec3(gx + 1, gy, gz), sum1); |
| 208 | image3d_st1(top_tm_blob, ivec3(gx + 2, gy, gz), sum2); |
| 209 | image3d_st1(top_tm_blob, ivec3(gx + 3, gy, gz), sum3); |
| 210 | #else |
| 211 | int gi = gz * psc(outcstep) + gy * psc(outw) + gx; |
| 212 | |
| 213 | buffer_st1(top_tm_blob_data, gi + 0, sum0); |
| 214 | if (gx + 1 < psc(outw)) buffer_st1(top_tm_blob_data, gi + 1, sum1); |
| 215 | if (gx + 2 < psc(outw)) buffer_st1(top_tm_blob_data, gi + 2, sum2); |
| 216 | if (gx + 3 < psc(outw)) buffer_st1(top_tm_blob_data, gi + 3, sum3); |
| 217 | #endif |
| 218 | } |