| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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 attn_mask = 0; |
| 27 | layout (constant_id = 1) const int M = 0; |
| 28 | layout (constant_id = 2) const int N = 0; |
| 29 | layout (constant_id = 3) const int K = 0; |
| 30 | layout (constant_id = 4) const int B = 0; |
| 31 | layout (constant_id = 5) const int attn_mask_dims = 0; |
| 32 | |
| 33 | #if NCNN_image_shader |
| 34 | layout (binding = 0) uniform unfp sampler3D q_blob_3d; |
| 35 | layout (binding = 1) uniform unfp sampler3D k_blob_3d; |
| 36 | layout (binding = 2, imfmtc4) writeonly uniform unfp image3D qkcross_blob_3d; |
| 37 | layout (binding = 3) uniform unfp sampler3D attn_mask_blob_3d; |
| 38 | #else |
| 39 | layout (binding = 0) readonly buffer q_blob { sfpvec4 q_blob_data[]; }; |
| 40 | layout (binding = 1) readonly buffer k_blob { sfpvec4 k_blob_data[]; }; |
| 41 | layout (binding = 2) writeonly buffer qkcross_blob { sfpvec4 qkcross_blob_data[]; }; |
| 42 | layout (binding = 3) readonly buffer attn_mask_blob { sfpvec4 attn_mask_blob_data[]; }; |
| 43 | #endif |
| 44 | |
| 45 | layout (push_constant) uniform parameter |
| 46 | { |
| 47 | int M; |
| 48 | int N; |
| 49 | int K; |
| 50 | int B; |
| 51 | int attn_mask_dims; |
| 52 | } p; |
| 53 | |
| 54 | #if NCNN_shader_local_memory |
| 55 | shared lfpvec4 tmp_q[8][LOCAL_MEMORY_UNROLL_INCH][4]; |
| 56 | shared lfpvec4 tmp_k[8][LOCAL_MEMORY_UNROLL_INCH]; |
| 57 | #endif |
| 58 | |
| 59 | void main() |
| 60 | { |
| 61 | int gx = int(gl_GlobalInvocationID.x); |
| 62 | int gy = int(gl_GlobalInvocationID.y); |
| 63 | int gz = int(gl_GlobalInvocationID.z); |
| 64 | |
| 65 | #if !NCNN_shader_local_memory |
| 66 | if (gx >= psc(N) || gy >= psc(M) || gz >= psc(B)) |
| 67 | return; |
| 68 | #endif |
| 69 | |
| 70 | afpvec4 sum = afpvec4(0.f); |
| 71 | |
| 72 | if (attn_mask == 1) |
| 73 | { |
| 74 | if (psc(attn_mask_dims) == 3) |
| 75 | { |
| 76 | #if NCNN_image_shader |
| 77 | sum = image3d_ld4(attn_mask_blob_3d, ivec3(gx, gy, gz)); |
| 78 | #else |
| 79 | const int mi = gz * psc(M) * psc(N) + gy * psc(N) + gx; |
| 80 | sum = buffer_ld4(attn_mask_blob_data, mi); |
| 81 | #endif |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | #if NCNN_image_shader |
| 86 | sum = image3d_ld4(attn_mask_blob_3d, ivec3(gx, gy, 0)); |
| 87 | #else |
| 88 | const int mi = gy * psc(N) + gx; |
| 89 | sum = buffer_ld4(attn_mask_blob_data, mi); |
| 90 | #endif |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #if !NCNN_image_shader && NCNN_shader_local_memory |
| 95 | const int NN = psc(K); |
| 96 | |
| 97 | const int lx = int(gl_LocalInvocationID.x); |
| 98 | const int ly = int(gl_LocalInvocationID.y); |
| 99 | |
| 100 | int ai = (gz * psc(M) * psc(K) + lx * psc(M) + gy) * 4; |
| 101 | int bi = gz * psc(N) * psc(K) + ly * psc(N) + gx; |
| 102 | |
| 103 | int k = 0; |
| 104 | for (; k + (LOCAL_MEMORY_UNROLL_INCH - 1) < NN; k += LOCAL_MEMORY_UNROLL_INCH) |
| 105 | { |
| 106 | { |
| 107 | tmp_q[ly][lx][0] = sfp2lfpvec4(q_blob_data[ai]); |
| 108 | tmp_q[ly][lx][1] = sfp2lfpvec4(q_blob_data[ai + 1]); |
| 109 | tmp_q[ly][lx][2] = sfp2lfpvec4(q_blob_data[ai + 2]); |
| 110 | tmp_q[ly][lx][3] = sfp2lfpvec4(q_blob_data[ai + 3]); |
| 111 | } |
| 112 | |
| 113 | { |
| 114 | tmp_k[lx][ly] = sfp2lfpvec4(k_blob_data[bi]); |
| 115 | } |
| 116 | |
| 117 | barrier(); |
| 118 | |
| 119 | for (int k4 = 0; k4 < LOCAL_MEMORY_UNROLL_INCH; k4++) |
| 120 | { |
| 121 | afpvec4 q0 = lfp2afpvec4(tmp_q[ly][k4][0]); |
| 122 | afpvec4 q1 = lfp2afpvec4(tmp_q[ly][k4][1]); |
| 123 | afpvec4 q2 = lfp2afpvec4(tmp_q[ly][k4][2]); |
| 124 | afpvec4 q3 = lfp2afpvec4(tmp_q[ly][k4][3]); |
| 125 | |
| 126 | afpvec4 k0 = lfp2afpvec4(tmp_k[lx][k4]); |
| 127 | |
| 128 | sum.r += dot(q0, k0); |
| 129 | sum.g += dot(q1, k0); |
| 130 | sum.b += dot(q2, k0); |
| 131 | sum.a += dot(q3, k0); |
| 132 | } |
| 133 | |
| 134 | ai += LOCAL_MEMORY_UNROLL_INCH * psc(M) * 4; |
| 135 | bi += LOCAL_MEMORY_UNROLL_INCH * psc(N); |
| 136 | |
| 137 | barrier(); |
| 138 | } |
| 139 | |
| 140 | if (k < NN) |
| 141 | { |
| 142 | const int remain = NN - k; |
| 143 | |
| 144 | if (lx < remain) |
| 145 | { |
| 146 | tmp_q[ly][lx][0] = sfp2lfpvec4(q_blob_data[ai]); |
| 147 | tmp_q[ly][lx][1] = sfp2lfpvec4(q_blob_data[ai + 1]); |
| 148 | tmp_q[ly][lx][2] = sfp2lfpvec4(q_blob_data[ai + 2]); |
| 149 | tmp_q[ly][lx][3] = sfp2lfpvec4(q_blob_data[ai + 3]); |
| 150 | } |
| 151 | |
| 152 | if (ly < remain) |
| 153 | { |
| 154 | tmp_k[lx][ly] = sfp2lfpvec4(k_blob_data[bi]); |
| 155 | } |
| 156 | |
| 157 | barrier(); |
| 158 | |
| 159 | for (int k4 = 0; k4 < remain; k4++) |
| 160 | { |
| 161 | afpvec4 q0 = lfp2afpvec4(tmp_q[ly][k4][0]); |
| 162 | afpvec4 q1 = lfp2afpvec4(tmp_q[ly][k4][1]); |
| 163 | afpvec4 q2 = lfp2afpvec4(tmp_q[ly][k4][2]); |
| 164 | afpvec4 q3 = lfp2afpvec4(tmp_q[ly][k4][3]); |
| 165 | |
| 166 | afpvec4 k0 = lfp2afpvec4(tmp_k[lx][k4]); |
| 167 | |
| 168 | sum.r += dot(q0, k0); |
| 169 | sum.g += dot(q1, k0); |
| 170 | sum.b += dot(q2, k0); |
| 171 | sum.a += dot(q3, k0); |
| 172 | } |
| 173 | } |
| 174 | #else |
| 175 | for (int k = 0; k < psc(K); k++) |
| 176 | { |
| 177 | #if NCNN_image_shader |
| 178 | afpvec4 q0 = image3d_ld4(q_blob_3d, ivec3(gy * 4, gz * psc(K) + k, 0)); |
| 179 | afpvec4 q1 = image3d_ld4(q_blob_3d, ivec3(gy * 4 + 1, gz * psc(K) + k, 0)); |
| 180 | afpvec4 q2 = image3d_ld4(q_blob_3d, ivec3(gy * 4 + 2, gz * psc(K) + k, 0)); |
| 181 | afpvec4 q3 = image3d_ld4(q_blob_3d, ivec3(gy * 4 + 3, gz * psc(K) + k, 0)); |
| 182 | |
| 183 | afpvec4 k0 = image3d_ld4(k_blob_3d, ivec3(gx, gz * psc(K) + k, 0)); |
| 184 | #else |
| 185 | const int ai = (gz * psc(M) * psc(K) + k * psc(M) + gy) * 4; |
| 186 | afpvec4 q0 = buffer_ld4(q_blob_data, ai); |
| 187 | afpvec4 q1 = buffer_ld4(q_blob_data, ai + 1); |
| 188 | afpvec4 q2 = buffer_ld4(q_blob_data, ai + 2); |
| 189 | afpvec4 q3 = buffer_ld4(q_blob_data, ai + 3); |
| 190 | |
| 191 | const int bi = gz * psc(N) * psc(K) + k * psc(N) + gx; |
| 192 | afpvec4 k0 = buffer_ld4(k_blob_data, bi); |
| 193 | #endif |
| 194 | |
| 195 | sum.r += dot(q0, k0); |
| 196 | sum.g += dot(q1, k0); |
| 197 | sum.b += dot(q2, k0); |
| 198 | sum.a += dot(q3, k0); |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | #if NCNN_shader_local_memory |
| 203 | if (gx >= psc(N) || gy >= psc(M) || gz >= psc(B)) |
| 204 | return; |
| 205 | #endif |
| 206 | |
| 207 | #if NCNN_image_shader |
| 208 | image3d_st4(qkcross_blob_3d, ivec3(gx, gz * psc(M) + gy, 0), sum); |
| 209 | #else |
| 210 | const int gi = gz * psc(M) * psc(N) + gy * psc(N) + gx; |
| 211 | buffer_st4(qkcross_blob_data, gi, sum); |
| 212 | #endif |
| 213 | } |