okami.mom/strata

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

Niko1221Trial2 merge f-0291-mac-q (8e7ed6818)01f804ea1

main
17.8 KiB327 linesraw
1// metal/kernels/gemv.metal - y[t][r] = W[r] . x[t] for a window of 1..8 tokens (dense projections, the shared
2// expert, the LM head), any format of quant.metal/units.metal.
3//
4// Shape of a threadgroup: SG simdgroups; KS of them split the row's units (KS = 1: no split), the other SG/KS cover
5// different rows.  In a simdgroup, LPR lanes share a row and 32/LPR rows run side by side; each lane holds NR rows.
6// The weights of a unit are decoded once and applied to every token.  Partial sums of a K split are added in a
7// fixed order (deterministic).
8
9struct GemvArgs {
10    uint n_in;
11    uint n_out;
12    uint n_tok;
13    uint row_bytes;
14    uint x_stride;   // floats between tokens' x rows
15    uint y_stride;   // floats between tokens' y rows
16};
17
18constant int NTOK [[function_constant(0)]];   // tokens of the window (1..8): the token loops are specialised
19// a bench probe (function constant 9): 1 = tokens past the first reuse token 0's x (no load; wrong values),
20// 2 = tokens past the first skip the combine (wrong values)
21constant int GEMV_PROBE [[function_constant(9)]];
22constant int gemv_probe = is_function_constant_defined(GEMV_PROBE) ? GEMV_PROBE : 0;
23
24template <int F, int LPR, int NR, int SG, int KS>
25kernel void gemv(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],
26                 device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],
27                 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
28                 ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
29    constexpr int UNIT = fmt<F>::UNIT, RS = SG / KS, RG = 32 / LPR;
30    threadgroup uint tab[fmt<F>::TAB];
31    threadgroup float red[KS > 1 ? KS * RS * RG * NR * 8 : 1];
32    load_tab<F>(tab, tid, SG * 32);
33    threadgroup_barrier(mem_flags::mem_threadgroup);
34    const int ks = sg / RS, rs = sg % RS;
35    const int li = lane % LPR;
36    const int lrow0 = (rs * RG + lane / LPR) * NR;
37    const int row0 = int(tg.x) * RS * RG * NR + lrow0;
38    const int U = int(a.n_in) / UNIT;
39    const int u0 = ks * U / KS, u1 = (ks + 1) * U / KS;
40    float acc[NR][8];
41    for (int r = 0; r < NR; ++r)
42        for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;
43    device const uchar* rp[NR];
44    for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;
45    const int s16n = int(a.n_in) / 16;
46    if (gemv_probe == 3) {
47        // rows outer, tokens inner: one row's decoded unit in registers at a time (x re-read per row from L1)
48        for (int u = u0 + li; u < u1; u += LPR)
49            for (int r = 0; r < NR; ++r) {
50                float q[UNIT];
51                float4 sc;
52                Dec<F>::run(rp[r], u, q, sc, tab);
53                for (int t = 0; t < NTOK; ++t) {
54                    float xs[UNIT], xsum[2];
55                    X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
56                    acc[r][t] += unit_combine<UNIT>(q, sc, xs, xsum);
57                }
58            }
59    } else
60    for (int u = u0 + li; u < u1; u += LPR) {
61        float q[NR][UNIT];
62        float4 sc[NR];
63        for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);
64        float xs[UNIT], xsum[2];
65        for (int t = 0; t < NTOK; ++t) {
66            if (gemv_probe != 1 || t == 0) X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
67            if (gemv_probe != 2 || t == 0)
68                for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);
69        }
70    }
71    constexpr int RPT = RS * RG * NR;
72    for (int r = 0; r < NR; ++r)
73        for (int t = 0; t < NTOK; ++t) {
74            const float v = lane_sum<LPR>(acc[r][t]);
75            if (KS == 1) {
76                if (li == 0 && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
77            } else if (li == 0) {
78                red[(ks * RPT + lrow0 + r) * 8 + t] = v;
79            }
80        }
81    if (KS > 1) {
82        threadgroup_barrier(mem_flags::mem_threadgroup);
83        if (ks == 0 && li == 0)
84            for (int r = 0; r < NR; ++r)
85                for (int t = 0; t < NTOK; ++t) {
86                    float v = 0.0f;
87                    for (int k = 0; k < KS; ++k) v += red[(k * RPT + lrow0 + r) * 8 + t];
88                    if (row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
89                }
90    }
91}
92
93#define GV(F, L, R, S, K) template [[host_name("gemv_" #F "_" #L "_" #R "_" #S "_" #K)]] kernel decltype(gemv<F, L, R, S, K>) gemv<F, L, R, S, K>;
94#define GV_SET(F) GV(F, 32, 1, 4, 1) GV(F, 32, 2, 4, 1) GV(F, 16, 1, 4, 1) GV(F, 16, 2, 4, 1) GV(F, 8, 1, 4, 1) \
95                  GV(F, 8, 2, 4, 1) GV(F, 4, 2, 4, 1) GV(F, 4, 1, 4, 1) GV(F, 32, 1, 4, 4) GV(F, 32, 2, 4, 2) \
96                  GV(F, 32, 1, 8, 8) GV(F, 16, 1, 8, 4) GV(F, 32, 1, 8, 2) GV(F, 8, 1, 8, 1)                   GV(F, 1, 1, 4, 1) GV(F, 1, 2, 4, 1) GV(F, 2, 1, 4, 1) GV(F, 2, 2, 4, 1) GV(F, 1, 1, 2, 1) GV(F, 4, 4, 4, 1)
97#ifndef GEMV_TYPES
98#define GEMV_TYPES GV_SET(T_F32) GV_SET(T_BF16) GV_SET(T_Q8_0) GV_SET(T_Q4_0) GV_SET(T_Q5_0) GV_SET(T_IQ4_NL) GV_SET(T_IQ4_XS) \
99                   GV_SET(T_Q2_0) GV_SET(T_IQ3_S) GV_SET(T_Q3_K) GV_SET(T_Q4_K) GV_SET(T_Q5_K) GV_SET(T_Q6_K)
100#endif
101GEMV_TYPES
102
103// The shared expert's gate and up in one dispatch: h[t][r] = silu(Wg[r] . x[t]) * (Wu[r] . x[t]), each with its own
104// format (and so its own prepared activation kind); h is written raw (its consumer reads raw activations).
105// KS = 1; tiles as gemv.  Buffers: Wg, xg (Wg's kind), Wu, xu (Wu's kind), s16, h, args (row_bytes = Wg's), rb_up.
106template <int F1, int F2, int LPR, int SG>
107kernel void gemv_pair(device const uchar* Wg [[buffer(0)]], device const float* xg [[buffer(1)]],
108                      device const uchar* Wu [[buffer(2)]], device const float* xu [[buffer(3)]],
109                      device const float* s16 [[buffer(4)]], device float* h [[buffer(5)]],
110                      constant GemvArgs& a [[buffer(6)]], constant uint& rb_up [[buffer(7)]],
111                      uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
112                      ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
113    constexpr int U1 = fmt<F1>::UNIT, U2 = fmt<F2>::UNIT, RG = 32 / LPR;
114    constexpr int TB = fmt<F1>::TAB > fmt<F2>::TAB ? fmt<F1>::TAB : fmt<F2>::TAB;
115    threadgroup uint tab1[fmt<F1>::TAB];
116    threadgroup uint tab2[fmt<F2>::TAB];
117    (void)TB;
118    load_tab<F1>(tab1, tid, SG * 32);
119    load_tab<F2>(tab2, tid, SG * 32);
120    threadgroup_barrier(mem_flags::mem_threadgroup);
121    const int li = lane % LPR;
122    const int row = (int(tg.x) * SG + sg) * RG + lane / LPR;
123    const int rr = min(row, int(a.n_out) - 1);
124    device const uchar* rg = Wg + ulong(rr) * a.row_bytes;
125    device const uchar* ru = Wu + ulong(rr) * rb_up;
126    const int s16n = int(a.n_in) / 16;
127    float ag[8], au[8];
128    for (int t = 0; t < 8; ++t) ag[t] = au[t] = 0.0f;
129    for (int u = li; u < int(a.n_in) / U1; u += LPR) {
130        float q[U1];
131        float4 sc;
132        Dec<F1>::run(rg, u, q, sc, tab1);
133        for (int t = 0; t < NTOK; ++t) {
134            float xs[U1], xsum[2];
135            X<F1>::load(xg + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
136            ag[t] += unit_combine<U1>(q, sc, xs, xsum);
137        }
138    }
139    for (int u = li; u < int(a.n_in) / U2; u += LPR) {
140        float q[U2];
141        float4 sc;
142        Dec<F2>::run(ru, u, q, sc, tab2);
143        for (int t = 0; t < NTOK; ++t) {
144            float xs[U2], xsum[2];
145            X<F2>::load(xu + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
146            au[t] += unit_combine<U2>(q, sc, xs, xsum);
147        }
148    }
149    for (int t = 0; t < NTOK; ++t) {
150        const float gv = lane_sum<LPR>(ag[t]), uv = lane_sum<LPR>(au[t]);
151        if (li == 0 && row < int(a.n_out)) h[t * int(a.y_stride) + row] = gv / (1.0f + exp(-gv)) * uv;
152    }
153}
154
155#define GP_(F1, F2) template [[host_name("gemv_pair_" #F1 "_" #F2)]] kernel decltype(gemv_pair<F1, F2, 32, 4>) gemv_pair<F1, F2, 32, 4>;
156#define GP(F1, F2) GP_(F1, F2)
157#define GP_ROW(F1) GP(F1, T_IQ3_S) GP(F1, T_IQ4_XS) GP(F1, T_Q2_0) GP(F1, T_Q3_K) GP(F1, T_Q4_K) GP(F1, T_Q5_K) GP(F1, T_Q6_K)
158GP_ROW(T_IQ3_S) GP_ROW(T_IQ4_XS) GP_ROW(T_Q2_0) GP_ROW(T_Q3_K) GP_ROW(T_Q4_K) GP_ROW(T_Q5_K) GP_ROW(T_Q6_K)
159// Q2_0 with raw activations (the shared expert's down reading the pair's raw h)
160GV_SET(T_Q2_0R)
161
162#ifdef STRATA_METAL_F16   // window.cpp compiles it in only under STRATA_METAL_F16=1
163// gemv16: gemv over the window's half plane with the fp16 decode and combine (units.metal Dec16 / X16 / unit_combine16)
164template <int F, int LPR, int NR, int SG, int KS>
165kernel void gemv16(device const uchar* W [[buffer(0)]], device const half* xp [[buffer(1)]],
166                   device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],
167                   uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
168                   ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
169    constexpr int UNIT = fmt<F>::UNIT, RS = SG / KS, RG = 32 / LPR;
170    threadgroup uint tab[fmt<F>::TAB];
171    threadgroup float red[KS > 1 ? KS * RS * RG * NR * 8 : 1];
172    load_tab<F>(tab, tid, SG * 32);
173    threadgroup_barrier(mem_flags::mem_threadgroup);
174    const int ks = sg / RS, rs = sg % RS;
175    const int li = lane % LPR;
176    const int lrow0 = (rs * RG + lane / LPR) * NR;
177    const int row0 = int(tg.x) * RS * RG * NR + lrow0;
178    const int U = int(a.n_in) / UNIT;
179    const int u0 = ks * U / KS, u1 = (ks + 1) * U / KS;
180    float acc[NR][8];
181    for (int r = 0; r < NR; ++r)
182        for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;
183    device const uchar* rp[NR];
184    for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;
185    const int s16n = int(a.n_in) / 16;
186    for (int u = u0 + li; u < u1; u += LPR) {
187        half q[NR][UNIT];
188        float4 sc[NR];
189        for (int r = 0; r < NR; ++r) Dec16<F>::run(rp[r], u, q[r], sc[r], tab);
190        half xs[UNIT];
191        float xsum[2];
192        for (int t = 0; t < NTOK; ++t) {
193            X16<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
194            for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine16<UNIT>(q[r], sc[r], xs, xsum);
195        }
196    }
197    constexpr int RPT = RS * RG * NR;
198    for (int r = 0; r < NR; ++r)
199        for (int t = 0; t < NTOK; ++t) {
200            const float v = lane_sum<LPR>(acc[r][t]);
201            if (KS == 1) {
202                if (li == 0 && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
203            } else if (li == 0) {
204                red[(ks * RPT + lrow0 + r) * 8 + t] = v;
205            }
206        }
207    if (KS > 1) {
208        threadgroup_barrier(mem_flags::mem_threadgroup);
209        if (ks == 0 && li == 0)
210            for (int r = 0; r < NR; ++r)
211                for (int t = 0; t < NTOK; ++t) {
212                    float v = 0.0f;
213                    for (int k = 0; k < KS; ++k) v += red[(k * RPT + lrow0 + r) * 8 + t];
214                    if (row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
215                }
216    }
217}
218#define GV16(F, L, R, S, K) template [[host_name("gemv16_" #F "_" #L "_" #R "_" #S "_" #K)]] kernel decltype(gemv16<F, L, R, S, K>) gemv16<F, L, R, S, K>;
219#define GV16_SET(F) GV16(F, 32, 1, 4, 1) GV16(F, 32, 2, 4, 1) GV16(F, 16, 1, 4, 1) GV16(F, 16, 2, 4, 1) GV16(F, 8, 1, 4, 1) \
220                    GV16(F, 8, 2, 4, 1) GV16(F, 4, 2, 4, 1) GV16(F, 4, 1, 4, 1) GV16(F, 32, 1, 8, 2) GV16(F, 16, 1, 8, 4) \
221                    GV16(F, 2, 1, 4, 1) GV16(F, 2, 2, 4, 1) GV16(F, 1, 1, 4, 1)
222GV16_SET(T_Q3_K) GV16_SET(T_Q4_K) GV16_SET(T_Q5_K) GV16_SET(T_Q6_K) GV16_SET(T_IQ4_XS) GV16_SET(T_Q2_0)
223#endif   // STRATA_METAL_F16
224
225// gemv_cat: two matrices of the same format and n_in over the same x in one dispatch - rows [0, n_split) are W's
226// (into y), rows [n_split, n_out) are W2's (into y2, from its row 0).  Each row's arithmetic is gemv's (KS = 1), so the
227// outputs are bitwise the two separate GEMVs'.  (GDN's ssm_alpha + ssm_beta: 48 + 48 rows.)
228// Buffers: W, xp, s16, y, args (n_out = both), W2, y2, n_split.
229template <int F, int LPR, int NR, int SG>
230kernel void gemv_cat(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],
231                     device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],
232                     device const uchar* W2 [[buffer(5)]], device float* y2 [[buffer(6)]], constant uint& n_split [[buffer(7)]],
233                     uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
234                     ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
235    constexpr int UNIT = fmt<F>::UNIT, RG = 32 / LPR;
236    threadgroup uint tab[fmt<F>::TAB];
237    load_tab<F>(tab, tid, SG * 32);
238    threadgroup_barrier(mem_flags::mem_threadgroup);
239    const int li = lane % LPR;
240    const int row0 = int(tg.x) * SG * RG * NR + (sg * RG + lane / LPR) * NR;
241    const int U = int(a.n_in) / UNIT, sp = int(n_split);
242    float acc[NR][8];
243    for (int r = 0; r < NR; ++r)
244        for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;
245    device const uchar* rp[NR];
246    for (int r = 0; r < NR; ++r) {
247        const int row = min(row0 + r, int(a.n_out) - 1);
248        rp[r] = row < sp ? W + (ulong)row * a.row_bytes : W2 + (ulong)(row - sp) * a.row_bytes;
249    }
250    const int s16n = int(a.n_in) / 16;
251    for (int u = li; u < U; u += LPR) {
252        float q[NR][UNIT];
253        float4 sc[NR];
254        for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);
255        for (int t = 0; t < NTOK; ++t) {
256            float xs[UNIT], xsum[2];
257            X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
258            for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);
259        }
260    }
261    for (int r = 0; r < NR; ++r)
262        for (int t = 0; t < NTOK; ++t) {
263            const float v = lane_sum<LPR>(acc[r][t]);
264            const int row = row0 + r;
265            if (li == 0 && row < int(a.n_out)) {
266                if (row < sp) y[t * int(a.y_stride) + row] = v;
267                else y2[t * int(a.y_stride) + row - sp] = v;
268            }
269        }
270}
271#define GVC_(F, L, R, S) template [[host_name("gemvc_" #F "_" #L "_" #R "_" #S)]] kernel decltype(gemv_cat<F, L, R, S>) gemv_cat<F, L, R, S>;
272#define GVC(F, L, R, S) GVC_(F, L, R, S)
273GVC(T_BF16, 32, 1, 4) GVC(T_BF16, 32, 1, 8) GVC(T_BF16, 16, 1, 4) GVC(T_BF16, 32, 2, 4)
274GVC(T_F32, 32, 1, 4) GVC(T_F32, 32, 1, 8) GVC(T_F32, 16, 1, 4) GVC(T_F32, 32, 2, 4)
275
276// gemv_x: gemv (KS = 1) with the window's prepared x (the format's plane, NTOK rows, and its per-16 sums) staged in
277// threadgroup memory once per threadgroup, so the per-unit x reads of every token come from threadgroup memory
278// instead of L1 (on the M4 the x traffic through L1 - 4 B a weight a token, shared by the rows in a simdgroup - is
279// what a window token costs).  Same tile meaning and the same arithmetic as gemv_<F>_<LPR>_<NR>_<SG>_1: bitwise.
280// Dynamic threadgroup memory 0: 4 * NTOK * (n_in + n_in / 16) bytes.
281template <int F, int LPR, int NR, int SG>
282kernel void gemv_x(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],
283                   device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],
284                   threadgroup float* xt [[threadgroup(0)]],
285                   uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
286                   ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
287    constexpr int UNIT = fmt<F>::UNIT, RG = 32 / LPR;
288    threadgroup uint tab[fmt<F>::TAB];
289    load_tab<F>(tab, tid, SG * 32);
290    const int n = int(a.n_in), s16n = n / 16;
291    threadgroup float* st = xt + NTOK * n;
292    for (int t = 0; t < NTOK; ++t) {
293        device const float4* src = (device const float4*)(xp + t * int(a.x_stride));
294        threadgroup float4* dst = (threadgroup float4*)(xt + t * n);
295        for (int i = tid; i < n / 4; i += SG * 32) dst[i] = src[i];
296        for (int i = tid; i < s16n; i += SG * 32) st[t * s16n + i] = s16[t * s16n + i];
297    }
298    threadgroup_barrier(mem_flags::mem_threadgroup);
299    const int li = lane % LPR;
300    const int row0 = int(tg.x) * SG * RG * NR + (sg * RG + lane / LPR) * NR;
301    const int U = n / UNIT;
302    float acc[NR][8];
303    for (int r = 0; r < NR; ++r)
304        for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;
305    device const uchar* rp[NR];
306    for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;
307    for (int u = li; u < U; u += LPR) {
308        float q[NR][UNIT];
309        float4 sc[NR];
310        for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);
311        for (int t = 0; t < NTOK; ++t) {
312            float xs[UNIT], xsum[2];
313            X<F>::load((threadgroup const float*)(xt + t * n), (threadgroup const float*)(st + t * s16n), u, xs, xsum);
314            for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);
315        }
316    }
317    for (int r = 0; r < NR; ++r)
318        for (int t = 0; t < NTOK; ++t) {
319            const float v = lane_sum<LPR>(acc[r][t]);
320            if (li == 0 && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
321        }
322}
323#define GVX(F, L, R, S) template [[host_name("gemvx_" #F "_" #L "_" #R "_" #S)]] kernel decltype(gemv_x<F, L, R, S>) gemv_x<F, L, R, S>;
324#define GVX_SET(F) GVX(F, 32, 1, 4) GVX(F, 32, 2, 4) GVX(F, 16, 1, 4) GVX(F, 16, 2, 4) GVX(F, 8, 1, 4) GVX(F, 8, 2, 4) \
325                   GVX(F, 4, 2, 4) GVX(F, 16, 2, 8) GVX(F, 8, 2, 8) GVX(F, 16, 4, 4) GVX(F, 32, 2, 8) GVX(F, 2, 2, 4)
326GVX_SET(T_BF16) GVX_SET(T_Q8_0) GVX_SET(T_Q4_0) GVX_SET(T_Q5_0) GVX_SET(T_IQ4_NL) GVX_SET(T_IQ4_XS) GVX_SET(T_Q2_0)
327GVX_SET(T_IQ3_S) GVX_SET(T_Q3_K) GVX_SET(T_Q4_K) GVX_SET(T_Q5_K) GVX_SET(T_Q6_K)