interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-vulkan / vulkan-shaders / flash_attn_decode_phase_2.comp

408 lines · 17643 bytes Raw Blame History
1#version 450
2
3#extension GL_EXT_control_flow_attributes : enable
4#extension GL_EXT_shader_16bit_storage : require
5#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
6#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
7#extension GL_KHR_memory_scope_semantics : enable
8#extension GL_KHR_shader_subgroup_basic : enable
9#extension GL_KHR_shader_subgroup_ballot : enable
10#extension GL_KHR_shader_subgroup_arithmetic : enable
11#extension GL_KHR_cooperative_matrix : enable
12#extension GL_EXT_shared_memory_block : enable
13
14layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
15
16layout (binding = 0) readonly buffer P {f16vec4 pStateVec4[];};
17layout (binding = 1) readonly buffer V {float16_t vState[];};
18layout (binding = 1) readonly buffer V_VEC4 {f16vec4 vStateVec4[];};
19layout (binding = 2) buffer MAX_FP32 {float max_f32[];};
20layout (binding = 3) buffer SINK_FP32 {float sink_f32[];};
21layout (binding = 4) buffer OUT_FP32 {float out_f32[];};
22layout (binding = 4) buffer OUT_VEC4 {vec4 out_f32_vec4[];};
23layout (binding = 4) buffer OUT_F16 {float16_t out_f16[];};
24
25layout (push_constant) uniform parameter
26{
27 uint kvSeqLen;
28 uint activationLength;
29 uint qHead;
30 uint kvHead;
31 uint qkRatio;
32 uint qkSubGroups;
33 uint flag;
34 uint kvStride1;
35 uint kvStride2;
36 uint batchStrideQ;
37 uint batchStrideK;
38 uint batchStrideV;
39 uint batchStrideM;
40 uint batchStrideO;
41 float softMaxScale;
42} p;
43
44layout (constant_id = 0) const uint GROUPSIZE = 256;
45layout (constant_id = 1) const uint GQA_RATIO = 8;
46layout (constant_id = 2) const uint HEAD_DIM = 128;
47layout (constant_id = 3) const uint N_TOKS_PER_GROUP = 1;
48layout (constant_id = 4) const uint WARPSIZE = 16;
49layout (constant_id = 5) const uint MATP_PER_LOOP = 64;
50layout (constant_id = 6) const uint MATP_REDUCE = 32;
51layout (constant_id = 7) const uint WARP_V_DIM = 16;
52
53#define TN WARPSIZE
54#define TM 8
55#define TK 16
56#define MAT_O_N (WARP_V_DIM / TM)
57#define MAT_P_M (GQA_RATIO * N_TOKS_PER_GROUP)
58#define ALIGNED_P_M ((MAT_P_M + WARPSIZE - 1) / WARPSIZE)
59#define V_HEAD_GROUPS (HEAD_DIM / WARP_V_DIM)
60
61#define SUBGROUP_COUNT (GROUPSIZE / WARPSIZE)
62#define SPLIT_P_GROUPS (MATP_PER_LOOP / TK)
63
64#define SLM_POOL_SIZE_O (SUBGROUP_COUNT * ALIGNED_P_M * TN * MAT_O_N * TM)
65
66#define P_LOAD_PER_LOOP (GROUPSIZE * 4)
67#define P_LOAD_LOOPS ((MAT_P_M * MATP_PER_LOOP + P_LOAD_PER_LOOP - 1) / P_LOAD_PER_LOOP)
68#define SLM_POOL_SIZE_P (P_LOAD_LOOPS * P_LOAD_PER_LOOP)
69#define SIZE_LOCAL_MAX (MAT_P_M * MATP_PER_LOOP / MATP_REDUCE)
70#define MAX_LOAD_LOOPS ((SIZE_LOCAL_MAX + GROUPSIZE - 1) / GROUPSIZE)
71#define SLM_POOL_SIZE_LOCAL_MAX (MAX_LOAD_LOOPS * GROUPSIZE)
72#define MAX_REDUCE_COUNT ((MAT_P_M + SUBGROUP_COUNT - 1) / SUBGROUP_COUNT)
73#define GLOBAL_MAX_SIZE (MAX_REDUCE_COUNT * SUBGROUP_COUNT)
74
75#define SLM_POOL_SIZE_SOFTMAX_SUM (SUBGROUP_COUNT * P_LOAD_LOOPS)
76
77#define SLM_OFFSET_P (GLOBAL_MAX_SIZE * 2 + SLM_POOL_SIZE_SOFTMAX_SUM * 2 + SLM_POOL_SIZE_LOCAL_MAX * 2 * 2)
78
79#define SLM_OFFSET_GLOBAL_MAX 0
80#define SLM_OFFSET_SOFTMAX_SUM (SLM_OFFSET_GLOBAL_MAX + GLOBAL_MAX_SIZE)
81#define SLM_OFFSET_O (SLM_OFFSET_SOFTMAX_SUM + SLM_POOL_SIZE_SOFTMAX_SUM)
82#define SLM_OFFSET_LOCAL_MAX (GLOBAL_MAX_SIZE + SLM_POOL_SIZE_SOFTMAX_SUM)
83
84#define P_REDUCE_VEC4 (MATP_PER_LOOP / 4)
85#define MAX_PER_LOOP (MATP_PER_LOOP / MATP_REDUCE)
86#define SLM_MAX_STRIDE (MATP_REDUCE / 4)
87#define SUB_GROUPS_PER_LINE (MATP_PER_LOOP / WARPSIZE / 4)
88
89shared slm_pool_block {
90 float slm_pool_o[GLOBAL_MAX_SIZE + SLM_POOL_SIZE_SOFTMAX_SUM + SLM_POOL_SIZE_O];
91} slm_pool_f32;
92
93shared slm_pool_alias_block {
94 float16_t slm_pool_pv[GLOBAL_MAX_SIZE * 2 + SLM_POOL_SIZE_SOFTMAX_SUM * 2 + SLM_POOL_SIZE_LOCAL_MAX * 2 * 2 + SLM_POOL_SIZE_P * 2];
95} slm_pool_alias_f16;
96
97void main() {
98 const uint lane = gl_SubgroupInvocationID;
99 const uint v = gl_WorkGroupID.y;
100 const uint d = gl_WorkGroupID.z;
101 const uint vWarpIdx = gl_WorkGroupID.x % V_HEAD_GROUPS;
102 const uint outTokIdx = gl_WorkGroupID.x / V_HEAD_GROUPS;
103 const uint localLinearId = gl_SubgroupID;
104 const uint wgLane = localLinearId * WARPSIZE + lane;
105 const uint splitIdx = localLinearId;
106 const uint maxDim = (p.kvSeqLen + MATP_REDUCE - 1) / MATP_REDUCE;
107 const uint pDim = maxDim * MATP_REDUCE;
108 const uint kvDim = p.kvStride1;
109 const uint oDim = p.qHead * HEAD_DIM;
110 const uint offsetBaseP = (d * p.activationLength * p.qHead + v * GQA_RATIO + outTokIdx * N_TOKS_PER_GROUP * p.qHead) * pDim / 4;
111 const uint offsetBaseMax = (d * p.activationLength * p.qHead + v * GQA_RATIO + outTokIdx * N_TOKS_PER_GROUP * p.qHead) * maxDim;
112 const uint offsetBaseV = (d * p.batchStrideV + v * p.kvStride2 + vWarpIdx * WARP_V_DIM + splitIdx * TK * kvDim);
113 const uint offsetSlmP = (SLM_OFFSET_P + wgLane * 4);
114 const float fp32Min = uintBitsToFloat(0xFEFFFFFF);
115 const float fp32Max = uintBitsToFloat(0x7EFFFFFF);
116 uint offsetV = offsetBaseV;
117
118 coopmat<float, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> sums[ALIGNED_P_M][MAT_O_N];
119 f16vec4 pStateTemp[P_LOAD_LOOPS];
120
121 float fp32CompensationP[P_LOAD_LOOPS];
122
123 uint loadRowBase[P_LOAD_LOOPS];
124 uint loadColBase[P_LOAD_LOOPS];
125 float fp32SoftMaxSum[P_LOAD_LOOPS];
126 float fp32GlobalMaxP[P_LOAD_LOOPS];
127 uint maxRowBase[MAX_LOAD_LOOPS];
128 uint maxColBase[MAX_LOAD_LOOPS];
129 uint outOffsets[ALIGNED_P_M];
130 bool outputMask[ALIGNED_P_M];
131 float fp32SinkCoeff[ALIGNED_P_M];
132
133 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
134 const uint flatOffset = pm * WARPSIZE + lane;
135 const uint inGroupTokIdx = flatOffset / GQA_RATIO;
136 const uint inGroupHeadIdx = flatOffset % GQA_RATIO;
137 outputMask[pm] = (N_TOKS_PER_GROUP * outTokIdx + inGroupTokIdx < p.activationLength) && (inGroupHeadIdx < GQA_RATIO) && (inGroupTokIdx < N_TOKS_PER_GROUP);
138 outOffsets[pm] = (inGroupTokIdx * oDim + inGroupHeadIdx * HEAD_DIM) / 4;
139 if ((0x1 & p.flag) != 0) {
140 fp32SinkCoeff[pm] = sink_f32[inGroupHeadIdx + v * GQA_RATIO];
141 }
142 }
143
144 [[unroll]] for (uint maxCount = 0; maxCount < MAX_REDUCE_COUNT; maxCount++) {
145 const uint flatIdx = maxCount * SUBGROUP_COUNT + localLinearId;
146 const uint rowIdx = flatIdx % GQA_RATIO;
147 const uint tokIdx = flatIdx / GQA_RATIO;
148
149 if (tokIdx < N_TOKS_PER_GROUP) {
150 float fp32MaxReduce = fp32Min;
151 const uint maxOffset = offsetBaseMax + (tokIdx * p.qHead + rowIdx) * maxDim;
152 [[unroll]] for (uint maxReduce = 0; maxReduce < (maxDim + WARPSIZE - 1) / WARPSIZE; maxReduce++) {
153 if (maxReduce * WARPSIZE + lane < maxDim) {
154 fp32MaxReduce = max(fp32MaxReduce, max_f32[maxOffset + maxReduce * WARPSIZE + lane]);
155 }
156 }
157 fp32MaxReduce = subgroupMax(fp32MaxReduce);
158 if (lane == 0) {
159 slm_pool_f32.slm_pool_o[SLM_OFFSET_GLOBAL_MAX + maxCount * SUBGROUP_COUNT + localLinearId] = fp32MaxReduce;
160 }
161 } else {
162 if (lane == 0) {
163 slm_pool_f32.slm_pool_o[SLM_OFFSET_GLOBAL_MAX + maxCount * SUBGROUP_COUNT + localLinearId] = fp32Max;
164 }
165 }
166 }
167
168 barrier();
169
170 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
171 const uint flatOffset = (pLoad * GROUPSIZE + wgLane) / P_REDUCE_VEC4;
172 const uint rowIdxFlat = flatOffset % GQA_RATIO;
173 const uint tokenIdxFlat = min(flatOffset / GQA_RATIO, N_TOKS_PER_GROUP - 1);
174 loadColBase[pLoad] = (pLoad * GROUPSIZE + wgLane) % P_REDUCE_VEC4;
175 loadRowBase[pLoad] = (tokenIdxFlat * p.qHead + rowIdxFlat);
176 fp32SoftMaxSum[pLoad] = 0.0f;
177 fp32GlobalMaxP[pLoad] = slm_pool_f32.slm_pool_o[SLM_OFFSET_GLOBAL_MAX + flatOffset];
178 }
179
180 [[unroll]] for (uint maxLoad = 0; maxLoad < MAX_LOAD_LOOPS; maxLoad++) {
181 const uint flatOffset = (maxLoad * GROUPSIZE + wgLane) / MAX_PER_LOOP;
182 const uint rowIdxFlat = flatOffset % GQA_RATIO;
183 const uint tokenIdxFlat = min(flatOffset / GQA_RATIO, N_TOKS_PER_GROUP - 1);
184 maxColBase[maxLoad] = (maxLoad * GROUPSIZE + wgLane) % MAX_PER_LOOP;
185 maxRowBase[maxLoad] = (tokenIdxFlat * p.qHead + rowIdxFlat);
186 }
187
188 [[unroll]] for (uint maxLoad = 0; maxLoad < MAX_LOAD_LOOPS; maxLoad++) {
189 const uint flatMaxOffset = maxRowBase[maxLoad] * maxDim + maxColBase[maxLoad];
190 slm_pool_f32.slm_pool_o[SLM_OFFSET_LOCAL_MAX + maxLoad * GROUPSIZE + wgLane] = max_f32[offsetBaseMax + flatMaxOffset];
191 maxColBase[maxLoad] = maxColBase[maxLoad] + MATP_PER_LOOP / MATP_REDUCE;
192 }
193
194 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
195 const uint flatOffset = loadRowBase[pLoad] * pDim / 4 + loadColBase[pLoad];
196 pStateTemp[pLoad] = pStateVec4[offsetBaseP + flatOffset];
197 }
198
199 [[unroll]] for (uint n = 0; n < ALIGNED_P_M; n++) {
200 [[unroll]] for (uint i = 0; i < MAT_O_N; i++) {
201 sums[n][i] = coopmat<float, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>(0.0f);
202 }
203 }
204
205 barrier();
206
207 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
208 const uint maxOffset = (pLoad * GROUPSIZE + wgLane) / SLM_MAX_STRIDE;
209 if (loadColBase[pLoad] < pDim / 4) {
210 fp32CompensationP[pLoad] = slm_pool_f32.slm_pool_o[SLM_OFFSET_LOCAL_MAX + maxOffset];
211 float pTemp[4] = float[4](pStateTemp[pLoad].x, pStateTemp[pLoad].y, pStateTemp[pLoad].z, pStateTemp[pLoad].w);
212 float compTemp = exp(fp32CompensationP[pLoad] - fp32GlobalMaxP[pLoad]);
213 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
214 pTemp[kk] = pTemp[kk] * compTemp;
215 fp32SoftMaxSum[pLoad] = fp32SoftMaxSum[pLoad] + pTemp[kk];
216 slm_pool_alias_f16.slm_pool_pv[offsetSlmP + pLoad * GROUPSIZE * 4 + kk] = float16_t(pTemp[kk]);
217 }
218 } else {
219 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
220 slm_pool_alias_f16.slm_pool_pv[offsetSlmP + pLoad * GROUPSIZE * 4 + kk] = float16_t(0.0f);
221 }
222 }
223
224 loadColBase[pLoad] = loadColBase[pLoad] + P_REDUCE_VEC4;
225 }
226
227 const uint loopCount = (p.kvSeqLen + MATP_PER_LOOP - 1) / MATP_PER_LOOP;
228
229 for (uint loop = 0; loop < loopCount; loop++) {
230 const uint slmPingPongLoad = (loop & 0x1);
231 const uint slmPingPongStore = ((loop + 1) & 0x1);
232
233 if (loop + 1 < loopCount) {
234 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
235 const uint flatOffset = loadRowBase[pLoad] * pDim / 4 + loadColBase[pLoad];
236 pStateTemp[pLoad] = pStateVec4[offsetBaseP + flatOffset];
237 }
238
239 [[unroll]] for (uint maxLoad = 0; maxLoad < MAX_LOAD_LOOPS; maxLoad++) {
240 const uint flatMaxOffset = maxRowBase[maxLoad] * maxDim + maxColBase[maxLoad];
241 slm_pool_f32.slm_pool_o[SLM_OFFSET_LOCAL_MAX + slmPingPongStore * SLM_POOL_SIZE_LOCAL_MAX + maxLoad * GROUPSIZE + wgLane] = max_f32[offsetBaseMax + flatMaxOffset];
242 maxColBase[maxLoad] = maxColBase[maxLoad] + MATP_PER_LOOP / MATP_REDUCE;
243 }
244 }
245
246 barrier();
247
248 {
249 const uint coopMatOffsetP = SLM_OFFSET_P + slmPingPongLoad * SLM_POOL_SIZE_P + splitIdx * TK;
250 coopmat<float16_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> matV[MAT_O_N];
251 [[unroll]] for (uint cc = 0; cc < MAT_O_N; cc++) {
252 coopMatLoad(
253 matV[cc],
254 vState,
255 offsetV + TM * cc,
256 kvDim,
257 gl_CooperativeMatrixLayoutColumnMajor);
258 }
259 [[unroll]] for (uint mo = 0; mo < ALIGNED_P_M; mo++) {
260 coopmat<float16_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> matP;
261 coopMatLoad(
262 matP,
263 slm_pool_alias_f16.slm_pool_pv,
264 coopMatOffsetP + mo * TN * MATP_PER_LOOP,
265 MATP_PER_LOOP,
266 gl_CooperativeMatrixLayoutColumnMajor);
267
268 [[unroll]] for (uint no = 0; no < MAT_O_N; no++) {
269 sums[mo][no] = coopMatMulAdd(matV[no], matP, sums[mo][no]);
270 }
271 }
272 }
273
274 offsetV += MATP_PER_LOOP * kvDim;
275 if (loop * MATP_PER_LOOP + splitIdx * TK >= p.kvSeqLen) {
276 offsetV = 0;
277 }
278 if (loop + 1 < loopCount) {
279 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
280 const uint maxOffset = (pLoad * GROUPSIZE + wgLane) / SLM_MAX_STRIDE;
281 if (loadColBase[pLoad] < pDim / 4) {
282 fp32CompensationP[pLoad] = slm_pool_f32.slm_pool_o[SLM_OFFSET_LOCAL_MAX + slmPingPongStore * SLM_POOL_SIZE_LOCAL_MAX + maxOffset];
283 float pTemp[4] = float[4](pStateTemp[pLoad].x, pStateTemp[pLoad].y, pStateTemp[pLoad].z, pStateTemp[pLoad].w);
284 float compTemp = exp(fp32CompensationP[pLoad] - fp32GlobalMaxP[pLoad]);
285 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
286 pTemp[kk] = pTemp[kk] * compTemp;
287 fp32SoftMaxSum[pLoad] = fp32SoftMaxSum[pLoad] + pTemp[kk];
288 slm_pool_alias_f16.slm_pool_pv[offsetSlmP + slmPingPongStore * SLM_POOL_SIZE_P + pLoad * GROUPSIZE * 4 + kk] = float16_t(pTemp[kk]);
289 }
290 } else {
291 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
292 slm_pool_alias_f16.slm_pool_pv[offsetSlmP + slmPingPongStore * SLM_POOL_SIZE_P + pLoad * GROUPSIZE * 4 + kk] = float16_t(0.0f);
293 }
294 }
295 loadColBase[pLoad] = loadColBase[pLoad] + P_REDUCE_VEC4;
296 }
297 }
298 }
299
300 barrier();
301
302 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
303 fp32SoftMaxSum[pLoad] = subgroupAdd(fp32SoftMaxSum[pLoad]);
304 }
305
306 [[unroll]] for (uint mo = 0; mo < ALIGNED_P_M; mo++) {
307 [[unroll]] for (uint no = 0; no < MAT_O_N; no++) {
308 coopMatStore(
309 sums[mo][no],
310 slm_pool_f32.slm_pool_o,
311 SLM_OFFSET_O + mo * TN * WARP_V_DIM + TM * no + localLinearId * ALIGNED_P_M * TN * WARP_V_DIM,
312 WARP_V_DIM,
313 gl_CooperativeMatrixLayoutColumnMajor);
314 }
315 }
316
317 [[unroll]] for (uint pLoad = 0; pLoad < P_LOAD_LOOPS; pLoad++) {
318 slm_pool_f32.slm_pool_o[SLM_OFFSET_SOFTMAX_SUM + pLoad * SUBGROUP_COUNT + localLinearId] = fp32SoftMaxSum[pLoad];
319 }
320
321 barrier();
322
323 if (localLinearId == 1) {
324 const uint sumBase = SLM_OFFSET_SOFTMAX_SUM + lane * SUB_GROUPS_PER_LINE;
325 float sumTemp[ALIGNED_P_M][SUB_GROUPS_PER_LINE];
326 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
327 [[unroll]] for (uint reduce = 0; reduce < SUB_GROUPS_PER_LINE; reduce++) {
328 sumTemp[pm][reduce] = slm_pool_f32.slm_pool_o[sumBase + pm * WARPSIZE * SUB_GROUPS_PER_LINE + reduce];
329 }
330 }
331
332 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
333 [[unroll]] for (uint reduce = 1; reduce < SUB_GROUPS_PER_LINE; reduce++) {
334 sumTemp[pm][0] = sumTemp[pm][0] + sumTemp[pm][reduce];
335 }
336 }
337
338 if ((0x1 & p.flag) != 0) {
339 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
340 float fp32GlobalMax = slm_pool_f32.slm_pool_o[SLM_OFFSET_GLOBAL_MAX + pm * WARPSIZE + lane];
341 float sinkCompensation = fp32GlobalMax - fp32SinkCoeff[pm];
342 sinkCompensation = exp(sinkCompensation);
343 float softmaxSumTemp = sumTemp[pm][0] * sinkCompensation;
344 sumTemp[pm][0] = sumTemp[pm][0] + 1.0f / sinkCompensation;
345 sumTemp[pm][0] = 1.0f / sumTemp[pm][0];
346 sinkCompensation = sinkCompensation / (1.0f + softmaxSumTemp);
347 sumTemp[pm][0] = fp32GlobalMax < fp32SinkCoeff[pm] ? sinkCompensation : sumTemp[pm][0];
348 slm_pool_f32.slm_pool_o[SLM_OFFSET_SOFTMAX_SUM + pm * WARPSIZE + lane] = sumTemp[pm][0];
349 }
350 } else {
351 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
352 slm_pool_f32.slm_pool_o[SLM_OFFSET_SOFTMAX_SUM + pm * WARPSIZE + lane] = 1.0f / sumTemp[pm][0];
353 }
354 }
355 }
356
357 [[unroll]] for (uint reduce = 2; reduce < SPLIT_P_GROUPS; reduce = reduce << 1 ) {
358 const uint stride = (reduce >> 1) * ALIGNED_P_M * TN * MAT_O_N * TM;
359 if ((localLinearId % reduce) == 0) {
360 const uint reduceBase = localLinearId * ALIGNED_P_M * TN * MAT_O_N * TM + SLM_OFFSET_O;
361 float sumTemp0[4];
362 float sumTemp1[4];
363 const uint reduceVec4Count = ALIGNED_P_M * TN * MAT_O_N * TM / 4 / WARPSIZE;
364 [[unroll]] for (uint totalLoads = 0; totalLoads < reduceVec4Count; totalLoads++) {
365 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
366 sumTemp0[kk] = slm_pool_f32.slm_pool_o[reduceBase + totalLoads * 4 * WARPSIZE + 4 * lane + kk];
367 sumTemp1[kk] = slm_pool_f32.slm_pool_o[reduceBase + stride + totalLoads * 4 * WARPSIZE + 4 * lane + kk];
368 }
369
370 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
371 sumTemp0[kk] = sumTemp0[kk] + sumTemp1[kk];
372 }
373
374 [[unroll]] for (uint kk = 0; kk < 4; kk++) {
375 slm_pool_f32.slm_pool_o[reduceBase + totalLoads * 4 * WARPSIZE + 4 * lane + kk] = sumTemp0[kk];
376 }
377 }
378 }
379 barrier();
380 }
381
382 if (localLinearId == 0) {
383 const uint slmBase0 = SLM_OFFSET_O + lane * WARP_V_DIM;
384 const uint slmBase1 = slmBase0 + SPLIT_P_GROUPS / 2 * ALIGNED_P_M * TN * MAT_O_N * TM;
385
386 const uint offsetOutBase = (d * p.batchStrideO + vWarpIdx * WARP_V_DIM + v * GQA_RATIO * HEAD_DIM + outTokIdx * oDim * N_TOKS_PER_GROUP) / 4;
387 float fp32SoftMaxMul[ALIGNED_P_M];
388 float fp32Output[ALIGNED_P_M][4];
389 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
390 fp32SoftMaxMul[pm] = slm_pool_f32.slm_pool_o[SLM_OFFSET_SOFTMAX_SUM + pm * WARPSIZE + lane];
391 }
392
393 [[unroll]] for (uint vg = 0; vg < WARP_V_DIM / 4; vg++) {
394 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
395 [[unroll]] for (uint vc = 0; vc < 4; vc++) {
396 fp32Output[pm][vc] = slm_pool_f32.slm_pool_o[slmBase0 + pm * WARPSIZE * WARP_V_DIM + vg * 4 + vc] * fp32SoftMaxMul[pm];
397 fp32Output[pm][vc] = fp32Output[pm][vc] + slm_pool_f32.slm_pool_o[slmBase1 + pm * WARPSIZE * WARP_V_DIM + vg * 4 + vc] * fp32SoftMaxMul[pm];
398 }
399 }
400
401 [[unroll]] for (uint pm = 0; pm < ALIGNED_P_M; pm++) {
402 if (outputMask[pm] == true) {
403 out_f32_vec4[offsetOutBase + outOffsets[pm] + vg] = vec4(fp32Output[pm][0], fp32Output[pm][1], fp32Output[pm][2], fp32Output[pm][3]);
404 }
405 }
406 }
407 }
408}