okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
3.8 KiB68 linesraw
1// metal/kernels/gemvu.metal - gemv.metal's kernel with the unit loop unrolled UF times, the weights of the UF units decoded (their
2// loads issued) before any of them is applied.  The sums are accumulated in the original's order (unit u, then u + LPR, ...), so the
3// result is bitwise gemv's; what changes is that a lane waits one memory latency for UF units instead of one for each.  After gemv.metal.
4template <int F, int LPR, int NR, int SG, int KS, int UF>
5kernel void gemv_u(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],
6                   device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],
7                   uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
8                   ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
9    constexpr int UNIT = fmt<F>::UNIT, RS = SG / KS, RG = 32 / LPR;
10    threadgroup uint tab[fmt<F>::TAB];
11    threadgroup float red[KS > 1 ? KS * RS * RG * NR * 8 : 1];
12    load_tab<F>(tab, tid, SG * 32);
13    threadgroup_barrier(mem_flags::mem_threadgroup);
14    const int ks = sg / RS, rs = sg % RS;
15    const int li = lane % LPR;
16    const int lrow0 = (rs * RG + lane / LPR) * NR;
17    const int row0 = int(tg.x) * RS * RG * NR + lrow0;
18    const int U = int(a.n_in) / UNIT;
19    const int u0 = ks * U / KS, u1 = (ks + 1) * U / KS;
20    float acc[NR][8];
21    for (int r = 0; r < NR; ++r)
22        for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;
23    device const uchar* rp[NR];
24    for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;
25    const int s16n = int(a.n_in) / 16;
26    for (int u = u0 + li; u < u1; u += LPR * UF) {
27        float q[UF][NR][UNIT];
28        float4 sc[UF][NR];
29        for (int k = 0; k < UF; ++k)
30            if (u + k * LPR < u1)
31                for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u + k * LPR, q[k][r], sc[k][r], tab);
32        for (int k = 0; k < UF; ++k)
33            if (u + k * LPR < u1) {
34                float xs[UNIT], xsum[2];
35                for (int t = 0; t < NTOK; ++t) {
36                    X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u + k * LPR, xs, xsum);
37                    for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[k][r], sc[k][r], xs, xsum);
38                }
39            }
40    }
41    constexpr int RPT = RS * RG * NR;
42    for (int r = 0; r < NR; ++r)
43        for (int t = 0; t < NTOK; ++t) {
44            const float v = lane_sum<LPR>(acc[r][t]);
45            if (KS == 1) {
46                if (li == 0 && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
47            } else if (li == 0) {
48                red[(ks * RPT + lrow0 + r) * 8 + t] = v;
49            }
50        }
51    if (KS > 1) {
52        threadgroup_barrier(mem_flags::mem_threadgroup);
53        if (ks == 0 && li == 0)
54            for (int r = 0; r < NR; ++r)
55                for (int t = 0; t < NTOK; ++t) {
56                    float v = 0.0f;
57                    for (int k = 0; k < KS; ++k) v += red[(k * RPT + lrow0 + r) * 8 + t];
58                    if (row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
59                }
60    }
61}
62
63#define GVU_(F, L, R, S, K, U) template [[host_name("gemvu_" #F "_" #L "_" #R "_" #S "_" #K "_" #U)]] kernel decltype(gemv_u<F, L, R, S, K, U>) gemv_u<F, L, R, S, K, U>;
64#define GVU(F, L, R, S, K, U) GVU_(F, L, R, S, K, U)
65#define GVU_SET(F, U) GVU(F, 16, 1, 4, 1, U) GVU(F, 32, 1, 8, 2, U) GVU(F, 8, 1, 4, 1, U) GVU(F, 32, 1, 4, 1, U)
66#define GVU_ALL(U) GVU_SET(T_F32, U) GVU_SET(T_BF16, U) GVU_SET(T_Q8_0, U) GVU_SET(T_Q4_0, U) GVU_SET(T_Q5_0, U) GVU_SET(T_IQ4_NL, U) GVU_SET(T_IQ4_XS, U) \
67                  GVU_SET(T_Q2_0, U) GVU_SET(T_IQ3_S, U) GVU_SET(T_Q3_K, U) GVU_SET(T_Q4_K, U) GVU_SET(T_Q5_K, U) GVU_SET(T_Q6_K, U)
68GVU_ALL(2)