char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

YiChen Lvmetal: per-op source split + parallel compile (#26561)b615f5b4b

char/aliceai-t5-moe
27.3 KiB735 linesraw
1#pragma once
2
3#include "common.h"
4
5#define GGML_COMMON_DECL_METAL
6#define GGML_COMMON_IMPL_METAL
7#if defined(GGML_METAL_EMBED_LIBRARY)
8__embed_ggml-common.h__
9#else
10#include "ggml-common.h"
11#endif
12
13#define QK_NL 16 // shared by mul_mm and get_rows_q instantiations
14
15// NOTE: this is not dequantizing - we are simply fitting the template
16template <typename type4x4>
17void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) {
18    reg = (type4x4)(*src);
19}
20
21template <typename type4>
22void dequantize_f32_t4(device const float4 * src, short il, thread type4 & reg) {
23    reg = (type4)(*src);
24}
25
26template <typename type4x4>
27void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) {
28    reg = (type4x4)(*src);
29}
30
31template <typename type4>
32void dequantize_f16_t4(device const half4 * src, short il, thread type4 & reg) {
33    reg = (type4)(*(src));
34}
35
36#if defined(GGML_METAL_HAS_BF16)
37template <typename type4x4>
38void dequantize_bf16(device const bfloat4x4 * src, short il, thread type4x4 & reg) {
39    reg = (type4x4)(*src);
40}
41
42template <typename type4>
43void dequantize_bf16_t4(device const bfloat4 * src, short il, thread type4 & reg) {
44    reg = (type4)(*(src));
45}
46#endif
47
48template <typename type4x4>
49void dequantize_q1_0(device const block_q1_0 * xb, short il, thread type4x4 & reg) {
50    device const uint8_t * qs = xb->qs;
51    const float d = xb->d;
52    const float neg_d = -d;
53
54    const int byte_offset = il * 2;  // il*16 bits = il*2 bytes
55    const uint8_t b0 = qs[byte_offset];
56    const uint8_t b1 = qs[byte_offset + 1];
57
58    float4x4 reg_f;
59
60    reg_f[0][0] = select(neg_d, d, bool(b0 & 0x01));
61    reg_f[0][1] = select(neg_d, d, bool(b0 & 0x02));
62    reg_f[0][2] = select(neg_d, d, bool(b0 & 0x04));
63    reg_f[0][3] = select(neg_d, d, bool(b0 & 0x08));
64    reg_f[1][0] = select(neg_d, d, bool(b0 & 0x10));
65    reg_f[1][1] = select(neg_d, d, bool(b0 & 0x20));
66    reg_f[1][2] = select(neg_d, d, bool(b0 & 0x40));
67    reg_f[1][3] = select(neg_d, d, bool(b0 & 0x80));
68
69    reg_f[2][0] = select(neg_d, d, bool(b1 & 0x01));
70    reg_f[2][1] = select(neg_d, d, bool(b1 & 0x02));
71    reg_f[2][2] = select(neg_d, d, bool(b1 & 0x04));
72    reg_f[2][3] = select(neg_d, d, bool(b1 & 0x08));
73    reg_f[3][0] = select(neg_d, d, bool(b1 & 0x10));
74    reg_f[3][1] = select(neg_d, d, bool(b1 & 0x20));
75    reg_f[3][2] = select(neg_d, d, bool(b1 & 0x40));
76    reg_f[3][3] = select(neg_d, d, bool(b1 & 0x80));
77
78    reg = (type4x4) reg_f;
79}
80
81template <typename type4>
82void dequantize_q1_0_t4(device const block_q1_0 * xb, short il, thread type4 & reg) {
83    const float d = xb->d;
84    const float neg_d = -d;
85    const int base = il * 4;
86    const uint8_t byte = xb->qs[base / 8];
87    const int s = base % 8;
88
89    float4 reg_f;
90    reg_f[0] = select(neg_d, d, bool((byte >> (s    )) & 1));
91    reg_f[1] = select(neg_d, d, bool((byte >> (s + 1)) & 1));
92    reg_f[2] = select(neg_d, d, bool((byte >> (s + 2)) & 1));
93    reg_f[3] = select(neg_d, d, bool((byte >> (s + 3)) & 1));
94
95    reg = (type4) reg_f;
96}
97
98template <typename type4x4>
99void dequantize_q2_0(device const block_q2_0 * xb, short il, thread type4x4 & reg) {
100    device const uint8_t * qs = xb->qs;
101    const float d = xb->d;
102
103    const int byte_offset = il * 4;  // il*16 elements = il*4 bytes (4 elements per byte)
104    float4x4 reg_f;
105
106    for (int i = 0; i < 4; i++) {
107        const uint8_t b = qs[byte_offset + i];
108        reg_f[i][0] = ((float)((b >> 0) & 3) - 1.0f) * d;
109        reg_f[i][1] = ((float)((b >> 2) & 3) - 1.0f) * d;
110        reg_f[i][2] = ((float)((b >> 4) & 3) - 1.0f) * d;
111        reg_f[i][3] = ((float)((b >> 6) & 3) - 1.0f) * d;
112    }
113
114    reg = (type4x4) reg_f;
115}
116
117template <typename type4>
118void dequantize_q2_0_t4(device const block_q2_0 * xb, short il, thread type4 & reg) {
119    const float d = xb->d;
120    const uint8_t b = xb->qs[il];
121
122    float4 reg_f;
123    reg_f[0] = ((float)((b >> 0) & 3) - 1.0f) * d;
124    reg_f[1] = ((float)((b >> 2) & 3) - 1.0f) * d;
125    reg_f[2] = ((float)((b >> 4) & 3) - 1.0f) * d;
126    reg_f[3] = ((float)((b >> 6) & 3) - 1.0f) * d;
127
128    reg = (type4) reg_f;
129}
130
131template <typename type4x4>
132void dequantize_q4_0(device const block_q4_0 * xb, short il, thread type4x4 & reg) {
133    device const uint16_t * qs = ((device const uint16_t *)xb + 1);
134    const float d1 = il ? (xb->d / 16.h) : xb->d;
135    const float d2 = d1 / 256.f;
136    const float md = -8.h * xb->d;
137    const ushort mask0 = il ? 0x00F0 : 0x000F;
138    const ushort mask1 = mask0 << 8;
139
140    float4x4 reg_f;
141
142    for (int i = 0; i < 8; i++) {
143        reg_f[i/2][2*(i%2) + 0] = d1 * (qs[i] & mask0) + md;
144        reg_f[i/2][2*(i%2) + 1] = d2 * (qs[i] & mask1) + md;
145    }
146
147    reg = (type4x4) reg_f;
148}
149
150template <typename type4>
151void dequantize_q4_0_t4(device const block_q4_0 * xb, short il, thread type4 & reg) {
152    device const uint16_t * qs = ((device const uint16_t *)xb + 1);
153    const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
154    const float d2 = d1 / 256.f;
155    const float md = -8.h * xb->d;
156    const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
157    const ushort mask1 = mask0 << 8;
158
159    for (int i = 0; i < 2; i++) {
160        reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + md;
161        reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + md;
162    }
163}
164
165
166
167template <typename type4x4>
168void dequantize_q4_1(device const block_q4_1 * xb, short il, thread type4x4 & reg) {
169    device const uint16_t * qs = ((device const uint16_t *)xb + 2);
170    const float d1 = il ? (xb->d / 16.h) : xb->d;
171    const float d2 = d1 / 256.f;
172    const float  m = xb->m;
173    const ushort mask0 = il ? 0x00F0 : 0x000F;
174    const ushort mask1 = mask0 << 8;
175
176    float4x4 reg_f;
177
178    for (int i = 0; i < 8; i++) {
179        reg_f[i/2][2*(i%2) + 0] = ((qs[i] & mask0) * d1) + m;
180        reg_f[i/2][2*(i%2) + 1] = ((qs[i] & mask1) * d2) + m;
181    }
182
183    reg = (type4x4) reg_f;
184}
185
186template <typename type4>
187void dequantize_q4_1_t4(device const block_q4_1 * xb, short il, thread type4 & reg) {
188    device const uint16_t * qs = ((device const uint16_t *)xb + 2);
189    const float d1 = (il/4) ? (xb->d / 16.h) : xb->d;
190    const float d2 = d1 / 256.f;
191    const float  m = xb->m;
192    const ushort mask0 = (il/4) ? 0x00F0 : 0x000F;
193    const ushort mask1 = mask0 << 8;
194
195    for (int i = 0; i < 2; i++) {
196        reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + m;
197        reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + m;
198    }
199}
200
201template <typename type4x4>
202void dequantize_q5_0(device const block_q5_0 * xb, short il, thread type4x4 & reg) {
203    device const uint16_t * qs = ((device const uint16_t *)xb + 3);
204    const float d = xb->d;
205    const float md = -16.h * xb->d;
206    const ushort mask = il ? 0x00F0 : 0x000F;
207
208    const uint32_t qh = *((device const uint32_t *)xb->qh);
209
210    const int x_mv = il ? 4 : 0;
211
212    const int gh_mv = il ? 12 : 0;
213    const int gh_bk = il ?  0 : 4;
214
215    float4x4 reg_f;
216
217    for (int i = 0; i < 8; i++) {
218        // extract the 5-th bits for x0 and x1
219        const uint8_t xh_0 = ((qh >> (gh_mv + 2*i  )) << gh_bk) & 0x10;
220        const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
221
222        // combine the 4-bits from qs with the 5th bit
223        const int32_t x0 = ((((qs[i]     ) & mask) >> x_mv) | xh_0);
224        const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
225
226        reg_f[i/2][2*(i%2) + 0] = d * x0 + md;
227        reg_f[i/2][2*(i%2) + 1] = d * x1 + md;
228    }
229
230    reg = (type4x4) reg_f;
231}
232
233template <typename type4>
234void dequantize_q5_0_t4(device const block_q5_0 * xb, short il, thread type4 & reg) {
235    device const uint16_t * qs = ((device const uint16_t *)xb + 3);
236    const float d = xb->d;
237    const float md = -16.h * xb->d;
238    const ushort mask = (il/4) ? 0x00F0 : 0x000F;
239
240    const uint32_t qh = *((device const uint32_t *)xb->qh);
241
242    const int x_mv = (il/4) ? 4 : 0;
243
244    const int gh_mv = (il/4) ? 12 : 0;
245    const int gh_bk = (il/4) ?  0 : 4;
246
247    for (int ii = 0; ii < 2; ii++) {
248        int i = 2*(il%4) + ii;
249
250        // extract the 5-th bits for x0 and x1
251        const uint8_t xh_0 = ((qh >> (gh_mv + 2*i  )) << gh_bk) & 0x10;
252        const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
253
254        // combine the 4-bits from qs with the 5th bit
255        const int32_t x0 = ((((qs[i]     ) & mask) >> x_mv) | xh_0);
256        const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
257
258        reg[2*ii + 0] = d * x0 + md;
259        reg[2*ii + 1] = d * x1 + md;
260    }
261}
262
263template <typename type4x4>
264void dequantize_q5_1(device const block_q5_1 * xb, short il, thread type4x4 & reg) {
265    device const uint16_t * qs = ((device const uint16_t *)xb + 4);
266    const float d = xb->d;
267    const float m = xb->m;
268    const ushort mask = il ? 0x00F0 : 0x000F;
269
270    const uint32_t qh = *((device const uint32_t *)xb->qh);
271
272    const int x_mv = il ? 4 : 0;
273
274    const int gh_mv = il ? 12 : 0;
275    const int gh_bk = il ?  0 : 4;
276
277    float4x4 reg_f;
278
279    for (int i = 0; i < 8; i++) {
280        // extract the 5-th bits for x0 and x1
281        const uint8_t xh_0 = ((qh >> (gh_mv + 2*i  )) << gh_bk) & 0x10;
282        const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
283
284        // combine the 4-bits from qs with the 5th bit
285        const int32_t x0 = ((((qs[i]     ) & mask) >> x_mv) | xh_0);
286        const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
287
288        reg_f[i/2][2*(i%2) + 0] = d * x0 + m;
289        reg_f[i/2][2*(i%2) + 1] = d * x1 + m;
290    }
291
292    reg = (type4x4) reg_f;
293}
294
295template <typename type4>
296void dequantize_q5_1_t4(device const block_q5_1 * xb, short il, thread type4 & reg) {
297    device const uint16_t * qs = ((device const uint16_t *)xb + 4);
298    const float d = xb->d;
299    const float m = xb->m;
300    const ushort mask = (il/4) ? 0x00F0 : 0x000F;
301
302    const uint32_t qh = *((device const uint32_t *)xb->qh);
303
304    const int x_mv = (il/4) ? 4 : 0;
305
306    const int gh_mv = (il/4) ? 12 : 0;
307    const int gh_bk = (il/4) ?  0 : 4;
308
309    for (int ii = 0; ii < 2; ii++) {
310        int i = 2*(il%4) + ii;
311
312        // extract the 5-th bits for x0 and x1
313        const uint8_t xh_0 = ((qh >> (gh_mv + 2*i  )) << gh_bk) & 0x10;
314        const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10;
315
316        // combine the 4-bits from qs with the 5th bit
317        const int32_t x0 = ((((qs[i]     ) & mask) >> x_mv) | xh_0);
318        const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1);
319
320        reg[2*ii + 0] = d * x0 + m;
321        reg[2*ii + 1] = d * x1 + m;
322    }
323}
324
325template <typename type4x4>
326void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) {
327    device const packed_char4 * qs = (device const packed_char4 *) xb->qs;
328    const float d = xb->d;
329
330    float4x4 reg_f;
331
332    for (int i = 0; i < 4; ++i) {
333        reg_f[i] = float4(qs[4*il + i]) * d;
334    }
335
336    reg = (type4x4) reg_f;
337}
338
339template <typename type4>
340void dequantize_q8_0_t4(device const block_q8_0 *xb, short il, thread type4 & reg) {
341    device const packed_char4 * qs = (device const packed_char4 *) xb->qs;
342    const float d = xb->d;
343
344    reg = (type4) (float4(qs[il]) * d);
345}
346
347template <typename type4x4>
348void dequantize_mxfp4(device const block_mxfp4 * xb, short il, thread type4x4 & reg) {
349    device const uint8_t * q2 = (device const uint8_t *)xb->qs;
350
351    const float d = e8m0_to_fp32(xb->e);
352    const uint8_t shr = il >= 1 ? 4 : 0;
353
354    for (int i = 0; i < 4; ++i) {
355        reg[i][0] = d * kvalues_mxfp4_f[(q2[4*i + 0] >> shr) & 0x0F];
356        reg[i][1] = d * kvalues_mxfp4_f[(q2[4*i + 1] >> shr) & 0x0F];
357        reg[i][2] = d * kvalues_mxfp4_f[(q2[4*i + 2] >> shr) & 0x0F];
358        reg[i][3] = d * kvalues_mxfp4_f[(q2[4*i + 3] >> shr) & 0x0F];
359    }
360}
361
362template <typename type4>
363void dequantize_mxfp4_t4(device const block_mxfp4 * xb, short il, thread type4 & reg) {
364    device const uint8_t * q2 = (device const uint8_t *)xb->qs;
365
366    const float d = e8m0_to_fp32(xb->e);
367    const short il4 = il%4;
368
369    const uint8_t shr = il >= 4 ? 4 : 0;
370
371    reg[0] = d * kvalues_mxfp4_f[(q2[4*il4 + 0] >> shr) & 0x0F];
372    reg[1] = d * kvalues_mxfp4_f[(q2[4*il4 + 1] >> shr) & 0x0F];
373    reg[2] = d * kvalues_mxfp4_f[(q2[4*il4 + 2] >> shr) & 0x0F];
374    reg[3] = d * kvalues_mxfp4_f[(q2[4*il4 + 3] >> shr) & 0x0F];
375}
376
377template <typename type4x4>
378void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) {
379    const float d = xb->d;
380    const float min = xb->dmin;
381    device const uint8_t * q = (device const uint8_t *)xb->qs;
382    float dl, ml;
383    uint8_t sc = xb->scales[il];
384
385    q = q + 32*(il/8) + 16*(il&1);
386    il = (il/2)%4;
387
388    half  coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
389    uchar mask = il>1 ? (il>2 ? 192    : 48)     : (il>0 ? 12    : 3);
390    dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4);
391    for (int i = 0; i < 16; ++i) {
392        reg[i/4][i%4] = dl * (q[i] & mask) - ml;
393    }
394}
395
396template <typename type4x4>
397void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) {
398    const half d_all = xb->d;
399    device const uint8_t * q = (device const uint8_t *)xb->qs;
400    device const uint8_t * h = (device const uint8_t *)xb->hmask;
401    device const int8_t * scales = (device const int8_t *)xb->scales;
402
403    q = q + 32 * (il/8) + 16 * (il&1);
404    h = h + 16 * (il&1);
405    uint8_t m = 1 << (il/2);
406    uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \
407                                 ((il/4)>0 ? 12  : 3);
408    uint16_t kmask2 = il/8 ? 0xF0 : 0x0F;
409    uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4];
410    int16_t  dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2)
411                               : (scale_2&kmask2) | ((scale_1&kmask1) << 4);
412    float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f);
413    const float ml = 4.f * dl;
414
415    il = (il/2) & 3;
416    const half    coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h);
417    const uint8_t mask = il>1 ? (il>2 ? 192    : 48)     : (il>0 ? 12    : 3);
418    dl *= coef;
419
420    for (int i = 0; i < 16; ++i) {
421        reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml);
422    }
423}
424
425static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) {
426    return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)}
427                 : uchar2{uchar((q[j+4+k] & 0xF) | ((q[j-4+k] & 0xc0) >> 2)), uchar((q[j+4+k] >> 4) | ((q[j-0+k] & 0xc0) >> 2))};
428}
429
430template <typename type4x4>
431void dequantize_q4_K(device const block_q4_K * xb, short il, thread type4x4 & reg) {
432    device const uchar * q = xb->qs;
433
434    short is = (il/4) * 2;
435    q = q + (il/4) * 32 + 16 * (il&1);
436    il = il & 3;
437    const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
438    const float d   = il < 2 ? xb->d : xb->d / 16.h;
439    const float min = xb->dmin;
440    const float dl = d * sc[0];
441    const float ml = min * sc[1];
442
443    const ushort mask = il < 2 ? 0x0F : 0xF0;
444    for (int i = 0; i < 16; ++i) {
445        reg[i/4][i%4] = dl * (q[i] & mask) - ml;
446    }
447}
448
449template <typename type4x4>
450void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) {
451    device const uint8_t * q  = xb->qs;
452    device const uint8_t * qh = xb->qh;
453
454    short is = (il/4) * 2;
455    q  = q + 32 * (il/4) + 16 * (il&1);
456    qh = qh + 16 * (il&1);
457    uint8_t ul = 1 << (il/2);
458    il = il & 3;
459    const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales);
460    const float d = il < 2 ? xb->d : xb->d / 16.f;
461    const float min = xb->dmin;
462    const float dl = d * sc[0];
463    const float ml = min * sc[1];
464
465    const ushort mask  = il<2 ? 0x0F : 0xF0;
466    const float qh_val = il<2 ? 16.f : 256.f;
467    for (int i = 0; i < 16; ++i) {
468        reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml;
469    }
470}
471
472template <typename type4x4>
473void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) {
474    const half d_all = xb->d;
475    device const uint16_t * ql = (device const uint16_t *)xb->ql;
476    device const uint16_t * qh = (device const uint16_t *)xb->qh;
477    device const int8_t * scales = (device const int8_t *)xb->scales;
478
479    ql = ql + 32*(il/8) + 16*((il/2)&1) + 8*(il&1);
480    qh = qh + 16*(il/8) + 8*(il&1);
481    float sc = scales[(il%2) + 2 * ((il/2))];
482    il = (il/2) & 3;
483
484    const uint32_t kmask1 = il>1 ? (il>2 ? 0xC0C0C0C0 : 0x30303030) : (il>0 ? 0x0C0C0C0C : 0x03030303);
485    const uint32_t kmask2 = il>1 ? 0xF0F0F0F0                       : 0x0F0F0F0F;
486    const float ml = d_all * sc * 32.f;
487    const float dl0 = d_all * sc;
488    const float dl1 = dl0 / 256.f;
489    const float dl2 = dl0 / (256.f * 256.f);
490    const float dl3 = dl0 / (256.f * 256.f * 256.f);
491    const uint8_t shr_h = il>2 ? 2 : 0;
492    const uint8_t shl_h = il>1 ? 0 : (il>0 ? 2 : 4);
493    const uint8_t shr_l = il>1 ? 4 : 0;
494    for (int i = 0; i < 4; ++i) {
495        const uint32_t  low = (ql[2*i] | (uint32_t)(ql[2*i+1] << 16)) & kmask2;
496        const uint32_t high = (qh[2*i] | (uint32_t)(qh[2*i+1] << 16)) & kmask1;
497        const uint32_t q = ((high << shl_h) >> shr_h) | (low >> shr_l);
498        reg[i][0] = dl0 *  ((half)(q & 0xFF))       - ml;
499        reg[i][1] = dl1 * ((float)(q & 0xFF00))     - ml;
500        reg[i][2] = dl2 * ((float)(q & 0xFF0000))   - ml;
501        reg[i][3] = dl3 * ((float)(q & 0xFF000000)) - ml;
502    }
503}
504
505template <typename type4x4>
506void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) {
507    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
508    const float d = xb->d;
509    const int ib32 = il/2;
510    il = il%2;
511    // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
512    // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's.
513    device const uint16_t * q2 = xb->qs + 4*ib32;
514    const uint32_t aux32_g = q2[0] | (q2[1] << 16);
515    const uint32_t aux32_s = q2[2] | (q2[3] << 16);
516    thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g;
517    const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f;
518    constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
519    uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127];
520    for (int i = 0; i < 8; ++i) {
521        reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
522    }
523    grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
524    signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127];
525    for (int i = 0; i < 8; ++i) {
526        reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
527    }
528}
529
530template <typename type4x4>
531void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) {
532    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
533    const float d = xb->d;
534    const int ib32 = il/2;
535    il = il%2;
536    // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
537    device const uint16_t * q2 = xb->qs + 4*ib32;
538    const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
539    constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511));
540    uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9];
541    for (int i = 0; i < 8; ++i) {
542        reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
543    }
544    grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511));
545    signs = ksigns_iq2xs[q2[2*il+1] >> 9];
546    for (int i = 0; i < 8; ++i) {
547        reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f);
548    }
549}
550
551template <typename type4x4>
552void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) {
553    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
554    const float d = xb->d;
555    const int ib32 = il/2;
556    il = il%2;
557    // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
558    device const uint8_t * q3 = xb->qs + 8*ib32;
559    device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32;
560    const uint32_t aux32 = gas[0] | (gas[1] << 16);
561    const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f;
562    constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]);
563    constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]);
564    uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127];
565    for (int i = 0; i < 4; ++i) {
566        reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
567        reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
568    }
569    grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]);
570    grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]);
571    signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127];
572    for (int i = 0; i < 4; ++i) {
573        reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f);
574        reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f);
575    }
576}
577
578template <typename type4x4>
579void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) {
580    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
581    const float d = xb->d;
582    const int ib32 = il/2;
583    il = il%2;
584    // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
585    device const uint8_t * qs = xb->qs + 8*ib32;
586    device const uint8_t * signs = xb->signs + 4*ib32 + 2*il;
587    const uint8_t qh = xb->qh[ib32] >> 4*il;
588    const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf));
589    constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256)));
590    constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256)));
591    for (int i = 0; i < 4; ++i) {
592        reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]);
593        reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]);
594    }
595    grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256)));
596    grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256)));
597    for (int i = 0; i < 4; ++i) {
598        reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]);
599        reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]);
600    }
601}
602
603template <typename type4x4>
604void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) {
605    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
606    const float d = xb->d;
607    const int ib32 = il/2;
608    il = il%2;
609    // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
610    device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
611    device const uint8_t * signs = qs + QK_K/8;
612    const uint8_t qh = xb->qh[ib32] >> 4*il;
613    const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f;
614    constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300)));
615    constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300)));
616    for (int i = 0; i < 8; ++i) {
617        reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]);
618        reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]);
619    }
620}
621
622template <typename type4x4>
623void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) {
624    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
625    const int ib32 = il/2;
626    il = il%2;
627    const float d = xb->d;
628    device const uint8_t  * qs = xb->qs + 4*ib32 + 2*il;
629    device const uint16_t * qh = xb->qh;
630    const float dl = d * (2*((qh[ib32] >> 12) & 7) + 1);
631    const float ml = dl * (qh[ib32] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA);
632    const uint16_t h = qh[ib32] >> 6*il;
633    constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((h << 8) & 0x700)));
634    constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((h << 5) & 0x700)));
635    for (int i = 0; i < 4; ++i) {
636        reg[0][i] = dl * (grid1[i] & 0xf) + ml;
637        reg[1][i] = dl * (grid1[i] >>  4) + ml;
638        reg[2][i] = dl * (grid2[i] & 0xf) + ml;
639        reg[3][i] = dl * (grid2[i] >>  4) + ml;
640    }
641}
642
643template <typename type4x4>
644void dequantize_iq1_m(device const block_iq1_m * xb, short il, thread type4x4 & reg) {
645    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
646    const int ib32 = il/2;
647    il = il%2;
648    device const uint16_t * sc = (device const uint16_t *)xb->scales;
649
650    iq1m_scale_t scale;
651    scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
652    const float d = scale.f16;
653
654    device const uint8_t * qs = xb->qs + 4*ib32 + 2*il;
655    device const uint8_t * qh = xb->qh + 2*ib32 + il;
656
657    const float dl  = d * (2*((sc[ib32/2] >> (6*(ib32%2)+3*il)) & 7) + 1);
658    const float ml1 = dl * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
659    const float ml2 = dl * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA);
660    constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700)));
661    constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700)));
662    for (int i = 0; i < 4; ++i) {
663        reg[0][i] = dl * (grid1[i] & 0xf) + ml1;
664        reg[1][i] = dl * (grid1[i] >>  4) + ml1;
665        reg[2][i] = dl * (grid2[i] & 0xf) + ml2;
666        reg[3][i] = dl * (grid2[i] >>  4) + ml2;
667    }
668}
669
670template <typename type4x4>
671void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) {
672    device const uint16_t * q4 = (device const uint16_t *)xb->qs;
673    const float d = xb->d;
674    uint32_t aux32;
675    thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
676    for (int i = 0; i < 4; ++i) {
677        aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f;
678        reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
679        reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
680        reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
681        reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
682    }
683}
684
685template <typename type4>
686void dequantize_iq4_nl_t4(device const block_iq4_nl * xb, short il, thread type4 & reg) {
687    device const uint16_t * q4 = (device const uint16_t *)xb->qs;
688    const float d = xb->d;
689    uint32_t aux32;
690    thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
691    aux32 = ((q4[2*(il%4)] | (q4[2*(il%4)+1] << 16)) >> 4*(il/4)) & 0x0f0f0f0f;
692    reg[0] = d * kvalues_iq4nl_f[q8[0]];
693    reg[1] = d * kvalues_iq4nl_f[q8[1]];
694    reg[2] = d * kvalues_iq4nl_f[q8[2]];
695    reg[3] = d * kvalues_iq4nl_f[q8[3]];
696}
697
698template <typename type4x4>
699void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) {
700    // il is 0...15 for QK_K = 256 => index of block of 32 is il/2
701    const int ib32 = il/2;
702    il = il%2;
703    // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16
704    device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32;
705    const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4);
706    const float d = (float)xb->d * (ls - 32);
707    uint32_t aux32;
708    thread const uint8_t * q8 = (thread const uint8_t *)&aux32;
709    for (int i = 0; i < 4; ++i) {
710        aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f;
711        reg[i][0] = d * kvalues_iq4nl_f[q8[0]];
712        reg[i][1] = d * kvalues_iq4nl_f[q8[1]];
713        reg[i][2] = d * kvalues_iq4nl_f[q8[2]];
714        reg[i][3] = d * kvalues_iq4nl_f[q8[3]];
715    }
716}
717
718template <typename type4x4>
719void dequantize_tq2_0(device const block_tq2_0 * xb, short il, thread type4x4 & reg) {
720    device const uint8_t * qs = xb->qs;
721    const float d = xb->d;
722
723    float4x4 reg_f;
724
725    // 2 bits per element, 4 elements per byte, 128 elements per 32-byte group
726    const short base = il * 16;
727    for (int k = 0; k < 16; k++) {
728        const int i = base + k;
729        const int byte = ((i >> 7) & 1) * 32 + (i & 31);
730        const int l = (i >> 5) & 3;
731        reg_f[k/4][k%4] = d * (float)(((qs[byte] >> (2*l)) & 3) - 1);
732    }
733
734    reg = (type4x4) reg_f;
735}