Elffuss BraidHub

kikocis/ncnn public

master · 5.0 KiBRawPermalink
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
26layout (constant_id = 0) const int M = 0;
27layout (constant_id = 1) const int N = 0;
28layout (constant_id = 2) const int K = 0;
29layout (constant_id = 3) const int B = 0;
30
31#if NCNN_image_shader
32layout (binding = 0) uniform unfp sampler3D qkcross_blob_3d;
33layout (binding = 1) uniform unfp sampler3D v_blob_3d;
34layout (binding = 2, imfmtc4) writeonly uniform unfp image3D qkvcross_blob_3d;
35#else
36layout (binding = 0) readonly buffer qkcross_blob { sfpvec4 qkcross_blob_data[]; };
37layout (binding = 1) readonly buffer v_blob { sfpvec4 v_blob_data[]; };
38layout (binding = 2) writeonly buffer qkvcross_blob { sfpvec4 qkvcross_blob_data[]; };
39#endif
40
41layout (push_constant) uniform parameter
42{
43 int M;
44 int N;
45 int K;
46 int B;
47} p;
48
49#if NCNN_shader_local_memory
50shared lfpvec4 tmp_qk[8][LOCAL_MEMORY_UNROLL_INCH];
51shared lfpvec4 tmp_v[8][LOCAL_MEMORY_UNROLL_INCH];
52#endif
53
54void main()
55{
56 int gx = int(gl_GlobalInvocationID.x);
57 int gy = int(gl_GlobalInvocationID.y);
58 int gz = int(gl_GlobalInvocationID.z);
59
60#if !NCNN_shader_local_memory
61 if (gx >= psc(N) || gy >= psc(M) || gz >= psc(B))
62 return;
63#endif
64
65 afpvec4 sum0 = afpvec4(0.f);
66 afpvec4 sum1 = afpvec4(0.f);
67 afpvec4 sum2 = afpvec4(0.f);
68 afpvec4 sum3 = afpvec4(0.f);
69
70#if !NCNN_image_shader && NCNN_shader_local_memory
71 const int NN = psc(K);
72
73 const int lx = int(gl_LocalInvocationID.x);
74 const int ly = int(gl_LocalInvocationID.y);
75
76 int ai = gz * psc(M) * psc(K) + gy * psc(K) + lx;
77 int bi = gz * psc(N) * psc(K) + gx * psc(K) + ly;
78
79 int k = 0;
80 for (; k + (LOCAL_MEMORY_UNROLL_INCH - 1) < NN; k += LOCAL_MEMORY_UNROLL_INCH)
81 {
82 {
83 tmp_qk[ly][lx] = sfp2lfpvec4(qkcross_blob_data[ai]);
84 }
85
86 {
87 tmp_v[lx][ly] = sfp2lfpvec4(v_blob_data[bi]);
88 }
89
90 barrier();
91
92 for (int k4 = 0; k4 < LOCAL_MEMORY_UNROLL_INCH; k4++)
93 {
94 afpvec4 qk0 = lfp2afpvec4(tmp_qk[ly][k4]);
95
96 afpvec4 v0 = lfp2afpvec4(tmp_v[lx][k4]);
97
98 sum0 += qk0.r * v0;
99 sum1 += qk0.g * v0;
100 sum2 += qk0.b * v0;
101 sum3 += qk0.a * v0;
102 }
103
104 ai += LOCAL_MEMORY_UNROLL_INCH;
105 bi += LOCAL_MEMORY_UNROLL_INCH;
106
107 barrier();
108 }
109
110 if (k < NN)
111 {
112 const int remain = NN - k;
113
114 if (lx < remain)
115 {
116 tmp_qk[ly][lx] = sfp2lfpvec4(qkcross_blob_data[ai]);
117 }
118
119 if (ly < remain)
120 {
121 tmp_v[lx][ly] = sfp2lfpvec4(v_blob_data[bi]);
122 }
123
124 barrier();
125
126 for (int k4 = 0; k4 < remain; k4++)
127 {
128 afpvec4 qk0 = lfp2afpvec4(tmp_qk[ly][k4]);
129
130 afpvec4 v0 = lfp2afpvec4(tmp_v[lx][k4]);
131
132 sum0 += qk0.r * v0;
133 sum1 += qk0.g * v0;
134 sum2 += qk0.b * v0;
135 sum3 += qk0.a * v0;
136 }
137 }
138#else
139 for (int k = 0; k < psc(K); k++)
140 {
141#if NCNN_image_shader
142 afpvec4 qk0 = image3d_ld4(qkcross_blob_3d, ivec3(k, gz * psc(M) + gy, 0));
143 afpvec4 v0 = image3d_ld4(v_blob_3d, ivec3(k, gz * psc(N) + gx, 0));
144#else
145 const int ai = gz * psc(M) * psc(K) + gy * psc(K) + k;
146 afpvec4 qk0 = buffer_ld4(qkcross_blob_data, ai);
147
148 const int bi = gz * psc(N) * psc(K) + gx * psc(K) + k;
149 afpvec4 v0 = buffer_ld4(v_blob_data, bi);
150#endif
151
152 sum0 += qk0.r * v0;
153 sum1 += qk0.g * v0;
154 sum2 += qk0.b * v0;
155 sum3 += qk0.a * v0;
156 }
157#endif
158
159#if NCNN_shader_local_memory
160 if (gx >= psc(N) || gy >= psc(M) || gz >= psc(B))
161 return;
162#endif
163
164#if NCNN_image_shader
165 image3d_st4(qkvcross_blob_3d, ivec3(gy * 4, gz * psc(N) + gx, 0), sum0);
166 image3d_st4(qkvcross_blob_3d, ivec3(gy * 4 + 1, gz * psc(N) + gx, 0), sum1);
167 image3d_st4(qkvcross_blob_3d, ivec3(gy * 4 + 2, gz * psc(N) + gx, 0), sum2);
168 image3d_st4(qkvcross_blob_3d, ivec3(gy * 4 + 3, gz * psc(N) + gx, 0), sum3);
169#else
170 const int gi = (gz * psc(M) * psc(N) + gx * psc(M) + gy) * 4;
171
172 buffer_st4(qkvcross_blob_data, gi, sum0);
173 buffer_st4(qkvcross_blob_data, gi + 1, sum1);
174 buffer_st4(qkvcross_blob_data, gi + 2, sum2);
175 buffer_st4(qkvcross_blob_data, gi + 3, sum3);
176#endif
177}