okami.mom/strata

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

Niko1221Merge f-mac-rel (Mac Metal support) into f-144-rel6a74320ca

main
4.4 KiB92 linesraw
1// metal/src/engine/msl/mtp_chain.metal - the two steps that keep a whole MTP draft round on the GPU (one command
2// buffer for every draft step; compiled after mac-kern's quant.metal and units.metal):
3//
4//   mtp_pick     draft j: the pick's token id (through the draft vocabulary) and its probability, the softmax of
5//                the draft head's logits at the pick (the host computed it after every step before)
6//   mtp_embed_F  the next step's input: the picked token's embedding row dequantized from the model's token_embd
7//                (format F) - the host dequantized it before
8//
9// mtp_embed reads a row with mac-kern's unit decoders (units.metal Dec<F>): a unit's dot with x is
10//   d1 * sum_{j < UNIT/2} q[j] xs[j] + d2 * sum_{j >= UNIT/2} q[j] xs[j] - (m1 xsum0 + m2 xsum1)
11// with xs the prepared x (raw x times the kind's 2^-k) and xsum the raw sums of 16, so weight j of the unit is
12//   w = d * (q[j] * 2^-k) - m        (d = d1 or d2 by half, m = m1 for the first 16 x values, m2 for the rest;
13//                                     a 16-wide unit has one sum: m1)
14// and its position in the row is what X<F>::load reads when x holds the positions 0 .. n-1.
15
16struct PickArgs {
17    uint nv;        // the draft head's rows (the draft vocabulary, or the whole vocabulary)
18    uint j;         // the draft step
19    uint use_map;   // 1: map the head's row through the draft vocabulary
20};
21
22// one threadgroup of 1024
23kernel void mtp_pick(device const float* lg [[buffer(0)]], device const int* pick [[buffer(1)]],
24                     device const int* vmap [[buffer(2)]], device int* dout [[buffer(3)]], device float* dprob [[buffer(4)]],
25                     constant PickArgs& a [[buffer(5)]], ushort tid [[thread_index_in_threadgroup]],
26                     ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) {
27    threadgroup float part[32];
28    const int di = pick[0];
29    const float mx = lg[di];
30    float s = 0.0f;
31    for (uint i = tid; i < a.nv; i += 1024) s += exp(lg[i] - mx);
32    s = simd_sum(s);
33    if (lane == 0) part[sg] = s;
34    threadgroup_barrier(mem_flags::mem_threadgroup);
35    if (tid == 0) {
36        float t = 0.0f;
37        for (int k = 0; k < 32; ++k) t += part[k];
38        dout[a.j] = a.use_map ? vmap[di] : di;
39        dprob[a.j] = 1.0f / t;
40    }
41}
42
43struct EmbArgs {
44    uint n;           // the row's width
45    uint row_bytes;
46    uint j;           // dout[j] is the token
47};
48
49template <int F> static inline float kind_scale(int pos) {
50    const int k = XKind<F>::K;
51    if (k == XK_Q2) {
52        const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f};
53        return q2s[pos & 7];
54    }
55    if (k == XK_P4) {
56        const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f};
57        return p4s[pos & 3];
58    }
59    if (k == XK_P2) return (pos & 1) ? 0.00390625f : 1.0f;
60    return 1.0f;
61}
62
63// one threadgroup of 256; posv = the floats 0 .. n-1
64template <int F>
65kernel void mtp_embed(device const uchar* W [[buffer(0)]], device const int* dout [[buffer(1)]],
66                      device const float* posv [[buffer(2)]], device float* y [[buffer(3)]], constant EmbArgs& a [[buffer(4)]],
67                      ushort tid [[thread_index_in_threadgroup]]) {
68    constexpr int UNIT = fmt<F>::UNIT, H = UNIT / 2;
69    threadgroup uint tab[fmt<F>::TAB];
70    load_tab<F>(tab, tid, 256);
71    threadgroup_barrier(mem_flags::mem_threadgroup);
72    device const uchar* row = W + (ulong) dout[a.j] * a.row_bytes;
73    const int U = int(a.n) / UNIT;
74    for (int u = tid; u < U; u += 256) {
75        float q[UNIT], ps[UNIT], xsum[2];
76        float4 sc;
77        Dec<F>::run(row, u, q, sc, tab);
78        X<F>::load(posv, posv, u, ps, xsum);
79        for (int j = 0; j < UNIT; ++j) {
80            const int pos = int(ps[j]);
81            const float d = j < H ? sc.x : sc.y;
82            const float m = (UNIT == 16 || j < 16) ? sc.z : sc.w;
83            y[pos] = d * (q[j] * kind_scale<F>(pos)) - m;
84        }
85    }
86}
87
88// host names by the format's GGUF type number (mtp_embed_11 = Q3_K): EMB expands F before EMB_ stringifies it
89#define EMB_(F) template [[host_name("mtp_embed_" #F)]] kernel decltype(mtp_embed<F>) mtp_embed<F>;
90#define EMB(F) EMB_(F)
91EMB(T_BF16) EMB(T_Q8_0) EMB(T_Q4_0) EMB(T_Q5_0) EMB(T_IQ4_NL) EMB(T_IQ4_XS) EMB(T_Q2_0) EMB(T_IQ3_S) EMB(T_Q3_K)
92EMB(T_Q4_K) EMB(T_Q5_K) EMB(T_Q6_K) EMB(T_IQ3_XXS)