Coverage Report

Created: 2026-03-21 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/ggml/src/ggml-cpu/quants.c
Line
Count
Source
1
#define GGML_COMMON_IMPL_C
2
#include "ggml-common.h"
3
4
#include "ggml-cpu-impl.h"
5
#include "simd-mappings.h"
6
#include "ggml-quants.h"
7
#include "quants.h"
8
9
#include "arch-fallback.h"
10
11
#include <string.h>
12
#include <assert.h>
13
#include <float.h>
14
#include <stdlib.h> // for qsort
15
#include <stdio.h>  // for GGML_ASSERT
16
17
#define GROUP_MAX_EPS 1e-15f
18
#define GROUP_MAX_EPS_IQ3_XXS 1e-8f
19
#define GROUP_MAX_EPS_IQ2_S 1e-8f
20
#define GROUP_MAX_EPS_IQ1_M 1e-7f
21
#define GROUP_MAX_EPS_IQ1_S 1e-12f
22
23
0
#define UNUSED GGML_UNUSED
24
25
0
void quantize_row_q4_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
26
0
    quantize_row_q4_0_ref(x, y, k);
27
0
}
28
29
0
void quantize_row_q4_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
30
0
    quantize_row_q4_1_ref(x, y, k);
31
0
}
32
33
0
void quantize_row_q5_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
34
0
    quantize_row_q5_0_ref(x, y, k);
35
0
}
36
37
0
void quantize_row_q5_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
38
0
    quantize_row_q5_1_ref(x, y, k);
39
0
}
40
41
0
void quantize_row_q8_0_generic(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
42
0
    quantize_row_q8_0_ref(x, y, k);
43
0
}
44
45
0
void quantize_row_q8_1_generic(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
46
0
    quantize_row_q8_1_ref(x, y, k);
47
0
}
48
49
0
void quantize_row_mxfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
50
0
    quantize_row_mxfp4_ref(x, y, k);
51
0
}
52
53
0
void quantize_row_nvfp4(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
54
0
    quantize_row_nvfp4_ref(x, y, k);
55
0
}
56
57
//
58
// 2-6 bit quantization in super-blocks
59
//
60
61
//========================- 2-bit (de)-quantization
62
63
0
void quantize_row_q2_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
64
0
    quantize_row_q2_K_ref(x, vy, k);
65
0
}
66
67
//========================= 3-bit (de)-quantization
68
69
0
void quantize_row_q3_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
70
0
    quantize_row_q3_K_ref(x, vy, k);
71
0
}
72
73
// ====================== 4-bit (de)-quantization
74
75
0
void quantize_row_q4_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
76
0
    assert(k % QK_K == 0);
77
0
    block_q4_K * GGML_RESTRICT y = vy;
78
0
    quantize_row_q4_K_ref(x, y, k);
79
0
}
80
81
// ====================== 5-bit (de)-quantization
82
83
0
void quantize_row_q5_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
84
0
    assert(k % QK_K == 0);
85
0
    block_q5_K * GGML_RESTRICT y = vy;
86
0
    quantize_row_q5_K_ref(x, y, k);
87
0
}
88
89
// ====================== 6-bit (de)-quantization
90
91
0
void quantize_row_q6_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
92
0
    assert(k % QK_K == 0);
93
0
    block_q6_K * GGML_RESTRICT y = vy;
94
0
    quantize_row_q6_K_ref(x, y, k);
95
0
}
96
97
// ====================== Ternary (de)-quantization (BitNet b1.58 and TriLMs)
98
99
0
void quantize_row_tq1_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
100
0
    assert(k % QK_K == 0);
101
0
    block_tq1_0 * GGML_RESTRICT y = vy;
102
0
    quantize_row_tq1_0_ref(x, y, k);
103
0
}
104
105
0
void quantize_row_tq2_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
106
0
    assert(k % QK_K == 0);
107
0
    block_tq2_0 * GGML_RESTRICT y = vy;
108
0
    quantize_row_tq2_0_ref(x, y, k);
109
0
}
110
111
//===================================== Q8_K ==============================================
112
113
0
void quantize_row_q8_K_generic(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
114
0
    quantize_row_q8_K_ref(x, y, k);
115
0
}
116
117
//===================================== Dot products =================================
118
119
0
void ggml_vec_dot_q4_0_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
120
0
    const int qk = QK8_0;
121
0
    const int nb = n / qk;
122
123
0
    assert(n % qk == 0);
124
0
    assert(nrc == 1);
125
0
    UNUSED(nrc);
126
0
    UNUSED(bx);
127
0
    UNUSED(by);
128
0
    UNUSED(bs);
129
130
0
    const block_q4_0 * GGML_RESTRICT x = vx;
131
0
    const block_q8_0 * GGML_RESTRICT y = vy;
132
133
0
    int ib = 0;
134
0
    float sumf = 0;
135
136
0
    for (; ib < nb; ++ib) {
137
0
        int sumi0 = 0;
138
0
        int sumi1 = 0;
139
140
0
        for (int j = 0; j < qk/2; ++j) {
141
0
            const int v0 = (x[ib].qs[j] & 0x0F) - 8;
142
0
            const int v1 = (x[ib].qs[j] >>   4) - 8;
143
144
0
            sumi0 += (v0 * y[ib].qs[j]);
145
0
            sumi1 += (v1 * y[ib].qs[j + qk/2]);
146
0
        }
147
148
0
        int sumi = sumi0 + sumi1;
149
0
        sumf += sumi*GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d);
150
0
    }
151
152
0
    *s = sumf;
153
0
}
154
155
// TODO: add WASM SIMD
156
0
void ggml_vec_dot_q4_1_q8_1_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
157
0
    const int qk = QK8_1;
158
0
    const int nb = n / qk;
159
160
0
    assert(n % qk == 0);
161
0
    assert(nrc == 1);
162
0
    UNUSED(nrc);
163
0
    UNUSED(bx);
164
0
    UNUSED(by);
165
0
    UNUSED(bs);
166
167
0
    const block_q4_1 * GGML_RESTRICT x = vx;
168
0
    const block_q8_1 * GGML_RESTRICT y = vy;
169
170
0
    int ib = 0;
171
0
    float sumf = 0;
172
173
0
    for (; ib < nb; ++ib) {
174
0
        int sumi0 = 0;
175
0
        int sumi1 = 0;
176
177
0
        for (int j = 0; j < qk/2; ++j) {
178
0
            const int v0 = (x[ib].qs[j] & 0x0F);
179
0
            const int v1 = (x[ib].qs[j] >>   4);
180
181
0
            sumi0 += (v0 * y[ib].qs[j]);
182
0
            sumi1 += (v1 * y[ib].qs[j + qk/2]);
183
0
        }
184
185
0
        int sumi = sumi0 + sumi1;
186
0
        sumf += (GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d))*sumi + GGML_CPU_FP16_TO_FP32(x[ib].m)*GGML_CPU_FP16_TO_FP32(y[ib].s);
187
0
    }
188
189
0
    *s = sumf;
190
0
}
191
192
0
void ggml_vec_dot_mxfp4_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
193
0
    assert(nrc == 1);
194
0
    UNUSED(nrc);
195
0
    UNUSED(bx);
196
0
    UNUSED(by);
197
0
    UNUSED(bs);
198
0
    assert(n % QK_MXFP4 == 0);
199
0
    static_assert(QK_MXFP4 == QK8_0, "QK_MXFP4 and QK8_0 must be the same");
200
201
0
    const block_mxfp4 * GGML_RESTRICT x = vx;
202
0
    const block_q8_0 * GGML_RESTRICT y = vy;
203
204
0
    const int nb = n / QK_MXFP4;
205
206
0
    int ib = 0;
207
0
    float sumf = 0;
208
209
0
    for (; ib < nb; ++ib) {
210
0
        const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_E8M0_TO_FP32_HALF(x[ib].e);
211
212
0
        int sumi1 = 0;
213
0
        int sumi2 = 0;
214
0
        for (int j = 0; j < QK_MXFP4/2; ++j) {
215
0
            sumi1 += y[ib].qs[j +          0] * kvalues_mxfp4[x[ib].qs[j] & 0xf];
216
0
            sumi2 += y[ib].qs[j + QK_MXFP4/2] * kvalues_mxfp4[x[ib].qs[j] >>  4];
217
0
        }
218
0
        sumf += d * (sumi1 + sumi2);
219
0
    }
220
0
    *s = sumf;
221
0
}
222
223
// NVFP4: super-block of 64 elements = 4 sub-blocks of 16 = 2 q8_0 blocks
224
0
void ggml_vec_dot_nvfp4_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
225
0
    assert(nrc == 1);
226
0
    UNUSED(nrc);
227
0
    UNUSED(bx);
228
0
    UNUSED(by);
229
0
    UNUSED(bs);
230
0
    assert(n % QK_NVFP4 == 0);
231
232
0
    const block_nvfp4 * GGML_RESTRICT x = vx;
233
0
    const block_q8_0 * GGML_RESTRICT y = vy;
234
235
0
    const int nb = n / QK_NVFP4;
236
237
0
    float sumf = 0;
238
239
0
    for (int ib = 0; ib < nb; ++ib) {
240
0
        for (int s_idx = 0; s_idx < 4; ++s_idx) {
241
0
            const float d = ggml_ue4m3_to_fp32(x[ib].d[s_idx]);
242
0
            const int q8_block = s_idx / 2;
243
0
            const int q8_off   = (s_idx % 2) * QK_NVFP4_SUB;
244
0
            const float dy = GGML_CPU_FP16_TO_FP32(y[2*ib + q8_block].d);
245
246
0
            int sumi_lo = 0, sumi_hi = 0;
247
0
            for (int j = 0; j < QK_NVFP4_SUB/2; ++j) {
248
0
                const uint8_t qv = x[ib].qs[s_idx*(QK_NVFP4_SUB/2) + j];
249
0
                sumi_lo += y[2*ib + q8_block].qs[q8_off + j +               0] * kvalues_mxfp4[qv & 0xf];
250
0
                sumi_hi += y[2*ib + q8_block].qs[q8_off + j + QK_NVFP4_SUB/2] * kvalues_mxfp4[qv >>  4];
251
0
            }
252
253
0
            sumf += dy * d * (sumi_lo + sumi_hi);
254
0
        }
255
0
    }
256
0
    *s = sumf;
257
0
}
258
259
0
void ggml_vec_dot_q5_0_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
260
0
    const int qk = QK8_0;
261
0
    const int nb = n / qk;
262
263
0
    int ib = 0;
264
0
    float sumf = 0;
265
266
0
    assert(n % qk == 0);
267
0
    assert(qk == QK5_0);
268
0
    assert(nrc == 1);
269
0
    UNUSED(nrc);
270
0
    UNUSED(bx);
271
0
    UNUSED(by);
272
0
    UNUSED(bs);
273
274
0
    const block_q5_0 * GGML_RESTRICT x = vx;
275
0
    const block_q8_0 * GGML_RESTRICT y = vy;
276
277
0
    for (; ib < nb; ++ib) {
278
0
        uint32_t qh;
279
0
        memcpy(&qh, x[ib].qh, sizeof(qh));
280
281
0
        int sumi0 = 0;
282
0
        int sumi1 = 0;
283
284
0
        for (int j = 0; j < qk/2; ++j) {
285
0
            const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
286
0
            const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
287
288
0
            const int32_t x0 = (int8_t)(((x[ib].qs[j] & 0x0F) | xh_0) - 16);
289
0
            const int32_t x1 = (int8_t)(((x[ib].qs[j] >>   4) | xh_1) - 16);
290
291
0
            sumi0 += (x0 * y[ib].qs[j]);
292
0
            sumi1 += (x1 * y[ib].qs[j + qk/2]);
293
0
        }
294
295
0
        int sumi = sumi0 + sumi1;
296
0
        sumf += (GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d)) * sumi;
297
0
    }
298
299
0
    *s = sumf;
300
0
}
301
302
0
void ggml_vec_dot_q5_1_q8_1_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
303
0
    const int qk = QK8_1;
304
0
    const int nb = n / qk;
305
306
0
    int ib = 0;
307
0
    float sumf = 0;
308
309
0
    assert(n % qk == 0);
310
0
    assert(qk == QK5_1);
311
0
    assert(nrc == 1);
312
0
    UNUSED(nrc);
313
0
    UNUSED(bx);
314
0
    UNUSED(by);
315
0
    UNUSED(bs);
316
317
0
    const block_q5_1 * GGML_RESTRICT x = vx;
318
0
    const block_q8_1 * GGML_RESTRICT y = vy;
319
320
0
    for (; ib < nb; ++ib) {
321
0
        uint32_t qh;
322
0
        memcpy(&qh, x[ib].qh, sizeof(qh));
323
324
0
        int sumi0 = 0;
325
0
        int sumi1 = 0;
326
327
0
        for (int j = 0; j < qk/2; ++j) {
328
0
            const uint8_t xh_0 = ((qh >> (j +  0)) << 4) & 0x10;
329
0
            const uint8_t xh_1 = ((qh >> (j + 12))     ) & 0x10;
330
331
0
            const int32_t x0 = (x[ib].qs[j] & 0xF) | xh_0;
332
0
            const int32_t x1 = (x[ib].qs[j] >>  4) | xh_1;
333
334
0
            sumi0 += (x0 * y[ib].qs[j]);
335
0
            sumi1 += (x1 * y[ib].qs[j + qk/2]);
336
0
        }
337
338
0
        int sumi = sumi0 + sumi1;
339
0
        sumf += (GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d))*sumi + GGML_CPU_FP16_TO_FP32(x[ib].m)*GGML_CPU_FP16_TO_FP32(y[ib].s);
340
0
    }
341
342
0
    *s = sumf;
343
0
}
344
345
0
void ggml_vec_dot_q8_0_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
346
0
    const int qk = QK8_0;
347
0
    const int nb = n / qk;
348
349
0
    assert(n % qk == 0);
350
0
    assert(nrc == 1);
351
0
    UNUSED(nrc);
352
0
    UNUSED(bx);
353
0
    UNUSED(by);
354
0
    UNUSED(bs);
355
356
0
    const block_q8_0 * GGML_RESTRICT x = vx;
357
0
    const block_q8_0 * GGML_RESTRICT y = vy;
358
359
0
    int ib = 0;
360
0
    float sumf = 0;
361
362
0
    for (; ib < nb; ++ib) {
363
0
        int sumi = 0;
364
365
0
        for (int j = 0; j < qk; j++) {
366
0
            sumi += x[ib].qs[j]*y[ib].qs[j];
367
0
        }
368
369
0
        sumf += sumi*(GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d));
370
0
    }
371
372
0
    *s = sumf;
373
0
}
374
375
0
void ggml_vec_dot_tq1_0_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
376
0
    assert(nrc == 1);
377
0
    UNUSED(nrc);
378
0
    UNUSED(bx);
379
0
    UNUSED(by);
380
0
    UNUSED(bs);
381
382
0
    const block_tq1_0 * GGML_RESTRICT x = vx;
383
0
    const block_q8_K  * GGML_RESTRICT y = vy;
384
385
0
    const int nb = n / QK_K;
386
387
0
    const uint8_t pow3[6] = {1, 3, 9, 27, 81, 243};
388
389
0
    float sumf = 0.0f;
390
391
0
    for (int i = 0; i < nb; ++i) {
392
0
        int sum = 0;
393
394
0
        for (size_t j = 0; j < sizeof(x->qs) - sizeof(x->qs) % 32; j += 32) {
395
0
            for (size_t l = 0; l < 5; ++l) {
396
0
                for (size_t m = 0; m < 32; ++m) {
397
0
                    uint8_t q = x[i].qs[j + m] * pow3[l];
398
0
                    uint16_t xi = ((uint16_t) q * 3) >> 8;
399
0
                    sum += (xi - 1) * y[i].qs[j*5 + l*32 + m];
400
0
                }
401
0
            }
402
0
        }
403
0
        for (size_t j = sizeof(x->qs) - sizeof(x->qs) % 32; j < sizeof(x->qs); j += 16) {
404
0
            for (size_t l = 0; l < 5; ++l) {
405
0
                for (size_t m = 0; m < 16; ++m) {
406
0
                    uint8_t q = x[i].qs[j + m] * pow3[l];
407
0
                    uint16_t xi = ((uint16_t) q * 3) >> 8;
408
0
                    sum += (xi - 1) * y[i].qs[j*5 + l*16 + m];
409
0
                }
410
0
            }
411
0
        }
412
413
0
        for (size_t l = 0; l < 4; ++l) {
414
0
            for (size_t j = 0; j < sizeof(x->qh); ++j) {
415
0
                uint8_t q = x[i].qh[j] * pow3[l];
416
0
                uint16_t xi = ((uint16_t) q * 3) >> 8;
417
0
                sum += (xi - 1) * y[i].qs[sizeof(x->qs)*5 + l*sizeof(x->qh) + j];
418
0
            }
419
0
        }
420
421
0
        sumf += (float) sum * (GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d);
422
0
    }
423
424
0
    *s = sumf;
425
0
}
426
427
0
void ggml_vec_dot_tq2_0_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
428
0
    assert(nrc == 1);
429
0
    UNUSED(nrc);
430
0
    UNUSED(bx);
431
0
    UNUSED(by);
432
0
    UNUSED(bs);
433
434
0
    const block_tq2_0 * GGML_RESTRICT x = vx;
435
0
    const block_q8_K  * GGML_RESTRICT y = vy;
436
437
0
    const int nb = n / QK_K;
438
0
    float sumf = 0.0f;
439
440
0
    for (int i = 0; i < nb; ++i) {
441
0
        int32_t sumi = 0;
442
443
0
        for (size_t j = 0; j < sizeof(x->qs); j += 32) {
444
0
            for (size_t l = 0; l < 4; ++l) {
445
0
                for (size_t k = 0; k < 32; ++k) {
446
0
                    sumi += y[i].qs[j*4 + l*32 + k] * (((x[i].qs[j + k] >> (l*2)) & 3) - 1);
447
0
                }
448
0
            }
449
0
        }
450
451
0
        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
452
453
0
        sumf += (float) sumi * d;
454
0
    }
455
456
0
    *s = sumf;
457
0
}
458
459
0
void ggml_vec_dot_q2_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
460
0
    assert(nrc == 1);
461
0
    UNUSED(nrc);
462
0
    UNUSED(bx);
463
0
    UNUSED(by);
464
0
    UNUSED(bs);
465
466
0
    const block_q2_K * GGML_RESTRICT x = vx;
467
0
    const block_q8_K * GGML_RESTRICT y = vy;
468
469
0
    const int nb = n / QK_K;
470
471
0
    float sumf = 0;
472
473
0
    for (int i = 0; i < nb; ++i) {
474
475
0
        const uint8_t * q2 = x[i].qs;
476
0
        const  int8_t * q8 = y[i].qs;
477
0
        const uint8_t * sc = x[i].scales;
478
479
0
        int summs = 0;
480
0
        for (int j = 0; j < 16; ++j) {
481
0
            summs += y[i].bsums[j] * (sc[j] >> 4);
482
0
        }
483
484
0
        const float dall = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
485
0
        const float dmin = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
486
487
0
        int isum = 0;
488
0
        int is = 0;
489
0
        int d;
490
0
        for (int k = 0; k < QK_K/128; ++k) {
491
0
            int shift = 0;
492
0
            for (int j = 0; j < 4; ++j) {
493
0
                d = sc[is++] & 0xF;
494
0
                int isuml = 0;
495
0
                for (int l =  0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
496
0
                isum += d * isuml;
497
0
                d = sc[is++] & 0xF;
498
0
                isuml = 0;
499
0
                for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
500
0
                isum += d * isuml;
501
0
                shift += 2;
502
0
                q8 += 32;
503
0
            }
504
0
            q2 += 32;
505
0
        }
506
0
        sumf += dall * isum - dmin * summs;
507
0
    }
508
0
    *s = sumf;
509
0
}
510
511
0
void ggml_vec_dot_q3_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
512
0
    assert(n % QK_K == 0);
513
0
    assert(nrc == 1);
514
0
    UNUSED(nrc);
515
0
    UNUSED(bx);
516
0
    UNUSED(by);
517
0
    UNUSED(bs);
518
519
0
    const uint32_t kmask1 = 0x03030303;
520
0
    const uint32_t kmask2 = 0x0f0f0f0f;
521
522
0
    const block_q3_K * GGML_RESTRICT x = vx;
523
0
    const block_q8_K * GGML_RESTRICT y = vy;
524
525
0
    const int nb = n / QK_K;
526
527
    // scalar version
528
    // This function is written like this so the compiler can manage to vectorize most of it
529
    // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
530
    // manually vectorized version above. Every other version I tried would run at least 4 times slower.
531
    // The ideal situation would be if we could just write the code once, and the compiler would
532
    // automatically produce the best possible set of machine instructions, instead of us having to manually
533
    // write vectorized versions for AVX, ARM_NEON, etc.
534
535
0
    int8_t  aux8[QK_K];
536
0
    int16_t aux16[8];
537
0
    float   sums [8];
538
0
    int32_t aux32[8];
539
0
    memset(sums, 0, 8*sizeof(float));
540
541
0
    uint32_t auxs[4];
542
0
    const int8_t * scales = (const int8_t*)auxs;
543
544
0
    float sumf = 0;
545
0
    for (int i = 0; i < nb; ++i) {
546
0
        const uint8_t * GGML_RESTRICT q3 = x[i].qs;
547
0
        const uint8_t * GGML_RESTRICT hm = x[i].hmask;
548
0
        const  int8_t * GGML_RESTRICT q8 = y[i].qs;
549
0
        memset(aux32, 0, 8*sizeof(int32_t));
550
0
        int8_t * GGML_RESTRICT a = aux8;
551
0
        uint8_t m = 1;
552
0
        for (int j = 0; j < QK_K; j += 128) {
553
0
            for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
554
0
            for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
555
0
            a += 32; m <<= 1;
556
0
            for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
557
0
            for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
558
0
            a += 32; m <<= 1;
559
0
            for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
560
0
            for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
561
0
            a += 32; m <<= 1;
562
0
            for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
563
0
            for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
564
0
            a += 32; m <<= 1;
565
0
            q3 += 32;
566
0
        }
567
0
        a = aux8;
568
569
0
        memcpy(auxs, x[i].scales, 12);
570
0
        uint32_t tmp = auxs[2];
571
0
        auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
572
0
        auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
573
0
        auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
574
0
        auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
575
0
        for (int j = 0; j < QK_K/16; ++j) {
576
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
577
0
            for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
578
0
            q8 += 8; a += 8;
579
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
580
0
            for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
581
0
            q8 += 8; a += 8;
582
0
        }
583
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
584
0
        for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
585
0
    }
586
0
    for (int l = 0; l < 8; ++l) sumf += sums[l];
587
0
    *s = sumf;
588
0
}
589
590
0
void ggml_vec_dot_q4_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
591
0
    assert(n % QK_K == 0);
592
0
    assert(nrc == 1);
593
0
    UNUSED(nrc);
594
0
    UNUSED(bx);
595
0
    UNUSED(by);
596
0
    UNUSED(bs);
597
598
0
    const block_q4_K * GGML_RESTRICT x = vx;
599
0
    const block_q8_K * GGML_RESTRICT y = vy;
600
601
0
    const int nb = n / QK_K;
602
603
0
    static const uint32_t kmask1 = 0x3f3f3f3f;
604
0
    static const uint32_t kmask2 = 0x0f0f0f0f;
605
0
    static const uint32_t kmask3 = 0x03030303;
606
607
0
    uint32_t utmp[4];
608
609
0
    const uint8_t * scales = (const uint8_t*)&utmp[0];
610
0
    const uint8_t * mins   = (const uint8_t*)&utmp[2];
611
612
0
    int8_t  aux8[QK_K];
613
0
    int16_t aux16[8];
614
0
    float   sums [8];
615
0
    int32_t aux32[8];
616
0
    memset(sums, 0, 8*sizeof(float));
617
618
0
    float sumf = 0;
619
0
    for (int i = 0; i < nb; ++i) {
620
0
        const uint8_t * GGML_RESTRICT q4 = x[i].qs;
621
0
        const  int8_t * GGML_RESTRICT q8 = y[i].qs;
622
0
        memset(aux32, 0, 8*sizeof(int32_t));
623
0
        int8_t * GGML_RESTRICT a = aux8;
624
0
        for (int j = 0; j < QK_K/64; ++j) {
625
0
            for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
626
0
            a += 32;
627
0
            for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l]  >> 4);
628
0
            a += 32; q4 += 32;
629
0
        }
630
0
        memcpy(utmp, x[i].scales, 12);
631
0
        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
632
0
        const uint32_t uaux = utmp[1] & kmask1;
633
0
        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
634
0
        utmp[2] = uaux;
635
0
        utmp[0] &= kmask1;
636
637
0
        int sumi = 0;
638
0
        for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
639
0
        a = aux8;
640
0
        int is = 0;
641
0
        for (int j = 0; j < QK_K/32; ++j) {
642
0
            int32_t scale = scales[is++];
643
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
644
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
645
0
            q8 += 8; a += 8;
646
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
647
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
648
0
            q8 += 8; a += 8;
649
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
650
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
651
0
            q8 += 8; a += 8;
652
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
653
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
654
0
            q8 += 8; a += 8;
655
0
        }
656
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
657
0
        for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
658
0
        const float dmin = GGML_CPU_FP16_TO_FP32(x[i].dmin) * y[i].d;
659
0
        sumf -= dmin * sumi;
660
0
    }
661
0
    for (int l = 0; l < 8; ++l) sumf += sums[l];
662
0
    *s = sumf;
663
0
}
664
665
0
void ggml_vec_dot_q5_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy,  size_t by, int nrc) {
666
0
    assert(n % QK_K == 0);
667
0
    assert(nrc == 1);
668
0
    UNUSED(nrc);
669
0
    UNUSED(bx);
670
0
    UNUSED(by);
671
0
    UNUSED(bs);
672
673
0
    const block_q5_K * GGML_RESTRICT x = vx;
674
0
    const block_q8_K * GGML_RESTRICT y = vy;
675
676
0
    const int nb = n / QK_K;
677
678
0
    static const uint32_t kmask1 = 0x3f3f3f3f;
679
0
    static const uint32_t kmask2 = 0x0f0f0f0f;
680
0
    static const uint32_t kmask3 = 0x03030303;
681
682
0
    uint32_t utmp[4];
683
684
0
    const uint8_t * scales = (const uint8_t*)&utmp[0];
685
0
    const uint8_t * mins   = (const uint8_t*)&utmp[2];
686
687
0
    int8_t  aux8[QK_K];
688
0
    int16_t aux16[8];
689
0
    float   sums [8];
690
0
    int32_t aux32[8];
691
0
    memset(sums, 0, 8*sizeof(float));
692
693
0
    float sumf = 0;
694
0
    for (int i = 0; i < nb; ++i) {
695
0
        const uint8_t * GGML_RESTRICT q4 = x[i].qs;
696
0
        const uint8_t * GGML_RESTRICT hm = x[i].qh;
697
0
        const  int8_t * GGML_RESTRICT q8 = y[i].qs;
698
0
        memset(aux32, 0, 8*sizeof(int32_t));
699
0
        int8_t * GGML_RESTRICT a = aux8;
700
0
        uint8_t m = 1;
701
0
        for (int j = 0; j < QK_K/64; ++j) {
702
0
            for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
703
0
            for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
704
0
            a += 32; m <<= 1;
705
0
            for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l]  >> 4);
706
0
            for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
707
0
            a += 32; m <<= 1;
708
0
            q4 += 32;
709
0
        }
710
0
        memcpy(utmp, x[i].scales, 12);
711
0
        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
712
0
        const uint32_t uaux = utmp[1] & kmask1;
713
0
        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
714
0
        utmp[2] = uaux;
715
0
        utmp[0] &= kmask1;
716
717
0
        int sumi = 0;
718
0
        for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
719
0
        a = aux8;
720
0
        int is = 0;
721
0
        for (int j = 0; j < QK_K/32; ++j) {
722
0
            int32_t scale = scales[is++];
723
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
724
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
725
0
            q8 += 8; a += 8;
726
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
727
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
728
0
            q8 += 8; a += 8;
729
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
730
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
731
0
            q8 += 8; a += 8;
732
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
733
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
734
0
            q8 += 8; a += 8;
735
0
        }
736
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
737
0
        for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
738
0
        const float dmin = GGML_CPU_FP16_TO_FP32(x[i].dmin) * y[i].d;
739
0
        sumf -= dmin * sumi;
740
0
    }
741
0
    for (int l = 0; l < 8; ++l) sumf += sums[l];
742
0
    *s = sumf;
743
0
}
744
745
0
void ggml_vec_dot_q6_K_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
746
0
    assert(n % QK_K == 0);
747
0
    assert(nrc == 1);
748
0
    UNUSED(nrc);
749
0
    UNUSED(bx);
750
0
    UNUSED(by);
751
0
    UNUSED(bs);
752
753
0
    const block_q6_K * GGML_RESTRICT x = vx;
754
0
    const block_q8_K * GGML_RESTRICT y = vy;
755
756
0
    const int nb = n / QK_K;
757
758
0
    int8_t  aux8[QK_K];
759
0
    int16_t aux16[8];
760
0
    float   sums [8];
761
0
    int32_t aux32[8];
762
0
    memset(sums, 0, 8*sizeof(float));
763
764
0
    float sumf = 0;
765
0
    for (int i = 0; i < nb; ++i) {
766
0
        const uint8_t * GGML_RESTRICT q4 = x[i].ql;
767
0
        const uint8_t * GGML_RESTRICT qh = x[i].qh;
768
0
        const  int8_t * GGML_RESTRICT q8 = y[i].qs;
769
0
        memset(aux32, 0, 8*sizeof(int32_t));
770
0
        int8_t * GGML_RESTRICT a = aux8;
771
0
        for (int j = 0; j < QK_K; j += 128) {
772
0
            for (int l = 0; l < 32; ++l) {
773
0
                a[l +  0] = (int8_t)((q4[l +  0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
774
0
                a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
775
0
                a[l + 64] = (int8_t)((q4[l +  0] >>  4) | (((qh[l] >> 4) & 3) << 4)) - 32;
776
0
                a[l + 96] = (int8_t)((q4[l + 32] >>  4) | (((qh[l] >> 6) & 3) << 4)) - 32;
777
0
            }
778
0
            a  += 128;
779
0
            q4 += 64;
780
0
            qh += 32;
781
0
        }
782
0
        a = aux8;
783
0
        int is = 0;
784
0
        for (int j = 0; j < QK_K/16; ++j) {
785
0
            int scale = x[i].scales[is++];
786
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
787
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
788
0
            q8 += 8; a += 8;
789
0
            for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
790
0
            for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
791
0
            q8 += 8; a += 8;
792
0
        }
793
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
794
0
        for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
795
0
    }
796
0
    for (int l = 0; l < 8; ++l) sumf += sums[l];
797
0
    *s = sumf;
798
0
}
799
800
0
void ggml_vec_dot_iq2_xxs_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
801
0
    assert(n % QK_K == 0);
802
0
    assert(nrc == 1);
803
0
    UNUSED(nrc);
804
0
    UNUSED(bx);
805
0
    UNUSED(by);
806
0
    UNUSED(bs);
807
808
0
    const block_iq2_xxs * GGML_RESTRICT x = vx;
809
0
    const block_q8_K    * GGML_RESTRICT y = vy;
810
811
0
    const int nb = n / QK_K;
812
813
0
    uint32_t aux32[2];
814
0
    const uint8_t * aux8 = (const uint8_t *)aux32;
815
816
0
    float sumf = 0.f;
817
0
    for (int i = 0; i < nb; ++i) {
818
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
819
0
        const uint16_t * GGML_RESTRICT q2 = x[i].qs;
820
0
        const int8_t   * GGML_RESTRICT q8 = y[i].qs;
821
0
        int32_t bsum = 0;
822
0
        for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
823
0
            memcpy(aux32, q2, 2*sizeof(uint32_t));
824
0
            q2 += 4;
825
0
            const uint32_t ls = 2*(aux32[1] >> 28) + 1;
826
0
            int32_t sumi = 0;
827
0
            for (int l = 0; l < 4; ++l) {
828
0
                const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
829
0
                const uint8_t  signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
830
0
                for (int j = 0; j < 8; ++j) {
831
0
                    sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
832
0
                }
833
0
                q8 += 8;
834
0
            }
835
0
            bsum += sumi * ls;
836
0
        }
837
0
        sumf += d * bsum;
838
0
    }
839
0
    *s = 0.125f * sumf;
840
0
}
841
842
0
void ggml_vec_dot_iq2_xs_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
843
0
    assert(n % QK_K == 0);
844
0
    assert(nrc == 1);
845
0
    UNUSED(nrc);
846
0
    UNUSED(bx);
847
0
    UNUSED(by);
848
0
    UNUSED(bs);
849
850
0
    const block_iq2_xs * GGML_RESTRICT x = vx;
851
0
    const block_q8_K   * GGML_RESTRICT y = vy;
852
853
0
    const int nb = n / QK_K;
854
855
0
    float sumf = 0.f;
856
0
    for (int i = 0; i < nb; ++i) {
857
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
858
0
        const uint16_t * GGML_RESTRICT q2 = x[i].qs;
859
0
        const uint8_t  * GGML_RESTRICT sc = x[i].scales;
860
0
        const int8_t   * GGML_RESTRICT q8 = y[i].qs;
861
0
        int32_t bsum = 0;
862
0
        for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
863
0
            const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
864
0
            const uint16_t ls2 = 2*(sc[ib32] >>  4) + 1;
865
0
            int32_t sumi = 0;
866
0
            for (int l = 0; l < 2; ++l) {
867
0
                const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
868
0
                const uint8_t  signs = ksigns_iq2xs[q2[l] >> 9];
869
0
                for (int j = 0; j < 8; ++j) {
870
0
                    sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
871
0
                }
872
0
                q8 += 8;
873
0
            }
874
0
            bsum += sumi * ls1;
875
0
            sumi = 0;
876
0
            for (int l = 2; l < 4; ++l) {
877
0
                const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
878
0
                const uint8_t  signs = ksigns_iq2xs[q2[l] >> 9];
879
0
                for (int j = 0; j < 8; ++j) {
880
0
                    sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
881
0
                }
882
0
                q8 += 8;
883
0
            }
884
0
            bsum += sumi * ls2;
885
0
            q2 += 4;
886
0
        }
887
0
        sumf += d * bsum;
888
0
    }
889
0
    *s = 0.125f * sumf;
890
0
}
891
892
0
void ggml_vec_dot_iq2_s_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
893
0
    assert(n % QK_K == 0);
894
0
    assert(nrc == 1);
895
0
    UNUSED(nrc);
896
0
    UNUSED(bx);
897
0
    UNUSED(by);
898
0
    UNUSED(bs);
899
900
0
    const block_iq2_s * GGML_RESTRICT x = vx;
901
0
    const block_q8_K  * GGML_RESTRICT y = vy;
902
903
0
    const int nb = n / QK_K;
904
905
0
    float sumf = 0;
906
0
    for (int i = 0; i < nb; i++) {
907
908
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
909
0
        const int8_t  * q8 = y[i].qs;
910
0
        const uint8_t * qs = x[i].qs;
911
0
        const uint8_t * qh = x[i].qh;
912
0
        const uint8_t * signs = qs + QK_K/8;
913
914
0
        int bsum = 0;
915
0
        for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
916
0
            int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
917
0
            int ls2 = 1 + 2*(x[i].scales[ib32] >>  4);
918
0
            int sumi1 = 0, sumi2 = 0;
919
0
            for (int l = 0; l < 2; ++l) {
920
0
                const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
921
0
                for (int j = 0; j < 8; ++j) {
922
0
                    sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
923
0
                }
924
0
                q8 += 8;
925
0
            }
926
0
            for (int l = 2; l < 4; ++l) {
927
0
                const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
928
0
                for (int j = 0; j < 8; ++j) {
929
0
                    sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
930
0
                }
931
0
                q8 += 8;
932
0
            }
933
0
            bsum += ls1 * sumi1 + ls2 * sumi2;
934
0
            qs += 4;
935
0
            signs += 4;
936
0
        }
937
938
0
        sumf += d * bsum;
939
0
    }
940
941
0
    *s = 0.125f * sumf;
942
0
}
943
944
0
void ggml_vec_dot_iq3_xxs_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
945
0
    assert(n % QK_K == 0);
946
0
    assert(nrc == 1);
947
0
    UNUSED(nrc);
948
0
    UNUSED(bx);
949
0
    UNUSED(by);
950
0
    UNUSED(bs);
951
952
0
    const block_iq3_xxs * GGML_RESTRICT x = vx;
953
0
    const block_q8_K    * GGML_RESTRICT y = vy;
954
955
0
    const int nb = n / QK_K;
956
957
0
    uint32_t aux32;
958
959
0
    float sumf = 0.f;
960
0
    for (int i = 0; i < nb; ++i) {
961
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
962
0
        const uint8_t * GGML_RESTRICT q3 = x[i].qs;
963
0
        const uint8_t * GGML_RESTRICT gas = x[i].qs + QK_K/4;
964
0
        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
965
0
        int32_t bsum = 0;
966
0
        for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
967
0
            memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
968
0
            const uint32_t ls = 2*(aux32 >> 28) + 1;
969
0
            int32_t sumi = 0;
970
0
            for (int l = 0; l < 4; ++l) {
971
0
                const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
972
0
                const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
973
0
                const uint8_t  signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
974
0
                for (int j = 0; j < 4; ++j) {
975
0
                    sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
976
0
                    sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
977
0
                }
978
0
                q8 += 8;
979
0
            }
980
0
            q3 += 8;
981
0
            bsum += sumi * ls;
982
0
        }
983
0
        sumf += d * bsum;
984
0
    }
985
0
    *s = 0.25f * sumf;
986
0
}
987
988
0
void ggml_vec_dot_iq3_s_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
989
0
    assert(n % QK_K == 0);
990
0
    assert(nrc == 1);
991
0
    UNUSED(nrc);
992
0
    UNUSED(bx);
993
0
    UNUSED(by);
994
0
    UNUSED(bs);
995
996
0
    const block_iq3_s * GGML_RESTRICT x = vx;
997
0
    const block_q8_K  * GGML_RESTRICT y = vy;
998
999
0
    const int nb = n / QK_K;
1000
1001
0
    float sumf = 0.f;
1002
0
    for (int i = 0; i < nb; ++i) {
1003
0
        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
1004
0
        const uint8_t * GGML_RESTRICT qs = x[i].qs;
1005
0
        const uint8_t * GGML_RESTRICT qh = x[i].qh;
1006
0
        const uint8_t * GGML_RESTRICT signs = x[i].signs;
1007
0
        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
1008
0
        int32_t bsum = 0;
1009
0
        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
1010
0
            const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
1011
0
            const uint32_t ls2 = 2*(x[i].scales[ib32/2] >>  4) + 1;
1012
0
            int32_t sumi = 0;
1013
0
            for (int l = 0; l < 4; ++l) {
1014
0
                const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
1015
0
                const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
1016
0
                for (int j = 0; j < 4; ++j) {
1017
0
                    sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
1018
0
                    sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
1019
0
                }
1020
0
                q8 += 8;
1021
0
            }
1022
0
            qs += 8;
1023
0
            signs += 4;
1024
0
            bsum += sumi * ls1;
1025
0
            sumi = 0;
1026
0
            for (int l = 0; l < 4; ++l) {
1027
0
                const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
1028
0
                const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
1029
0
                for (int j = 0; j < 4; ++j) {
1030
0
                    sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
1031
0
                    sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
1032
0
                }
1033
0
                q8 += 8;
1034
0
            }
1035
0
            qs += 8;
1036
0
            signs += 4;
1037
0
            bsum += sumi * ls2;
1038
0
        }
1039
0
        sumf += d * bsum;
1040
0
    }
1041
0
    *s = sumf;
1042
0
}
1043
1044
0
void ggml_vec_dot_iq1_s_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1045
0
    assert(n % QK_K == 0);
1046
0
    assert(nrc == 1);
1047
0
    UNUSED(nrc);
1048
0
    UNUSED(bx);
1049
0
    UNUSED(by);
1050
0
    UNUSED(bs);
1051
1052
0
    const block_iq1_s * GGML_RESTRICT x = vx;
1053
0
    const block_q8_K  * GGML_RESTRICT y = vy;
1054
1055
0
    const int nb = n / QK_K;
1056
1057
0
    float sumf = 0;
1058
0
    for (int i = 0; i < nb; i++) {
1059
1060
0
        const int8_t   * q8 = y[i].qs;
1061
0
        const uint8_t  * qs = x[i].qs;
1062
0
        const uint16_t * qh = x[i].qh;
1063
1064
0
        int sumi = 0, sumi1 = 0;
1065
0
        for (int ib = 0; ib < QK_K/32; ++ib) {
1066
0
            const int ls = 2*((qh[ib] >> 12) & 7) + 1;
1067
0
            const int delta = qh[ib] & 0x8000 ? -1 : 1;
1068
0
            int lsum = 0;
1069
0
            for (int l = 0; l < 4; ++l) {
1070
0
                const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
1071
0
                for (int j = 0; j < 8; ++j) {
1072
0
                    lsum += q8[j] * grid[j];
1073
0
                }
1074
0
                q8 += 8;
1075
0
            }
1076
0
            sumi  += ls * lsum;
1077
0
            sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
1078
0
            qs += 4;
1079
0
        }
1080
1081
0
        sumf += GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
1082
0
    }
1083
1084
0
    *s = sumf;
1085
0
}
1086
1087
0
void ggml_vec_dot_iq1_m_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1088
0
    assert(n % QK_K == 0);
1089
0
    assert(nrc == 1);
1090
0
    UNUSED(nrc);
1091
0
    UNUSED(bx);
1092
0
    UNUSED(by);
1093
0
    UNUSED(bs);
1094
1095
0
    const block_iq1_m * GGML_RESTRICT x = vx;
1096
0
    const block_q8_K  * GGML_RESTRICT y = vy;
1097
1098
0
    const int nb = n / QK_K;
1099
1100
0
    iq1m_scale_t scale;
1101
1102
0
    int sum1[2], sum2[2], delta[4];
1103
1104
0
    float sumf = 0;
1105
0
    for (int i = 0; i < nb; i++) {
1106
1107
0
        const int8_t   * q8 = y[i].qs;
1108
0
        const uint8_t  * qs = x[i].qs;
1109
0
        const uint8_t  * qh = x[i].qh;
1110
0
        const uint16_t * sc = (const uint16_t *)x[i].scales;
1111
1112
0
        scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
1113
1114
0
        int sumi1 = 0, sumi2 = 0;
1115
0
        for (int ib = 0; ib < QK_K/32; ++ib) {
1116
0
            delta[0] = qh[0] & 0x08 ? -1 : 1;
1117
0
            delta[1] = qh[0] & 0x80 ? -1 : 1;
1118
0
            delta[2] = qh[1] & 0x08 ? -1 : 1;
1119
0
            delta[3] = qh[1] & 0x80 ? -1 : 1;
1120
0
            sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
1121
0
            for (int l = 0; l < 4; ++l) {
1122
0
                const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
1123
0
                int lsum1 = 0, lsum2 = 0;
1124
0
                for (int j = 0; j < 8; ++j) {
1125
0
                    lsum1 += q8[j] * grid[j];
1126
0
                    lsum2 += q8[j];
1127
0
                }
1128
0
                q8 += 8;
1129
0
                sum1[l/2] += lsum1;
1130
0
                sum2[l/2] += lsum2*delta[l];
1131
0
            }
1132
1133
0
            const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
1134
0
            const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
1135
1136
0
            sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
1137
0
            sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
1138
0
            qs += 4;
1139
0
            qh += 2;
1140
0
        }
1141
1142
0
        sumf += GGML_CPU_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
1143
0
    }
1144
1145
0
    *s = sumf;
1146
0
}
1147
1148
0
void ggml_vec_dot_iq4_nl_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1149
0
    assert(nrc == 1);
1150
0
    UNUSED(nrc);
1151
0
    UNUSED(bx);
1152
0
    UNUSED(by);
1153
0
    UNUSED(bs);
1154
0
    assert(n % QK4_NL == 0);
1155
0
    static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
1156
1157
0
    const block_iq4_nl * GGML_RESTRICT x = vx;
1158
0
    const block_q8_0   * GGML_RESTRICT y = vy;
1159
1160
0
    const int nb = n / QK4_NL;
1161
1162
0
    int ib = 0;
1163
0
    float sumf = 0;
1164
1165
0
    for (; ib < nb; ++ib) {
1166
0
        const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_CPU_FP16_TO_FP32(x[ib].d);
1167
0
        int sumi1 = 0, sumi2 = 0;
1168
0
        for (int j = 0; j < QK4_NL/2; ++j) {
1169
0
            sumi1 += y[ib].qs[j+       0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
1170
0
            sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >>  4];
1171
0
        }
1172
0
        sumf += d * (sumi1 + sumi2);
1173
0
    }
1174
0
    *s = sumf;
1175
0
}
1176
1177
0
void ggml_vec_dot_iq4_xs_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1178
0
    assert(nrc == 1);
1179
0
    UNUSED(nrc);
1180
0
    UNUSED(bx);
1181
0
    UNUSED(by);
1182
0
    UNUSED(bs);
1183
0
    assert(n % QK_K == 0);
1184
1185
0
    const block_iq4_xs * GGML_RESTRICT x = vx;
1186
0
    const block_q8_K   * GGML_RESTRICT y = vy;
1187
1188
0
    const int nb = n / QK_K;
1189
1190
0
    float sumf = 0;
1191
0
    for (int ibl = 0; ibl < nb; ++ibl) {
1192
0
        const float d4d8 = GGML_CPU_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
1193
0
        uint16_t h = x[ibl].scales_h;
1194
0
        const uint8_t * qs = x[ibl].qs;
1195
0
        const int8_t  * q8 = y[ibl].qs;
1196
0
        for (int ib = 0; ib < QK_K/32; ib += 2) {
1197
0
            const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
1198
0
            const uint8_t ls2 = (x[ibl].scales_l[ib/2] >>  4) | ((h << 2) & 0x30);
1199
0
            h >>= 4;
1200
0
            const float d1 = d4d8*(ls1 - 32);
1201
0
            const float d2 = d4d8*(ls2 - 32);
1202
0
            int sumi1 = 0, sumi2 = 0;
1203
0
            for (int j = 0; j < 16; ++j) {
1204
0
                sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
1205
0
                sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >>  4];
1206
0
            }
1207
0
            sumf += d1 * (sumi1 + sumi2);
1208
0
            qs += 16;
1209
0
            q8 += 32;
1210
0
            sumi1 = sumi2 = 0;
1211
0
            for (int j = 0; j < 16; ++j) {
1212
0
                sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
1213
0
                sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >>  4];
1214
0
            }
1215
0
            sumf += d2 * (sumi1 + sumi2);
1216
0
            qs += 16;
1217
0
            q8 += 32;
1218
0
        }
1219
0
    }
1220
0
    *s = sumf;
1221
0
}
1222
1223
// ============================ 4-bit non-linear quants
1224
1225
0
void quantize_row_iq4_nl(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
1226
0
    assert(k % QK4_NL == 0);
1227
0
    quantize_row_iq4_nl_ref(x, y, k);
1228
0
}
1229
1230
0
void quantize_row_iq4_xs(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
1231
0
    assert(k % QK_K == 0);
1232
    quantize_iq4_xs(x, y, 1, k, NULL);
1233
0
}