llama-cpp-native 0.32.0

llama.cpp built from its sources with CMake, the native library llama-cpp-bindings-sys links against
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#version 450

#extension GL_EXT_control_flow_attributes : enable
#extension GL_EXT_shader_16bit_storage : require
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_shader_subgroup_basic : enable
#extension GL_KHR_shader_subgroup_arithmetic : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shared_memory_block : enable

layout (local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;

layout (binding = 0) readonly buffer PV4 {f16vec4 data_pv4[];};
layout (binding = 1) readonly buffer V {float16_t data_v[];};
layout (binding = 1) readonly buffer VV4 {f16vec4 data_vv4[];};
layout (binding = 2) readonly buffer MAX {float data_max[];};
layout (binding = 3) readonly buffer S {float data_s[];};
layout (binding = 4) buffer OV4 {vec4 data_ov4[];};

layout (push_constant) uniform parameter
{
    uint neq1;
    uint neq2;
    uint neq3;
    uint nek1;
    uint nek2;
    uint nek3;
    uint nev2;
    uint nev3;
    uint nem1;
    uint nem2;
    uint nem3;
    uint nbq1;
    uint nbq2;
    uint nbk1;
    uint nbk2;
    uint nbv1;
    uint nbv2;
    uint nbq3;
    uint nbk3;
    uint nbv3;
    uint nb3;
    uint flag;
    float scale;
} p;

layout (constant_id = 0) const uint WorkGroupSize = 256;
layout (constant_id = 1) const uint gqa_ratio = 8;
layout (constant_id = 2) const uint HSV = 128;
layout (constant_id = 3) const uint Wr = 1;
layout (constant_id = 4) const uint SubGroupSize = 16;
layout (constant_id = 5) const uint p_per_loop = 64;
layout (constant_id = 6) const uint p_reduce = 32;
layout (constant_id = 7) const uint Bc = 16;
layout (constant_id = 8) const uint TM = 8;
layout (constant_id = 9) const uint TN = 16;
layout (constant_id = 10) const uint Flags = 0;

#define UNALIGNED_SEQ_LEN_BIT (1 << 0)
#define SINK_ENABLE_BIT (1 << 16)

const uint TK = 16;
const uint o_row = gqa_ratio * Wr;
const uint omat_row = (o_row + TN - 1) / TN;
const uint o_scalar_row = (o_row + SubGroupSize - 1) / SubGroupSize;

const uint omat_col = Bc / TM;
const uint v_head_wgs = HSV / Bc;

const uint sg_count = WorkGroupSize / SubGroupSize;
const uint split_p_sg = p_per_loop / TK;
const uint p_loop_col = WorkGroupSize * 4;
const uint p_loops = (o_row * p_per_loop + p_loop_col - 1) / p_loop_col;

const uint size_max = o_row * p_per_loop / p_reduce;
const uint max_loops = (size_max + WorkGroupSize - 1) / WorkGroupSize;
const uint global_max_loops = (o_row + sg_count - 1) / sg_count;

#define SLM_POOL_SIZE_GLOBAL_MAX ((global_max_loops * sg_count + SubGroupSize - 1) / SubGroupSize * SubGroupSize)
#define SLM_POOL_SIZE_O (sg_count * omat_row * TN * omat_col * TM)
#define SLM_POOL_SIZE_MAX (max_loops * WorkGroupSize)
#define SLM_POOL_SIZE_SOFTMAX_SUM ((sg_count * p_loops + SubGroupSize - 1) / SubGroupSize * SubGroupSize)
#define SLM_POOL_SIZE_OOB_STAGING (sg_count * TM * TK / 4)

const uint psh_size = (p_loops * p_loop_col < omat_row * TN * p_per_loop) ? (omat_row * TN * p_per_loop) : (p_loops * p_loop_col);
const uint psh_base = SLM_POOL_SIZE_GLOBAL_MAX * 2 + SLM_POOL_SIZE_SOFTMAX_SUM * 2 + SLM_POOL_SIZE_MAX * 2 * 2;

const uint global_maxsh_offset = 0;
const uint lfsh_offset = global_maxsh_offset + SLM_POOL_SIZE_GLOBAL_MAX;
const uint osh_offset = lfsh_offset + SLM_POOL_SIZE_SOFTMAX_SUM;
const uint maxsh_offset = SLM_POOL_SIZE_GLOBAL_MAX + SLM_POOL_SIZE_SOFTMAX_SUM;

const uint p_v4 = p_per_loop / 4;
const uint max_per_loop = p_per_loop / p_reduce;
const uint maxsh_stride = p_reduce / 4;
const uint sg_per_line = p_per_loop / SubGroupSize / 4;
const bool seq_boundary_check = (Flags & UNALIGNED_SEQ_LEN_BIT) != 0;

shared sh_block {
    float F32sh[SLM_POOL_SIZE_GLOBAL_MAX + SLM_POOL_SIZE_SOFTMAX_SUM + SLM_POOL_SIZE_O];
} F32Poolsh;

shared shf16_alias {
    float16_t F16sh[SLM_POOL_SIZE_GLOBAL_MAX * 2 + SLM_POOL_SIZE_SOFTMAX_SUM * 2 + SLM_POOL_SIZE_MAX * 2 * 2 + psh_size * 2];
} F16Poolsh;

shared shf16v4_alias {
    f16vec4 F16V4sh[(SLM_POOL_SIZE_GLOBAL_MAX + SLM_POOL_SIZE_SOFTMAX_SUM + SLM_POOL_SIZE_MAX * 2) / 2 + SLM_POOL_SIZE_OOB_STAGING];
} F16V4Poolsh;

void main() {
    const uint lane = gl_SubgroupInvocationID;
    const uint head_v_idx = gl_WorkGroupID.y;
    const uint batch_v_ratio = p.neq3 / p.nev3;
    const uint batch_idx = gl_WorkGroupID.z;
    const uint batch_v_idx = batch_idx / batch_v_ratio;
    const uint v_idx = gl_WorkGroupID.x % v_head_wgs;
    const uint out_row_idx = gl_WorkGroupID.x / v_head_wgs;
    const uint sg = gl_SubgroupID;
    const uint wg_lane = sg * SubGroupSize + lane;
    const uint max_dim = (p.nek1 + p_reduce - 1) / p_reduce;
    const uint p_dim = max_dim * p_reduce;
    const uint v_dim = p.nbv1;
    const uint o_dim = p.neq2 * HSV;
    const uint p_base = (batch_idx * p.neq1 * p.neq2 + head_v_idx * gqa_ratio + out_row_idx * Wr * p.neq2) * p_dim / 4;
    const uint max_base = (batch_idx * p.neq1 * p.neq2 + head_v_idx * gqa_ratio + out_row_idx * Wr * p.neq2) * max_dim;
    const uint v_base = batch_v_idx * p.nbv3 + head_v_idx * p.nbv2 + v_idx * Bc + sg * TK * v_dim;
    const uint o_base = (batch_idx * p.nb3 + v_idx * Bc + head_v_idx * gqa_ratio * HSV + out_row_idx * o_dim * Wr) / 4;
    const uint psh_offset = psh_base + wg_lane * 4;
    const float NEG_FLT_MAX_OVER_2 = uintBitsToFloat(0xFEFFFFFF);
    const float POS_FLT_MAX_OVER_2 = uintBitsToFloat(0x7EFFFFFF);
    uint v_offset = v_base;

    coopmat<float, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> OMat[omat_row][omat_col];
    f16vec4 pv4_temp[p_loops];

    float eMf[p_loops];

    uint p_row_base[p_loops];
    uint p_col_base[p_loops];
    float Lf[p_loops];
    float global_max[p_loops];
    uint max_row_base[max_loops];
    uint max_col_base[max_loops];
    uint o_offset[o_scalar_row];
    bool o_mask[o_scalar_row];
    float sink[o_scalar_row];

    [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
        const uint flat_offset = r * SubGroupSize + lane;
        const uint row_idx = flat_offset / gqa_ratio;
        const uint col_idx = flat_offset % gqa_ratio;
        o_mask[r] = (Wr * out_row_idx + row_idx < p.neq1) && (col_idx < gqa_ratio) && (row_idx < Wr);
        o_offset[r] = (row_idx * o_dim + col_idx * HSV) / 4;
        if ((p.flag & SINK_ENABLE_BIT) != 0) {
            sink[r] = data_s[col_idx + head_v_idx * gqa_ratio];
        }
    }

    if (p.nek1 == 0) {
        if (sg == 0) {
            [[unroll]] for (uint c = 0; c < Bc / 4; ++c) {
                [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
                    if (o_mask[r]) {
                        data_ov4[o_base + o_offset[r] + c] = vec4(0.0f, 0.0f, 0.0f, 0.0f);
                    }
                }
            }
        }

        return;
    }

    [[unroll]] for (uint idx = 0; idx < global_max_loops; ++idx) {
        const uint flat_offset = idx * sg_count + sg;
        const uint row_idx = flat_offset % gqa_ratio;
        const uint tok_idx = flat_offset / gqa_ratio;

        if (tok_idx < Wr) {
            float Maxf = NEG_FLT_MAX_OVER_2;
            const uint max_offset = max_base + (tok_idx * p.neq2 + row_idx) * max_dim;
            [[unroll]] for (uint max_reduce = 0; max_reduce < (max_dim + SubGroupSize - 1) / SubGroupSize; ++max_reduce) {
                if (max_reduce * SubGroupSize + lane < max_dim) {
                    Maxf = max(Maxf, data_max[max_offset + max_reduce * SubGroupSize + lane]);
                }
            }
            Maxf = subgroupMax(Maxf);
            if (lane == 0) {
                F32Poolsh.F32sh[global_maxsh_offset + idx * sg_count + sg] = Maxf;
            }
        } else {
            if (lane == 0) {
                F32Poolsh.F32sh[global_maxsh_offset + idx * sg_count + sg] = POS_FLT_MAX_OVER_2;
            }
        }
    }

    barrier();

    [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
        const uint flat_offset = (idx * WorkGroupSize + wg_lane) / p_v4;
        const uint row_idx = flat_offset % gqa_ratio;
        const uint tok_idx = min(flat_offset / gqa_ratio, Wr - 1);
        p_col_base[idx] = (idx * WorkGroupSize + wg_lane) % p_v4;
        p_row_base[idx] = (tok_idx * p.neq2 + row_idx);
        Lf[idx] = 0.0f;
        global_max[idx] = F32Poolsh.F32sh[global_maxsh_offset + flat_offset];
    }

    [[unroll]] for (uint idx = 0; idx < max_loops; ++idx) {
        const uint flat_offset = (idx * WorkGroupSize + wg_lane) / max_per_loop;
        const uint row_idx = flat_offset % gqa_ratio;
        const uint tok_idx = min(flat_offset / gqa_ratio, Wr - 1);
        max_col_base[idx] = (idx * WorkGroupSize + wg_lane) % max_per_loop;
        max_row_base[idx] = (tok_idx * p.neq2 + row_idx);
    }

    [[unroll]] for (uint idx = 0; idx < max_loops; ++idx) {
        const uint flat_offset = max_row_base[idx] * max_dim + max_col_base[idx];
        if (seq_boundary_check) {
            F32Poolsh.F32sh[maxsh_offset + idx * WorkGroupSize + wg_lane] =
                (max_col_base[idx] < max_dim) ? data_max[max_base + flat_offset] : NEG_FLT_MAX_OVER_2;
        } else {
            F32Poolsh.F32sh[maxsh_offset + idx * WorkGroupSize + wg_lane] = data_max[max_base + flat_offset];
        }
        max_col_base[idx] = max_col_base[idx] + p_per_loop / p_reduce;
    }

    [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
        const uint flat_offset = p_row_base[idx] * p_dim / 4 + p_col_base[idx];
        if (seq_boundary_check) {
            pv4_temp[idx] = (p_col_base[idx] < p_dim / 4) ? data_pv4[p_base + flat_offset] : f16vec4(0.0f, 0.0f, 0.0f, 0.0f);
        } else {
            pv4_temp[idx] = data_pv4[p_base + flat_offset];
        }
    }

    [[unroll]] for (uint r = 0; r < omat_row; ++r) {
        [[unroll]] for (uint c = 0; c < omat_col; ++c) {
            OMat[r][c] = coopmat<float, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>(0.0f);
        }
    }

    barrier();

    [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
        const uint max_offset = (idx * WorkGroupSize + wg_lane) / maxsh_stride;
        eMf[idx] = F32Poolsh.F32sh[maxsh_offset + max_offset];
        float p_temp[4] = float[4](pv4_temp[idx].x, pv4_temp[idx].y, pv4_temp[idx].z, pv4_temp[idx].w);
        float eMf_temp = exp(eMf[idx] - global_max[idx]);
        [[unroll]] for (uint i = 0; i < 4; ++i) {
            p_temp[i] = p_temp[i] * eMf_temp;
            Lf[idx] = Lf[idx] + p_temp[i];
            F16Poolsh.F16sh[psh_offset + idx * WorkGroupSize * 4 + i] = float16_t(p_temp[i]);
        }
        p_col_base[idx] = p_col_base[idx] + p_v4;
    }

    const uint loop_count = seq_boundary_check ? ((p.nek1 + p_per_loop - 1) / p_per_loop - 1) : (p.nek1 / p_per_loop);
    uint sh_pingpong_load = 0;
    uint sh_pingpong_store = 0;

    for (uint loop = 0; loop < loop_count; ++loop) {
        sh_pingpong_load = loop & 0x1;
        sh_pingpong_store = (loop + 1) & 0x1;

        if (seq_boundary_check || loop + 1 < loop_count) {
            [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
                const uint flat_offset = p_row_base[idx] * p_dim / 4 + p_col_base[idx];
                if (seq_boundary_check) {
                    pv4_temp[idx] = (p_col_base[idx] < p_dim / 4) ? data_pv4[p_base + flat_offset] : f16vec4(0.0f, 0.0f, 0.0f, 0.0f);
                } else {
                    pv4_temp[idx] = data_pv4[p_base + flat_offset];
                }
            }

            [[unroll]] for (uint idx = 0; idx < max_loops; ++idx) {
                const uint flat_offset = max_row_base[idx] * max_dim + max_col_base[idx];
                if (seq_boundary_check) {
                    F32Poolsh.F32sh[maxsh_offset + sh_pingpong_store * SLM_POOL_SIZE_MAX + idx * WorkGroupSize + wg_lane] =
                        (max_col_base[idx] < max_dim) ? data_max[max_base + flat_offset] : NEG_FLT_MAX_OVER_2;
                } else {
                    F32Poolsh.F32sh[maxsh_offset + sh_pingpong_store * SLM_POOL_SIZE_MAX + idx * WorkGroupSize + wg_lane] = data_max[max_base + flat_offset];
                }
                max_col_base[idx] = max_col_base[idx] + p_per_loop / p_reduce;
            }
        }

        barrier();

        {
            const uint coord_p = psh_base + sh_pingpong_load * psh_size + sg * TK;
            coopmat<float16_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> VMat[omat_col];
            [[unroll]] for (uint c = 0; c < omat_col; ++c) {
                coopMatLoad(VMat[c], data_v, v_offset + TM * c, v_dim, gl_CooperativeMatrixLayoutColumnMajor);
            }
            [[unroll]] for (uint r = 0; r < omat_row; ++r) {
                coopmat<float16_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> PMat;
                coopMatLoad(PMat, F16Poolsh.F16sh, coord_p + r * TN * p_per_loop, p_per_loop, gl_CooperativeMatrixLayoutColumnMajor);

                [[unroll]] for (uint c = 0; c < omat_col; ++c) {
                    OMat[r][c] = coopMatMulAdd(VMat[c], PMat, OMat[r][c]);
                }
            }
        }

        v_offset += p_per_loop * v_dim;

        if (seq_boundary_check || loop + 1 < loop_count) {
            [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
                const uint max_offset = (idx * WorkGroupSize + wg_lane) / maxsh_stride;
                eMf[idx] = F32Poolsh.F32sh[maxsh_offset + sh_pingpong_store * SLM_POOL_SIZE_MAX + max_offset];
                float p_temp[4] = float[4](pv4_temp[idx].x, pv4_temp[idx].y, pv4_temp[idx].z, pv4_temp[idx].w);
                float eMf_temp = exp(eMf[idx] - global_max[idx]);
                [[unroll]] for (uint i = 0; i < 4; ++i) {
                    p_temp[i] = p_temp[i] * eMf_temp;
                    Lf[idx] = Lf[idx] + p_temp[i];
                    F16Poolsh.F16sh[psh_offset + sh_pingpong_store * psh_size + idx * WorkGroupSize * 4 + i] = float16_t(p_temp[i]);
                }
                p_col_base[idx] = p_col_base[idx] + p_v4;
            }
        }
    }

    barrier();

    // last loop for oob handling
    if (seq_boundary_check) {
        const uint coord_p = psh_base + sh_pingpong_store * psh_size + sg * TK;
        coopmat<float16_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> VMat[omat_col];
        coopmat<float16_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> PMat[omat_row];

        [[unroll]] for (uint r = 0; r < omat_row; ++r) {
            coopMatLoad(PMat[r], F16Poolsh.F16sh, coord_p + r * TN * p_per_loop, p_per_loop, gl_CooperativeMatrixLayoutColumnMajor);
        }

        barrier();
        const uint v_global_coord_base = sg * TK + loop_count * p_per_loop;
        const uint v_global_offset = (v_base + loop_count * p_per_loop * p.nbv1) / 4;
        const uint vv4sh_base = psh_base / 4 + sg * TM * TK / 4;
        [[unroll]] for (uint c = 0; c < omat_col; ++c) {
            [[unroll]] for (uint idx = 0; idx < TM * TK / SubGroupSize / 4; ++idx) {
                const uint vv4_idx = idx * SubGroupSize + lane;
                const uint row_idx = vv4_idx / (TM / 4);
                const uint col_idx = vv4_idx % (TM / 4);
                const uint vv4_offset = v_global_offset + row_idx * p.nbv1 / 4 + c * TM / 4 + col_idx;

                f16vec4 vv4_temp = (v_global_coord_base + row_idx < p.nek1) ? data_vv4[vv4_offset] : f16vec4(0.0f, 0.0f, 0.0f, 0.0f);
                F16V4Poolsh.F16V4sh[vv4sh_base + vv4_idx] = vv4_temp;
            }

            subgroupBarrier();
            coopMatLoad(VMat[c], F16V4Poolsh.F16V4sh, vv4sh_base, TM / 4, gl_CooperativeMatrixLayoutColumnMajor);
            subgroupBarrier();
        }

        [[unroll]] for (uint r = 0; r < omat_row; ++r) {
            [[unroll]] for (uint c = 0; c < omat_col; ++c) {
                OMat[r][c] = coopMatMulAdd(VMat[c], PMat[r], OMat[r][c]);
            }
        }
    }

    barrier();

    [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
        Lf[idx] = subgroupAdd(Lf[idx]);
    }

    [[unroll]] for (uint r = 0; r < omat_row; ++r) {
        [[unroll]] for (uint c = 0; c < omat_col; ++c) {
            const uint coord = osh_offset + r * TN * Bc + TM * c + sg * omat_row * TN * Bc;
            coopMatStore(OMat[r][c], F32Poolsh.F32sh, coord, Bc, gl_CooperativeMatrixLayoutColumnMajor);
        }
    }

    [[unroll]] for (uint idx = 0; idx < p_loops; ++idx) {
        if (lane == 0) {
            F32Poolsh.F32sh[lfsh_offset + idx * sg_count + sg] = Lf[idx];
        }
    }

    barrier();

    if (sg == 1) {
        const uint lf_base = lfsh_offset + lane * sg_per_line;
        float Lfrcp_temp[o_scalar_row][sg_per_line];
        [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
            [[unroll]] for (uint reduce = 0; reduce < sg_per_line; ++reduce) {
                Lfrcp_temp[r][reduce] = F32Poolsh.F32sh[lf_base + r * SubGroupSize * sg_per_line + reduce];
            }
        }

        [[unroll]] for (uint pm = 0; pm < o_scalar_row; ++pm) {
            [[unroll]] for (uint reduce = 1; reduce < sg_per_line; ++reduce) {
                Lfrcp_temp[pm][0] = Lfrcp_temp[pm][0] + Lfrcp_temp[pm][reduce];
            }
        }

        subgroupBarrier();

        if ((p.flag & SINK_ENABLE_BIT) != 0) {
            [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
                float Mf = F32Poolsh.F32sh[global_maxsh_offset + r * SubGroupSize + lane];
                float sink_emf = Mf - sink[r];
                sink_emf = exp(sink_emf);
                float Lf_sink = Lfrcp_temp[r][0] * sink_emf;
                Lfrcp_temp[r][0] = Lfrcp_temp[r][0] + 1.0f / sink_emf;
                Lfrcp_temp[r][0] = 1.0f / Lfrcp_temp[r][0];
                sink_emf = sink_emf / (1.0f + Lf_sink);
                Lfrcp_temp[r][0] = (Mf < sink[r]) ? sink_emf : Lfrcp_temp[r][0];
                F32Poolsh.F32sh[lfsh_offset + r * SubGroupSize + lane] = Lfrcp_temp[r][0];
            }
        } else {
            [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
                F32Poolsh.F32sh[lfsh_offset + r * SubGroupSize + lane] = (Lfrcp_temp[r][0] == 0.0f) ? 0.0f : (1.0f / Lfrcp_temp[r][0]);
            }
        }
    }

    [[unroll]] for (uint reduce = 2; reduce < split_p_sg; reduce = reduce << 1) {
        const uint stride = (reduce >> 1) * omat_row * TN * omat_col * TM;
        if ((sg % reduce) == 0) {
            const uint reduce_base = sg * omat_row * TN * omat_col * TM + osh_offset;
            float sum_temp0[4];
            float sum_temp1[4];
            const uint reduce_count = omat_row * TN * omat_col * TM / 4 / SubGroupSize;
            [[unroll]] for (uint idx = 0; idx < reduce_count; ++idx) {
                [[unroll]] for (uint i = 0; i < 4; ++i) {
                    sum_temp0[i] = F32Poolsh.F32sh[reduce_base + idx * 4 * SubGroupSize + 4 * lane + i];
                    sum_temp1[i] = F32Poolsh.F32sh[reduce_base + stride + idx * 4 * SubGroupSize + 4 * lane + i];
                }

                [[unroll]] for (uint i = 0; i < 4; ++i) {
                    sum_temp0[i] = sum_temp0[i] + sum_temp1[i];
                }

                [[unroll]] for (uint i = 0; i < 4; ++i) {
                    F32Poolsh.F32sh[reduce_base + idx * 4 * SubGroupSize + 4 * lane + i] = sum_temp0[i];
                }
            }
        }
        barrier();
    }

    if (sg == 0) {
        const uint sh_base0 = osh_offset + lane * Bc;
        const uint sh_base1 = sh_base0 + split_p_sg / 2 * omat_row * TN * omat_col * TM;

        float Lfrcp[o_scalar_row];
        float Of[o_scalar_row][4];
        [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
            Lfrcp[r] = F32Poolsh.F32sh[lfsh_offset + r * SubGroupSize + lane];
        }

        [[unroll]] for (uint c = 0; c < Bc / 4; ++c) {
            [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
                [[unroll]] for (uint vc = 0; vc < 4; ++vc) {
                    Of[r][vc] = F32Poolsh.F32sh[sh_base0 + r * SubGroupSize * Bc + c * 4 + vc] * Lfrcp[r];
                    Of[r][vc] = Of[r][vc] + F32Poolsh.F32sh[sh_base1 + r * SubGroupSize * Bc + c * 4 + vc] * Lfrcp[r];
                }
            }

            [[unroll]] for (uint r = 0; r < o_scalar_row; ++r) {
                if (o_mask[r]) {
                    data_ov4[o_base + o_offset[r] + c] = vec4(Of[r][0], Of[r][1], Of[r][2], Of[r][3]);
                }
            }
        }
    }
}