okami.mom/strata

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

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

main
25.3 KiB547 linesraw
1// metal/kernels/units.metal - the dot of one unit with the window's tokens (after quant.metal).
2//
3// X<F>::load(xrow, u, xs, xsum) prepares unit u's x slice of one token (xrow = the token's row start), once per unit
4// and shared by every weight row the lane reads: the UNIT x values in the order the decoder produces weights, and
5// up to two partial sums (for the formats with an offset or a min).  Where the codes are read with the mask trick
6// (an AND keeps a field in place, so the code arrives multiplied by 2^k), the x values are pre-scaled by 2^-k here:
7// exact in float.
8
9// the threadgroup-memory form (x staged once per threadgroup; the same values)
10static inline void load16(threadgroup const float* p, thread float* xs) {
11    threadgroup const float4* v = (threadgroup const float4*)p;
12    for (int j = 0; j < 4; ++j) {
13        const float4 a = v[j];
14        xs[4 * j] = a.x;
15        xs[4 * j + 1] = a.y;
16        xs[4 * j + 2] = a.z;
17        xs[4 * j + 3] = a.w;
18    }
19}
20static inline void load16(device const float* p, thread float* xs) {
21    device const float4* v = (device const float4*)p;
22    for (int j = 0; j < 4; ++j) {
23        const float4 a = v[j];
24        xs[4 * j] = a.x;
25        xs[4 * j + 1] = a.y;
26        xs[4 * j + 2] = a.z;
27        xs[4 * j + 3] = a.w;
28    }
29}
30
31// ---- prepared x (xprep): XP holds, per token row of n floats, the kinds
32//   XK_RAW  x                                  (every format without the mask trick)
33//   XK_Q2   x * 4^-(i % 8)                     (Q2_0: 2-bit fields in place in a 16-bit word)
34//   XK_P4   x * 2^-(8 (i % 4))                 (Q4_K, Q5_K: byte fields in place in a 32-bit word)
35//   XK_P2   x * 2^-(8 (i % 2))                 (Q6_K, Q3_K: byte fields in place in a 16-bit word)
36// and S16 = the sum of every 16 consecutive raw x (the formats with an offset or a min).
37#define XK_RAW 0
38#define XK_Q2 1
39#define XK_P4 2
40#define XK_P2 3
41template <int F> struct XKind { enum { K = XK_RAW }; };
42template <> struct XKind<T_Q2_0> { enum { K = XK_Q2 }; };
43template <> struct XKind<T_Q4_K> { enum { K = XK_P4 }; };
44template <> struct XKind<T_Q5_K> { enum { K = XK_P4 }; };
45template <> struct XKind<T_Q6_K> { enum { K = XK_P2 }; };
46template <> struct XKind<T_Q3_K> { enum { K = XK_P2 }; };
47
48// X<F>::load(xp, s16, u, xs, xsum): unit u's prepared x (xp = the token's row of the format's kind, s16 = its sums)
49template <int F> struct X {
50    template <typename P> static inline void load(P xp, P, int u, thread float* xs, thread float* xsum) {
51        constexpr int UNIT = fmt<F>::UNIT;
52        for (int k = 0; k < UNIT; k += 16) load16(xp + u * UNIT + k, xs + k);
53        xsum[0] = xsum[1] = 0.0f;
54    }
55};
56template <> struct X<T_Q2_0> {
57    template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {
58        load16(xp + 16 * u, xs);
59        xsum[0] = s16[u];
60        xsum[1] = 0.0f;
61    }
62};
63// Q4_K / Q5_K: unit (block b, chunk c, half m): run0 at 256b + 64c + 16m, run1 = run0 + 32
64template <> struct X<T_Q4_K> {
65    template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {
66        const int p0 = 256 * (u >> 3) + 64 * ((u >> 1) & 3) + 16 * (u & 1);
67        load16(xp + p0, xs);
68        load16(xp + p0 + 32, xs + 16);
69        xsum[0] = s16[p0 >> 4];
70        xsum[1] = s16[(p0 >> 4) + 2];
71    }
72};
73template <> struct X<T_Q5_K> {
74    template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {
75        X<T_Q4_K>::load(xp, s16, u, xs, xsum);
76    }
77};
78// Q6_K: unit (block b, half n, m, p): run0 at 256b + 128n + 32p + 16m, run1 = run0 + 64
79template <> struct X<T_Q6_K> {
80    template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {
81        const int p0 = 256 * (u >> 3) + 128 * ((u >> 2) & 1) + 32 * (u & 1) + 16 * ((u >> 1) & 1);
82        load16(xp + p0, xs);
83        load16(xp + p0 + 64, xs + 16);
84        xsum[0] = s16[p0 >> 4];
85        xsum[1] = s16[(p0 >> 4) + 4];
86    }
87};
88// Q5_1: 32 contiguous raw x, the two per-16 sums formed from them (the GEMVs that read Q5_1 - the routed down - get
89// h without prepared sums)
90template <> struct X<T_Q5_1> {
91    template <typename P> static inline void load(P xp, P, int u, thread float* xs, thread float* xsum) {
92        load16(xp + 32 * u, xs);
93        load16(xp + 32 * u + 16, xs + 16);
94        float a = 0.0f, b = 0.0f;
95        for (int j = 0; j < 16; ++j) { a += xs[j]; b += xs[16 + j]; }
96        xsum[0] = a;
97        xsum[1] = b;
98    }
99};
100// Q3_K: 32 contiguous, sums per 16
101template <> struct X<T_Q3_K> {
102    template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {
103        load16(xp + 32 * u, xs);
104        load16(xp + 32 * u + 16, xs + 16);
105        xsum[0] = s16[2 * u];
106        xsum[1] = s16[2 * u + 1];
107    }
108};
109
110// xprep: one thread per 16 consecutive values of one token row; XP = [4 kinds][n_tok][n] then S16 [n_tok][n / 16]
111kernel void xprep(device const float* x [[buffer(0)]], device float* xp [[buffer(1)]], constant uint& n [[buffer(2)]],
112                  constant uint& n_tok [[buffer(3)]], uint gid [[thread_position_in_grid]]) {
113    const uint per = n / 16;
114    if (gid >= per * n_tok) return;
115    const uint t = gid / per, i = gid % per;
116    device const float* src = x + t * n + 16 * i;
117    const uint plane = n_tok * n;
118    device float* raw = xp + t * n + 16 * i;
119    float v[16];
120    load16(src, v);
121    float s0 = 0, s1 = 0;
122    for (int j = 0; j < 16; j += 2) { s0 += v[j]; s1 += v[j + 1]; }
123    const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f};
124    const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f};
125    for (int j = 0; j < 16; ++j) {
126        raw[j] = v[j];
127        raw[plane + j] = v[j] * q2s[j & 7];
128        raw[2 * plane + j] = v[j] * p4s[j & 3];
129        raw[3 * plane + j] = (j & 1) ? v[j] * 0.00390625f : v[j];
130        if (xh16) put_x16(xp, plane, t * n + 16 * i + j, j, v[j]);
131    }
132    xp[4 * plane + t * per + i] = s0 + s1;
133}
134
135// ---- decoders: Dec<F>::run(row, u, q, sc) decodes unit u of a row into q[UNIT] and sc = (d1, d2, m1, m2), so that
136// for any token: dot = d1 * sum_{j < UNIT/2} q[j] xs[j] + d2 * sum_{j >= UNIT/2} q[j] xs[j] - (m1 xsum0 + m2 xsum1)
137// with xs, xsum from X<F>::load.  A unit's weights are decoded once per row and applied to every token.
138template <int F> struct Dec {
139    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint* tab) {
140        constexpr int UNIT = fmt<F>::UNIT, UPB = fmt<F>::QK / UNIT;
141        const float s = deq<F>(row + (u / UPB) * fmt<F>::BB, u % UPB, q, tab);
142        sc = float4(s, s, 0.0f, 0.0f);
143    }
144};
145
146template <> struct Dec<T_Q2_0> {
147    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
148        device const uchar* b = row + (u >> 2) * 18;
149        device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3));
150        const ushort a = p[0], c = p[1];
151        for (int j = 0; j < 8; ++j) {
152            q[j] = float(a & ushort(3u << (2 * j)));
153            q[8 + j] = float(c & ushort(3u << (2 * j)));
154        }
155        const float d = rdh(b);
156        sc = float4(d, d, d, 0.0f);
157    }
158};
159
160// Q2_0 with raw x: q = c - 1 (shift, and, convert, subtract), one scale
161template <> struct Dec<T_Q2_0R> {
162    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
163        device const uchar* b = row + (u >> 2) * 18;
164        device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3));
165        const ushort a = p[0], c = p[1];
166        for (int j = 0; j < 8; ++j) {
167            q[j] = float((a >> (2 * j)) & 3) - 1.0f;
168            q[8 + j] = float((c >> (2 * j)) & 3) - 1.0f;
169        }
170        const float d = rdh(b);
171        sc = float4(d, d, 0.0f, 0.0f);
172    }
173};
174
175static inline void scale_min_k4(int j, device const uchar* q, thread float& d, thread float& m) {
176    if (j < 4) {
177        d = float(q[j] & 63);
178        m = float(q[j + 4] & 63);
179    } else {
180        d = float((q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4));
181        m = float((q[j + 4] >> 4) | ((q[j] >> 6) << 4));
182    }
183}
184static inline void bytes4(uint w, thread float* q) {
185    q[0] = float(w & 0xFFu);
186    q[1] = float(w & 0xFF00u);
187    q[2] = float(w & 0xFF0000u);
188    q[3] = float(w & 0xFF000000u);
189}
190static inline void bytes2(ushort w, thread float* q) {
191    q[0] = float(w & 0xFF);
192    q[1] = float(w & 0xFF00);
193}
194
195template <> struct Dec<T_Q5_1> {   // w = d q + m:  sum = d (q . x) + m (sum x)
196    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint* tab) {
197        device const uchar* b = row + u * 24;
198        const float d = deq<T_Q5_1>(b, 0, q, tab), m = rdh(b + 2);
199        sc = float4(d, d, -m, -m);
200    }
201};
202
203template <> struct Dec<T_Q4_K> {
204    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
205        device const uchar* b = row + (u >> 3) * 144;
206        const int c = (u >> 1) & 3, m = u & 1;
207        const float d = rdh(b), dm = rdh(b + 2);
208        float s1, m1, s2, m2;
209        scale_min_k4(2 * c, b + 4, s1, m1);
210        scale_min_k4(2 * c + 1, b + 4, s2, m2);
211        device const uint* qp = (device const uint*)(b + 16 + 32 * c + 16 * m);
212        for (int w = 0; w < 4; ++w) {
213            const uint v = qp[w];
214            bytes4(v & 0x0F0F0F0Fu, q + 4 * w);
215            bytes4((v >> 4) & 0x0F0F0F0Fu, q + 16 + 4 * w);
216        }
217        sc = float4(d * s1, d * s2, dm * m1, dm * m2);
218    }
219};
220
221template <> struct Dec<T_Q5_K> {
222    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
223        device const uchar* b = row + (u >> 3) * 176;
224        const int c = (u >> 1) & 3, m = u & 1;
225        const float d = rdh(b), dm = rdh(b + 2);
226        float s1, m1, s2, m2;
227        scale_min_k4(2 * c, b + 4, s1, m1);
228        scale_min_k4(2 * c + 1, b + 4, s2, m2);
229        device const uint* qp = (device const uint*)(b + 48 + 32 * c + 16 * m);
230        device const uint* hp = (device const uint*)(b + 16 + 16 * m);
231        for (int w = 0; w < 4; ++w) {
232            const uint v = qp[w], h = hp[w];
233            bytes4((v & 0x0F0F0F0Fu) | (((h >> (2 * c)) & 0x01010101u) << 4), q + 4 * w);
234            bytes4(((v >> 4) & 0x0F0F0F0Fu) | (((h >> (2 * c + 1)) & 0x01010101u) << 4), q + 16 + 4 * w);
235        }
236        sc = float4(d * s1, d * s2, dm * m1, dm * m2);
237    }
238};
239
240template <> struct Dec<T_Q6_K> {
241    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
242        device const uchar* b = row + (u >> 3) * 210;
243        const int n = (u >> 2) & 1, m = (u >> 1) & 1, p = u & 1;
244        device const ushort* ql = (device const ushort*)(b + 64 * n + 32 * p + 16 * m);
245        device const ushort* qh = (device const ushort*)(b + 128 + 32 * n + 16 * m);
246        device const char* scs = (device const char*)(b + 192);
247        const float d = rdh(b + 208);
248        for (int w = 0; w < 8; ++w) {
249            const ushort l = ql[w], h = qh[w];
250            bytes2((l & 0x0F0F) | (((h >> (2 * p)) & 0x0303) << 4), q + 2 * w);
251            bytes2(((l >> 4) & 0x0F0F) | (((h >> (2 * p + 4)) & 0x0303) << 4), q + 16 + 2 * w);
252        }
253        const float d1 = d * float(scs[8 * n + m + 2 * p]), d2 = d * float(scs[8 * n + m + 2 * p + 4]);
254        sc = float4(d1, d2, 32.0f * d1, 32.0f * d2);
255    }
256};
257
258template <> struct Dec<T_Q3_K> {
259    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
260        device const uchar* b = row + (u >> 3) * 110;
261        const int n = (u >> 2) & 1, j = u & 3;
262        // qs and hmask as packed (2-byte aligned) vectors: blocks are 110 bytes, so odd blocks are only 2-aligned
263        device const packed_ushort4* qv = (device const packed_ushort4*)(b + 32 + 32 * n);
264        device const packed_ushort4* hv = (device const packed_ushort4*)b;
265        ushort qq[16], hm[16];
266        for (int v = 0; v < 4; ++v) {
267            const ushort4 a4 = ushort4(qv[v]), h4 = ushort4(hv[v]);
268            qq[4 * v] = a4.x; qq[4 * v + 1] = a4.y; qq[4 * v + 2] = a4.z; qq[4 * v + 3] = a4.w;
269            hm[4 * v] = h4.x; hm[4 * v + 1] = h4.y; hm[4 * v + 2] = h4.z; hm[4 * v + 3] = h4.w;
270        }
271        device const uchar* s = b + 96;
272        const int i1 = 8 * n + 2 * j, i2 = i1 + 1;
273        const int l1 = (i1 < 8 ? s[i1] & 0xF : s[i1 - 8] >> 4) | (((s[8 + (i1 & 3)] >> (2 * (i1 >> 2))) & 3) << 4);
274        const int l2 = (i2 < 8 ? s[i2] & 0xF : s[i2 - 8] >> 4) | (((s[8 + (i2 & 3)] >> (2 * (i2 >> 2))) & 3) << 4);
275        const float d = rdh(b + 108);
276        const int hs = 4 * n + j;
277        for (int w = 0; w < 8; ++w) {
278            bytes2(((qq[w] >> (2 * j)) & 0x0303) | (((hm[w] >> hs) & 0x0101) << 2), q + 2 * w);
279            bytes2(((qq[w + 8] >> (2 * j)) & 0x0303) | (((hm[w + 8] >> hs) & 0x0101) << 2), q + 16 + 2 * w);
280        }
281        const float d1 = d * float(l1 - 32), d2 = d * float(l2 - 32);
282        sc = float4(d1, d2, 4.0f * d1, 4.0f * d2);
283    }
284};
285
286template <> struct Dec<T_F32> {   // 16 floats a unit (the Unsloth files' router / alpha / beta / inject rows)
287    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
288        device const float4* p = (device const float4*)(row + 64 * u);
289        for (int k = 0; k < 4; ++k) {
290            const float4 v = p[k];
291            q[4 * k] = v.x; q[4 * k + 1] = v.y; q[4 * k + 2] = v.z; q[4 * k + 3] = v.w;
292        }
293        sc = float4(1.0f, 1.0f, 0.0f, 0.0f);
294    }
295};
296
297template <> struct Dec<T_BF16> {
298    static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) {
299        device const uint4* p = (device const uint4*)(row + 32 * u);
300        const uint4 a = p[0], c = p[1];
301        const uint v[8] = {a.x, a.y, a.z, a.w, c.x, c.y, c.z, c.w};
302        for (int k = 0; k < 8; ++k) {
303            q[2 * k] = as_type<float>(v[k] << 16);
304            q[2 * k + 1] = as_type<float>(v[k] & 0xFFFF0000u);
305        }
306        sc = float4(1.0f, 1.0f, 0.0f, 0.0f);
307    }
308};
309
310// acc += unit dot of decoded q / sc with one token's prepared slice
311template <int UNIT>
312static inline float unit_combine(thread const float* q, float4 sc, thread const float* xs, thread const float* xsum) {
313    constexpr int H = UNIT / 2;
314    float s0 = 0, s1 = 0, s2 = 0, s3 = 0;
315    for (int j = 0; j < H; j += 2) {
316        s0 = fma(q[j], xs[j], s0);
317        s1 = fma(q[j + 1], xs[j + 1], s1);
318        s2 = fma(q[H + j], xs[H + j], s2);
319        s3 = fma(q[H + j + 1], xs[H + j + 1], s3);
320    }
321    return sc.x * (s0 + s1) + sc.y * (s2 + s3) - (sc.z * xsum[0] + sc.w * xsum[1]);
322}
323
324#ifdef STRATA_METAL_F16   // window.cpp compiles it in only under STRATA_METAL_F16=1
325// ---- the fp16 window path (opt-in, STRATA_METAL_F16): the same decode into half, the window's x as a half plane, the
326// products of a half unit (16 weights, 8 for Q2_0) summed in half, the scales and the sum across units in float.
327// Unguarded: a product or a half unit's sum past 65504 is inf (IQ4_XS from |x| ~516, Q6_K ~1040, Q5_K ~2113, Q2_0
328// ~21835; up to 8x lower for same-sign runs), and |x| below ~2^-6 (2^-8 kinds) or ~2^-8 (Q2_0's 4^-3) is subnormal.
329static inline void bytes2h(ushort w, thread half* q) {
330    q[0] = half(w & 0xFF);
331    q[1] = half(w & 0xFF00);
332}
333template <int F> struct Dec16;
334template <> struct Dec16<T_Q3_K> {
335    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) {
336        device const uchar* b = row + (u >> 3) * 110;
337        const int n = (u >> 2) & 1, j = u & 3;
338        device const packed_ushort4* qv = (device const packed_ushort4*)(b + 32 + 32 * n);
339        device const packed_ushort4* hv = (device const packed_ushort4*)b;
340        ushort qq[16], hm[16];
341        for (int v = 0; v < 4; ++v) {
342            const ushort4 a4 = ushort4(qv[v]), h4 = ushort4(hv[v]);
343            qq[4 * v] = a4.x; qq[4 * v + 1] = a4.y; qq[4 * v + 2] = a4.z; qq[4 * v + 3] = a4.w;
344            hm[4 * v] = h4.x; hm[4 * v + 1] = h4.y; hm[4 * v + 2] = h4.z; hm[4 * v + 3] = h4.w;
345        }
346        device const uchar* s = b + 96;
347        const int i1 = 8 * n + 2 * j, i2 = i1 + 1;
348        const int l1 = (i1 < 8 ? s[i1] & 0xF : s[i1 - 8] >> 4) | (((s[8 + (i1 & 3)] >> (2 * (i1 >> 2))) & 3) << 4);
349        const int l2 = (i2 < 8 ? s[i2] & 0xF : s[i2 - 8] >> 4) | (((s[8 + (i2 & 3)] >> (2 * (i2 >> 2))) & 3) << 4);
350        const float d = rdh(b + 108);
351        const int hs = 4 * n + j;
352        for (int w = 0; w < 8; ++w) {
353            bytes2h(((qq[w] >> (2 * j)) & 0x0303) | (((hm[w] >> hs) & 0x0101) << 2), q + 2 * w);
354            bytes2h(((qq[w + 8] >> (2 * j)) & 0x0303) | (((hm[w + 8] >> hs) & 0x0101) << 2), q + 16 + 2 * w);
355        }
356        const float d1 = d * float(l1 - 32), d2 = d * float(l2 - 32);
357        sc = float4(d1, d2, 4.0f * d1, 4.0f * d2);
358    }
359};
360// Q3_K's prepared x (32 contiguous of the 2^-8 plane, sums per 16 in float) from the half plane
361template <int F> struct X16;
362template <> struct X16<T_Q3_K> {
363    static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) {
364        device const half4* v = (device const half4*)(xp + 32 * u);
365        for (int k = 0; k < 8; ++k) {
366            const half4 a = v[k];
367            xs[4 * k] = a.x; xs[4 * k + 1] = a.y; xs[4 * k + 2] = a.z; xs[4 * k + 3] = a.w;
368        }
369        xsum[0] = s16[2 * u];
370        xsum[1] = s16[2 * u + 1];
371    }
372};
373template <int UNIT>
374static inline float unit_combine16(thread const half* q, float4 sc, thread const half* xs, thread const float* xsum) {
375    constexpr int H = UNIT / 2;
376    half s0 = 0, s1 = 0, s2 = 0, s3 = 0;
377    for (int j = 0; j < H; j += 2) {
378        s0 = fma(q[j], xs[j], s0);
379        s1 = fma(q[j + 1], xs[j + 1], s1);
380        s2 = fma(q[H + j], xs[H + j], s2);
381        s3 = fma(q[H + j + 1], xs[H + j + 1], s3);
382    }
383    return sc.x * float(s0 + s1) + sc.y * float(s2 + s3) - (sc.z * xsum[0] + sc.w * xsum[1]);
384}
385// a 32-bit word of nibble bytes (the K-quants' mask trick) split into two 16-bit halves: the bytes come out with the
386// factors 1, 2^8, 1, 2^8 - the 2^-8 plane, which half holds (values <= 31 * 256)
387static inline void bytes4h(uint w, thread half* q) {
388    bytes2h(ushort(w & 0xFFFFu), q);
389    bytes2h(ushort(w >> 16), q + 2);
390}
391template <> struct Dec16<T_Q4_K> {
392    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) {
393        device const uchar* b = row + (u >> 3) * 144;
394        const int c = (u >> 1) & 3, m = u & 1;
395        const float d = rdh(b), dm = rdh(b + 2);
396        float s1, m1, s2, m2;
397        scale_min_k4(2 * c, b + 4, s1, m1);
398        scale_min_k4(2 * c + 1, b + 4, s2, m2);
399        device const uint* qp = (device const uint*)(b + 16 + 32 * c + 16 * m);
400        for (int w = 0; w < 4; ++w) {
401            const uint v = qp[w];
402            bytes4h(v & 0x0F0F0F0Fu, q + 4 * w);
403            bytes4h((v >> 4) & 0x0F0F0F0Fu, q + 16 + 4 * w);
404        }
405        sc = float4(d * s1, d * s2, dm * m1, dm * m2);
406    }
407};
408template <> struct Dec16<T_Q5_K> {
409    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) {
410        device const uchar* b = row + (u >> 3) * 176;
411        const int c = (u >> 1) & 3, m = u & 1;
412        const float d = rdh(b), dm = rdh(b + 2);
413        float s1, m1, s2, m2;
414        scale_min_k4(2 * c, b + 4, s1, m1);
415        scale_min_k4(2 * c + 1, b + 4, s2, m2);
416        device const uint* qp = (device const uint*)(b + 48 + 32 * c + 16 * m);
417        device const uint* hp = (device const uint*)(b + 16 + 16 * m);
418        for (int w = 0; w < 4; ++w) {
419            const uint v = qp[w], h = hp[w];
420            bytes4h((v & 0x0F0F0F0Fu) | (((h >> (2 * c)) & 0x01010101u) << 4), q + 4 * w);
421            bytes4h(((v >> 4) & 0x0F0F0F0Fu) | (((h >> (2 * c + 1)) & 0x01010101u) << 4), q + 16 + 4 * w);
422        }
423        sc = float4(d * s1, d * s2, dm * m1, dm * m2);
424    }
425};
426template <> struct Dec16<T_Q6_K> {
427    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) {
428        device const uchar* b = row + (u >> 3) * 210;
429        const int n = (u >> 2) & 1, m = (u >> 1) & 1, p = u & 1;
430        device const ushort* ql = (device const ushort*)(b + 64 * n + 32 * p + 16 * m);
431        device const ushort* qh = (device const ushort*)(b + 128 + 32 * n + 16 * m);
432        device const char* scs = (device const char*)(b + 192);
433        const float d = rdh(b + 208);
434        for (int w = 0; w < 8; ++w) {
435            const ushort l = ql[w], h = qh[w];
436            bytes2h((l & 0x0F0F) | (((h >> (2 * p)) & 0x0303) << 4), q + 2 * w);
437            bytes2h(((l >> 4) & 0x0F0F) | (((h >> (2 * p + 4)) & 0x0303) << 4), q + 16 + 2 * w);
438        }
439        const float d1 = d * float(scs[8 * n + m + 2 * p]), d2 = d * float(scs[8 * n + m + 2 * p + 4]);
440        sc = float4(d1, d2, 32.0f * d1, 32.0f * d2);
441    }
442};
443// IQ4_XS: the grid's values are half already (load_tab), no conversion
444template <> struct Dec16<T_IQ4_XS> {
445    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint* tab) {
446        device const uchar* b = row + (u >> 3) * 136;
447        const int ui = u & 7;
448        device const ushort* p = (device const ushort*)(b + 8 + 16 * ui);
449        for (int j = 0; j < 8; ++j) {
450            const ushort v = p[j];
451            const half2 w0 = as_type<half2>(tab[v & 255]), w1 = as_type<half2>(tab[v >> 8]);
452            q[2 * j] = w0.x;
453            q[2 * j + 16] = w0.y;
454            q[2 * j + 1] = w1.x;
455            q[2 * j + 17] = w1.y;
456        }
457        const uint sh = *(device const ushort*)(b + 2);
458        const int ls = int((b[4 + (ui >> 1)] >> (4 * (ui & 1))) & 15) | int(((sh >> (2 * ui)) & 3) << 4);
459        const float s = rdh(b) * float(ls - 32);
460        sc = float4(s, s, 0.0f, 0.0f);
461    }
462};
463// Q2_0 (units of 16): each 16-bit word split into bytes, the code masked in place within its byte - factors 4^(i % 4),
464// so the half plane is x * 4^-(i % 4) (no smaller than 1/64)
465template <> struct Dec16<T_Q2_0> {
466    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) {
467        device const uchar* b = row + (u >> 2) * 18;
468        device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3));
469        const ushort a = p[0], c = p[1];
470        const ushort a1 = a >> 8, c1 = c >> 8;
471        for (int j = 0; j < 4; ++j) {
472            const ushort mk = ushort(3u << (2 * j));
473            q[j] = half(a & mk);
474            q[4 + j] = half(a1 & mk);
475            q[8 + j] = half(c & mk);
476            q[12 + j] = half(c1 & mk);
477        }
478        const float d = rdh(b);
479        sc = float4(d, d, d, 0.0f);
480    }
481};
482static inline void load16h(device const half* p, thread half* xs) {
483    device const half4* v = (device const half4*)p;
484    for (int k = 0; k < 4; ++k) {
485        const half4 a = v[k];
486        xs[4 * k] = a.x; xs[4 * k + 1] = a.y; xs[4 * k + 2] = a.z; xs[4 * k + 3] = a.w;
487    }
488}
489template <> struct X16<T_Q4_K> {
490    static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) {
491        const int p0 = 256 * (u >> 3) + 64 * ((u >> 1) & 3) + 16 * (u & 1);
492        load16h(xp + p0, xs);
493        load16h(xp + p0 + 32, xs + 16);
494        xsum[0] = s16[p0 >> 4];
495        xsum[1] = s16[(p0 >> 4) + 2];
496    }
497};
498template <> struct X16<T_Q5_K> {
499    static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) {
500        X16<T_Q4_K>::load(xp, s16, u, xs, xsum);
501    }
502};
503template <> struct X16<T_Q6_K> {
504    static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) {
505        const int p0 = 256 * (u >> 3) + 128 * ((u >> 2) & 1) + 32 * (u & 1) + 16 * ((u >> 1) & 1);
506        load16h(xp + p0, xs);
507        load16h(xp + p0 + 64, xs + 16);
508        xsum[0] = s16[p0 >> 4];
509        xsum[1] = s16[(p0 >> 4) + 4];
510    }
511};
512template <> struct X16<T_IQ4_XS> {
513    static inline void load(device const half* xp, device const float*, int u, thread half* xs, thread float* xsum) {
514        load16h(xp + 32 * u, xs);
515        load16h(xp + 32 * u + 16, xs + 16);
516        xsum[0] = xsum[1] = 0.0f;
517    }
518};
519template <> struct X16<T_Q2_0> {
520    static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) {
521        load16h(xp + 16 * u, xs);
522        xsum[0] = s16[u];
523        xsum[1] = 0.0f;
524    }
525};
526
527// Q2_0 with raw x (the routed down over h): q = code - 1 in half
528template <> struct Dec16<T_Q2_0R> {
529    static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) {
530        device const uchar* b = row + (u >> 2) * 18;
531        device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3));
532        const ushort a = p[0], c = p[1];
533        for (int j = 0; j < 8; ++j) {
534            q[j] = half((a >> (2 * j)) & 3) - 1.0h;
535            q[8 + j] = half((c >> (2 * j)) & 3) - 1.0h;
536        }
537        const float d = rdh(b);
538        sc = float4(d, d, 0.0f, 0.0f);
539    }
540};
541template <> struct X16<T_Q2_0R> {
542    static inline void load(device const half* xp, device const float*, int u, thread half* xs, thread float* xsum) {
543        load16h(xp + 16 * u, xs);
544        xsum[0] = xsum[1] = 0.0f;
545    }
546};
547#endif   // STRATA_METAL_F16