interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-quants.c

5665 lines · 228271 bytes Raw Blame History
1#define GGML_COMMON_IMPL_C
2#include "ggml-common.h"
3
4#include "ggml-quants.h"
5#include "ggml-impl.h"
6#include "ggml-cpu/ggml-cpu-impl.h"
7#include "ggml-cpu.h"
8
9#include <math.h>
10#include <string.h>
11#include <assert.h>
12#include <float.h>
13#include <stdlib.h> // for qsort
14#include <stdio.h> // for GGML_ASSERT
15
16#ifdef GGML_USE_OPENMP
17#include <omp.h>
18#endif
19
20#define GROUP_MAX_EPS 1e-15f
21#define GROUP_MAX_EPS_IQ3_XXS 1e-8f
22#define GROUP_MAX_EPS_IQ2_S 1e-8f
23#define GROUP_MAX_EPS_IQ1_M 1e-7f
24#define GROUP_MAX_EPS_IQ1_S 1e-12f
25
26#define UNUSED GGML_UNUSED
27
28// index of the nearest kvalues_iq4nl value, with mid[k] = (values[k] + values[k+1])/2
29// for this table it matches a binary search with the tie rule x - values[k] < values[k+1] - x for every float x
30static inline int best_index_iq4nl(const float * GGML_RESTRICT mid, float x) {
31 int l = 15;
32 for (int k = 0; k < 15; ++k) l -= x < mid[k];
33 return l;
34}
35
36// reference implementation for deterministic creation of model files
37void quantize_row_q1_0_ref(const float * GGML_RESTRICT x, block_q1_0 * GGML_RESTRICT y, int64_t k) {
38 static const int qk = QK1_0;
39
40 assert(k % qk == 0);
41
42 const int nb = k / qk;
43
44 for (int i = 0; i < nb; i++) {
45 float sum_abs = 0.0f;
46 for (int j = 0; j < qk; j++) {
47 sum_abs += fabsf(x[i*qk + j]);
48 }
49 const float d = sum_abs / qk;
50
51 y[i].d = GGML_FP32_TO_FP16(d);
52
53 // Clear all bits first
54 for (int j = 0; j < qk / 8; ++j) {
55 y[i].qs[j] = 0;
56 }
57
58 // Just store sign of each weight directly (no normalization)
59 for (int j = 0; j < qk; ++j) {
60 const int bit_index = j;
61 const int byte_index = bit_index / 8;
62 const int bit_offset = bit_index % 8;
63
64 if (x[i*qk + j] >= 0.0f) {
65 y[i].qs[byte_index] |= (1 << bit_offset);
66 }
67 }
68 }
69}
70
71void quantize_row_q2_0_ref(const float * GGML_RESTRICT x, block_q2_0 * GGML_RESTRICT y, int64_t k) {
72 static const int qk = QK2_0;
73
74 assert(k % qk == 0);
75
76 const int nb = k / qk;
77
78 for (int i = 0; i < nb; i++) {
79 // Compute scale as max absolute value in the block
80 float amax = 0.0f;
81 for (int j = 0; j < qk; j++) {
82 const float a = fabsf(x[i*qk + j]);
83 if (a > amax) amax = a;
84 }
85 const float d = amax;
86 const float id = d > 0.0f ? 1.0f / d : 0.0f;
87
88 y[i].d = GGML_FP32_TO_FP16(d);
89
90 // Clear quant bytes
91 for (int j = 0; j < qk / 4; ++j) {
92 y[i].qs[j] = 0;
93 }
94
95 // Encode 2-bit values: round(w/d) clamped to [-1, 2], then add 1
96 // 00 (-1) = -scale, 01 (0) = 0, 10 (+1) = +scale, 11 (+2) = 2*scale
97 for (int j = 0; j < qk; ++j) {
98 const float w = x[i*qk + j];
99 int q = (int)roundf(w * id) + 1;
100 if (q < 0) q = 0;
101 if (q > 3) q = 3;
102 const int byte_index = j / 4;
103 const int bit_offset = (j % 4) * 2;
104 y[i].qs[byte_index] |= ((uint8_t)q << bit_offset);
105 }
106 }
107}
108
109// reference implementation for deterministic creation of model files
110void quantize_row_q4_0_ref(const float * GGML_RESTRICT x, block_q4_0 * GGML_RESTRICT y, int64_t k) {
111 static const int qk = QK4_0;
112
113 assert(k % qk == 0);
114
115 const int nb = k / qk;
116
117 for (int i = 0; i < nb; i++) {
118 float amax = 0.0f; // absolute max
119 float max = 0.0f;
120
121 for (int j = 0; j < qk; j++) {
122 const float v = x[i*qk + j];
123 if (amax < fabsf(v)) {
124 amax = fabsf(v);
125 max = v;
126 }
127 }
128
129 const float d = max / -8;
130 const float id = d ? 1.0f/d : 0.0f;
131
132 y[i].d = GGML_FP32_TO_FP16(d);
133
134 for (int j = 0; j < qk/2; ++j) {
135 const float x0 = x[i*qk + 0 + j]*id;
136 const float x1 = x[i*qk + qk/2 + j]*id;
137
138 const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
139 const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
140
141 y[i].qs[j] = xi0;
142 y[i].qs[j] |= xi1 << 4;
143 }
144 }
145}
146
147void quantize_row_q4_1_ref(const float * GGML_RESTRICT x, block_q4_1 * GGML_RESTRICT y, int64_t k) {
148 const int qk = QK4_1;
149
150 assert(k % qk == 0);
151
152 const int nb = k / qk;
153
154 for (int i = 0; i < nb; i++) {
155 float min = FLT_MAX;
156 float max = -FLT_MAX;
157
158 for (int j = 0; j < qk; j++) {
159 const float v = x[i*qk + j];
160
161 if (v < min) min = v;
162 if (v > max) max = v;
163 }
164
165 const float d = (max - min) / ((1 << 4) - 1);
166 const float id = d ? 1.0f/d : 0.0f;
167
168 y[i].d = GGML_FP32_TO_FP16(d);
169 y[i].m = GGML_FP32_TO_FP16(min);
170
171 for (int j = 0; j < qk/2; ++j) {
172 const float x0 = (x[i*qk + 0 + j] - min)*id;
173 const float x1 = (x[i*qk + qk/2 + j] - min)*id;
174
175 const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
176 const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
177
178 y[i].qs[j] = xi0;
179 y[i].qs[j] |= xi1 << 4;
180 }
181 }
182}
183
184void quantize_row_q5_0_ref(const float * GGML_RESTRICT x, block_q5_0 * GGML_RESTRICT y, int64_t k) {
185 static const int qk = QK5_0;
186
187 assert(k % qk == 0);
188
189 const int nb = k / qk;
190
191 for (int i = 0; i < nb; i++) {
192 float amax = 0.0f; // absolute max
193 float max = 0.0f;
194
195 for (int j = 0; j < qk; j++) {
196 const float v = x[i*qk + j];
197 if (amax < fabsf(v)) {
198 amax = fabsf(v);
199 max = v;
200 }
201 }
202
203 const float d = max / -16;
204 const float id = d ? 1.0f/d : 0.0f;
205
206 y[i].d = GGML_FP32_TO_FP16(d);
207
208 uint32_t qh = 0;
209
210 for (int j = 0; j < qk/2; ++j) {
211 const float x0 = x[i*qk + 0 + j]*id;
212 const float x1 = x[i*qk + qk/2 + j]*id;
213
214 const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
215 const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
216
217 y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
218
219 // get the 5-th bit and store it in qh at the right position
220 qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
221 qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
222 }
223
224 memcpy(&y[i].qh, &qh, sizeof(qh));
225 }
226}
227
228void quantize_row_q5_1_ref(const float * GGML_RESTRICT x, block_q5_1 * GGML_RESTRICT y, int64_t k) {
229 const int qk = QK5_1;
230
231 assert(k % qk == 0);
232
233 const int nb = k / qk;
234
235 for (int i = 0; i < nb; i++) {
236 float min = FLT_MAX;
237 float max = -FLT_MAX;
238
239 for (int j = 0; j < qk; j++) {
240 const float v = x[i*qk + j];
241
242 if (v < min) min = v;
243 if (v > max) max = v;
244 }
245
246 const float d = (max - min) / ((1 << 5) - 1);
247 const float id = d ? 1.0f/d : 0.0f;
248
249 y[i].d = GGML_FP32_TO_FP16(d);
250 y[i].m = GGML_FP32_TO_FP16(min);
251
252 uint32_t qh = 0;
253
254 for (int j = 0; j < qk/2; ++j) {
255 const float x0 = (x[i*qk + 0 + j] - min)*id;
256 const float x1 = (x[i*qk + qk/2 + j] - min)*id;
257
258 const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
259 const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
260
261 y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
262
263 // get the 5-th bit and store it in qh at the right position
264 qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
265 qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
266 }
267
268 memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
269 }
270}
271
272// reference implementation for deterministic creation of model files
273void quantize_row_q8_0_ref(const float * GGML_RESTRICT x, block_q8_0 * GGML_RESTRICT y, int64_t k) {
274 assert(k % QK8_0 == 0);
275 const int nb = k / QK8_0;
276
277 for (int i = 0; i < nb; i++) {
278 float amax = 0.0f; // absolute max
279
280 for (int j = 0; j < QK8_0; j++) {
281 const float v = x[i*QK8_0 + j];
282 amax = MAX(amax, fabsf(v));
283 }
284
285 const float d = amax / ((1 << 7) - 1);
286 const float id = d ? 1.0f/d : 0.0f;
287
288 y[i].d = GGML_FP32_TO_FP16(d);
289
290 for (int j = 0; j < QK8_0; ++j) {
291 const float x0 = x[i*QK8_0 + j]*id;
292
293 y[i].qs[j] = roundf(x0);
294 }
295 }
296}
297
298// reference implementation for deterministic creation of model files
299void quantize_row_q8_1_ref(const float * GGML_RESTRICT x, block_q8_1 * GGML_RESTRICT y, int64_t k) {
300 assert(QK8_1 == 32);
301 assert(k % QK8_1 == 0);
302 const int nb = k / QK8_1;
303
304 for (int i = 0; i < nb; i++) {
305 float amax = 0.0f; // absolute max
306
307 for (int j = 0; j < QK8_1; j++) {
308 const float v = x[i*QK8_1 + j];
309 amax = MAX(amax, fabsf(v));
310 }
311
312 const float d = amax / ((1 << 7) - 1);
313 const float id = d ? 1.0f/d : 0.0f;
314
315 y[i].d = GGML_FP32_TO_FP16(d);
316
317 int sum = 0;
318
319 for (int j = 0; j < QK8_1/2; ++j) {
320 const float v0 = x[i*QK8_1 + j]*id;
321 const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
322
323 y[i].qs[ j] = roundf(v0);
324 y[i].qs[QK8_1/2 + j] = roundf(v1);
325
326 sum += y[i].qs[ j];
327 sum += y[i].qs[QK8_1/2 + j];
328 }
329
330 y[i].s = GGML_FP32_TO_FP16(sum*d);
331 }
332}
333
334static inline int best_index_mxfp4(float x, float e) {
335 int best_index = 0;
336 float best_err = fabsf(kvalues_mxfp4[0]*e - x);
337 for (int i = 1; i < 16; i++) {
338 float err = fabsf(kvalues_mxfp4[i]*e - x);
339 if (err < best_err) {
340 best_index = i;
341 best_err = err;
342 }
343 }
344 return best_index;
345}
346
347void quantize_row_mxfp4_ref(const float * GGML_RESTRICT x, block_mxfp4 * GGML_RESTRICT y, int64_t k) {
348 static const int qk = QK_MXFP4;
349
350 assert(k % qk == 0);
351
352 const int nb = k / qk;
353
354 for (int i = 0; i < nb; i++) {
355 float amax = 0.0f; // absolute max
356
357 for (int j = 0; j < qk; j++) {
358 const float v = x[i*qk + j];
359
360 if (amax < fabsf(v)) {
361 amax = fabsf(v);
362 }
363 }
364
365 const uint8_t e = amax > 0.0f ? (uint8_t) (floorf(log2f(amax)) - 2 + 127) : 0;
366
367 const float d = GGML_E8M0_TO_FP32_HALF(e);
368
369 y[i].e = e;
370
371 for (int j = 0; j < qk/2; ++j) {
372 const uint8_t x0 = best_index_mxfp4(x[i*qk + 0 + j], d);
373 const uint8_t x1 = best_index_mxfp4(x[i*qk + qk/2 + j], d);
374
375 y[i].qs[j] = x0;
376 y[i].qs[j] |= x1 << 4;
377 }
378 }
379}
380
381void quantize_row_nvfp4_ref(const float * GGML_RESTRICT x, block_nvfp4 * GGML_RESTRICT y, int64_t k) {
382 static const int qk = QK_NVFP4;
383 static const int qk_sub = QK_NVFP4_SUB;
384 static const int n_sub = QK_NVFP4 / QK_NVFP4_SUB;
385
386 assert(k % qk == 0);
387
388 const int nb = k / qk;
389
390 for (int i = 0; i < nb; i++) {
391 for (int s = 0; s < n_sub; s++) {
392 const float * xb = x + i*qk + s*qk_sub;
393
394 float amax = 0.0f;
395 for (int j = 0; j < qk_sub; j++) {
396 if (amax < fabsf(xb[j])) {
397 amax = fabsf(xb[j]);
398 }
399 }
400
401 // UE4M3 scale: amax / 6.0 maps the max E2M1 value (6.0) to amax
402 const uint8_t ue = ggml_fp32_to_ue4m3(amax / 6.0f);
403 y[i].d[s] = ue;
404 const float d = ggml_ue4m3_to_fp32(ue);
405
406 for (int j = 0; j < qk_sub/2; ++j) {
407 const uint8_t x0 = best_index_mxfp4(xb[0 + j], d);
408 const uint8_t x1 = best_index_mxfp4(xb[qk_sub/2 + j], d);
409
410 y[i].qs[s*(qk_sub/2) + j] = x0 | (x1 << 4);
411 }
412 }
413 }
414}
415
416void quantize_row_f8_e4m3_ref(const float * GGML_RESTRICT x, ggml_fp8_e4m3_t * GGML_RESTRICT y, int64_t k) {
417 for (int64_t i = 0; i < k; ++i) {
418 y[i].bits = ggml_fp32_to_f8_e4m3(x[i]);
419 }
420}
421
422void dequantize_row_q1_0(const block_q1_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
423 static const int qk = QK1_0;
424
425 assert(k % qk == 0);
426
427 const int nb = k / qk;
428
429 for (int i = 0; i < nb; i++) {
430 const float d = GGML_FP16_TO_FP32(x[i].d);
431 const float neg_d = -d;
432
433 for (int j = 0; j < qk; ++j) {
434 const int byte_index = j / 8;
435 const int bit_offset = j % 8;
436 const uint8_t bit = (x[i].qs[byte_index] >> bit_offset) & 1;
437 y[i*qk + j] = bit ? d : neg_d;
438 }
439 }
440}
441
442void dequantize_row_q2_0(const block_q2_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
443 static const int qk = QK2_0;
444
445 assert(k % qk == 0);
446
447 const int nb = k / qk;
448
449 for (int i = 0; i < nb; i++) {
450 const float d = GGML_FP16_TO_FP32(x[i].d);
451
452 for (int j = 0; j < qk; ++j) {
453 const int byte_index = j / 4;
454 const int bit_offset = (j % 4) * 2;
455 const uint8_t q = (x[i].qs[byte_index] >> bit_offset) & 0x03;
456 // 00=-1, 01=0, 10=+1, 11=+2
457 y[i*qk + j] = ((int)q - 1) * d;
458 }
459 }
460}
461
462void dequantize_row_q4_0(const block_q4_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
463 static const int qk = QK4_0;
464
465 assert(k % qk == 0);
466
467 const int nb = k / qk;
468
469 for (int i = 0; i < nb; i++) {
470 const float d = GGML_FP16_TO_FP32(x[i].d);
471
472 for (int j = 0; j < qk/2; ++j) {
473 const int x0 = (x[i].qs[j] & 0x0F) - 8;
474 const int x1 = (x[i].qs[j] >> 4) - 8;
475
476 y[i*qk + j + 0 ] = x0*d;
477 y[i*qk + j + qk/2] = x1*d;
478 }
479 }
480}
481
482void dequantize_row_q4_1(const block_q4_1 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
483 static const int qk = QK4_1;
484
485 assert(k % qk == 0);
486
487 const int nb = k / qk;
488
489 for (int i = 0; i < nb; i++) {
490 const float d = GGML_FP16_TO_FP32(x[i].d);
491 const float m = GGML_FP16_TO_FP32(x[i].m);
492
493 for (int j = 0; j < qk/2; ++j) {
494 const int x0 = (x[i].qs[j] & 0x0F);
495 const int x1 = (x[i].qs[j] >> 4);
496
497 y[i*qk + j + 0 ] = x0*d + m;
498 y[i*qk + j + qk/2] = x1*d + m;
499 }
500 }
501}
502
503void dequantize_row_q5_0(const block_q5_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
504 static const int qk = QK5_0;
505
506 assert(k % qk == 0);
507
508 const int nb = k / qk;
509
510 for (int i = 0; i < nb; i++) {
511 const float d = GGML_FP16_TO_FP32(x[i].d);
512
513 uint32_t qh;
514 memcpy(&qh, x[i].qh, sizeof(qh));
515
516 for (int j = 0; j < qk/2; ++j) {
517 const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
518 const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
519
520 const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
521 const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
522
523 y[i*qk + j + 0 ] = x0*d;
524 y[i*qk + j + qk/2] = x1*d;
525 }
526 }
527}
528
529void dequantize_row_q5_1(const block_q5_1 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
530 static const int qk = QK5_1;
531
532 assert(k % qk == 0);
533
534 const int nb = k / qk;
535
536 for (int i = 0; i < nb; i++) {
537 const float d = GGML_FP16_TO_FP32(x[i].d);
538 const float m = GGML_FP16_TO_FP32(x[i].m);
539
540 uint32_t qh;
541 memcpy(&qh, x[i].qh, sizeof(qh));
542
543 for (int j = 0; j < qk/2; ++j) {
544 const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
545 const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
546
547 const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
548 const int x1 = (x[i].qs[j] >> 4) | xh_1;
549
550 y[i*qk + j + 0 ] = x0*d + m;
551 y[i*qk + j + qk/2] = x1*d + m;
552 }
553 }
554}
555
556void dequantize_row_q8_0(const block_q8_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
557 static const int qk = QK8_0;
558
559 assert(k % qk == 0);
560
561 const int nb = k / qk;
562
563 for (int i = 0; i < nb; i++) {
564 const float d = GGML_FP16_TO_FP32(x[i].d);
565
566 for (int j = 0; j < qk; ++j) {
567 y[i*qk + j] = x[i].qs[j]*d;
568 }
569 }
570}
571
572void dequantize_row_mxfp4(const block_mxfp4 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
573 static const int qk = QK_MXFP4;
574
575 assert(k % qk == 0);
576
577 const int nb = k / qk;
578
579 for (int i = 0; i < nb; i++) {
580 const float d = GGML_E8M0_TO_FP32_HALF(x[i].e);
581
582 for (int j = 0; j < qk/2; ++j) {
583 const int8_t x0 = kvalues_mxfp4[x[i].qs[j] & 0x0F];
584 const int8_t x1 = kvalues_mxfp4[x[i].qs[j] >> 4];
585
586 y[i*qk + j + 0 ] = x0*d;
587 y[i*qk + j + qk/2] = x1*d;
588 }
589 }
590}
591
592void dequantize_row_nvfp4(const block_nvfp4 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
593 static const int qk = QK_NVFP4;
594 static const int qk_sub = QK_NVFP4_SUB;
595 static const int n_sub = QK_NVFP4 / QK_NVFP4_SUB;
596
597 assert(k % qk == 0);
598
599 const int nb = k / qk;
600
601 for (int i = 0; i < nb; i++) {
602 for (int s = 0; s < n_sub; s++) {
603 const float d = ggml_ue4m3_to_fp32(x[i].d[s]);
604 float * yb = y + i*qk + s*qk_sub;
605
606 for (int j = 0; j < qk_sub/2; ++j) {
607 const int8_t v0 = kvalues_mxfp4[x[i].qs[s*(qk_sub/2) + j] & 0x0F];
608 const int8_t v1 = kvalues_mxfp4[x[i].qs[s*(qk_sub/2) + j] >> 4];
609
610 yb[j + 0 ] = v0*d;
611 yb[j + qk_sub/2] = v1*d;
612 }
613 }
614 }
615}
616
617void dequantize_row_f8_e4m3(const ggml_fp8_e4m3_t * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
618 for (int64_t i = 0; i < k; ++i) {
619 y[i] = ggml_f8_e4m3_to_fp32(x[i].bits);
620 }
621}
622
623//
624// 2-6 bit quantization in super-blocks
625//
626
627//
628// ===================== Helper functions
629//
630static inline int nearest_int(float fval) {
631 assert(fabsf(fval) <= 4194303.f);
632 float val = fval + 12582912.f;
633 int i; memcpy(&i, &val, sizeof(int));
634 return (i & 0x007fffff) - 0x00400000;
635}
636
637static float make_qx_quants(int n, int nmax, const float * GGML_RESTRICT x, int8_t * GGML_RESTRICT L, int rmse_type,
638 const float * GGML_RESTRICT qw) {
639 float max = 0;
640 float amax = 0;
641 for (int i = 0; i < n; ++i) {
642 float ax = fabsf(x[i]);
643 if (ax > amax) { amax = ax; max = x[i]; }
644 }
645 if (amax < GROUP_MAX_EPS) { // all zero
646 for (int i = 0; i < n; ++i) {
647 L[i] = 0;
648 }
649 return 0.f;
650 }
651 float iscale = -nmax / max;
652 if (rmse_type == 0) {
653 for (int i = 0; i < n; ++i) {
654 int l = nearest_int(iscale * x[i]);
655 L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
656 }
657 return 1/iscale;
658 }
659 bool return_early = false;
660 if (rmse_type < 0) {
661 rmse_type = -rmse_type;
662 return_early = true;
663 }
664 float sumlx = 0;
665 float suml2 = 0;
666#ifdef HAVE_BUGGY_APPLE_LINKER
667 // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
668 for (volatile int i = 0; i < n; ++i) {
669#else
670 for (int i = 0; i < n; ++i) {
671#endif
672 int l = nearest_int(iscale * x[i]);
673 l = MAX(-nmax, MIN(nmax-1, l));
674 L[i] = l + nmax;
675 float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
676 sumlx += w*x[i]*l;
677 suml2 += w*l*l;
678 }
679 float scale = suml2 ? sumlx/suml2 : 0.0f;
680 if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
681 float best = scale * sumlx;
682 for (int is = -9; is <= 9; ++is) {
683 if (is == 0) {
684 continue;
685 }
686 iscale = -(nmax + 0.1f*is) / max;
687 sumlx = suml2 = 0;
688 for (int i = 0; i < n; ++i) {
689 int l = nearest_int(iscale * x[i]);
690 l = MAX(-nmax, MIN(nmax-1, l));
691 float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
692 sumlx += w*x[i]*l;
693 suml2 += w*l*l;
694 }
695 if (suml2 > 0 && sumlx*sumlx > best*suml2) {
696 for (int i = 0; i < n; ++i) {
697 int l = nearest_int(iscale * x[i]);
698 L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
699 }
700 scale = sumlx/suml2; best = scale*sumlx;
701 }
702 }
703 return scale;
704}
705
706static float make_q3_quants(int n, int nmax, const float * GGML_RESTRICT x, int8_t * GGML_RESTRICT L, bool do_rmse) {
707 float max = 0;
708 float amax = 0;
709 for (int i = 0; i < n; ++i) {
710 float ax = fabsf(x[i]);
711 if (ax > amax) { amax = ax; max = x[i]; }
712 }
713 if (amax < GROUP_MAX_EPS) { // all zero
714 for (int i = 0; i < n; ++i) { L[i] = 0; }
715 return 0.f;
716 }
717 float iscale = -nmax / max;
718 if (do_rmse) {
719 float sumlx = 0;
720 float suml2 = 0;
721 for (int i = 0; i < n; ++i) {
722 int l = nearest_int(iscale * x[i]);
723 l = MAX(-nmax, MIN(nmax-1, l));
724 L[i] = l;
725 float w = x[i]*x[i];
726 sumlx += w*x[i]*l;
727 suml2 += w*l*l;
728 }
729 for (int itry = 0; itry < 5; ++itry) {
730 int n_changed = 0;
731 for (int i = 0; i < n; ++i) {
732 float w = x[i]*x[i];
733 float slx = sumlx - w*x[i]*L[i];
734 if (slx > 0) {
735 float sl2 = suml2 - w*L[i]*L[i];
736 int new_l = nearest_int(x[i] * sl2 / slx);
737 new_l = MAX(-nmax, MIN(nmax-1, new_l));
738 if (new_l != L[i]) {
739 slx += w*x[i]*new_l;
740 sl2 += w*new_l*new_l;
741 if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
742 L[i] = new_l; sumlx = slx; suml2 = sl2;
743 ++n_changed;
744 }
745 }
746 }
747 }
748 if (!n_changed) {
749 break;
750 }
751 }
752 for (int i = 0; i < n; ++i) {
753 L[i] += nmax;
754 }
755 return suml2 > 0.0f ? sumlx / suml2 : 0.0f;
756 }
757 for (int i = 0; i < n; ++i) {
758 int l = nearest_int(iscale * x[i]);
759 l = MAX(-nmax, MIN(nmax-1, l));
760 L[i] = l + nmax;
761 }
762 return 1/iscale;
763}
764
765static float make_qkx1_quants(int n, int nmax, const float * GGML_RESTRICT x, uint8_t * GGML_RESTRICT L, float * GGML_RESTRICT the_min,
766 int ntry, float alpha) {
767 float min = x[0];
768 float max = x[0];
769 for (int i = 1; i < n; ++i) {
770 if (x[i] < min) min = x[i];
771 if (x[i] > max) max = x[i];
772 }
773 if (max == min) {
774 for (int i = 0; i < n; ++i) L[i] = 0;
775 *the_min = 0;
776 return 0.f;
777 }
778 if (min > 0) min = 0;
779 float iscale = nmax/(max - min);
780 float scale = 1/iscale;
781 for (int itry = 0; itry < ntry; ++itry) {
782 float sumlx = 0; int suml2 = 0;
783 bool did_change = false;
784 for (int i = 0; i < n; ++i) {
785 int l = nearest_int(iscale*(x[i] - min));
786 l = MAX(0, MIN(nmax, l));
787 if (l != L[i]) {
788 L[i] = l;
789 did_change = true;
790 }
791 sumlx += (x[i] - min)*l;
792 suml2 += l*l;
793 }
794 scale = sumlx/suml2;
795 float sum = 0;
796 for (int i = 0; i < n; ++i) {
797 sum += x[i] - scale*L[i];
798 }
799 min = alpha*min + (1 - alpha)*sum/n;
800 if (min > 0) min = 0;
801 iscale = 1/scale;
802 if (!did_change) break;
803 }
804 *the_min = -min;
805 return scale;
806}
807
808static float make_qkx2_quants(int n, int nmax, const float * GGML_RESTRICT x, const float * GGML_RESTRICT weights,
809 uint8_t * GGML_RESTRICT L, float * GGML_RESTRICT the_min, uint8_t * GGML_RESTRICT Laux,
810 float rmin, float rdelta, int nstep, bool use_mad) {
811 float min = x[0];
812 float max = x[0];
813 float sum_w = weights[0];
814 float sum_x = sum_w * x[0];
815#ifdef HAVE_BUGGY_APPLE_LINKER
816 // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
817 for (volatile int i = 1; i < n; ++i) {
818#else
819 for (int i = 1; i < n; ++i) {
820#endif
821 if (x[i] < min) min = x[i];
822 if (x[i] > max) max = x[i];
823 float w = weights[i];
824 sum_w += w;
825 sum_x += w * x[i];
826 }
827 if (min > 0) min = 0;
828 if (max == min) {
829 for (int i = 0; i < n; ++i) L[i] = 0;
830 *the_min = -min;
831 return 0.f;
832 }
833 float iscale = nmax/(max - min);
834 float scale = 1/iscale;
835 float best_error = 0;
836 for (int i = 0; i < n; ++i) {
837 int l = nearest_int(iscale*(x[i] - min));
838 L[i] = MAX(0, MIN(nmax, l));
839 float diff = scale * L[i] + min - x[i];
840 diff = use_mad ? fabsf(diff) : diff * diff;
841 float w = weights[i];
842 best_error += w * diff;
843 }
844 if (nstep < 1) {
845 *the_min = -min;
846 return scale;
847 }
848 for (int is = 0; is <= nstep; ++is) {
849 iscale = (rmin + rdelta*is + nmax)/(max - min);
850 float sum_l = 0, sum_l2 = 0, sum_xl = 0;
851 for (int i = 0; i < n; ++i) {
852 int l = nearest_int(iscale*(x[i] - min));
853 l = MAX(0, MIN(nmax, l));
854 Laux[i] = l;
855 float w = weights[i];
856 sum_l += w*l;
857 sum_l2 += w*l*l;
858 sum_xl += w*l*x[i];
859 }
860 float D = sum_w * sum_l2 - sum_l * sum_l;
861 if (D > 0) {
862 float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
863 float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
864 if (this_min > 0) {
865 this_min = 0;
866 this_scale = sum_xl / sum_l2;
867 }
868 float cur_error = 0;
869 for (int i = 0; i < n; ++i) {
870 float diff = this_scale * Laux[i] + this_min - x[i];
871 diff = use_mad ? fabsf(diff) : diff * diff;
872 float w = weights[i];
873 cur_error += w * diff;
874 }
875 if (cur_error < best_error) {
876 for (int i = 0; i < n; ++i) {
877 L[i] = Laux[i];
878 }
879 best_error = cur_error;
880 scale = this_scale;
881 min = this_min;
882 }
883 }
884 }
885 *the_min = -min;
886 return scale;
887}
888
889static inline void get_scale_min_k4(int j, const uint8_t * GGML_RESTRICT q, uint8_t * GGML_RESTRICT d, uint8_t * GGML_RESTRICT m) {
890 if (j < 4) {
891 *d = q[j] & 63; *m = q[j + 4] & 63;
892 } else {
893 *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
894 *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
895 }
896}
897
898//========================- 2-bit (de)-quantization
899
900void quantize_row_q2_K_ref(const float * GGML_RESTRICT x, block_q2_K * GGML_RESTRICT y, int64_t k) {
901 assert(k % QK_K == 0);
902 const int nb = k / QK_K;
903
904 uint8_t L[QK_K];
905 uint8_t Laux[16];
906 float weights[16];
907 float mins[QK_K/16];
908 float scales[QK_K/16];
909
910 const float q4scale = 15.f;
911
912 for (int i = 0; i < nb; i++) {
913 float max_scale = 0; // as we are deducting the min, scales are always positive
914 float max_min = 0;
915 for (int j = 0; j < QK_K/16; ++j) {
916 for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
917 scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
918 float scale = scales[j];
919 if (scale > max_scale) {
920 max_scale = scale;
921 }
922 float min = mins[j];
923 if (min > max_min) {
924 max_min = min;
925 }
926 }
927
928 if (max_scale > 0) {
929 float iscale = q4scale/max_scale;
930 for (int j = 0; j < QK_K/16; ++j) {
931 int l = nearest_int(iscale*scales[j]);
932 y[i].scales[j] = l;
933 }
934 y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
935 } else {
936 for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
937 y[i].d = GGML_FP32_TO_FP16(0.f);
938 }
939 if (max_min > 0) {
940 float iscale = q4scale/max_min;
941 for (int j = 0; j < QK_K/16; ++j) {
942 int l = nearest_int(iscale*mins[j]);
943 y[i].scales[j] |= (l << 4);
944 }
945 y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
946 } else {
947 y[i].dmin = GGML_FP32_TO_FP16(0.f);
948 }
949 for (int j = 0; j < QK_K/16; ++j) {
950 const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
951 if (!d) continue;
952 const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
953 for (int ii = 0; ii < 16; ++ii) {
954 int l = nearest_int((x[16*j + ii] + dm)/d);
955 l = MAX(0, MIN(3, l));
956 L[16*j + ii] = l;
957 }
958 }
959
960 for (int j = 0; j < QK_K; j += 128) {
961 for (int l = 0; l < 32; ++l) {
962 y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
963 }
964 }
965
966 x += QK_K;
967 }
968}
969
970void dequantize_row_q2_K(const block_q2_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
971 assert(k % QK_K == 0);
972 const int nb = k / QK_K;
973
974 for (int i = 0; i < nb; i++) {
975
976 const float d = GGML_FP16_TO_FP32(x[i].d);
977 const float min = GGML_FP16_TO_FP32(x[i].dmin);
978
979 const uint8_t * q = x[i].qs;
980
981 int is = 0;
982 float dl, ml;
983 for (int n = 0; n < QK_K; n += 128) {
984 int shift = 0;
985 for (int j = 0; j < 4; ++j) {
986
987 uint8_t sc = x[i].scales[is++];
988 dl = d * (sc & 0xF); ml = min * (sc >> 4);
989 for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
990
991 sc = x[i].scales[is++];
992 dl = d * (sc & 0xF); ml = min * (sc >> 4);
993 for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
994
995 shift += 2;
996 }
997 q += 32;
998 }
999 }
1000}
1001
1002static float make_qkx3_quants(int n, int nmax, const float * GGML_RESTRICT x, const float * GGML_RESTRICT weights,
1003 uint8_t * GGML_RESTRICT L, float * GGML_RESTRICT the_min, uint8_t * GGML_RESTRICT Laux,
1004 float rmin, float rdelta, int nstep, bool use_mad) {
1005 float min = x[0];
1006 float max = x[0];
1007 float sum_w = weights ? weights[0] : x[0]*x[0];
1008 float sum_x = sum_w * x[0];
1009#ifdef HAVE_BUGGY_APPLE_LINKER
1010 // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
1011 for (volatile int i = 1; i < n; ++i) {
1012#else
1013 for (int i = 1; i < n; ++i) {
1014#endif
1015 if (x[i] < min) min = x[i];
1016 if (x[i] > max) max = x[i];
1017 float w = weights ? weights[i] : x[i]*x[i];
1018 sum_w += w;
1019 sum_x += w * x[i];
1020 }
1021 if (min > 0) {
1022 min = 0;
1023 }
1024 if (max <= min) {
1025 memset(L, 0, n);
1026 *the_min = -min;
1027 return 0.f;
1028 }
1029 float iscale = nmax/(max - min);
1030 float scale = 1/iscale;
1031 float best_mad = 0;
1032 for (int i = 0; i < n; ++i) {
1033 int l = nearest_int(iscale*(x[i] - min));
1034 L[i] = MAX(0, MIN(nmax, l));
1035 float diff = scale * L[i] + min - x[i];
1036 diff = use_mad ? fabsf(diff) : diff*diff;
1037 float w = weights ? weights[i] : x[i]*x[i];
1038 best_mad += w * diff;
1039 }
1040 if (nstep < 1) {
1041 *the_min = -min;
1042 return scale;
1043 }
1044 for (int is = 0; is <= nstep; ++is) {
1045 iscale = (rmin + rdelta*is + nmax)/(max - min);
1046 float sum_l = 0, sum_l2 = 0, sum_xl = 0;
1047 for (int i = 0; i < n; ++i) {
1048 // min is the best fit so far and can be at or near max, so v can be inf, nan or out of range for nearest_int
1049 const float v = iscale*(x[i] - min);
1050 const int l = v > 0 ? nearest_int(MIN(v, nmax)) : 0;
1051 Laux[i] = l;
1052 float w = weights ? weights[i] : x[i]*x[i];
1053 sum_l += w*l;
1054 sum_l2 += w*l*l;
1055 sum_xl += w*l*x[i];
1056 }
1057 float D = sum_w * sum_l2 - sum_l * sum_l;
1058 if (D > 0) {
1059 float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
1060 float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
1061 if (this_min > 0) {
1062 this_min = 0;
1063 this_scale = sum_xl / sum_l2;
1064 }
1065 float mad = 0;
1066 for (int i = 0; i < n; ++i) {
1067 float diff = this_scale * Laux[i] + this_min - x[i];
1068 diff = use_mad ? fabsf(diff) : diff*diff;
1069 float w = weights ? weights[i] : x[i]*x[i];
1070 mad += w * diff;
1071 }
1072 if (mad < best_mad) {
1073 for (int i = 0; i < n; ++i) {
1074 L[i] = Laux[i];
1075 }
1076 best_mad = mad;
1077 scale = this_scale;
1078 min = this_min;
1079 }
1080 }
1081 }
1082 *the_min = -min;
1083 return scale;
1084}
1085
1086static float make_qp_quants(int n, int nmax, const float * GGML_RESTRICT x, uint8_t * GGML_RESTRICT L, const float * quant_weights) {
1087 float max = 0;
1088 for (int i = 0; i < n; ++i) {
1089 max = MAX(max, x[i]);
1090 }
1091 if (max < GROUP_MAX_EPS) { // all zero
1092 for (int i = 0; i < n; ++i) { L[i] = 0; }
1093 return 0.f;
1094 }
1095 float iscale = nmax / max;
1096 for (int i = 0; i < n; ++i) {
1097 L[i] = nearest_int(iscale * x[i]);
1098 }
1099 float scale = 1/iscale;
1100 float best_mse = 0;
1101 for (int i = 0; i < n; ++i) {
1102 float diff = x[i] - scale*L[i];
1103 float w = quant_weights[i];
1104 best_mse += w*diff*diff;
1105 }
1106 for (int is = -4; is <= 4; ++is) {
1107 if (is == 0) continue;
1108 float iscale_is = (0.1f*is + nmax)/max;
1109 float scale_is = 1/iscale_is;
1110 float mse = 0;
1111 for (int i = 0; i < n; ++i) {
1112 int l = nearest_int(iscale_is*x[i]);
1113 l = MIN(nmax, l);
1114 float diff = x[i] - scale_is*l;
1115 float w = quant_weights[i];
1116 mse += w*diff*diff;
1117 }
1118 if (mse < best_mse) {
1119 best_mse = mse;
1120 iscale = iscale_is;
1121 }
1122 }
1123 float sumlx = 0;
1124 float suml2 = 0;
1125 for (int i = 0; i < n; ++i) {
1126 int l = nearest_int(iscale * x[i]);
1127 l = MIN(nmax, l);
1128 L[i] = l;
1129 float w = quant_weights[i];
1130 sumlx += w*x[i]*l;
1131 suml2 += w*l*l;
1132 }
1133 for (int itry = 0; itry < 5; ++itry) {
1134 int n_changed = 0;
1135 for (int i = 0; i < n; ++i) {
1136 float w = quant_weights[i];
1137 float slx = sumlx - w*x[i]*L[i];
1138 float sl2 = suml2 - w*L[i]*L[i];
1139 if (slx > 0 && sl2 > 0) {
1140 int new_l = nearest_int(x[i] * sl2 / slx);
1141 new_l = MIN(nmax, new_l);
1142 if (new_l != L[i]) {
1143 slx += w*x[i]*new_l;
1144 sl2 += w*new_l*new_l;
1145 if (slx*slx*suml2 > sumlx*sumlx*sl2) {
1146 L[i] = new_l; sumlx = slx; suml2 = sl2;
1147 ++n_changed;
1148 }
1149 }
1150 }
1151 }
1152 if (!n_changed) {
1153 break;
1154 }
1155 }
1156 return suml2 > 0.0f ? sumlx / suml2 : 0.0f;
1157}
1158
1159static void quantize_row_q2_K_impl(const float * GGML_RESTRICT x, block_q2_K * GGML_RESTRICT y, int k, const float * GGML_RESTRICT quant_weights) {
1160 GGML_ASSERT(quant_weights);
1161 assert(k % QK_K == 0);
1162 const int nb = k / QK_K;
1163 const bool requantize = true;
1164
1165 uint8_t L[QK_K];
1166 uint8_t Laux[16];
1167 float mins[QK_K/16];
1168 float scales[QK_K/16];
1169 float sw[QK_K/16];
1170 float weight[16];
1171 uint8_t Ls[QK_K/16], Lm[QK_K/16];
1172
1173 for (int i = 0; i < nb; i++) {
1174 memset(sw, 0, QK_K/16*sizeof(float));
1175 float sumx2 = 0;
1176 for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
1177 float sigma2 = sumx2/QK_K;
1178 for (int j = 0; j < QK_K/16; ++j) {
1179 const float * GGML_RESTRICT qw = quant_weights + QK_K * i + 16*j;
1180 for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
1181 for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
1182 scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
1183 }
1184
1185 float dm, mm;
1186 dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
1187 mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
1188
1189 y[i].d = GGML_FP32_TO_FP16(dm);
1190 y[i].dmin = GGML_FP32_TO_FP16(mm);
1191 dm = GGML_FP16_TO_FP32(y[i].d);
1192 mm = GGML_FP16_TO_FP32(y[i].dmin);
1193
1194 for (int j = 0; j < QK_K/16; ++j) {
1195 y[i].scales[j] = Ls[j] | (Lm[j] << 4);
1196 }
1197
1198 if (requantize) {
1199 for (int j = 0; j < QK_K/16; ++j) {
1200 const float d = dm * (y[i].scales[j] & 0xF);
1201 if (!d) continue;
1202 const float m = mm * (y[i].scales[j] >> 4);
1203 for (int ii = 0; ii < 16; ++ii) {
1204 int l = nearest_int((x[16*j + ii] + m)/d);
1205 l = MAX(0, MIN(3, l));
1206 L[16*j + ii] = l;
1207 }
1208 }
1209 }
1210
1211 for (int j = 0; j < QK_K; j += 128) {
1212 for (int l = 0; l < 32; ++l) {
1213 y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
1214 }
1215 }
1216
1217 x += QK_K;
1218 }
1219}
1220
1221size_t quantize_q2_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
1222 size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
1223 if (!quant_weights) {
1224 quantize_row_q2_K_ref(src, dst, (int64_t)nrow*n_per_row);
1225 }
1226 else {
1227 char * qrow = (char *)dst;
1228 for (int64_t row = 0; row < nrow; ++row) {
1229 quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
1230 src += n_per_row;
1231 qrow += row_size;
1232 }
1233 }
1234 return nrow * row_size;
1235}
1236
1237//========================= 3-bit (de)-quantization
1238
1239void quantize_row_q3_K_ref(const float * GGML_RESTRICT x, block_q3_K * GGML_RESTRICT y, int64_t k) {
1240 assert(k % QK_K == 0);
1241 const int nb = k / QK_K;
1242
1243 int8_t L[QK_K];
1244 float scales[QK_K / 16];
1245
1246 for (int i = 0; i < nb; i++) {
1247
1248 float max_scale = 0;
1249 float amax = 0;
1250 for (int j = 0; j < QK_K/16; ++j) {
1251 scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
1252 float scale = fabsf(scales[j]);
1253 if (scale > amax) {
1254 amax = scale; max_scale = scales[j];
1255 }
1256 }
1257
1258 memset(y[i].scales, 0, 12);
1259 if (max_scale) {
1260 float iscale = -32.f/max_scale;
1261 for (int j = 0; j < QK_K/16; ++j) {
1262 int8_t l = nearest_int(iscale*scales[j]);
1263 l = MAX(-32, MIN(31, l)) + 32;
1264 if (j < 8) {
1265 y[i].scales[j] = l & 0xF;
1266 } else {
1267 y[i].scales[j-8] |= ((l & 0xF) << 4);
1268 }
1269 l >>= 4;
1270 y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
1271 }
1272 y[i].d = GGML_FP32_TO_FP16(1/iscale);
1273 } else {
1274 y[i].d = GGML_FP32_TO_FP16(0.f);
1275 }
1276
1277 int8_t sc;
1278 for (int j = 0; j < QK_K/16; ++j) {
1279 sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
1280 sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
1281 float d = GGML_FP16_TO_FP32(y[i].d) * sc;
1282 if (!d) {
1283 continue;
1284 }
1285 for (int ii = 0; ii < 16; ++ii) {
1286 int l = nearest_int(x[16*j + ii]/d);
1287 l = MAX(-4, MIN(3, l));
1288 L[16*j + ii] = l + 4;
1289 }
1290 }
1291
1292 memset(y[i].hmask, 0, QK_K/8);
1293 // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
1294 int m = 0;
1295 uint8_t hm = 1;
1296 for (int j = 0; j < QK_K; ++j) {
1297 if (L[j] > 3) {
1298 y[i].hmask[m] |= hm;
1299 L[j] -= 4;
1300 }
1301 if (++m == QK_K/8) {
1302 m = 0; hm <<= 1;
1303 }
1304 }
1305 for (int j = 0; j < QK_K; j += 128) {
1306 for (int l = 0; l < 32; ++l) {
1307 y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
1308 }
1309 }
1310
1311 x += QK_K;
1312 }
1313}
1314
1315void dequantize_row_q3_K(const block_q3_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
1316 assert(k % QK_K == 0);
1317 const int nb = k / QK_K;
1318
1319 const uint32_t kmask1 = 0x03030303;
1320 const uint32_t kmask2 = 0x0f0f0f0f;
1321
1322 uint32_t aux[4];
1323 const int8_t * scales = (const int8_t*)aux;
1324
1325 for (int i = 0; i < nb; i++) {
1326
1327 const float d_all = GGML_FP16_TO_FP32(x[i].d);
1328
1329 const uint8_t * GGML_RESTRICT q = x[i].qs;
1330 const uint8_t * GGML_RESTRICT hm = x[i].hmask;
1331 uint8_t m = 1;
1332
1333 memcpy(aux, x[i].scales, 12);
1334 uint32_t tmp = aux[2];
1335 aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
1336 aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
1337 aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
1338 aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
1339
1340 int is = 0;
1341 float dl;
1342 for (int n = 0; n < QK_K; n += 128) {
1343 int shift = 0;
1344 for (int j = 0; j < 4; ++j) {
1345
1346 dl = d_all * (scales[is++] - 32);
1347 for (int l = 0; l < 16; ++l) {
1348 *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
1349 }
1350
1351 dl = d_all * (scales[is++] - 32);
1352 for (int l = 0; l < 16; ++l) {
1353 *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
1354 }
1355
1356 shift += 2;
1357 m <<= 1;
1358 }
1359 q += 32;
1360 }
1361
1362 }
1363}
1364
1365static void quantize_row_q3_K_impl(const float * GGML_RESTRICT x, block_q3_K * GGML_RESTRICT y, int64_t n_per_row, const float * GGML_RESTRICT quant_weights) {
1366 assert(n_per_row % QK_K == 0);
1367 const int nb = n_per_row / QK_K;
1368
1369 int8_t L[QK_K];
1370 float scales[QK_K / 16];
1371 float weight[16];
1372 float sw[QK_K / 16];
1373 int8_t Ls[QK_K / 16];
1374
1375 for (int i = 0; i < nb; i++) {
1376
1377 float sumx2 = 0;
1378 for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
1379 float sigma2 = 2*sumx2/QK_K;
1380
1381 for (int j = 0; j < QK_K/16; ++j) {
1382 if (quant_weights) {
1383 const float * qw = quant_weights + QK_K * i + 16*j;
1384 for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
1385 } else {
1386 for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
1387 }
1388 float sumw = 0;
1389 for (int l = 0; l < 16; ++l) sumw += weight[l];
1390 sw[j] = sumw;
1391
1392 scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
1393
1394 }
1395
1396 memset(y[i].scales, 0, 12);
1397
1398 float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
1399 for (int j = 0; j < QK_K/16; ++j) {
1400 int l = Ls[j];
1401 if (j < 8) {
1402 y[i].scales[j] = l & 0xF;
1403 } else {
1404 y[i].scales[j-8] |= ((l & 0xF) << 4);
1405 }
1406 l >>= 4;
1407 y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
1408 }
1409 y[i].d = GGML_FP32_TO_FP16(d_block);
1410
1411 int8_t sc;
1412 for (int j = 0; j < QK_K/16; ++j) {
1413 sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
1414 sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
1415 float d = GGML_FP16_TO_FP32(y[i].d) * sc;
1416 if (!d) {
1417 continue;
1418 }
1419 for (int ii = 0; ii < 16; ++ii) {
1420 int l = nearest_int(x[16*j + ii]/d);
1421 l = MAX(-4, MIN(3, l));
1422 L[16*j + ii] = l + 4;
1423 }
1424 }
1425
1426 memset(y[i].hmask, 0, QK_K/8);
1427 // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
1428 int m = 0;
1429 uint8_t hm = 1;
1430 for (int j = 0; j < QK_K; ++j) {
1431 if (L[j] > 3) {
1432 y[i].hmask[m] |= hm;
1433 L[j] -= 4;
1434 }
1435 if (++m == QK_K/8) {
1436 m = 0; hm <<= 1;
1437 }
1438 }
1439 for (int j = 0; j < QK_K; j += 128) {
1440 for (int l = 0; l < 32; ++l) {
1441 y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
1442 }
1443 }
1444
1445 x += QK_K;
1446 }
1447}
1448
1449size_t quantize_q3_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
1450 size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
1451 if (!quant_weights) {
1452 quantize_row_q3_K_ref(src, dst, (int64_t)nrow*n_per_row);
1453 }
1454 else {
1455 char * qrow = (char *)dst;
1456 for (int64_t row = 0; row < nrow; ++row) {
1457 quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
1458 src += n_per_row;
1459 qrow += row_size;
1460 }
1461 }
1462 return nrow * row_size;
1463}
1464
1465// ====================== 4-bit (de)-quantization
1466
1467void quantize_row_q4_K_ref(const float * GGML_RESTRICT x, block_q4_K * GGML_RESTRICT y, int64_t k) {
1468 assert(k % QK_K == 0);
1469 const int nb = k / QK_K;
1470
1471 uint8_t L[QK_K];
1472 uint8_t Laux[32];
1473 float weights[32];
1474 float mins[QK_K/32];
1475 float scales[QK_K/32];
1476
1477 for (int i = 0; i < nb; i++) {
1478 float max_scale = 0; // as we are deducting the min, scales are always positive
1479 float max_min = 0;
1480 for (int j = 0; j < QK_K/32; ++j) {
1481 //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
1482 float sum_x2 = 0;
1483 for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
1484 float av_x = sqrtf(sum_x2/32);
1485 for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
1486 scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
1487 float scale = scales[j];
1488 if (scale > max_scale) {
1489 max_scale = scale;
1490 }
1491 float min = mins[j];
1492 if (min > max_min) {
1493 max_min = min;
1494 }
1495 }
1496
1497 float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
1498 float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
1499 for (int j = 0; j < QK_K/32; ++j) {
1500 uint8_t ls = nearest_int(inv_scale*scales[j]);
1501 uint8_t lm = nearest_int(inv_min*mins[j]);
1502 ls = MIN(63, ls);
1503 lm = MIN(63, lm);
1504 if (j < 4) {
1505 y[i].scales[j] = ls;
1506 y[i].scales[j+4] = lm;
1507 } else {
1508 y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
1509 y[i].scales[j-4] |= ((ls >> 4) << 6);
1510 y[i].scales[j-0] |= ((lm >> 4) << 6);
1511 }
1512 }
1513 y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
1514 y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
1515
1516 uint8_t sc, m;
1517 for (int j = 0; j < QK_K/32; ++j) {
1518 get_scale_min_k4(j, y[i].scales, &sc, &m);
1519 const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
1520 if (!d) continue;
1521 const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
1522 for (int ii = 0; ii < 32; ++ii) {
1523 int l = nearest_int((x[32*j + ii] + dm)/d);
1524 l = MAX(0, MIN(15, l));
1525 L[32*j + ii] = l;
1526 }
1527 }
1528
1529 uint8_t * q = y[i].qs;
1530 for (int j = 0; j < QK_K; j += 64) {
1531 for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
1532 q += 32;
1533 }
1534
1535 x += QK_K;
1536 }
1537}
1538
1539void dequantize_row_q4_K(const block_q4_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
1540 assert(k % QK_K == 0);
1541 const int nb = k / QK_K;
1542
1543 for (int i = 0; i < nb; i++) {
1544 const uint8_t * q = x[i].qs;
1545
1546 const float d = GGML_FP16_TO_FP32(x[i].d);
1547 const float min = GGML_FP16_TO_FP32(x[i].dmin);
1548
1549 int is = 0;
1550 uint8_t sc, m;
1551 for (int j = 0; j < QK_K; j += 64) {
1552 get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
1553 const float d1 = d * sc; const float m1 = min * m;
1554 get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
1555 const float d2 = d * sc; const float m2 = min * m;
1556 for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
1557 for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
1558 q += 32; is += 2;
1559 }
1560 }
1561}
1562
1563static void quantize_row_q4_K_impl(const float * GGML_RESTRICT x, block_q4_K * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
1564 assert(n_per_row % QK_K == 0);
1565 const int64_t nb = n_per_row / QK_K;
1566
1567 uint8_t L[QK_K];
1568 uint8_t Laux[32];
1569 uint8_t Ls[QK_K/32];
1570 uint8_t Lm[QK_K/32];
1571 float weights[32];
1572 float sw[QK_K/32];
1573 float mins[QK_K/32];
1574 float scales[QK_K/32];
1575
1576 for (int i = 0; i < nb; i++) {
1577
1578 float sum_x2 = 0;
1579 for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
1580 float sigma2 = 2*sum_x2/QK_K;
1581 float av_x = sqrtf(sigma2);
1582
1583 for (int j = 0; j < QK_K/32; ++j) {
1584 if (quant_weights) {
1585 const float * qw = quant_weights + QK_K*i + 32*j;
1586 for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
1587 } else {
1588 for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
1589 }
1590 float sumw = 0;
1591 for (int l = 0; l < 32; ++l) sumw += weights[l];
1592 sw[j] = sumw;
1593 scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
1594 }
1595
1596 float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
1597 float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
1598 for (int j = 0; j < QK_K/32; ++j) {
1599 uint8_t ls = Ls[j];
1600 uint8_t lm = Lm[j];
1601 if (j < 4) {
1602 y[i].scales[j] = ls;
1603 y[i].scales[j+4] = lm;
1604 } else {
1605 y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
1606 y[i].scales[j-4] |= ((ls >> 4) << 6);
1607 y[i].scales[j-0] |= ((lm >> 4) << 6);
1608 }
1609 }
1610 y[i].d = GGML_FP32_TO_FP16(d_block);
1611 y[i].dmin = GGML_FP32_TO_FP16(m_block);
1612
1613 uint8_t sc, m;
1614 for (int j = 0; j < QK_K/32; ++j) {
1615 get_scale_min_k4(j, y[i].scales, &sc, &m);
1616 const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
1617 if (!d) continue;
1618 const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
1619 for (int ii = 0; ii < 32; ++ii) {
1620 int l = nearest_int((x[32*j + ii] + dm)/d);
1621 l = MAX(0, MIN(15, l));
1622 L[32*j + ii] = l;
1623 }
1624 }
1625 uint8_t * q = y[i].qs;
1626 for (int j = 0; j < QK_K; j += 64) {
1627 for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
1628 q += 32;
1629 }
1630
1631 x += QK_K;
1632
1633 }
1634}
1635
1636size_t quantize_q4_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
1637 size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
1638 if (!quant_weights) {
1639 quantize_row_q4_K_ref(src, dst, (int64_t)nrow*n_per_row);
1640 }
1641 else {
1642 char * qrow = (char *)dst;
1643 for (int64_t row = 0; row < nrow; ++row) {
1644 quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
1645 src += n_per_row;
1646 qrow += row_size;
1647 }
1648 }
1649 return nrow * row_size;
1650}
1651
1652// ====================== 5-bit (de)-quantization
1653
1654void quantize_row_q5_K_ref(const float * GGML_RESTRICT x, block_q5_K * GGML_RESTRICT y, int64_t k) {
1655 assert(k % QK_K == 0);
1656 const int64_t nb = k / QK_K;
1657
1658 uint8_t L[QK_K];
1659 float mins[QK_K/32];
1660 float scales[QK_K/32];
1661 float weights[32];
1662 uint8_t Laux[32];
1663
1664 for (int i = 0; i < nb; i++) {
1665 float max_scale = 0; // as we are deducting the min, scales are always positive
1666 float max_min = 0;
1667 for (int j = 0; j < QK_K/32; ++j) {
1668 //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
1669 float sum_x2 = 0;
1670 for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
1671 float av_x = sqrtf(sum_x2/32);
1672 for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
1673 scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
1674 float scale = scales[j];
1675 if (scale > max_scale) {
1676 max_scale = scale;
1677 }
1678 float min = mins[j];
1679 if (min > max_min) {
1680 max_min = min;
1681 }
1682 }
1683
1684 float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
1685 float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
1686 for (int j = 0; j < QK_K/32; ++j) {
1687 uint8_t ls = nearest_int(inv_scale*scales[j]);
1688 uint8_t lm = nearest_int(inv_min*mins[j]);
1689 ls = MIN(63, ls);
1690 lm = MIN(63, lm);
1691 if (j < 4) {
1692 y[i].scales[j] = ls;
1693 y[i].scales[j+4] = lm;
1694 } else {
1695 y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
1696 y[i].scales[j-4] |= ((ls >> 4) << 6);
1697 y[i].scales[j-0] |= ((lm >> 4) << 6);
1698 }
1699 }
1700 y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
1701 y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
1702
1703 uint8_t sc, m;
1704 for (int j = 0; j < QK_K/32; ++j) {
1705 get_scale_min_k4(j, y[i].scales, &sc, &m);
1706 const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
1707 if (!d) continue;
1708 const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
1709 for (int ii = 0; ii < 32; ++ii) {
1710 int l = nearest_int((x[32*j + ii] + dm)/d);
1711 l = MAX(0, MIN(31, l));
1712 L[32*j + ii] = l;
1713 }
1714 }
1715
1716 uint8_t * GGML_RESTRICT qh = y[i].qh;
1717 uint8_t * GGML_RESTRICT ql = y[i].qs;
1718 memset(qh, 0, QK_K/8);
1719
1720 uint8_t m1 = 1, m2 = 2;
1721 for (int n = 0; n < QK_K; n += 64) {
1722 for (int j = 0; j < 32; ++j) {
1723 int l1 = L[n + j];
1724 if (l1 > 15) {
1725 l1 -= 16; qh[j] |= m1;
1726 }
1727 int l2 = L[n + j + 32];
1728 if (l2 > 15) {
1729 l2 -= 16; qh[j] |= m2;
1730 }
1731 ql[j] = l1 | (l2 << 4);
1732 }
1733 m1 <<= 2; m2 <<= 2;
1734 ql += 32;
1735 }
1736
1737 x += QK_K;
1738 }
1739}
1740
1741void dequantize_row_q5_K(const block_q5_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
1742 assert(k % QK_K == 0);
1743 const int64_t nb = k / QK_K;
1744
1745 for (int i = 0; i < nb; i++) {
1746 const uint8_t * ql = x[i].qs;
1747 const uint8_t * qh = x[i].qh;
1748
1749 const float d = GGML_FP16_TO_FP32(x[i].d);
1750 const float min = GGML_FP16_TO_FP32(x[i].dmin);
1751
1752 int is = 0;
1753 uint8_t sc, m;
1754 uint8_t u1 = 1, u2 = 2;
1755 for (int j = 0; j < QK_K; j += 64) {
1756 get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
1757 const float d1 = d * sc; const float m1 = min * m;
1758 get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
1759 const float d2 = d * sc; const float m2 = min * m;
1760 for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
1761 for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
1762 ql += 32; is += 2;
1763 u1 <<= 2; u2 <<= 2;
1764 }
1765 }
1766}
1767
1768static void quantize_row_q5_K_impl(const float * GGML_RESTRICT x, block_q5_K * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
1769 assert(n_per_row % QK_K == 0);
1770 const int64_t nb = n_per_row / QK_K;
1771
1772 uint8_t L[QK_K];
1773 uint8_t Laux[32];
1774 uint8_t Ls[QK_K/32];
1775 uint8_t Lm[QK_K/32];
1776 float mins[QK_K/32];
1777 float scales[QK_K/32];
1778 float sw[QK_K/32];
1779 float weights[32];
1780
1781 for (int i = 0; i < nb; i++) {
1782
1783 float sum_x2 = 0;
1784 for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
1785 float sigma2 = 2*sum_x2/QK_K;
1786 float av_x = sqrtf(sigma2);
1787
1788 for (int j = 0; j < QK_K/32; ++j) {
1789 if (quant_weights) {
1790 const float * qw = quant_weights + QK_K*i + 32*j;
1791 for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
1792 } else {
1793 for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
1794 }
1795 float sumw = 0;
1796 for (int l = 0; l < 32; ++l) sumw += weights[l];
1797 sw[j] = sumw;
1798
1799 scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
1800 }
1801
1802 float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
1803 float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
1804
1805 for (int j = 0; j < QK_K/32; ++j) {
1806 uint8_t ls = Ls[j];
1807 uint8_t lm = Lm[j];
1808 ls = MIN(63, ls);
1809 lm = MIN(63, lm);
1810 if (j < 4) {
1811 y[i].scales[j] = ls;
1812 y[i].scales[j+4] = lm;
1813 } else {
1814 y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
1815 y[i].scales[j-4] |= ((ls >> 4) << 6);
1816 y[i].scales[j-0] |= ((lm >> 4) << 6);
1817 }
1818 }
1819 y[i].d = GGML_FP32_TO_FP16(d_block);
1820 y[i].dmin = GGML_FP32_TO_FP16(m_block);
1821
1822 uint8_t sc, m;
1823 for (int j = 0; j < QK_K/32; ++j) {
1824 get_scale_min_k4(j, y[i].scales, &sc, &m);
1825 const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
1826 if (!d) continue;
1827 const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
1828 for (int ii = 0; ii < 32; ++ii) {
1829 int l = nearest_int((x[32*j + ii] + dm)/d);
1830 l = MAX(0, MIN(31, l));
1831 L[32*j + ii] = l;
1832 }
1833 }
1834
1835 uint8_t * GGML_RESTRICT qh = y[i].qh;
1836 uint8_t * GGML_RESTRICT ql = y[i].qs;
1837 memset(qh, 0, QK_K/8);
1838
1839 uint8_t m1 = 1, m2 = 2;
1840 for (int n = 0; n < QK_K; n += 64) {
1841 for (int j = 0; j < 32; ++j) {
1842 int l1 = L[n + j];
1843 if (l1 > 15) {
1844 l1 -= 16; qh[j] |= m1;
1845 }
1846 int l2 = L[n + j + 32];
1847 if (l2 > 15) {
1848 l2 -= 16; qh[j] |= m2;
1849 }
1850 ql[j] = l1 | (l2 << 4);
1851 }
1852 m1 <<= 2; m2 <<= 2;
1853 ql += 32;
1854 }
1855
1856 x += QK_K;
1857
1858 }
1859}
1860
1861size_t quantize_q5_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
1862 size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
1863 if (!quant_weights) {
1864 quantize_row_q5_K_ref(src, dst, (int64_t)nrow*n_per_row);
1865 }
1866 else {
1867 char * qrow = (char *)dst;
1868 for (int64_t row = 0; row < nrow; ++row) {
1869 quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
1870 src += n_per_row;
1871 qrow += row_size;
1872 }
1873 }
1874 return nrow * row_size;
1875}
1876
1877// ====================== 6-bit (de)-quantization
1878
1879void quantize_row_q6_K_ref(const float * GGML_RESTRICT x, block_q6_K * GGML_RESTRICT y, int64_t k) {
1880 assert(k % QK_K == 0);
1881 const int64_t nb = k / QK_K;
1882
1883 int8_t L[QK_K];
1884 float scales[QK_K/16];
1885
1886 for (int i = 0; i < nb; i++) {
1887
1888 float max_scale = 0;
1889 float max_abs_scale = 0;
1890
1891 for (int ib = 0; ib < QK_K/16; ++ib) {
1892
1893 const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
1894 scales[ib] = scale;
1895
1896 const float abs_scale = fabsf(scale);
1897 if (abs_scale > max_abs_scale) {
1898 max_abs_scale = abs_scale;
1899 max_scale = scale;
1900 }
1901
1902 }
1903
1904 if (max_abs_scale < GROUP_MAX_EPS) {
1905 memset(&y[i], 0, sizeof(block_q6_K));
1906 y[i].d = GGML_FP32_TO_FP16(0.f);
1907 x += QK_K;
1908 continue;
1909 }
1910
1911 float iscale = -128.f/max_scale;
1912 y[i].d = GGML_FP32_TO_FP16(1/iscale);
1913 for (int ib = 0; ib < QK_K/16; ++ib) {
1914 y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
1915 }
1916
1917 for (int j = 0; j < QK_K/16; ++j) {
1918 float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
1919 if (!d) {
1920 continue;
1921 }
1922 for (int ii = 0; ii < 16; ++ii) {
1923 int l = nearest_int(x[16*j + ii]/d);
1924 l = MAX(-32, MIN(31, l));
1925 L[16*j + ii] = l + 32;
1926 }
1927 }
1928
1929 uint8_t * GGML_RESTRICT ql = y[i].ql;
1930 uint8_t * GGML_RESTRICT qh = y[i].qh;
1931 for (int j = 0; j < QK_K; j += 128) {
1932 for (int l = 0; l < 32; ++l) {
1933 const uint8_t q1 = L[j + l + 0] & 0xF;
1934 const uint8_t q2 = L[j + l + 32] & 0xF;
1935 const uint8_t q3 = L[j + l + 64] & 0xF;
1936 const uint8_t q4 = L[j + l + 96] & 0xF;
1937 ql[l+ 0] = q1 | (q3 << 4);
1938 ql[l+32] = q2 | (q4 << 4);
1939 qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
1940 }
1941 ql += 64;
1942 qh += 32;
1943 }
1944
1945 x += QK_K;
1946 }
1947}
1948
1949void dequantize_row_q6_K(const block_q6_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
1950 assert(k % QK_K == 0);
1951 const int64_t nb = k / QK_K;
1952
1953 for (int i = 0; i < nb; i++) {
1954 const float d = GGML_FP16_TO_FP32(x[i].d);
1955
1956 const uint8_t * GGML_RESTRICT ql = x[i].ql;
1957 const uint8_t * GGML_RESTRICT qh = x[i].qh;
1958 const int8_t * GGML_RESTRICT sc = x[i].scales;
1959
1960 for (int n = 0; n < QK_K; n += 128) {
1961 for (int l = 0; l < 32; ++l) {
1962 int is = l/16;
1963 const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
1964 const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
1965 const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
1966 const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
1967 y[l + 0] = d * sc[is + 0] * q1;
1968 y[l + 32] = d * sc[is + 2] * q2;
1969 y[l + 64] = d * sc[is + 4] * q3;
1970 y[l + 96] = d * sc[is + 6] * q4;
1971 }
1972 y += 128;
1973 ql += 64;
1974 qh += 32;
1975 sc += 8;
1976 }
1977 }
1978}
1979
1980static void quantize_row_q6_K_impl(const float * GGML_RESTRICT x, block_q6_K * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
1981 assert(n_per_row % QK_K == 0);
1982 const int64_t nb = n_per_row / QK_K;
1983
1984 int8_t L[QK_K];
1985 float scales[QK_K/16];
1986 //float weights[16];
1987
1988 for (int i = 0; i < nb; i++) {
1989
1990 //float sum_x2 = 0;
1991 //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
1992 //float sigma2 = sum_x2/QK_K;
1993
1994 float max_scale = 0;
1995 float max_abs_scale = 0;
1996
1997 for (int ib = 0; ib < QK_K/16; ++ib) {
1998
1999 float scale;
2000 if (quant_weights) {
2001 const float * qw = quant_weights + QK_K*i + 16*ib;
2002 //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
2003 //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
2004 scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
2005 } else {
2006 scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
2007 }
2008 scales[ib] = scale;
2009
2010 const float abs_scale = fabsf(scale);
2011 if (abs_scale > max_abs_scale) {
2012 max_abs_scale = abs_scale;
2013 max_scale = scale;
2014 }
2015
2016 }
2017
2018 if (max_abs_scale < GROUP_MAX_EPS) {
2019 memset(&y[i], 0, sizeof(block_q6_K));
2020 y[i].d = GGML_FP32_TO_FP16(0.f);
2021 x += QK_K;
2022 continue;
2023 }
2024
2025 float iscale = -128.f/max_scale;
2026 y[i].d = GGML_FP32_TO_FP16(1/iscale);
2027 for (int ib = 0; ib < QK_K/16; ++ib) {
2028 y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
2029 }
2030
2031 for (int j = 0; j < QK_K/16; ++j) {
2032 float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
2033 if (!d) {
2034 continue;
2035 }
2036 for (int ii = 0; ii < 16; ++ii) {
2037 int l = nearest_int(x[16*j + ii]/d);
2038 l = MAX(-32, MIN(31, l));
2039 L[16*j + ii] = l + 32;
2040 }
2041 }
2042
2043 uint8_t * GGML_RESTRICT ql = y[i].ql;
2044 uint8_t * GGML_RESTRICT qh = y[i].qh;
2045 for (int j = 0; j < QK_K; j += 128) {
2046 for (int l = 0; l < 32; ++l) {
2047 const uint8_t q1 = L[j + l + 0] & 0xF;
2048 const uint8_t q2 = L[j + l + 32] & 0xF;
2049 const uint8_t q3 = L[j + l + 64] & 0xF;
2050 const uint8_t q4 = L[j + l + 96] & 0xF;
2051 ql[l+ 0] = q1 | (q3 << 4);
2052 ql[l+32] = q2 | (q4 << 4);
2053 qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
2054 }
2055 ql += 64;
2056 qh += 32;
2057 }
2058
2059 x += QK_K;
2060
2061 }
2062}
2063
2064size_t quantize_q6_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2065 size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
2066 if (!quant_weights) {
2067 quantize_row_q6_K_ref(src, dst, (int64_t)nrow*n_per_row);
2068 }
2069 else {
2070 char * qrow = (char *)dst;
2071 for (int64_t row = 0; row < nrow; ++row) {
2072 quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
2073 src += n_per_row;
2074 qrow += row_size;
2075 }
2076 }
2077 return nrow * row_size;
2078}
2079
2080static void quantize_row_q4_0_impl(const float * GGML_RESTRICT x, block_q4_0 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
2081 static_assert(QK4_0 == 32, "QK4_0 must be 32");
2082
2083 if (!quant_weights) {
2084 quantize_row_q4_0_ref(x, y, n_per_row);
2085 return;
2086 }
2087
2088 float weight[QK4_0];
2089 int8_t L[QK4_0];
2090
2091 float sum_x2 = 0;
2092 for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
2093 float sigma2 = sum_x2/n_per_row;
2094
2095 const int64_t nb = n_per_row/QK4_0;
2096 for (int ib = 0; ib < nb; ++ib) {
2097 const float * xb = x + QK4_0 * ib;
2098 const float * qw = quant_weights + QK4_0 * ib;
2099 for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
2100 float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
2101 y[ib].d = GGML_FP32_TO_FP16(d);
2102 for (int j = 0; j < 16; ++j) {
2103 y[ib].qs[j] = L[j] | (L[j+16] << 4);
2104 }
2105 }
2106}
2107
2108size_t quantize_q1_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2109 if (!quant_weights) {
2110 quantize_row_q1_0_ref(src, dst, (int64_t)nrow*n_per_row);
2111 return nrow * ggml_row_size(GGML_TYPE_Q1_0, n_per_row);
2112 }
2113 size_t row_size = ggml_row_size(GGML_TYPE_Q1_0, n_per_row);
2114 char * qrow = (char *)dst;
2115 for (int64_t row = 0; row < nrow; ++row) {
2116 quantize_row_q1_0_ref(src, (block_q1_0*)qrow, n_per_row);
2117 src += n_per_row;
2118 qrow += row_size;
2119 }
2120 return nrow * row_size;
2121}
2122
2123size_t quantize_q2_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2124 if (!quant_weights) {
2125 quantize_row_q2_0_ref(src, dst, (int64_t)nrow*n_per_row);
2126 return nrow * ggml_row_size(GGML_TYPE_Q2_0, n_per_row);
2127 }
2128 size_t row_size = ggml_row_size(GGML_TYPE_Q2_0, n_per_row);
2129 char * qrow = (char *)dst;
2130 for (int64_t row = 0; row < nrow; ++row) {
2131 quantize_row_q2_0_ref(src, (block_q2_0*)qrow, n_per_row);
2132 src += n_per_row;
2133 qrow += row_size;
2134 }
2135 return nrow * row_size;
2136}
2137
2138size_t quantize_q4_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2139 if (!quant_weights) {
2140 quantize_row_q4_0_ref(src, dst, (int64_t)nrow*n_per_row);
2141 return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
2142 }
2143 size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
2144 char * qrow = (char *)dst;
2145 for (int64_t row = 0; row < nrow; ++row) {
2146 quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
2147 src += n_per_row;
2148 qrow += row_size;
2149 }
2150 return nrow * row_size;
2151}
2152
2153static void quantize_row_q4_1_impl(const float * GGML_RESTRICT x, block_q4_1 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
2154 static_assert(QK4_1 == 32, "QK4_1 must be 32");
2155
2156 if (!quant_weights) {
2157 quantize_row_q4_1_ref(x, y, n_per_row);
2158 return;
2159 }
2160
2161 float weight[QK4_1];
2162 uint8_t L[QK4_1], Laux[QK4_1];
2163
2164 float sum_x2 = 0;
2165 for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
2166 float sigma2 = sum_x2/n_per_row;
2167
2168 const int64_t nb = n_per_row/QK4_1;
2169 for (int ib = 0; ib < nb; ++ib) {
2170 const float * xb = x + QK4_1 * ib;
2171 const float * qw = quant_weights + QK4_1 * ib;
2172 for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
2173 float min;
2174 float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
2175 y[ib].d = GGML_FP32_TO_FP16(d);
2176 y[ib].m = GGML_FP32_TO_FP16(-min);
2177 for (int j = 0; j < 16; ++j) {
2178 y[ib].qs[j] = L[j] | (L[j+16] << 4);
2179 }
2180 }
2181}
2182
2183size_t quantize_q4_1(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2184 if (!quant_weights) {
2185 quantize_row_q4_1_ref(src, dst, (int64_t)nrow*n_per_row);
2186 return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
2187 }
2188 size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
2189 char * qrow = (char *)dst;
2190 for (int64_t row = 0; row < nrow; ++row) {
2191 quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
2192 src += n_per_row;
2193 qrow += row_size;
2194 }
2195 return nrow * row_size;
2196}
2197
2198static void quantize_row_q5_0_impl(const float * GGML_RESTRICT x, block_q5_0 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
2199 static_assert(QK5_0 == 32, "QK5_0 must be 32");
2200
2201 if (!quant_weights) {
2202 quantize_row_q5_0_ref(x, y, n_per_row);
2203 return;
2204 }
2205
2206 float weight[QK5_0];
2207 int8_t L[QK5_0];
2208
2209 float sum_x2 = 0;
2210 for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
2211 float sigma2 = sum_x2/n_per_row;
2212
2213 const int64_t nb = n_per_row/QK5_0;
2214 for (int ib = 0; ib < nb; ++ib) {
2215 const float * xb = x + QK5_0 * ib;
2216 const float * qw = quant_weights + QK5_0 * ib;
2217 for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
2218 float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
2219 y[ib].d = GGML_FP32_TO_FP16(d);
2220
2221 uint32_t qh = 0;
2222
2223 for (int j = 0; j < 16; ++j) {
2224 const uint8_t xi0 = L[j];
2225 const uint8_t xi1 = L[j+16];
2226 y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
2227
2228 // get the 5-th bit and store it in qh at the right position
2229 qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
2230 qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
2231 }
2232
2233 memcpy(&y[ib].qh, &qh, sizeof(qh));
2234 }
2235}
2236
2237size_t quantize_q5_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2238 if (!quant_weights) {
2239 quantize_row_q5_0_ref(src, dst, (int64_t)nrow*n_per_row);
2240 return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
2241 }
2242 size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
2243 char * qrow = (char *)dst;
2244 for (int64_t row = 0; row < nrow; ++row) {
2245 quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
2246 src += n_per_row;
2247 qrow += row_size;
2248 }
2249 return nrow * row_size;
2250}
2251
2252static void quantize_row_q5_1_impl(const float * GGML_RESTRICT x, block_q5_1 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
2253 static_assert(QK5_1 == 32, "QK5_1 must be 32");
2254
2255 if (!quant_weights) {
2256 quantize_row_q5_1_ref(x, y, n_per_row);
2257 return;
2258 }
2259
2260 float weight[QK5_1];
2261 uint8_t L[QK5_1], Laux[QK5_1];
2262
2263 float sum_x2 = 0;
2264 for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
2265 float sigma2 = sum_x2/n_per_row;
2266
2267 const int64_t nb = n_per_row/QK5_1;
2268 for (int ib = 0; ib < nb; ++ib) {
2269 const float * xb = x + QK5_1 * ib;
2270 const float * qw = quant_weights + QK5_1 * ib;
2271 for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
2272 float min;
2273 float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
2274 y[ib].d = GGML_FP32_TO_FP16(d);
2275 y[ib].m = GGML_FP32_TO_FP16(-min);
2276
2277 uint32_t qh = 0;
2278 for (int j = 0; j < 16; ++j) {
2279 const uint8_t xi0 = L[j];
2280 const uint8_t xi1 = L[j+16];
2281 y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
2282 // get the 5-th bit and store it in qh at the right position
2283 qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
2284 qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
2285 }
2286 memcpy(&y[ib].qh, &qh, sizeof(qh));
2287 }
2288}
2289
2290size_t quantize_q5_1(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2291 if (!quant_weights) {
2292 quantize_row_q5_1_ref(src, dst, (int64_t)nrow*n_per_row);
2293 return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
2294 }
2295 size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
2296 char * qrow = (char *)dst;
2297 for (int64_t row = 0; row < nrow; ++row) {
2298 quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
2299 src += n_per_row;
2300 qrow += row_size;
2301 }
2302 return nrow * row_size;
2303}
2304
2305size_t quantize_q8_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2306 (void)quant_weights; // not used
2307 const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
2308 quantize_row_q8_0_ref(src, dst, (int64_t)nrow*n_per_row);
2309 return nrow * row_size;
2310}
2311
2312size_t quantize_mxfp4(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2313 GGML_UNUSED(quant_weights);
2314 quantize_row_mxfp4_ref(src, dst, (int64_t)nrow*n_per_row);
2315 return nrow * ggml_row_size(GGML_TYPE_MXFP4, n_per_row);
2316}
2317
2318size_t quantize_nvfp4(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2319 GGML_UNUSED(quant_weights);
2320 quantize_row_nvfp4_ref(src, dst, (int64_t)nrow*n_per_row);
2321 return nrow * ggml_row_size(GGML_TYPE_NVFP4, n_per_row);
2322}
2323
2324size_t quantize_f8_e4m3(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2325 GGML_UNUSED(quant_weights);
2326 quantize_row_f8_e4m3_ref(src, dst, (int64_t)nrow*n_per_row);
2327 return nrow * ggml_row_size(GGML_TYPE_F8_E4M3, n_per_row);
2328}
2329
2330// ====================== Ternary (de)-quantization (BitNet b1.58 and TriLMs)
2331
2332void quantize_row_tq1_0_ref(const float * GGML_RESTRICT x, block_tq1_0 * GGML_RESTRICT y, int64_t k) {
2333 assert(k % QK_K == 0);
2334 const int64_t nb = k / QK_K;
2335
2336 for (int64_t i = 0; i < nb; i++) {
2337 float amax = 0.0f; // absolute max
2338
2339 for (int j = 0; j < QK_K; j++) {
2340 const float v = x[j];
2341 amax = MAX(amax, fabsf(v));
2342 }
2343
2344 const float d = amax;
2345 const float id = d ? 1.0f/d : 0.0f;
2346
2347 y[i].d = GGML_FP32_TO_FP16(d);
2348
2349 // 5 elements per byte, along 32 bytes
2350 for (size_t j = 0; j < sizeof(y->qs) - sizeof(y->qs) % 32; j += 32) {
2351 for (size_t m = 0; m < 32; ++m) {
2352 uint8_t q = 0;
2353 for (size_t n = 0; n < 5; ++n) {
2354 int xi = lroundf(x[m + n*32] * id) + 1; // -1, 0, 1 -> 0, 1, 2
2355 q *= 3;
2356 q += xi;
2357 }
2358 // ceiling division (243 == pow(3, 5))
2359 q = ((uint16_t)q * 256 + (243 - 1)) / 243;
2360 y[i].qs[j + m] = q;
2361 }
2362 x += 5*32;
2363 }
2364 // along 16 bytes
2365 for (size_t j = sizeof(y->qs) - sizeof(y->qs) % 32; j < sizeof(y->qs); j += 16) {
2366 for (size_t m = 0; m < 16; ++m) {
2367 uint8_t q = 0;
2368 for (size_t n = 0; n < 5; ++n) {
2369 int xi = lroundf(x[m + n*16] * id) + 1; // -1, 0, 1 -> 0, 1, 2
2370 q *= 3;
2371 q += xi;
2372 }
2373 // ceiling division (243 == pow(3, 5))
2374 q = ((uint16_t)q * 256 + (243 - 1)) / 243;
2375 y[i].qs[j + m] = q;
2376 }
2377 x += 5*16;
2378 }
2379 // 4 elements per byte
2380 for (size_t j = 0; j < sizeof(y->qh); ++j) {
2381 uint8_t q = 0;
2382 for (size_t m = 0; m < 4; ++m) {
2383 // -1, 0, 1 -> 0, 1, 2
2384 int xi = lroundf(x[j + m*sizeof(y->qh)] * id) + 1;
2385 q *= 3;
2386 q += xi;
2387 }
2388 // shift the first value to the most significant trit
2389 q *= 3;
2390 // ceiling division (243 == pow(3, 5))
2391 q = ((uint16_t)q * 256 + (243 - 1)) / 243;
2392 y[i].qh[j] = q;
2393 }
2394 x += 4*sizeof(y->qh);
2395 }
2396}
2397
2398void quantize_row_tq2_0_ref(const float * GGML_RESTRICT x, block_tq2_0 * GGML_RESTRICT y, int64_t k) {
2399 assert(k % QK_K == 0);
2400 const int64_t nb = k / QK_K;
2401
2402 for (int64_t i = 0; i < nb; i++) {
2403 float amax = 0.0f; // absolute max
2404
2405 for (int j = 0; j < QK_K; j++) {
2406 const float v = x[j];
2407 amax = MAX(amax, fabsf(v));
2408 }
2409
2410 const float d = amax;
2411 const float id = d ? 1.0f/d : 0.0f;
2412
2413 y[i].d = GGML_FP32_TO_FP16(d);
2414
2415 for (size_t j = 0; j < sizeof(y->qs); j += 32) {
2416 for (size_t m = 0; m < 32; ++m) {
2417 uint8_t q = 0;
2418 for (size_t n = 0; n < 4; ++n) {
2419 // -1, 0, 1 -> 0, 1, 2
2420 int xi = lroundf(x[m + n*32] * id) + 1;
2421 q += (xi & 3) << (2*n);
2422 }
2423 y[i].qs[j + m] = q;
2424 }
2425 x += 4*32;
2426 }
2427 }
2428}
2429
2430size_t quantize_tq1_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2431 (void)quant_weights; // not used
2432 const size_t row_size = ggml_row_size(GGML_TYPE_TQ1_0, n_per_row);
2433 quantize_row_tq1_0_ref(src, dst, (int64_t)nrow*n_per_row);
2434 return nrow * row_size;
2435}
2436
2437size_t quantize_tq2_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
2438 (void)quant_weights; // not used
2439 const size_t row_size = ggml_row_size(GGML_TYPE_TQ2_0, n_per_row);
2440 quantize_row_tq2_0_ref(src, dst, (int64_t)nrow*n_per_row);
2441 return nrow * row_size;
2442}
2443
2444void dequantize_row_tq1_0(const block_tq1_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2445 assert(k % QK_K == 0);
2446 const int64_t nb = k / QK_K;
2447
2448 const uint8_t pow3[6] = {1, 3, 9, 27, 81, 243};
2449
2450 for (int64_t i = 0; i < nb; ++i) {
2451
2452 const float d = GGML_FP16_TO_FP32(x[i].d);
2453
2454 for (size_t j = 0; j < sizeof(x->qs) - sizeof(x->qs) % 32; j += 32) {
2455 for (size_t n = 0; n < 5; ++n) {
2456 for (size_t m = 0; m < 32; ++m) {
2457 uint8_t q = x[i].qs[j + m] * pow3[n];
2458 int16_t xi = ((uint16_t) q * 3) >> 8;
2459 *y++ = (float) (xi - 1) * d;
2460 }
2461 }
2462 }
2463 for (size_t j = sizeof(x->qs) - sizeof(x->qs) % 32; j < sizeof(x->qs); j += 16) {
2464 for (size_t n = 0; n < 5; ++n) {
2465 for (size_t m = 0; m < 16; ++m) {
2466 uint8_t q = x[i].qs[j + m] * pow3[n];
2467 int16_t xi = ((uint16_t) q * 3) >> 8;
2468 *y++ = (float) (xi - 1) * d;
2469 }
2470 }
2471 }
2472
2473 for (size_t n = 0; n < 4; ++n) {
2474 for (size_t j = 0; j < sizeof(x->qh); ++j) {
2475 uint8_t q = x[i].qh[j] * pow3[n];
2476 int16_t xi = ((uint16_t) q * 3) >> 8;
2477 *y++ = (float) (xi - 1) * d;
2478 }
2479 }
2480 }
2481}
2482
2483void dequantize_row_tq2_0(const block_tq2_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2484 assert(k % QK_K == 0);
2485 const int64_t nb = k / QK_K;
2486
2487 for (int64_t i = 0; i < nb; ++i) {
2488
2489 const float d = GGML_FP16_TO_FP32(x[i].d);
2490
2491 for (size_t j = 0; j < sizeof(x->qs); j += 32) {
2492 for (size_t l = 0; l < 4; ++l) {
2493 for (size_t m = 0; m < 32; ++m) {
2494 int8_t q = (x[i].qs[j + m] >> (l*2)) & 3;
2495 *y++ = (float) (q - 1) * d;
2496 }
2497 }
2498 }
2499 }
2500}
2501
2502// ====================== "True" 2-bit (de)-quantization
2503
2504void dequantize_row_iq2_xxs(const block_iq2_xxs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2505 assert(k % QK_K == 0);
2506 const int64_t nb = k / QK_K;
2507
2508 uint32_t aux32[2];
2509 const uint8_t * aux8 = (const uint8_t *)aux32;
2510
2511 for (int i = 0; i < nb; i++) {
2512
2513 const float d = GGML_FP16_TO_FP32(x[i].d);
2514
2515 for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
2516 memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
2517 const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
2518 for (int l = 0; l < 4; ++l) {
2519 const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
2520 const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
2521 for (int j = 0; j < 8; ++j) {
2522 y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
2523 }
2524 y += 8;
2525 }
2526 }
2527 }
2528}
2529
2530// ====================== 2.3125 bpw (de)-quantization
2531
2532void dequantize_row_iq2_xs(const block_iq2_xs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2533 assert(k % QK_K == 0);
2534 const int64_t nb = k / QK_K;
2535
2536 float db[2];
2537
2538 for (int i = 0; i < nb; i++) {
2539
2540 const float d = GGML_FP16_TO_FP32(x[i].d);
2541
2542 for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
2543 db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
2544 db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
2545 for (int l = 0; l < 4; ++l) {
2546 const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
2547 const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
2548 for (int j = 0; j < 8; ++j) {
2549 y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
2550 }
2551 y += 8;
2552 }
2553 }
2554 }
2555}
2556
2557// ====================== 2.5625 bpw (de)-quantization
2558
2559void dequantize_row_iq2_s(const block_iq2_s * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2560 assert(k % QK_K == 0);
2561 const int64_t nb = k / QK_K;
2562
2563 float db[2];
2564
2565 for (int i = 0; i < nb; i++) {
2566
2567 const float d = GGML_FP16_TO_FP32(x[i].d);
2568 const uint8_t * qs = x[i].qs;
2569 const uint8_t * qh = x[i].qh;
2570 const uint8_t * signs = qs + QK_K/8;
2571
2572 for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
2573 db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
2574 db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
2575 for (int l = 0; l < 4; ++l) {
2576 const float dl = db[l/2];
2577 const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
2578 for (int j = 0; j < 8; ++j) {
2579 y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
2580 }
2581 y += 8;
2582 }
2583 qs += 4;
2584 signs += 4;
2585 }
2586 }
2587}
2588
2589// ====================== 3.0625 bpw (de)-quantization
2590
2591void dequantize_row_iq3_xxs(const block_iq3_xxs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2592 assert(k % QK_K == 0);
2593 const int64_t nb = k / QK_K;
2594
2595 uint32_t aux32;
2596
2597 for (int i = 0; i < nb; i++) {
2598
2599 const float d = GGML_FP16_TO_FP32(x[i].d);
2600 const uint8_t * qs = x[i].qs;
2601 const uint8_t * scales_and_signs = qs + QK_K/4;
2602
2603 for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
2604 memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
2605 const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
2606 for (int l = 0; l < 4; ++l) {
2607 const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
2608 const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
2609 const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
2610 for (int j = 0; j < 4; ++j) {
2611 y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
2612 y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
2613 }
2614 y += 8;
2615 }
2616 qs += 8;
2617 }
2618 }
2619}
2620
2621// ====================== 3.3125 bpw (de)-quantization
2622
2623void dequantize_row_iq3_s(const block_iq3_s * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2624 assert(k % QK_K == 0);
2625 const int64_t nb = k / QK_K;
2626
2627 for (int i = 0; i < nb; i++) {
2628
2629 const float d = GGML_FP16_TO_FP32(x[i].d);
2630 const uint8_t * qs = x[i].qs;
2631 const uint8_t * qh = x[i].qh;
2632 const uint8_t * signs = x[i].signs;
2633
2634 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2635 const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
2636 const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
2637 for (int l = 0; l < 4; ++l) {
2638 const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
2639 const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
2640 for (int j = 0; j < 4; ++j) {
2641 y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
2642 y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
2643 }
2644 y += 8;
2645 }
2646 qs += 8;
2647 signs += 4;
2648 for (int l = 0; l < 4; ++l) {
2649 const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
2650 const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
2651 for (int j = 0; j < 4; ++j) {
2652 y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
2653 y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
2654 }
2655 y += 8;
2656 }
2657 qh += 2;
2658 qs += 8;
2659 signs += 4;
2660 }
2661 }
2662}
2663
2664// ====================== 1.5625 bpw (de)-quantization
2665
2666void dequantize_row_iq1_s(const block_iq1_s * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2667 assert(k % QK_K == 0);
2668 const int64_t nb = k / QK_K;
2669
2670 for (int i = 0; i < nb; i++) {
2671
2672 const float d = GGML_FP16_TO_FP32(x[i].d);
2673 const uint8_t * qs = x[i].qs;
2674 const uint16_t * qh = x[i].qh;
2675
2676 for (int ib = 0; ib < QK_K/32; ++ib) {
2677 const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
2678 const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
2679 for (int l = 0; l < 4; ++l) {
2680 const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
2681 for (int j = 0; j < 8; ++j) {
2682 y[j] = dl * (grid[j] + delta);
2683 }
2684 y += 8;
2685 }
2686 qs += 4;
2687 }
2688 }
2689}
2690
2691void dequantize_row_iq1_m(const block_iq1_m * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2692 assert(k % QK_K == 0);
2693 const int64_t nb = k / QK_K;
2694
2695 float delta[4];
2696 uint16_t idx[4];
2697
2698 iq1m_scale_t scale;
2699
2700 for (int i = 0; i < nb; i++) {
2701
2702 const uint16_t * sc = (const uint16_t *)x[i].scales;
2703 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
2704 const float d = GGML_FP16_TO_FP32(scale.f16);
2705
2706 const uint8_t * qs = x[i].qs;
2707 const uint8_t * qh = x[i].qh;
2708
2709 for (int ib = 0; ib < QK_K/32; ++ib) {
2710 const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
2711 const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
2712
2713 idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
2714 idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
2715 idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
2716 idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
2717 delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
2718 delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
2719 delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
2720 delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
2721 for (int l = 0; l < 2; ++l) {
2722 const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
2723 for (int j = 0; j < 8; ++j) {
2724 y[j] = dl1 * (grid[j] + delta[l]);
2725 }
2726 y += 8;
2727 }
2728 for (int l = 2; l < 4; ++l) {
2729 const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
2730 for (int j = 0; j < 8; ++j) {
2731 y[j] = dl2 * (grid[j] + delta[l]);
2732 }
2733 y += 8;
2734 }
2735 qs += 4;
2736 qh += 2;
2737 }
2738 }
2739}
2740
2741void dequantize_row_iq4_nl(const block_iq4_nl * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2742 assert(k % QK4_NL == 0);
2743 const int64_t nb = k / QK4_NL;
2744
2745 for (int i = 0; i < nb; i++) {
2746
2747 const uint8_t * qs = x[i].qs;
2748
2749 const float d = GGML_FP16_TO_FP32(x[i].d);
2750 for (int j = 0; j < QK4_NL/2; ++j) {
2751 y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
2752 y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
2753 }
2754 y += QK4_NL;
2755 qs += QK4_NL/2;
2756 }
2757}
2758
2759void dequantize_row_iq4_xs(const block_iq4_xs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2760 assert(k % QK_K == 0);
2761 const int64_t nb = k / QK_K;
2762
2763 for (int i = 0; i < nb; i++) {
2764
2765 const uint8_t * qs = x[i].qs;
2766
2767 const float d = GGML_FP16_TO_FP32(x[i].d);
2768
2769 for (int ib = 0; ib < QK_K/32; ++ib) {
2770 const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
2771 const float dl = d * (ls - 32);
2772 for (int j = 0; j < 16; ++j) {
2773 y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
2774 y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
2775 }
2776 y += 32;
2777 qs += 16;
2778 }
2779 }
2780}
2781
2782//===================================== Q8_K ==============================================
2783
2784void quantize_row_q8_K_ref(const float * GGML_RESTRICT x, block_q8_K * GGML_RESTRICT y, int64_t k) {
2785 assert(k % QK_K == 0);
2786 const int64_t nb = k / QK_K;
2787
2788 for (int i = 0; i < nb; i++) {
2789
2790 float max = 0;
2791 float amax = 0;
2792 for (int j = 0; j < QK_K; ++j) {
2793 float ax = fabsf(x[j]);
2794 if (ax > amax) {
2795 amax = ax; max = x[j];
2796 }
2797 }
2798 if (!amax) {
2799 y[i].d = 0;
2800 memset(y[i].qs, 0, QK_K);
2801 x += QK_K;
2802 continue;
2803 }
2804 //const float iscale = -128.f/max;
2805 // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
2806 const float iscale = -127.f/max;
2807 for (int j = 0; j < QK_K; ++j) {
2808 int v = nearest_int(iscale*x[j]);
2809 y[i].qs[j] = MIN(127, v);
2810 }
2811 for (int j = 0; j < QK_K/16; ++j) {
2812 int sum = 0;
2813 for (int ii = 0; ii < 16; ++ii) {
2814 sum += y[i].qs[j*16 + ii];
2815 }
2816 y[i].bsums[j] = sum;
2817 }
2818 y[i].d = 1/iscale;
2819 x += QK_K;
2820 }
2821}
2822
2823void dequantize_row_q8_K(const block_q8_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
2824 assert(k % QK_K == 0);
2825 const int64_t nb = k / QK_K;
2826
2827 for (int i = 0; i < nb; i++) {
2828 for (int j = 0; j < QK_K; ++j) {
2829 *y++ = x[i].d * x[i].qs[j];
2830 }
2831 }
2832}
2833
2834// ================================ IQ2 quantization =============================================
2835
2836typedef struct {
2837 uint64_t * grid;
2838 int * map;
2839 uint16_t * neighbours;
2840} iq2_entry_t;
2841
2842static iq2_entry_t iq2_data[4] = {
2843 {NULL, NULL, NULL},
2844 {NULL, NULL, NULL},
2845 {NULL, NULL, NULL},
2846 {NULL, NULL, NULL},
2847};
2848
2849static inline int iq2_data_index(enum ggml_type type) {
2850 GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
2851 return type == GGML_TYPE_IQ2_XXS ? 0 :
2852 type == GGML_TYPE_IQ2_XS ? 1 :
2853 type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
2854}
2855
2856static inline int iq2_grid_size(enum ggml_type type) {
2857 GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
2858 return type == GGML_TYPE_IQ2_XXS ? 256 :
2859 type == GGML_TYPE_IQ2_XS ? 512 :
2860 type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
2861}
2862
2863static int iq2_compare_func(const void * left, const void * right) {
2864 const int * l = (const int *)left;
2865 const int * r = (const int *)right;
2866 return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
2867}
2868
2869void iq2xs_init_impl(enum ggml_type type) {
2870 const int gindex = iq2_data_index(type);
2871 const int grid_size = iq2_grid_size(type);
2872 if (iq2_data[gindex].grid) {
2873 return;
2874 }
2875 static const uint16_t kgrid_2bit_256[256] = {
2876 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
2877 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
2878 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
2879 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
2880 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
2881 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
2882 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
2883 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
2884 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
2885 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
2886 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
2887 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
2888 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
2889 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
2890 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
2891 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
2892 };
2893 static const uint16_t kgrid_2bit_512[512] = {
2894 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
2895 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
2896 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
2897 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
2898 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
2899 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
2900 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
2901 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
2902 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
2903 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
2904 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
2905 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
2906 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
2907 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
2908 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
2909 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
2910 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
2911 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
2912 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
2913 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
2914 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
2915 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
2916 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
2917 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
2918 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
2919 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
2920 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
2921 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
2922 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
2923 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
2924 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
2925 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
2926 };
2927 static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
2928 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
2929 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
2930 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
2931 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
2932 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
2933 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
2934 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
2935 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
2936 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
2937 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
2938 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
2939 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
2940 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
2941 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
2942 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
2943 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
2944 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
2945 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
2946 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
2947 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
2948 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
2949 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
2950 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
2951 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
2952 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
2953 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
2954 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
2955 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
2956 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
2957 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
2958 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
2959 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
2960 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
2961 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
2962 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
2963 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
2964 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
2965 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
2966 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
2967 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
2968 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
2969 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
2970 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
2971 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
2972 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
2973 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
2974 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
2975 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
2976 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
2977 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
2978 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
2979 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
2980 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
2981 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
2982 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
2983 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
2984 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
2985 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
2986 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
2987 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
2988 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
2989 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
2990 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
2991 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
2992 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
2993 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
2994 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
2995 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
2996 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
2997 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
2998 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
2999 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
3000 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
3001 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
3002 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
3003 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
3004 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
3005 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
3006 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
3007 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
3008 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
3009 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
3010 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
3011 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
3012 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
3013 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
3014 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
3015 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
3016 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
3017 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
3018 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
3019 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
3020 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
3021 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
3022 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
3023 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
3024 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
3025 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
3026 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
3027 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
3028 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
3029 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
3030 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
3031 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
3032 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
3033 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
3034 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
3035 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
3036 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
3037 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
3038 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
3039 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
3040 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
3041 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
3042 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
3043 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
3044 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
3045 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
3046 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
3047 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
3048 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
3049 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
3050 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
3051 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
3052 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
3053 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
3054 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
3055 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
3056 };
3057 static const uint16_t kgrid_2bit_1024[1024] = {
3058 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
3059 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
3060 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
3061 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
3062 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
3063 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
3064 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
3065 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
3066 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
3067 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
3068 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
3069 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
3070 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
3071 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
3072 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
3073 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
3074 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
3075 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
3076 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
3077 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
3078 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
3079 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
3080 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
3081 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
3082 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
3083 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
3084 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
3085 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
3086 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
3087 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
3088 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
3089 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
3090 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
3091 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
3092 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
3093 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
3094 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
3095 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
3096 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
3097 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
3098 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
3099 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
3100 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
3101 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
3102 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
3103 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
3104 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
3105 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
3106 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
3107 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
3108 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
3109 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
3110 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
3111 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
3112 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
3113 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
3114 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
3115 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
3116 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
3117 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
3118 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
3119 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
3120 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
3121 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
3122 };
3123
3124 const int kmap_size = 43692;
3125 //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
3126 const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
3127 const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
3128 type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
3129 type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
3130 uint64_t * kgrid_q2xs;
3131 int * kmap_q2xs;
3132 uint16_t * kneighbors_q2xs;
3133
3134 //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
3135 uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
3136 for (int k = 0; k < grid_size; ++k) {
3137 int8_t * pos = (int8_t *)(the_grid + k);
3138 for (int i = 0; i < 8; ++i) {
3139 int l = (kgrid[k] >> 2*i) & 0x3;
3140 pos[i] = 2*l + 1;
3141 }
3142 }
3143 kgrid_q2xs = the_grid;
3144 iq2_data[gindex].grid = the_grid;
3145 kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
3146 iq2_data[gindex].map = kmap_q2xs;
3147 for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
3148 uint64_t aux64;
3149 uint8_t * aux8 = (uint8_t *)&aux64;
3150 for (int i = 0; i < grid_size; ++i) {
3151 aux64 = kgrid_q2xs[i];
3152 uint16_t index = 0;
3153 for (int k=0; k<8; ++k) {
3154 uint16_t q = (aux8[k] - 1)/2;
3155 index |= (q << 2*k);
3156 }
3157 kmap_q2xs[index] = i;
3158 }
3159 // The neighbour search runs in three passes:
3160 // 1. Parallel: for each i, qsort and count its neighbours into n_per_i,
3161 // and reduce the totals (num_neighbors, num_not_in_map).
3162 // 2. Serial: prefix-sum n_per_i into offsets[], so each i has a
3163 // pre-assigned slice of kneighbors_q2xs to write into.
3164 // 3. Parallel: redo the qsort and write each i's neighbour list at
3165 // offsets[i].
3166 int * n_per_i = (int *)malloc(kmap_size*sizeof(int));
3167 GGML_ASSERT(n_per_i);
3168 int num_neighbors = 0, num_not_in_map = 0;
3169#ifdef GGML_USE_OPENMP
3170 #pragma omp parallel reduction(+:num_neighbors,num_not_in_map)
3171#endif
3172 {
3173 int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
3174 GGML_ASSERT(dist2);
3175 int8_t pos[8];
3176 int i;
3177#ifdef GGML_USE_OPENMP
3178 #pragma omp for schedule(dynamic, 64)
3179#endif
3180 for (i = 0; i < kmap_size; ++i) {
3181 if (kmap_q2xs[i] >= 0) {
3182 n_per_i[i] = 0;
3183 continue;
3184 }
3185 ++num_not_in_map;
3186 for (int k = 0; k < 8; ++k) {
3187 int l = (i >> 2*k) & 0x3;
3188 pos[k] = 2*l + 1;
3189 }
3190 for (int j = 0; j < grid_size; ++j) {
3191 const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
3192 int d2 = 0;
3193 for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
3194 dist2[2*j+0] = d2;
3195 dist2[2*j+1] = j;
3196 }
3197 qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
3198 int n = 0; int d2 = dist2[0];
3199 int nhave = 1;
3200 for (int j = 0; j < grid_size; ++j) {
3201 if (dist2[2*j] > d2) {
3202 if (nhave == nwant) break;
3203 d2 = dist2[2*j];
3204 ++nhave;
3205 }
3206 ++n;
3207 }
3208 n_per_i[i] = n;
3209 num_neighbors += n;
3210 }
3211 free(dist2);
3212 }
3213 //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
3214 kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
3215 iq2_data[gindex].neighbours = kneighbors_q2xs;
3216
3217 int * offsets = (int *)malloc(kmap_size*sizeof(int));
3218 GGML_ASSERT(offsets);
3219 int counter = 0;
3220 for (int i = 0; i < kmap_size; ++i) {
3221 if (kmap_q2xs[i] >= 0) {
3222 offsets[i] = -1;
3223 continue;
3224 }
3225 offsets[i] = counter;
3226 counter += 1 + n_per_i[i];
3227 }
3228
3229#ifdef GGML_USE_OPENMP
3230 #pragma omp parallel
3231#endif
3232 {
3233 int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
3234 GGML_ASSERT(dist2);
3235 int8_t pos[8];
3236 int i;
3237#ifdef GGML_USE_OPENMP
3238 #pragma omp for schedule(dynamic, 64)
3239#endif
3240 for (i = 0; i < kmap_size; ++i) {
3241 if (kmap_q2xs[i] >= 0) continue;
3242 for (int k = 0; k < 8; ++k) {
3243 int l = (i >> 2*k) & 0x3;
3244 pos[k] = 2*l + 1;
3245 }
3246 for (int j = 0; j < grid_size; ++j) {
3247 const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
3248 int d2 = 0;
3249 for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
3250 dist2[2*j+0] = d2;
3251 dist2[2*j+1] = j;
3252 }
3253 qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
3254 int local_counter = offsets[i];
3255 kmap_q2xs[i] = -(local_counter + 1);
3256 int d2 = dist2[0];
3257 uint16_t * start = &kneighbors_q2xs[local_counter++];
3258 int n = 0, nhave = 1;
3259 for (int j = 0; j < grid_size; ++j) {
3260 if (dist2[2*j] > d2) {
3261 if (nhave == nwant) break;
3262 d2 = dist2[2*j];
3263 ++nhave;
3264 }
3265 kneighbors_q2xs[local_counter++] = dist2[2*j+1];
3266 ++n;
3267 }
3268 *start = n;
3269 }
3270 free(dist2);
3271 }
3272 free(offsets);
3273 free(n_per_i);
3274}
3275
3276void iq2xs_free_impl(enum ggml_type type) {
3277 GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
3278 const int gindex = iq2_data_index(type);
3279 if (iq2_data[gindex].grid) {
3280 free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
3281 free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
3282 free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
3283 }
3284}
3285
3286static int iq2_find_best_neighbour(const uint16_t * GGML_RESTRICT neighbours, const uint64_t * GGML_RESTRICT grid,
3287 const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float scale, int8_t * GGML_RESTRICT L) {
3288 int num_neighbors = neighbours[0];
3289 GGML_ASSERT(num_neighbors > 0);
3290 float best_d2 = FLT_MAX;
3291 int grid_index = -1;
3292 for (int j = 1; j <= num_neighbors; ++j) {
3293 const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
3294 float d2 = 0;
3295 for (int i = 0; i < 8; ++i) {
3296 float q = pg[i];
3297 float diff = scale*q - xval[i];
3298 d2 += weight[i]*diff*diff;
3299 }
3300 if (d2 < best_d2) {
3301 best_d2 = d2; grid_index = neighbours[j];
3302 }
3303 }
3304 GGML_ASSERT(grid_index >= 0);
3305 const int8_t * pg = (const int8_t *)(grid + grid_index);
3306 for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
3307 return grid_index;
3308}
3309
3310static void quantize_row_iq2_xxs_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights) {
3311
3312 const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
3313
3314 const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
3315 const int * kmap_q2xs = iq2_data[gindex].map;
3316 const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
3317
3318 GGML_ASSERT(quant_weights && "missing quantization weights");
3319 GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
3320 GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
3321 GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
3322 GGML_ASSERT(n%QK_K == 0);
3323
3324 const int kMaxQ = 3;
3325
3326 const int64_t nbl = n/QK_K;
3327
3328 block_iq2_xxs * y = vy;
3329
3330 float scales[QK_K/32];
3331 float weight[32];
3332 float xval[32];
3333 int8_t L[32];
3334 int8_t Laux[32];
3335 float waux[32];
3336 uint8_t block_signs[4];
3337 uint32_t q2[2*(QK_K/32)];
3338
3339 for (int ibl = 0; ibl < nbl; ++ibl) {
3340
3341 y[ibl].d = GGML_FP32_TO_FP16(0.f);
3342 memset(q2, 0, QK_K/4);
3343
3344 float max_scale = 0;
3345
3346 const float * xbl = x + QK_K*ibl;
3347 float sumx2 = 0;
3348 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
3349 float sigma2 = sumx2/QK_K;
3350
3351 for (int ib = 0; ib < QK_K/32; ++ib) {
3352 const float * xb = xbl + 32*ib;
3353 const float * qw = quant_weights + QK_K*ibl + 32*ib;
3354 for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
3355 for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
3356 for (int k = 0; k < 4; ++k) {
3357 int nflip = 0;
3358 uint8_t s = 0;
3359 for (int i = 0; i < 8; ++i) {
3360 if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
3361 else {
3362 xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
3363 }
3364 }
3365 if (nflip%2) {
3366 int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
3367 for (int i = 1; i < 8; ++i) {
3368 float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
3369 if (ax < min) {
3370 min = ax; imin = i;
3371 }
3372 }
3373 xval[8*k+imin] = -xval[8*k+imin];
3374 s ^= (1 << imin);
3375 }
3376 block_signs[k] = s & 127;
3377 }
3378 float max = xval[0];
3379 for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
3380 if (max < GROUP_MAX_EPS) {
3381 scales[ib] = 0;
3382 memset(L, 0, 32);
3383 continue;
3384 }
3385 float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
3386 float eff_max = scale*kMaxQ;
3387 if (eff_max <= 0) {
3388 scales[ib] = 0;
3389 memset(L, 0, 32);
3390 continue;
3391 }
3392 float best = 0;
3393 for (int is = -6; is <= 6; ++is) {
3394 float id = (2*kMaxQ-1+is*0.1f)/eff_max;
3395 float this_scale = 1/id;
3396 for (int k = 0; k < 4; ++k) {
3397 for (int i = 0; i < 8; ++i) {
3398 int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
3399 Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
3400 }
3401 uint16_t u = 0;
3402 for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
3403 int grid_index = kmap_q2xs[u];
3404 if (grid_index < 0) {
3405 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
3406 grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
3407 }
3408 }
3409 float sumqx = 0, sumq2 = 0;
3410 for (int i = 0; i < 32; ++i) {
3411 float w = weight[i];
3412 float q = 2*Laux[i] + 1;
3413 sumqx += w*xval[i]*q;
3414 sumq2 += w*q*q;
3415 }
3416 if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
3417 scale = sumqx/sumq2; best = scale*sumqx;
3418 memcpy(L, Laux, 32);
3419 }
3420 }
3421 if (scale > 0) {
3422 float id = 1/scale;
3423 for (int k = 0; k < 4; ++k) {
3424 uint16_t u = 0;
3425 for (int i = 0; i < 8; ++i) {
3426 int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
3427 l = MAX(0, MIN(kMaxQ-1, l));
3428 u |= (l << 2*i);
3429 }
3430 int grid_index = kmap_q2xs[u];
3431 if (grid_index < 0) {
3432 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
3433 grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
3434 }
3435 const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
3436 for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
3437 }
3438 float sumqx = 0, sumq2 = 0;
3439 for (int i = 0; i < 32; ++i) {
3440 float w = weight[i];
3441 float q = 2*L[i] + 1;
3442 sumqx += w*xval[i]*q;
3443 sumq2 += w*q*q;
3444 }
3445 if (sumq2 > 0) scale = sumqx/sumq2;
3446 }
3447 if (scale < 0) {
3448 // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
3449 // and correspondingly flip quant signs.
3450 scale = -scale;
3451 for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
3452 }
3453 for (int k = 0; k < 4; ++k) {
3454 uint16_t u = 0;
3455 for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
3456 int grid_index = kmap_q2xs[u];
3457 if (grid_index < 0) {
3458 printf("Oops: found point %u not on grid:", u);
3459 for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
3460 printf("\n");
3461 GGML_ABORT("fatal error");
3462 }
3463 q2[2*ib+0] |= ((uint32_t) grid_index << 8*k);
3464 q2[2*ib+1] |= (block_signs[k] << 7*k);
3465 }
3466 GGML_ASSERT(scale >= 0);
3467 scales[ib] = scale;
3468 max_scale = MAX(max_scale, scale);
3469 }
3470
3471 if (!max_scale) {
3472 memset(y[ibl].qs, 0, QK_K/4);
3473 continue;
3474 }
3475
3476 float d = max_scale/31;
3477 y[ibl].d = GGML_FP32_TO_FP16(d);
3478 float id = 1/d;
3479 for (int ib = 0; ib < QK_K/32; ++ib) {
3480 int l = nearest_int(0.5f*(id*scales[ib]-1));
3481 l = MAX(0, MIN(15, l));
3482 q2[2*ib+1] |= ((uint32_t)l << 28);
3483 }
3484 memcpy(y[ibl].qs, q2, QK_K/4);
3485 }
3486}
3487
3488static void quantize_row_iq2_xs_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights) {
3489
3490 const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
3491
3492 const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
3493 const int * kmap_q2xs = iq2_data[gindex].map;
3494 const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
3495
3496 GGML_ASSERT(quant_weights && "missing quantization weights");
3497 GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
3498 GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
3499 GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
3500 GGML_ASSERT(n%QK_K == 0);
3501
3502 const int kMaxQ = 3;
3503
3504 const int64_t nbl = n/QK_K;
3505
3506 block_iq2_xs * y = vy;
3507
3508 float scales[QK_K/16];
3509 float weight[16];
3510 float xval[16];
3511 int8_t L[16];
3512 int8_t Laux[16];
3513 float waux[16];
3514 bool is_on_grid[2];
3515 bool is_on_grid_aux[2];
3516 uint8_t block_signs[2];
3517 uint16_t q2[2*(QK_K/16)];
3518
3519 for (int ibl = 0; ibl < nbl; ++ibl) {
3520
3521 y[ibl].d = GGML_FP32_TO_FP16(0.f);
3522 memset(q2, 0, QK_K/4);
3523 memset(y[ibl].scales, 0, QK_K/32);
3524
3525 float max_scale = 0;
3526
3527 const float * xbl = x + QK_K*ibl;
3528 float sumx2 = 0;
3529 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
3530 float sigma2 = sumx2/QK_K;
3531
3532 for (int ib = 0; ib < QK_K/16; ++ib) {
3533 const float * xb = xbl + 16*ib;
3534 const float * qw = quant_weights + QK_K*ibl + 16*ib;
3535 for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
3536 for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
3537 for (int k = 0; k < 2; ++k) {
3538 int nflip = 0;
3539 uint8_t s = 0;
3540 for (int i = 0; i < 8; ++i) {
3541 if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
3542 else {
3543 xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
3544 }
3545 }
3546 if (nflip%2) {
3547 int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
3548 for (int i = 1; i < 8; ++i) {
3549 float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
3550 if (ax < min) {
3551 min = ax; imin = i;
3552 }
3553 }
3554 xval[8*k+imin] = -xval[8*k+imin];
3555 s ^= (1 << imin);
3556 }
3557 block_signs[k] = s & 127;
3558 }
3559 float max = xval[0];
3560 for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
3561 memset(L, 0, 16);
3562 if (max < GROUP_MAX_EPS) {
3563 scales[ib] = 0;
3564 continue;
3565 }
3566 float best = 0;
3567 float scale = max/(2*kMaxQ-1);
3568 is_on_grid[0] = is_on_grid[1] = true;
3569 for (int is = -9; is <= 9; ++is) {
3570 float id = (2*kMaxQ-1+is*0.1f)/max;
3571 float this_scale = 1/id;
3572 for (int k = 0; k < 2; ++k) {
3573 for (int i = 0; i < 8; ++i) {
3574 int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
3575 Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
3576 }
3577 uint16_t u = 0;
3578 for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
3579 int grid_index = kmap_q2xs[u];
3580 is_on_grid_aux[k] = true;
3581 if (grid_index < 0) {
3582 is_on_grid_aux[k] = false;
3583 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
3584 grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
3585 }
3586 }
3587 float sumqx = 0, sumq2 = 0;
3588 for (int i = 0; i < 16; ++i) {
3589 float w = weight[i];
3590 float q = 2*Laux[i] + 1;
3591 sumqx += w*xval[i]*q;
3592 sumq2 += w*q*q;
3593 }
3594 if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
3595 scale = sumqx/sumq2; best = scale*sumqx;
3596 for (int i = 0; i < 16; ++i) L[i] = Laux[i];
3597 for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
3598 }
3599 }
3600 int n_not_ongrid = 0;
3601 for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
3602 if (n_not_ongrid > 0 && scale > 0) {
3603 float id = 1/scale;
3604 for (int k = 0; k < 2; ++k) {
3605 if (is_on_grid[k]) continue;
3606 uint16_t u = 0;
3607 for (int i = 0; i < 8; ++i) {
3608 int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
3609 l = MAX(0, MIN(kMaxQ-1, l));
3610 u |= (l << 2*i);
3611 L[8*k + i] = l;
3612 }
3613 int grid_index = kmap_q2xs[u];
3614 if (grid_index < 0) {
3615 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
3616 grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
3617 }
3618 }
3619 float sumqx = 0, sumq2 = 0;
3620 for (int i = 0; i < 16; ++i) {
3621 float w = weight[i];
3622 float q = 2*L[i] + 1;
3623 sumqx += w*xval[i]*q;
3624 sumq2 += w*q*q;
3625 }
3626 if (sumq2 > 0) scale = sumqx/sumq2;
3627 }
3628 if (scale < 0) {
3629 scale = -scale;
3630 for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
3631 }
3632 for (int k = 0; k < 2; ++k) {
3633 uint16_t u = 0;
3634 for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
3635 int grid_index = kmap_q2xs[u];
3636 if (grid_index < 0) {
3637 printf("Oops: found point %u not on grid:", u);
3638 for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
3639 printf("\n");
3640 GGML_ABORT("fatal error");
3641 }
3642 q2[2*ib+k] = grid_index | (block_signs[k] << 9);
3643 }
3644 GGML_ASSERT(scale >= 0);
3645 scales[ib] = scale;
3646 max_scale = MAX(max_scale, scale);
3647 }
3648
3649 if (!max_scale) {
3650 memset(y[ibl].qs, 0, QK_K/4);
3651 continue;
3652 }
3653
3654 float d = max_scale/31;
3655 y[ibl].d = GGML_FP32_TO_FP16(d);
3656 float id = 1/d;
3657 for (int ib = 0; ib < QK_K/16; ++ib) {
3658 int l = nearest_int(0.5f*(id*scales[ib]-1));
3659 l = MAX(0, MIN(15, l));
3660 if (ib%2 == 0) y[ibl].scales[ib/2] = l;
3661 else y[ibl].scales[ib/2] |= (l << 4);
3662 }
3663 memcpy(y[ibl].qs, q2, QK_K/4);
3664
3665 }
3666}
3667
3668size_t quantize_iq2_xxs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
3669 GGML_ASSERT(n_per_row%QK_K == 0);
3670 int64_t nblock = n_per_row/QK_K;
3671 char * qrow = (char *)dst;
3672 for (int64_t row = 0; row < nrow; ++row) {
3673 quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
3674 src += n_per_row;
3675 qrow += nblock*sizeof(block_iq2_xxs);
3676 }
3677 return nrow * nblock * sizeof(block_iq2_xxs);
3678}
3679
3680size_t quantize_iq2_xs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
3681 GGML_ASSERT(n_per_row%QK_K == 0);
3682 int64_t nblock = n_per_row/QK_K;
3683 char * qrow = (char *)dst;
3684 for (int64_t row = 0; row < nrow; ++row) {
3685 quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
3686 src += n_per_row;
3687 qrow += nblock*sizeof(block_iq2_xs);
3688 }
3689 return nrow * nblock * sizeof(block_iq2_xs);
3690}
3691
3692//
3693// ============================================= 3-bit using D4 lattice
3694//
3695
3696typedef struct {
3697 uint32_t * grid;
3698 int * map;
3699 uint16_t * neighbours;
3700} iq3_entry_t;
3701
3702static iq3_entry_t iq3_data[2] = {
3703 {NULL, NULL, NULL},
3704 {NULL, NULL, NULL},
3705};
3706
3707static inline int iq3_data_index(int grid_size) {
3708 (void)grid_size;
3709 GGML_ASSERT(grid_size == 256 || grid_size == 512);
3710 return grid_size == 256 ? 0 : 1;
3711}
3712
3713static int iq3_compare_func(const void * left, const void * right) {
3714 const int * l = (const int *)left;
3715 const int * r = (const int *)right;
3716 return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
3717}
3718
3719void iq3xs_init_impl(int grid_size) {
3720 const int gindex = iq3_data_index(grid_size);
3721 if (iq3_data[gindex].grid) {
3722 return;
3723 }
3724 static const uint16_t kgrid_256[256] = {
3725 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
3726 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
3727 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
3728 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
3729 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
3730 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
3731 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
3732 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
3733 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
3734 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
3735 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
3736 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
3737 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
3738 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
3739 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
3740 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
3741 };
3742 static const uint16_t kgrid_512[512] = {
3743 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
3744 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
3745 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
3746 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
3747 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
3748 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
3749 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
3750 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
3751 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
3752 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
3753 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
3754 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
3755 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
3756 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
3757 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
3758 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
3759 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
3760 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
3761 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
3762 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
3763 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
3764 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
3765 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
3766 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
3767 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
3768 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
3769 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
3770 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
3771 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
3772 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
3773 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
3774 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
3775 };
3776
3777 const int kmap_size = 4096;
3778 const int nwant = grid_size == 256 ? 2 : 3;
3779 const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
3780 uint32_t * kgrid_q3xs;
3781 int * kmap_q3xs;
3782 uint16_t * kneighbors_q3xs;
3783
3784 //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
3785 uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
3786 for (int k = 0; k < grid_size; ++k) {
3787 int8_t * pos = (int8_t *)(the_grid + k);
3788 for (int i = 0; i < 4; ++i) {
3789 int l = (kgrid[k] >> 3*i) & 0x7;
3790 pos[i] = 2*l + 1;
3791 }
3792 }
3793 kgrid_q3xs = the_grid;
3794 iq3_data[gindex].grid = the_grid;
3795 kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
3796 iq3_data[gindex].map = kmap_q3xs;
3797 for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
3798 uint32_t aux32;
3799 uint8_t * aux8 = (uint8_t *)&aux32;
3800 for (int i = 0; i < grid_size; ++i) {
3801 aux32 = kgrid_q3xs[i];
3802 uint16_t index = 0;
3803 for (int k=0; k<4; ++k) {
3804 uint16_t q = (aux8[k] - 1)/2;
3805 index |= (q << 3*k);
3806 }
3807 kmap_q3xs[index] = i;
3808 }
3809 // See explanation of parallelism in iq2xs_init_impl
3810 int * n_per_i = (int *)malloc(kmap_size*sizeof(int));
3811 GGML_ASSERT(n_per_i);
3812 int num_neighbors = 0, num_not_in_map = 0;
3813#ifdef GGML_USE_OPENMP
3814 #pragma omp parallel reduction(+:num_neighbors,num_not_in_map)
3815#endif
3816 {
3817 int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
3818 GGML_ASSERT(dist2);
3819 int8_t pos[4];
3820 int i;
3821#ifdef GGML_USE_OPENMP
3822 #pragma omp for schedule(dynamic, 64)
3823#endif
3824 for (i = 0; i < kmap_size; ++i) {
3825 if (kmap_q3xs[i] >= 0) {
3826 n_per_i[i] = 0;
3827 continue;
3828 }
3829 ++num_not_in_map;
3830 for (int k = 0; k < 4; ++k) {
3831 int l = (i >> 3*k) & 0x7;
3832 pos[k] = 2*l + 1;
3833 }
3834 for (int j = 0; j < grid_size; ++j) {
3835 const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
3836 int d2 = 0;
3837 for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
3838 dist2[2*j+0] = d2;
3839 dist2[2*j+1] = j;
3840 }
3841 qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
3842 int n = 0; int d2 = dist2[0];
3843 int nhave = 1;
3844 for (int j = 0; j < grid_size; ++j) {
3845 if (dist2[2*j] > d2) {
3846 if (nhave == nwant) break;
3847 d2 = dist2[2*j];
3848 ++nhave;
3849 }
3850 ++n;
3851 }
3852 n_per_i[i] = n;
3853 num_neighbors += n;
3854 }
3855 free(dist2);
3856 }
3857 //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
3858 kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
3859 iq3_data[gindex].neighbours = kneighbors_q3xs;
3860
3861 int * offsets = (int *)malloc(kmap_size*sizeof(int));
3862 GGML_ASSERT(offsets);
3863 int counter = 0;
3864 for (int i = 0; i < kmap_size; ++i) {
3865 if (kmap_q3xs[i] >= 0) {
3866 offsets[i] = -1;
3867 continue;
3868 }
3869 offsets[i] = counter;
3870 counter += 1 + n_per_i[i];
3871 }
3872
3873#ifdef GGML_USE_OPENMP
3874 #pragma omp parallel
3875#endif
3876 {
3877 int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
3878 GGML_ASSERT(dist2);
3879 int8_t pos[4];
3880 int i;
3881#ifdef GGML_USE_OPENMP
3882 #pragma omp for schedule(dynamic, 64)
3883#endif
3884 for (i = 0; i < kmap_size; ++i) {
3885 if (kmap_q3xs[i] >= 0) continue;
3886 for (int k = 0; k < 4; ++k) {
3887 int l = (i >> 3*k) & 0x7;
3888 pos[k] = 2*l + 1;
3889 }
3890 for (int j = 0; j < grid_size; ++j) {
3891 const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
3892 int d2 = 0;
3893 for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
3894 dist2[2*j+0] = d2;
3895 dist2[2*j+1] = j;
3896 }
3897 qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
3898 int local_counter = offsets[i];
3899 kmap_q3xs[i] = -(local_counter + 1);
3900 int d2 = dist2[0];
3901 uint16_t * start = &kneighbors_q3xs[local_counter++];
3902 int n = 0, nhave = 1;
3903 for (int j = 0; j < grid_size; ++j) {
3904 if (dist2[2*j] > d2) {
3905 if (nhave == nwant) break;
3906 d2 = dist2[2*j];
3907 ++nhave;
3908 }
3909 kneighbors_q3xs[local_counter++] = dist2[2*j+1];
3910 ++n;
3911 }
3912 *start = n;
3913 }
3914 free(dist2);
3915 }
3916 free(offsets);
3917 free(n_per_i);
3918}
3919
3920void iq3xs_free_impl(int grid_size) {
3921 GGML_ASSERT(grid_size == 256 || grid_size == 512);
3922 const int gindex = iq3_data_index(grid_size);
3923 if (iq3_data[gindex].grid) {
3924 free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
3925 free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
3926 free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
3927 }
3928}
3929
3930static int iq3_find_best_neighbour(const uint16_t * GGML_RESTRICT neighbours, const uint32_t * GGML_RESTRICT grid,
3931 const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float scale, int8_t * GGML_RESTRICT L) {
3932 int num_neighbors = neighbours[0];
3933 GGML_ASSERT(num_neighbors > 0);
3934 float best_d2 = FLT_MAX;
3935 int grid_index = -1;
3936 for (int j = 1; j <= num_neighbors; ++j) {
3937 const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
3938 float d2 = 0;
3939 for (int i = 0; i < 4; ++i) {
3940 float q = pg[i];
3941 float diff = scale*q - xval[i];
3942 d2 += weight[i]*diff*diff;
3943 }
3944 if (d2 < best_d2) {
3945 best_d2 = d2; grid_index = neighbours[j];
3946 }
3947 }
3948 GGML_ASSERT(grid_index >= 0);
3949 const int8_t * pg = (const int8_t *)(grid + grid_index);
3950 for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
3951 return grid_index;
3952}
3953
3954static void quantize_row_iq3_xxs_impl(int grid_size, const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n,
3955 const float * GGML_RESTRICT quant_weights) {
3956
3957 const int gindex = iq3_data_index(grid_size);
3958
3959 const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
3960 const int * kmap_q3xs = iq3_data[gindex].map;
3961 const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
3962
3963 //GGML_ASSERT(quant_weights && "missing quantization weights");
3964 GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
3965 GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
3966 GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
3967 GGML_ASSERT(n%QK_K == 0);
3968
3969 const int kMaxQ = 8;
3970
3971 const int64_t nbl = n/QK_K;
3972
3973 ggml_fp16_t * dh;
3974 uint8_t * qs;
3975 int block_size;
3976 if (grid_size == 256) {
3977 block_iq3_xxs * y = vy;
3978 dh = &y->d;
3979 qs = y->qs;
3980 block_size = sizeof(block_iq3_xxs);
3981 } else {
3982 block_iq3_s * y = vy;
3983 dh = &y->d;
3984 qs = y->qs;
3985 block_size = sizeof(block_iq3_s);
3986 }
3987 int quant_size = block_size - sizeof(ggml_fp16_t);
3988
3989 float scales[QK_K/32];
3990 float weight[32];
3991 float xval[32];
3992 int8_t L[32];
3993 int8_t Laux[32];
3994 float waux[32];
3995 bool is_on_grid[8];
3996 bool is_on_grid_aux[8];
3997 uint8_t block_signs[8];
3998 uint8_t q3[3*(QK_K/8)+QK_K/32];
3999 uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
4000 uint8_t * qh = q3 + 3*(QK_K/8);
4001
4002 for (int ibl = 0; ibl < nbl; ++ibl) {
4003
4004 dh[0] = GGML_FP32_TO_FP16(0.f);
4005 memset(q3, 0, 3*QK_K/8+QK_K/32);
4006
4007 float max_scale = 0;
4008
4009 const float * xbl = x + QK_K*ibl;
4010 float sumx2 = 0;
4011 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
4012 float sigma2 = 2*sumx2/QK_K;
4013
4014 for (int ib = 0; ib < QK_K/32; ++ib) {
4015 const float * xb = xbl + 32*ib;
4016 if (quant_weights) {
4017 const float * qw = quant_weights + QK_K*ibl + 32*ib;
4018 for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
4019 } else {
4020 for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
4021 }
4022 for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
4023 for (int k = 0; k < 4; ++k) {
4024 int nflip = 0;
4025 uint8_t s = 0;
4026 for (int i = 0; i < 8; ++i) {
4027 if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
4028 else {
4029 xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
4030 }
4031 }
4032 if (nflip%2) {
4033 int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
4034 for (int i = 1; i < 8; ++i) {
4035 float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
4036 if (ax < min) {
4037 min = ax; imin = i;
4038 }
4039 }
4040 xval[8*k+imin] = -xval[8*k+imin];
4041 s ^= (1 << imin);
4042 }
4043 block_signs[k] = s & 127;
4044 }
4045 float max = xval[0];
4046 for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
4047 memset(L, 0, 32);
4048 if (max < GROUP_MAX_EPS_IQ3_XXS) {
4049 scales[ib] = 0;
4050 continue;
4051 }
4052 float best = 0;
4053 float scale = max/(2*kMaxQ-1);
4054 for (int k = 0; k < 8; ++k) is_on_grid[k] = true;
4055 for (int is = -15; is <= 15; ++is) {
4056 float id = (2*kMaxQ-1+is*0.2f)/max;
4057 float this_scale = 1/id;
4058 for (int k = 0; k < 8; ++k) {
4059 for (int i = 0; i < 4; ++i) {
4060 int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
4061 Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
4062 }
4063 uint16_t u = 0;
4064 for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
4065 int grid_index = kmap_q3xs[u];
4066 is_on_grid_aux[k] = true;
4067 if (grid_index < 0) {
4068 is_on_grid_aux[k] = false;
4069 const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
4070 grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
4071 }
4072 }
4073 float sumqx = 0, sumq2 = 0;
4074 for (int i = 0; i < 32; ++i) {
4075 float w = weight[i];
4076 float q = 2*Laux[i] + 1;
4077 sumqx += w*xval[i]*q;
4078 sumq2 += w*q*q;
4079 }
4080 if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
4081 scale = sumqx/sumq2; best = scale*sumqx;
4082 for (int i = 0; i < 32; ++i) L[i] = Laux[i];
4083 for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
4084 }
4085 }
4086 int n_not_ongrid = 0;
4087 for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
4088 if (n_not_ongrid > 0 && scale > 0) {
4089 float id = 1/scale;
4090 for (int k = 0; k < 8; ++k) {
4091 if (is_on_grid[k]) continue;
4092 uint16_t u = 0;
4093 for (int i = 0; i < 4; ++i) {
4094 int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
4095 l = MAX(0, MIN(kMaxQ-1, l));
4096 u |= (l << 3*i);
4097 }
4098 int grid_index = kmap_q3xs[u];
4099 if (grid_index < 0) {
4100 const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
4101 grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
4102 }
4103 const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
4104 for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
4105 }
4106 float sumqx = 0, sumq2 = 0;
4107 for (int i = 0; i < 32; ++i) {
4108 float w = weight[i];
4109 float q = 2*L[i] + 1;
4110 sumqx += w*xval[i]*q;
4111 sumq2 += w*q*q;
4112 }
4113 if (sumq2 > 0) scale = sumqx/sumq2;
4114 }
4115 if (scale < 0) {
4116 // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
4117 // and correspondingly flip quant signs.
4118 scale = -scale;
4119 for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
4120 }
4121 for (int k = 0; k < 8; ++k) {
4122 uint16_t u = 0;
4123 for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
4124 int grid_index = kmap_q3xs[u];
4125 if (grid_index < 0) {
4126 printf("Oops: found point %u not on grid:", u);
4127 for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
4128 printf("\n");
4129 GGML_ABORT("fatal error");
4130 }
4131 if (grid_size == 256) {
4132 q3[8*ib+k] = grid_index;
4133 } else {
4134 q3[8*ib+k] = grid_index & 255;
4135 qh[ib] |= ((grid_index >> 8) << k);
4136 }
4137
4138 }
4139 scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
4140 GGML_ASSERT(scale >= 0);
4141 scales[ib] = scale;
4142 max_scale = MAX(max_scale, scale);
4143 }
4144
4145 if (!max_scale) {
4146 memset(qs, 0, quant_size);
4147 dh += block_size/sizeof(ggml_fp16_t);
4148 qs += block_size;
4149 continue;
4150 }
4151
4152 float d = max_scale/31;
4153 dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
4154 float id = 1/d;
4155 for (int ib = 0; ib < QK_K/32; ++ib) {
4156 int l = nearest_int(0.5f*(id*scales[ib]-1));
4157 l = MAX(0, MIN(15, l));
4158 scales_and_signs[ib] |= ((uint32_t)l << 28);
4159 }
4160 memcpy(qs, q3, quant_size);
4161
4162 dh += block_size/sizeof(ggml_fp16_t);
4163 qs += block_size;
4164
4165 }
4166}
4167
4168size_t quantize_iq3_xxs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
4169 GGML_ASSERT(n_per_row%QK_K == 0);
4170 int64_t nblock = n_per_row/QK_K;
4171 char * qrow = (char *)dst;
4172 for (int64_t row = 0; row < nrow; ++row) {
4173 quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
4174 src += n_per_row;
4175 qrow += nblock*sizeof(block_iq3_xxs);
4176 }
4177 return nrow * nblock * sizeof(block_iq3_xxs);
4178}
4179
4180void quantize_row_iq3_xxs_ref(const float * GGML_RESTRICT x, block_iq3_xxs * GGML_RESTRICT y, int64_t k) {
4181 assert(k % QK_K == 0);
4182 quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
4183}
4184
4185static void quantize_row_iq3_s_impl(int block_size, const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int n,
4186 const float * GGML_RESTRICT quant_weights,
4187 float * scales,
4188 float * weight,
4189 float * xval,
4190 int8_t * L,
4191 int8_t * Laux,
4192 float * waux,
4193 bool * is_on_grid,
4194 bool * is_on_grid_aux,
4195 uint8_t * block_signs) {
4196
4197 const int gindex = iq3_data_index(512);
4198
4199 const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
4200 const int * kmap_q3xs = iq3_data[gindex].map;
4201 const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
4202
4203 //GGML_ASSERT(quant_weights && "missing quantization weights");
4204 GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
4205 GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
4206 GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
4207 GGML_ASSERT(n%QK_K == 0);
4208
4209 const int kMaxQ = 8;
4210
4211 const int64_t nbl = n/QK_K;
4212
4213 block_iq3_s * y = vy;
4214
4215 const int bs4 = block_size/4;
4216 const int bs8 = block_size/8;
4217
4218 for (int ibl = 0; ibl < nbl; ++ibl) {
4219
4220 memset(&y[ibl], 0, sizeof(block_iq3_s));
4221 y[ibl].d = GGML_FP32_TO_FP16(0.f);
4222
4223 uint8_t * qs = y[ibl].qs;
4224 uint8_t * qh = y[ibl].qh;
4225 uint8_t * signs = y[ibl].signs;
4226
4227 float max_scale = 0;
4228
4229 const float * xbl = x + QK_K*ibl;
4230 float sumx2 = 0;
4231 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
4232 float sigma2 = 2*sumx2/QK_K;
4233
4234 for (int ib = 0; ib < QK_K/block_size; ++ib) {
4235 const float * xb = xbl + block_size*ib;
4236 if (quant_weights) {
4237 const float * qw = quant_weights + QK_K*ibl + block_size*ib;
4238 for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
4239 } else {
4240 for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
4241 }
4242 for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
4243 for (int k = 0; k < bs8; ++k) {
4244 uint8_t s = 0;
4245 for (int i = 0; i < 8; ++i) {
4246 if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
4247 else {
4248 xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
4249 }
4250 }
4251 block_signs[k] = s;
4252 }
4253 float max = xval[0];
4254 for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
4255 memset(L, 0, block_size);
4256 if (!max) {
4257 scales[ib] = 0;
4258 continue;
4259 }
4260 float best = 0;
4261 float scale = max/(2*kMaxQ-1);
4262 for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
4263 for (int is = -9; is <= 9; ++is) {
4264 float id = (2*kMaxQ-1+is*0.2f)/max;
4265 float this_scale = 1/id;
4266 for (int k = 0; k < bs4; ++k) {
4267 for (int i = 0; i < 4; ++i) {
4268 int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
4269 Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
4270 }
4271 uint16_t u = 0;
4272 for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
4273 int grid_index = kmap_q3xs[u];
4274 is_on_grid_aux[k] = true;
4275 if (grid_index < 0) {
4276 is_on_grid_aux[k] = false;
4277 const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
4278 grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
4279 }
4280 }
4281 float sumqx = 0, sumq2 = 0;
4282 for (int i = 0; i < block_size; ++i) {
4283 float w = weight[i];
4284 float q = 2*Laux[i] + 1;
4285 sumqx += w*xval[i]*q;
4286 sumq2 += w*q*q;
4287 }
4288 if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
4289 scale = sumqx/sumq2; best = scale*sumqx;
4290 for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
4291 for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
4292 }
4293 }
4294 int n_not_ongrid = 0;
4295 for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
4296 if (n_not_ongrid > 0 && scale > 0) {
4297 float id = 1/scale;
4298 for (int k = 0; k < bs4; ++k) {
4299 //if (is_on_grid[k]) continue;
4300 uint16_t u = 0;
4301 for (int i = 0; i < 4; ++i) {
4302 int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
4303 l = MAX(0, MIN(kMaxQ-1, l));
4304 u |= (l << 3*i);
4305 }
4306 int grid_index = kmap_q3xs[u];
4307 if (grid_index < 0) {
4308 const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
4309 grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
4310 }
4311 const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
4312 for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
4313 }
4314 float sumqx = 0, sumq2 = 0;
4315 for (int i = 0; i < block_size; ++i) {
4316 float w = weight[i];
4317 float q = 2*L[i] + 1;
4318 sumqx += w*xval[i]*q;
4319 sumq2 += w*q*q;
4320 }
4321 if (sumq2 > 0) scale = sumqx/sumq2;
4322 }
4323 if (scale < 0) {
4324 // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
4325 // and correspondingly flip quant signs.
4326 scale = -scale;
4327 for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
4328 }
4329 for (int k = 0; k < bs4; ++k) {
4330 uint16_t u = 0;
4331 for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
4332 int grid_index = kmap_q3xs[u];
4333 if (grid_index < 0) {
4334 printf("Oops: found point %u not on grid:", u);
4335 for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
4336 printf("\n");
4337 GGML_ABORT("fatal error");
4338 }
4339 qs[k] = grid_index & 255;
4340 qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
4341 }
4342 qs += bs4;
4343 for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
4344 signs += bs8;
4345 GGML_ASSERT(scale >= 0);
4346 scales[ib] = scale;
4347 max_scale = MAX(max_scale, scale);
4348 }
4349
4350 if (!max_scale) {
4351 continue;
4352 }
4353
4354 float d = max_scale/31;
4355 y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
4356 float id = 1/d;
4357 for (int ib = 0; ib < QK_K/block_size; ib += 2) {
4358 int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
4359 l1 = MAX(0, MIN(15, l1));
4360 int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
4361 l2 = MAX(0, MIN(15, l2));
4362 y[ibl].scales[ib/2] = l1 | (l2 << 4);
4363 }
4364
4365 }
4366}
4367
4368#define IQ3S_BLOCK_SIZE 32
4369size_t quantize_iq3_s(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
4370 GGML_ASSERT(n_per_row%QK_K == 0);
4371 int64_t nblock = n_per_row/QK_K;
4372 float scales[QK_K/IQ3S_BLOCK_SIZE];
4373 float weight[IQ3S_BLOCK_SIZE];
4374 float xval[IQ3S_BLOCK_SIZE];
4375 int8_t L[IQ3S_BLOCK_SIZE];
4376 int8_t Laux[IQ3S_BLOCK_SIZE];
4377 float waux[IQ3S_BLOCK_SIZE];
4378 bool is_on_grid[IQ3S_BLOCK_SIZE/4];
4379 bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
4380 uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
4381 char * qrow = (char *)dst;
4382 for (int64_t row = 0; row < nrow; ++row) {
4383 quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
4384 scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
4385 src += n_per_row;
4386 qrow += nblock*sizeof(block_iq3_s);
4387 }
4388 return nrow * nblock * sizeof(block_iq3_s);
4389}
4390
4391void quantize_row_iq3_s_ref(const float * GGML_RESTRICT x, block_iq3_s * GGML_RESTRICT y, int64_t k) {
4392 assert(k % QK_K == 0);
4393 quantize_iq3_s(x, y, 1, k, NULL);
4394}
4395
4396
4397// =================================== 1.5 bpw ===================================================
4398
4399static int iq1_find_best_neighbour(const uint16_t * GGML_RESTRICT neighbours, const uint64_t * GGML_RESTRICT grid,
4400 const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float * scale, int8_t * GGML_RESTRICT L, int ngrid) {
4401 int num_neighbors = neighbours[0];
4402 GGML_ASSERT(num_neighbors > 0);
4403 float best_score = -FLT_MAX;
4404 int grid_index = -1;
4405 for (int j = 1; j <= num_neighbors; ++j) {
4406 const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
4407 float sumqx = 0, sumq2 = 0;
4408 for (int i = 0; i < 8; ++i) {
4409 float q = (pg[i] - 3)/2;
4410 float w = weight[i];
4411 sumqx += w*q*xval[i];
4412 sumq2 += w*q*q;
4413 }
4414 if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
4415 *scale = sumqx/sumq2; best_score = *scale * sumqx;
4416 grid_index = neighbours[j];
4417 }
4418 }
4419 if (grid_index < 0) {
4420 for (int i = 0; i < ngrid; ++i) {
4421 const int8_t * grid_i = (const int8_t *)(grid + i);
4422 float sumqx = 0, sumq2 = 0;
4423 for (int j = 0; j < 8; ++j) {
4424 float w = weight[j];
4425 float q = (grid_i[j] - 3)/2;
4426 sumqx += w*q*xval[j];
4427 sumq2 += w*q*q;
4428 }
4429 if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
4430 *scale = sumqx/sumq2; best_score = *scale*sumqx;
4431 grid_index = i;
4432 }
4433 }
4434 }
4435 if (grid_index < 0) {
4436 printf("Oops, did not find grid point\n");
4437 printf("Have %d neighbours\n", num_neighbors);
4438 for (int j = 1; j <= num_neighbors; ++j) {
4439 const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
4440 float sumqx = 0, sumq2 = 0;
4441 for (int i = 0; i < 8; ++i) {
4442 float q = (pg[i] - 3)/2;
4443 float w = weight[i];
4444 sumqx += w*q*xval[i];
4445 sumq2 += w*q*q;
4446 }
4447 printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
4448 }
4449 }
4450 GGML_ASSERT(grid_index >= 0);
4451 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4452 *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
4453 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4454 const int8_t * pg = (const int8_t *)(grid + grid_index);
4455 for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
4456 return grid_index;
4457}
4458
4459static int iq1_find_best_neighbour2(const uint16_t * GGML_RESTRICT neighbours, const uint64_t * GGML_RESTRICT grid,
4460 const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float scale, const float * GGML_RESTRICT xg, int8_t * GGML_RESTRICT L, int ngrid) {
4461 int num_neighbors = neighbours[0];
4462 GGML_ASSERT(num_neighbors > 0);
4463 float best_score = FLT_MAX;
4464 int grid_index = -1;
4465 for (int j = 1; j <= num_neighbors; ++j) {
4466 const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
4467 float d2 = 0;
4468 for (int i = 0; i < 8; ++i) {
4469 float q = xg[(pg[i] - 1)/2];
4470 float w = weight[i];
4471 float diff = scale*q - xval[i];
4472 d2 += w*diff*diff;
4473 }
4474 if (d2 < best_score) {
4475 best_score = d2;
4476 grid_index = neighbours[j];
4477 }
4478 }
4479 if (grid_index < 0) {
4480 for (int i = 0; i < ngrid; ++i) {
4481 const int8_t * grid_i = (const int8_t *)(grid + i);
4482 float d2 = 0;
4483 for (int j = 0; j < 8; ++j) {
4484 float w = weight[j];
4485 float q = xg[(grid_i[j] - 1)/2];
4486 float diff = scale*q - xval[i];
4487 d2 += w*diff*diff;
4488 }
4489 if (d2 < best_score) {
4490 best_score = d2;
4491 grid_index = i;
4492 }
4493 }
4494 }
4495 if (grid_index < 0) {
4496 printf("Oops, did not find grid point\n");
4497 printf("Have %d neighbours\n", num_neighbors);
4498 for (int j = 1; j <= num_neighbors; ++j) {
4499 const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
4500 float sumqx = 0, sumq2 = 0;
4501 for (int i = 0; i < 8; ++i) {
4502 float q = xg[(pg[i] - 1)/2];
4503 float w = weight[i];
4504 sumqx += w*q*xval[i];
4505 sumq2 += w*q*q;
4506 }
4507 printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
4508 }
4509 }
4510 GGML_ASSERT(grid_index >= 0);
4511 const int8_t * pg = (const int8_t *)(grid + grid_index);
4512 for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
4513 return grid_index;
4514}
4515
4516static int iq1_sort_helper(const void * left, const void * right) {
4517 const float * l = left;
4518 const float * r = right;
4519 return *l < *r ? -1 : *l > *r ? 1 : 0;
4520}
4521
4522#define IQ1S_BLOCK_SIZE 32
4523#define IQ1M_BLOCK_SIZE 16
4524static void quantize_row_iq1_s_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights,
4525 float * scales,
4526 float * weight,
4527 float * sumx,
4528 float * sumw,
4529 float * pairs,
4530 int8_t * L,
4531 uint16_t * index,
4532 int8_t * shifts) {
4533
4534 const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
4535
4536 const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
4537 const int * kmap_q2xs = iq2_data[gindex].map;
4538 const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
4539
4540 GGML_ASSERT(quant_weights && "missing quantization weights");
4541 GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
4542 GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
4543 GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
4544 GGML_ASSERT(n%QK_K == 0);
4545
4546 block_iq1_s * y = vy;
4547
4548 const int64_t nbl = n/QK_K;
4549
4550 const int block_size = IQ1S_BLOCK_SIZE;
4551
4552 const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
4553 const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
4554
4555
4556 int * idx = (int *)(pairs + 1);
4557
4558 for (int ibl = 0; ibl < nbl; ++ibl) {
4559
4560 y[ibl].d = GGML_FP32_TO_FP16(0.f);
4561 memset(y[ibl].qs, 0, QK_K/8);
4562 memset(y[ibl].qh, 0, QK_K/16);
4563
4564 float max_scale = 0;
4565
4566 const float * xbl = x + QK_K*ibl;
4567 float sumx2 = 0;
4568 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
4569 float sigma2 = 2*sumx2/QK_K;
4570
4571 for (int ib = 0; ib < QK_K/block_size; ++ib) {
4572 const float * xb = xbl + block_size*ib;
4573 const float * qw = quant_weights + QK_K*ibl + block_size*ib;
4574 for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
4575 float max = fabsf(xb[0]);
4576 for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
4577 if (max < GROUP_MAX_EPS_IQ1_S) {
4578 scales[ib] = 0;
4579 shifts[ib] = 1;
4580 memset(L, 1, block_size);
4581 continue;
4582 }
4583 // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
4584 // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
4585 // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
4586 // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
4587 // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
4588 // for each possible and score for each split.
4589 for (int j = 0; j < block_size; ++j) {
4590 pairs[2*j] = xb[j];
4591 idx[2*j] = j;
4592 }
4593 qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
4594 {
4595 sumx[0] = sumw[0] = 0;
4596 for (int j = 0; j < block_size; ++j) {
4597 int i = idx[2*j];
4598 sumx[j+1] = sumx[j] + weight[i]*xb[i];
4599 sumw[j+1] = sumw[j] + weight[i];
4600 }
4601 }
4602 float best_score = -FLT_MAX, scale = max;
4603 int besti1 = -1, besti2 = -1, best_shift = 0;
4604 for (int i1 = 0; i1 <= block_size; ++i1) {
4605 for (int i2 = i1; i2 <= block_size; ++i2) {
4606 float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
4607 float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[block_size] - sumw[i2])*x_p[2]*x_p[2];
4608 if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
4609 scale = sumqx/sumq2; best_score = scale*sumqx;
4610 besti1 = i1; besti2 = i2; best_shift = 1;
4611 }
4612 sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
4613 sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[block_size] - sumw[i2])*x_m[2]*x_m[2];
4614 if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
4615 scale = sumqx/sumq2; best_score = scale*sumqx;
4616 besti1 = i1; besti2 = i2; best_shift = -1;
4617 }
4618 }
4619 }
4620 if (besti1 < 0 || besti2 < 0 || best_shift == 0) {
4621 scales[ib] = 0;
4622 shifts[ib] = 1;
4623 memset(L, 1, block_size);
4624 continue;
4625 }
4626 for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
4627 for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
4628 for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
4629 if (scale < 0) {
4630 for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
4631 scale = -scale; best_shift = -best_shift;
4632 }
4633 bool all_on_grid = true;
4634 const float * xx = best_shift == 1 ? x_p : x_m;
4635 for (int k = 0; k < block_size/8; ++k) {
4636 uint16_t u = 0;
4637 for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
4638 int grid_index = kmap_q2xs[u];
4639 if (grid_index < 0) {
4640 all_on_grid = false;
4641 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
4642 grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
4643 GGML_ASSERT(grid_index >= 0);
4644 }
4645 index[k] = grid_index;
4646 }
4647 if (!all_on_grid) {
4648 float sumqx = 0, sumq2 = 0;
4649 for (int k = 0; k < block_size/8; ++k) {
4650 const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
4651 for (int j = 0; j < 8; ++j) {
4652 float w = weight[8*k + j];
4653 float q = xx[(pg[j] - 1)/2];
4654 sumqx += w*q*xb[8*k+j];
4655 sumq2 += w*q*q;
4656 }
4657 }
4658 if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
4659 }
4660 uint16_t h = 0;
4661 for (int k = 0; k < block_size/8; ++k) {
4662 y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
4663 h |= (index[k] >> 8) << 3*k;
4664 }
4665 y[ibl].qh[ib] = h;
4666 GGML_ASSERT(scale >= 0);
4667 scales[ib] = scale;
4668 shifts[ib] = best_shift;
4669 max_scale = MAX(max_scale, scale);
4670 }
4671
4672 if (!max_scale) {
4673 continue;
4674 }
4675
4676 float d = max_scale/15;
4677 y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
4678 float id = 1/d;
4679 for (int ib = 0; ib < QK_K/block_size; ++ib) {
4680 int l = nearest_int(0.5f*(id*scales[ib]-1));
4681 l = MAX(0, MIN(7, l));
4682 if (shifts[ib] == -1) l |= 8;
4683 y[ibl].qh[ib] |= (l << 12);
4684 }
4685 }
4686}
4687
4688size_t quantize_iq1_s(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
4689 GGML_ASSERT(n_per_row%QK_K == 0);
4690 float scales[QK_K/IQ1S_BLOCK_SIZE];
4691 float weight[IQ1S_BLOCK_SIZE];
4692 int8_t L[IQ1S_BLOCK_SIZE];
4693 float sumx[IQ1S_BLOCK_SIZE+1];
4694 float sumw[IQ1S_BLOCK_SIZE+1];
4695 float pairs[2*IQ1S_BLOCK_SIZE];
4696 uint16_t index[IQ1S_BLOCK_SIZE/8];
4697 int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
4698 int64_t nblock = n_per_row/QK_K;
4699 char * qrow = (char *)dst;
4700 for (int64_t row = 0; row < nrow; ++row) {
4701 quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
4702 src += n_per_row;
4703 qrow += nblock*sizeof(block_iq1_s);
4704 }
4705 return nrow * nblock * sizeof(block_iq1_s);
4706}
4707
4708static void quantize_row_iq1_m_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights,
4709 float * scales,
4710 float * weight,
4711 float * pairs,
4712 int8_t * L,
4713 uint16_t * index,
4714 int8_t * shifts) {
4715
4716 const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
4717
4718 const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
4719 const int * kmap_q2xs = iq2_data[gindex].map;
4720 const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
4721
4722 //GGML_ASSERT(quant_weights && "missing quantization weights");
4723 GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
4724 GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
4725 GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
4726 GGML_ASSERT(n%QK_K == 0);
4727
4728 block_iq1_m * y = vy;
4729
4730 const int64_t nbl = n/QK_K;
4731
4732 const int block_size = IQ1M_BLOCK_SIZE;
4733
4734 const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
4735 const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
4736 const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
4737
4738 int * idx = (int *)(pairs + 1);
4739
4740 float sumqx[4], sumq2[4];
4741
4742 iq1m_scale_t s;
4743 const float * xx;
4744
4745 for (int ibl = 0; ibl < nbl; ++ibl) {
4746 memset(y[ibl].qs, 0, QK_K/8);
4747 memset(y[ibl].qh, 0, QK_K/16);
4748 memset(y[ibl].scales, 0, QK_K/32);
4749
4750 float max_scale = 0;
4751
4752 const float * xbl = x + QK_K*ibl;
4753 float sumx2 = 0;
4754 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
4755 float sigma2 = 2*sumx2/QK_K;
4756
4757 for (int ib = 0; ib < QK_K/block_size; ++ib) {
4758 const float * xb = xbl + block_size*ib;
4759 if (quant_weights) {
4760 const float * qw = quant_weights + QK_K*ibl + block_size*ib;
4761 for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
4762 } else {
4763 for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
4764 }
4765 float max = fabsf(xb[0]);
4766 for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
4767 if (max < GROUP_MAX_EPS_IQ1_M) {
4768 scales[ib] = 0;
4769 shifts[ib] = 0;
4770 memset(L, 1, block_size);
4771 continue;
4772 }
4773 // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
4774 // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
4775 // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
4776 // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
4777 // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
4778 // for each possible and score for each split.
4779 for (int j = 0; j < block_size; ++j) {
4780 pairs[2*j] = xb[j];
4781 idx[2*j] = j;
4782 }
4783 qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
4784 float best_score = -FLT_MAX, scale = max;
4785 int besti1 = -1, besti2 = -1, best_k = -1;
4786 // 0: +, +
4787 // 1: +, -
4788 // 2: -, +
4789 // 3: -, -
4790 // prefix sums are kept per half of the block because each half can use a different sign (x_p or x_m)
4791 // since v[0]-v[1] = v[1]-v[2] = -1 for both x_p and x_m, the 3-group sum for a split collapses to T*v[2] - px[i1] - px[i2]
4792 {
4793 float px[2][IQ1M_BLOCK_SIZE+1];
4794 float pw[2][IQ1M_BLOCK_SIZE+1];
4795 px[0][0] = px[1][0] = 0;
4796 pw[0][0] = pw[1][0] = 0;
4797 for (int j = 0; j < block_size; ++j) {
4798 const int i = idx[2*j];
4799 const int h = i < block_size/2 ? 0 : 1;
4800 px[h][j+1] = px[h][j] + weight[i]*xb[i];
4801 px[1-h][j+1] = px[1-h][j];
4802 pw[h][j+1] = pw[h][j] + weight[i];
4803 pw[1-h][j+1] = pw[1-h][j];
4804 }
4805 const float txs[2] = {px[0][block_size], px[1][block_size]}; // total weight*x per half
4806 const float tws[2] = {pw[0][block_size], pw[1][block_size]}; // total weight per half
4807 const float p2 = x_p[2], m2 = x_m[2];
4808 const float cp1 = x_p[0]*x_p[0] - x_p[1]*x_p[1];
4809 const float cp2 = x_p[1]*x_p[1] - x_p[2]*x_p[2];
4810 const float cm1 = x_m[0]*x_m[0] - x_m[1]*x_m[1];
4811 const float cm2 = x_m[1]*x_m[1] - x_m[2]*x_m[2];
4812 for (int i1 = 0; i1 <= block_size; ++i1) {
4813 for (int i2 = i1; i2 <= block_size; ++i2) {
4814 float qx_p[2], qx_m[2], q2_p[2], q2_m[2];
4815 for (int h = 0; h < 2; ++h) {
4816 const float sx = px[h][i1] + px[h][i2];
4817 qx_p[h] = txs[h]*p2 - sx;
4818 qx_m[h] = txs[h]*m2 - sx;
4819 q2_p[h] = tws[h]*p2*p2 + pw[h][i1]*cp1 + pw[h][i2]*cp2;
4820 q2_m[h] = tws[h]*m2*m2 + pw[h][i1]*cm1 + pw[h][i2]*cm2;
4821 }
4822 sumqx[0] = qx_p[0] + qx_p[1];
4823 sumqx[1] = qx_p[0] + qx_m[1];
4824 sumqx[2] = qx_m[0] + qx_p[1];
4825 sumqx[3] = qx_m[0] + qx_m[1];
4826 sumq2[0] = q2_p[0] + q2_p[1];
4827 sumq2[1] = q2_p[0] + q2_m[1];
4828 sumq2[2] = q2_m[0] + q2_p[1];
4829 sumq2[3] = q2_m[0] + q2_m[1];
4830 for (int k = 0; k < 4; ++k) {
4831 if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
4832 scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
4833 besti1 = i1; besti2 = i2; best_k = k;
4834 }
4835 }
4836 }
4837 }
4838 }
4839 if (besti1 < 0 || besti2 < 0 || best_k < 0) {
4840 scales[ib] = 0;
4841 shifts[ib] = 0;
4842 memset(L, 1, block_size);
4843 continue;
4844 }
4845 for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
4846 for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
4847 for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
4848 if (scale < 0) {
4849 for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
4850 scale = -scale;
4851 best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
4852 }
4853 bool all_on_grid = true;
4854 for (int k = 0; k < block_size/8; ++k) {
4855 if (k == 0) xx = best_k < 2 ? x_p : x_m;
4856 else xx = best_k%2 == 0 ? x_p : x_m;
4857 uint16_t u = 0;
4858 for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
4859 int grid_index = kmap_q2xs[u];
4860 if (grid_index < 0) {
4861 all_on_grid = false;
4862 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
4863 grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
4864 GGML_ASSERT(grid_index >= 0);
4865 }
4866 index[k] = grid_index;
4867 }
4868 if (!all_on_grid) {
4869 float sumqx_f = 0, sumq2_f = 0;
4870 for (int k = 0; k < block_size/8; ++k) {
4871 if (k == 0) xx = best_k < 2 ? x_p : x_m;
4872 else xx = best_k%2 == 0 ? x_p : x_m;
4873 const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
4874 for (int j = 0; j < 8; ++j) {
4875 float w = weight[8*k + j];
4876 float q = xx[(pg[j] - 1)/2];
4877 sumqx_f += w*q*xb[8*k+j];
4878 sumq2_f += w*q*q;
4879 }
4880 }
4881 if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
4882 }
4883 y[ibl].qs[2*ib + 0] = index[0] & 255;
4884 y[ibl].qs[2*ib + 1] = index[1] & 255;
4885 y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
4886 GGML_ASSERT(scale >= 0);
4887 scales[ib] = scale;
4888 shifts[ib] = best_k;
4889 max_scale = MAX(max_scale, scale);
4890 }
4891
4892 if (!max_scale) {
4893 continue;
4894 }
4895
4896 uint16_t * sc = (uint16_t *)y[ibl].scales;
4897 float d = max_scale/15;
4898 float id = 1/d;
4899 float sumqx_f = 0, sumq2_f = 0;
4900 for (int ib = 0; ib < QK_K/block_size; ++ib) {
4901 int l = nearest_int(0.5f*(id*scales[ib+0]-1));
4902 l = MAX(0, MIN(7, l));
4903 sc[ib/4] |= (l << 3*(ib%4));
4904 y[ibl].qh[ib] |= masks[shifts[ib]];
4905 const float * xb = xbl + block_size*ib;
4906 if (quant_weights) {
4907 const float * qw = quant_weights + QK_K*ibl + block_size*ib;
4908 for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
4909 } else {
4910 for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
4911 }
4912 for (int k = 0; k < block_size/8; ++k) {
4913 if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
4914 else xx = shifts[ib]%2 == 0 ? x_p : x_m;
4915 const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
4916 for (int j = 0; j < 8; ++j) {
4917 float w = weight[8*k + j];
4918 float q = xx[(pg[j] - 1)/2]*(2*l+1);
4919 sumqx_f += w*q*xb[8*k+j];
4920 sumq2_f += w*q*q;
4921 }
4922 }
4923 }
4924 if (sumq2_f > 0) d = sumqx_f/sumq2_f;
4925 s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
4926 sc[0] |= ((s.u16 & 0x000f) << 12);
4927 sc[1] |= ((s.u16 & 0x00f0) << 8);
4928 sc[2] |= ((s.u16 & 0x0f00) << 4);
4929 sc[3] |= ((s.u16 & 0xf000) << 0);
4930 }
4931}
4932
4933size_t quantize_iq1_m(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
4934 GGML_ASSERT(n_per_row%QK_K == 0);
4935 float scales[QK_K/IQ1M_BLOCK_SIZE];
4936 float weight[IQ1M_BLOCK_SIZE];
4937 int8_t L[IQ1M_BLOCK_SIZE];
4938 float pairs[2*IQ1M_BLOCK_SIZE];
4939 uint16_t index[IQ1M_BLOCK_SIZE/8];
4940 int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
4941 int64_t nblock = n_per_row/QK_K;
4942 char * qrow = (char *)dst;
4943 for (int64_t row = 0; row < nrow; ++row) {
4944 quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
4945 src += n_per_row;
4946 qrow += nblock*sizeof(block_iq1_m);
4947 }
4948 return nrow * nblock * sizeof(block_iq1_m);
4949}
4950
4951// ============================ 4-bit non-linear quants
4952
4953static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * GGML_RESTRICT x,
4954 ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
4955 float * scales, float * weight, uint8_t * L,
4956 const int8_t * values,
4957 const float * quant_weights,
4958 const int ntry) {
4959
4960 float sigma2 = 0;
4961 for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
4962 sigma2 *= 2.f/super_block_size;
4963
4964 memset(q4, 0, super_block_size/2);
4965 dh[0] = GGML_FP32_TO_FP16(0.f);
4966
4967 float mid[15];
4968 float fval[16];
4969 for (int k = 0; k < 15; ++k) mid[k] = 0.5f*(values[k] + values[k+1]);
4970 for (int k = 0; k < 16; ++k) fval[k] = values[k];
4971
4972 float max_scale = 0, amax_scale = 0;
4973 for (int ib = 0; ib < super_block_size/block_size; ++ib) {
4974 const float * xb = x + ib*block_size;
4975 uint8_t * Lb = L + ib*block_size;
4976 if (quant_weights) {
4977 const float * qw = quant_weights + ib*block_size;
4978 for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
4979 } else {
4980 for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
4981 }
4982 float amax = 0, max = 0;
4983 for (int j = 0; j < block_size; ++j) {
4984 float ax = fabsf(xb[j]);
4985 if (ax > amax) {
4986 amax = ax; max = xb[j];
4987 }
4988 }
4989 if (amax < GROUP_MAX_EPS) {
4990 scales[ib] = 0;
4991 continue;
4992 }
4993 float d = ntry > 0 ? -max/values[0] : max/values[0];
4994 // lane 0 is the initial guess, lanes 1..nt-1 are itry = -ntry..ntry; each lane sums over j in order
4995 const int nt = ntry >= 0 ? 2*ntry + 2 : 1;
4996 float idt[16], sqx[16], sq2[16];
4997 idt[0] = 1/d;
4998 for (int t = 1; t < nt; ++t) idt[t] = (t - 1 - ntry + values[0])/max;
4999 for (int t = 0; t < nt; ++t) sqx[t] = sq2[t] = 0;
5000 for (int j = 0; j < block_size; ++j) {
5001 Lb[j] = best_index_iq4nl(mid, idt[0]*xb[j]);
5002 }
5003 for (int j = 0; j < block_size; ++j) {
5004 float w = weight[j];
5005 for (int t = 0; t < nt; ++t) {
5006 float q = fval[best_index_iq4nl(mid, idt[t]*xb[j])];
5007 sqx[t] += w*q*xb[j];
5008 sq2[t] += w*q*q;
5009 }
5010 }
5011 d = sq2[0] > 0 ? sqx[0]/sq2[0] : 0.f;
5012 float best = d*sqx[0];
5013 for (int t = 1; t < nt; ++t) {
5014 if (sq2[t] > 0 && sqx[t]*sqx[t] > best*sq2[t]) {
5015 d = sqx[t]/sq2[t]; best = d * sqx[t];
5016 }
5017 }
5018 scales[ib] = d;
5019 float abs_d = fabsf(d);
5020 if (abs_d > amax_scale) {
5021 amax_scale = abs_d; max_scale = d;
5022 }
5023 }
5024
5025 if (super_block_size/block_size > 1) {
5026 int nb = super_block_size/block_size;
5027 memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
5028 float d = -max_scale/32;
5029 dh[0] = GGML_FP32_TO_FP16(d);
5030 float id = d ? 1/d : 0.f;
5031 for (int ib = 0; ib < super_block_size/block_size; ++ib) {
5032 int l = nearest_int(id*scales[ib]);
5033 l = MAX(-32, MIN(31, l));
5034 float dl = d * l;
5035 float idl = dl ? 1/dl : 0.f;
5036 uint8_t * Lb = L + ib*block_size;
5037 const float * xb = x + ib*block_size;
5038 for (int j = 0; j < block_size; ++j) {
5039 Lb[j] = best_index_iq4nl(mid, idl*xb[j]);
5040 }
5041 l += 32;
5042 uint8_t l_l = l & 0xf;
5043 uint8_t l_h = l >> 4;
5044 if (ib%2 == 0) scales_l[ib/2] = l_l;
5045 else scales_l[ib/2] |= (l_l << 4);
5046 scales_h[ib/8] |= (l_h << 2*(ib%8));
5047 }
5048 } else {
5049 dh[0] = GGML_FP32_TO_FP16(scales[0]);
5050 if (ntry > 0) {
5051 float id = scales[0] ? 1/scales[0] : 0;
5052 for (int j = 0; j < super_block_size; ++j) {
5053 L[j] = best_index_iq4nl(mid, id*x[j]);
5054 }
5055 }
5056 }
5057
5058 for (int i = 0; i < super_block_size/32; ++i) {
5059 for (int j = 0; j < 16; ++j) {
5060 q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
5061 }
5062 }
5063}
5064
5065size_t quantize_iq4_nl(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
5066 GGML_ASSERT(n_per_row%QK4_NL == 0);
5067 int64_t nblock = n_per_row/QK4_NL;
5068 char * qrow = (char *)dst;
5069 uint8_t L[QK4_NL];
5070 float weight[QK4_NL];
5071 uint16_t unused_h;
5072 uint8_t * unused_l = NULL;
5073 float scale;
5074 for (int64_t row = 0; row < nrow; ++row) {
5075 block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
5076 for (int ibl = 0; ibl < nblock; ++ibl) {
5077 const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
5078 quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
5079 &scale, weight, L, kvalues_iq4nl, qw, 7);
5080 }
5081 src += n_per_row;
5082 qrow += nblock*sizeof(block_iq4_nl);
5083 }
5084 return nrow * nblock * sizeof(block_iq4_nl);
5085}
5086
5087//void quantize_row_iq4_nl_ref(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
5088void quantize_row_iq4_nl_ref(const float * GGML_RESTRICT x, block_iq4_nl * GGML_RESTRICT y, int64_t k) {
5089 GGML_ASSERT(k%QK4_NL == 0);
5090 int64_t nblock = k/QK4_NL;
5091 uint8_t L[QK4_NL];
5092 float weight[QK4_NL];
5093 uint16_t unused_h;
5094 uint8_t * unused_l = NULL;
5095 float scale;
5096 block_iq4_nl * iq4 = y;
5097 for (int ibl = 0; ibl < nblock; ++ibl) {
5098 quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
5099 &scale, weight, L, kvalues_iq4nl, NULL, -1);
5100 }
5101}
5102
5103size_t quantize_iq4_xs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
5104 GGML_ASSERT(n_per_row%QK_K == 0);
5105 int64_t nblock = n_per_row/QK_K;
5106 char * qrow = (char *)dst;
5107 uint8_t L[QK_K];
5108 float weight[32];
5109 float scales[QK_K/32];
5110 for (int64_t row = 0; row < nrow; ++row) {
5111 block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
5112 for (int ibl = 0; ibl < nblock; ++ibl) {
5113 const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
5114 quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
5115 scales, weight, L, kvalues_iq4nl, qw, 7);
5116 }
5117 src += n_per_row;
5118 qrow += nblock*sizeof(block_iq4_xs);
5119 }
5120 return nrow * nblock * sizeof(block_iq4_xs);
5121}
5122
5123void quantize_row_iq4_xs_ref(const float * GGML_RESTRICT x, block_iq4_xs * GGML_RESTRICT y, int64_t k) {
5124 assert(k % QK_K == 0);
5125 quantize_iq4_xs(x, y, 1, k, NULL);
5126}
5127
5128// =============================== 2.5625 bpw
5129
5130static void quantize_row_iq2_s_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights) {
5131
5132 const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
5133
5134 const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
5135 const int * kmap_q2xs = iq2_data[gindex].map;
5136 const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
5137
5138 GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
5139 GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
5140 GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
5141 GGML_ASSERT(n%QK_K == 0);
5142
5143 const int kMaxQ = 3;
5144
5145 const int64_t nbl = n/QK_K;
5146
5147 block_iq2_s * y = vy;
5148
5149 float scales[QK_K/16];
5150 float weight[16];
5151 float xval[16];
5152 int8_t L[16];
5153 int8_t Laux[16];
5154 float waux[16];
5155 bool is_on_grid[2];
5156 bool is_on_grid_aux[2];
5157 uint8_t block_signs[2];
5158
5159 for (int ibl = 0; ibl < nbl; ++ibl) {
5160
5161 memset(&y[ibl], 0, sizeof(block_iq2_s));
5162 y[ibl].d = GGML_FP32_TO_FP16(0.f);
5163
5164 float max_scale = 0;
5165
5166 const float * xbl = x + QK_K*ibl;
5167 float sumx2 = 0;
5168 for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
5169 float sigma2 = 2*sumx2/QK_K;
5170
5171 for (int ib = 0; ib < QK_K/16; ++ib) {
5172 const float * xb = xbl + 16*ib;
5173 if (quant_weights) {
5174 const float * qw = quant_weights + QK_K*ibl + 16*ib;
5175 for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
5176 } else {
5177 for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
5178 }
5179 for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
5180 for (int k = 0; k < 2; ++k) {
5181 uint8_t s = 0;
5182 for (int i = 0; i < 8; ++i) {
5183 if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
5184 else {
5185 xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
5186 }
5187 }
5188 block_signs[k] = s;
5189 }
5190 float max = xval[0];
5191 for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
5192 memset(L, 0, 16);
5193 if (max < GROUP_MAX_EPS_IQ2_S) {
5194 scales[ib] = 0;
5195 continue;
5196 }
5197 float best = 0;
5198 float scale = max/(2*kMaxQ-1);
5199 is_on_grid[0] = is_on_grid[1] = true;
5200 for (int is = -9; is <= 9; ++is) {
5201 float id = (2*kMaxQ-1+is*0.1f)/max;
5202 float this_scale = 1/id;
5203 for (int k = 0; k < 2; ++k) {
5204 for (int i = 0; i < 8; ++i) {
5205 int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
5206 Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
5207 }
5208 uint16_t u = 0;
5209 for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
5210 int grid_index = kmap_q2xs[u];
5211 is_on_grid_aux[k] = true;
5212 if (grid_index < 0) {
5213 is_on_grid_aux[k] = false;
5214 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
5215 grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
5216 }
5217 }
5218 float sumqx = 0, sumq2 = 0;
5219 for (int i = 0; i < 16; ++i) {
5220 float w = weight[i];
5221 float q = 2*Laux[i] + 1;
5222 sumqx += w*xval[i]*q;
5223 sumq2 += w*q*q;
5224 }
5225 if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
5226 scale = sumqx/sumq2; best = scale*sumqx;
5227 for (int i = 0; i < 16; ++i) L[i] = Laux[i];
5228 for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
5229 }
5230 }
5231 int n_not_ongrid = 0;
5232 for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
5233 if (n_not_ongrid > 0 && scale > 0) {
5234 float id = 1/scale;
5235 for (int k = 0; k < 2; ++k) {
5236 if (is_on_grid[k]) continue;
5237 uint16_t u = 0;
5238 for (int i = 0; i < 8; ++i) {
5239 int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
5240 l = MAX(0, MIN(kMaxQ-1, l));
5241 u |= (l << 2*i);
5242 L[8*k + i] = l;
5243 }
5244 int grid_index = kmap_q2xs[u];
5245 if (grid_index < 0) {
5246 const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
5247 grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
5248 }
5249 }
5250 float sumqx = 0, sumq2 = 0;
5251 for (int i = 0; i < 16; ++i) {
5252 float w = weight[i];
5253 float q = 2*L[i] + 1;
5254 sumqx += w*xval[i]*q;
5255 sumq2 += w*q*q;
5256 }
5257 if (sumq2 > 0) scale = sumqx/sumq2;
5258 }
5259 if (scale < 0) {
5260 scale = -scale;
5261 for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
5262 }
5263 for (int k = 0; k < 2; ++k) {
5264 uint16_t u = 0;
5265 for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
5266 int grid_index = kmap_q2xs[u];
5267 if (grid_index < 0) {
5268 printf("Oops: found point %u not on grid:", u);
5269 for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
5270 printf("\n");
5271 GGML_ABORT("fatal error");
5272 }
5273 const int i8 = 2*ib + k;
5274 y[ibl].qs[i8] = grid_index & 255;
5275 y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
5276 y[ibl].qs[QK_K/8 + i8] = block_signs[k];
5277 }
5278 GGML_ASSERT(scale >= 0);
5279 scales[ib] = scale;
5280 max_scale = MAX(max_scale, scale);
5281 }
5282
5283 if (!max_scale) {
5284 continue;
5285 }
5286
5287 float d = max_scale/31;
5288 y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
5289 float id = 1/d;
5290 for (int ib = 0; ib < QK_K/16; ++ib) {
5291 int l = nearest_int(0.5f*(id*scales[ib]-1));
5292 l = MAX(0, MIN(15, l));
5293 if (ib%2 == 0) y[ibl].scales[ib/2] = l;
5294 else y[ibl].scales[ib/2] |= (l << 4);
5295 }
5296 }
5297}
5298
5299size_t quantize_iq2_s(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
5300 GGML_ASSERT(n_per_row%QK_K == 0);
5301 int64_t nblock = n_per_row/QK_K;
5302 char * qrow = (char *)dst;
5303 for (int64_t row = 0; row < nrow; ++row) {
5304 quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
5305 src += n_per_row;
5306 qrow += nblock*sizeof(block_iq2_s);
5307 }
5308 return nrow * nblock * sizeof(block_iq2_s);
5309}
5310
5311void quantize_row_iq2_s_ref(const float * GGML_RESTRICT x, block_iq2_s * GGML_RESTRICT y, int64_t k) {
5312 assert(k % QK_K == 0);
5313 quantize_iq2_s(x, y, 1, k, NULL);
5314}
5315
5316// =============================== data validation
5317
5318static bool validate_float(float f, size_t i) {
5319 if (isinf(f)) {
5320 fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
5321 return false;
5322 }
5323
5324 if (isnan(f)) {
5325 fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
5326 return false;
5327 }
5328
5329 return true;
5330}
5331
5332static bool isinf_fp16(ggml_fp16_t f) {
5333 return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) == 0;
5334}
5335
5336static bool isnan_fp16(ggml_fp16_t f) {
5337 return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) != 0;
5338}
5339
5340static bool validate_fp16(ggml_fp16_t f, size_t i) {
5341 if (isinf_fp16(f)) {
5342 fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
5343 return false;
5344 }
5345
5346 if (isnan_fp16(f)) {
5347 fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
5348 return false;
5349 }
5350
5351 return true;
5352}
5353
5354static bool validate_e_e8m0(uint8_t e, size_t i) {
5355 if (e == 0xff) {
5356 fprintf(stderr, "ggml_validate_row_data: found invalid e value %d at block %zu\n", e, i);
5357 return false;
5358 }
5359
5360 return true;
5361}
5362
5363#define VALIDATE_ROW_DATA_D_F16_IMPL(type, data, nb) \
5364 const type * q = (const type *) (data); \
5365 for (size_t i = 0; i < (nb); ++i) { \
5366 if (!validate_fp16(q[i].d, i)) { \
5367 return false; \
5368 } \
5369 }
5370
5371#define VALIDATE_ROW_DATA_DM_F16_IMPL(type, data, nb, d, m) \
5372 const type * q = (const type *) (data); \
5373 for (size_t i = 0; i < (nb); ++i) { \
5374 if (!validate_fp16(q[i].d, i) || !validate_fp16(q[i].m, i)) { \
5375 return false; \
5376 } \
5377 }
5378
5379#define VALIDATE_ROW_DATA_E_E8M0_IMPL(type, data, nb) \
5380 const type * q = (const type *) (data); \
5381 for (size_t i = 0; i < (nb); ++i) { \
5382 if (!validate_e_e8m0(q[i].e, i)) { \
5383 return false; \
5384 } \
5385 }
5386
5387#define VALIDATE_ROW_DATA_DVEC_F16_IMPL(type, data, nb, nr) \
5388 const type * q = (const type *) (data); \
5389 for (size_t i = 0; i < (nb); ++i) { \
5390 for (size_t j = 0; j < (nr); ++j) { \
5391 if (!validate_fp16(q[i].d[j], i)) { \
5392 return false; \
5393 } \
5394 } \
5395 }
5396
5397bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes) {
5398 if (type < 0 || type >= GGML_TYPE_COUNT) {
5399 fprintf(stderr, "%s: invalid type %d\n", __func__, type);
5400 return false;
5401 }
5402
5403 if (nbytes % ggml_type_size(type) != 0) {
5404 fprintf(stderr, "%s: invalid size %zu for type %s (type size = %zu)\n", __func__, nbytes, ggml_type_name(type), ggml_type_size(type));
5405 return false;
5406 }
5407
5408 const size_t nb = nbytes/ggml_type_size(type);
5409
5410 switch (type) {
5411 case GGML_TYPE_BF16:
5412 {
5413 int nans = 0;
5414 int infs = 0;
5415 const unsigned short * f = (const unsigned short *) data;
5416 for (size_t i = 0; i < nb; ++i) {
5417 nans += (f[i] & 0x7fff) > 0x7f80;
5418 infs += (f[i] & 0x7fff) == 0x7f80;
5419 }
5420 if (nans) {
5421 fprintf(stderr, "%s: found %d NaNs in row of %zu BF16 values\n", __func__, nans, nb);
5422 return false;
5423 }
5424 if (infs) {
5425 fprintf(stderr, "%s: found %d infinities in row of %zu BF16 values\n", __func__, infs, nb);
5426 return false;
5427 }
5428 } break;
5429 case GGML_TYPE_F16:
5430 {
5431 const ggml_fp16_t * f = (const ggml_fp16_t *) data;
5432 size_t i = 0;
5433#if defined(__AVX2__)
5434 for (; i + 15 < nb; i += 16) {
5435 __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
5436 __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi16(0x7c00));
5437 __m256i cmp = _mm256_cmpeq_epi16(vexp, _mm256_set1_epi16(0x7c00));
5438 int mask = _mm256_movemask_epi8(cmp);
5439 if (mask) {
5440 for (size_t j = 0; j < 16; ++j) {
5441 if (!validate_fp16(f[i + j], i + j)) {
5442 return false;
5443 }
5444 }
5445 GGML_UNREACHABLE();
5446 }
5447 }
5448#elif defined(__ARM_NEON)
5449 for (; i + 7 < nb; i += 8) {
5450 uint16x8_t v = vld1q_u16(f + i);
5451 uint16x8_t vexp = vandq_u16(v, vdupq_n_u16(0x7c00));
5452 uint16x8_t cmp = vceqq_u16(vexp, vdupq_n_u16(0x7c00));
5453 uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(cmp, 4)), 0);
5454 if (mask) {
5455 for (size_t j = 0; j < 8; ++j) {
5456 if (!validate_fp16(f[i + j], i + j)) {
5457 return false;
5458 }
5459 }
5460 GGML_UNREACHABLE();
5461 }
5462 }
5463#endif
5464 for (; i < nb; ++i) {
5465 if (!validate_fp16(f[i], i)) {
5466 return false;
5467 }
5468 }
5469 } break;
5470 case GGML_TYPE_F32:
5471 {
5472 const float * f = (const float *) data;
5473 size_t i = 0;
5474#if defined(__AVX2__)
5475 for (; i + 7 < nb; i += 8) {
5476 __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
5477 __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi32(0x7f800000));
5478 __m256i cmp = _mm256_cmpeq_epi32(vexp, _mm256_set1_epi32(0x7f800000));
5479 int mask = _mm256_movemask_epi8(cmp);
5480 if (mask) {
5481 for (size_t j = 0; j < 8; ++j) {
5482 if (!validate_float(f[i + j], i + j)) {
5483 return false;
5484 }
5485 }
5486 GGML_UNREACHABLE();
5487 }
5488 }
5489#elif defined(__ARM_NEON)
5490 for (; i + 3 < nb; i += 4) {
5491 uint32x4_t v = vld1q_u32((const uint32_t *)f + i);
5492 uint32x4_t vexp = vandq_u32(v, vdupq_n_u32(0x7f800000));
5493 uint32x4_t cmp = vceqq_u32(vexp, vdupq_n_u32(0x7f800000));
5494 uint64_t mask = vget_lane_u64(vreinterpret_u64_u16(vshrn_n_u32(cmp, 8)), 0);
5495 if (mask) {
5496 for (size_t j = 0; j < 4; ++j) {
5497 if (!validate_float(f[i + j], i + j)) {
5498 return false;
5499 }
5500 }
5501 GGML_UNREACHABLE();
5502 }
5503 }
5504#endif
5505 for (; i < nb; ++i) {
5506 if (!validate_float(f[i], i)) {
5507 return false;
5508 }
5509 }
5510 } break;
5511 case GGML_TYPE_F64:
5512 {
5513 const double * f = (const double *) data;
5514 for (size_t i = 0; i < nb; ++i) {
5515 if (!validate_float(f[i], i)) {
5516 return false;
5517 }
5518 }
5519 } break;
5520 case GGML_TYPE_Q1_0:
5521 {
5522 VALIDATE_ROW_DATA_D_F16_IMPL(block_q1_0, data, nb);
5523 } break;
5524 case GGML_TYPE_Q2_0:
5525 {
5526 VALIDATE_ROW_DATA_D_F16_IMPL(block_q2_0, data, nb);
5527 } break;
5528 case GGML_TYPE_Q4_0:
5529 {
5530 VALIDATE_ROW_DATA_D_F16_IMPL(block_q4_0, data, nb);
5531 } break;
5532 case GGML_TYPE_Q4_1:
5533 {
5534 VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_1, data, nb, d, m);
5535 } break;
5536 case GGML_TYPE_Q5_0:
5537 {
5538 VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_0, data, nb);
5539 } break;
5540 case GGML_TYPE_Q5_1:
5541 {
5542 VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_1, data, nb, d, m);
5543 } break;
5544 case GGML_TYPE_Q8_0:
5545 {
5546 VALIDATE_ROW_DATA_D_F16_IMPL(block_q8_0, data, nb);
5547 } break;
5548 case GGML_TYPE_MXFP4:
5549 {
5550 VALIDATE_ROW_DATA_E_E8M0_IMPL(block_mxfp4, data, nb);
5551 } break;
5552 case GGML_TYPE_NVFP4:
5553 {
5554 // TODO: validate each unsigned E4M3 scale; NaN encodings and scales with the sign bit set are invalid.
5555 GGML_UNUSED(data);
5556 GGML_UNUSED(nb);
5557 } break;
5558 case GGML_TYPE_F8_E4M3:
5559 {
5560 const ggml_fp8_e4m3_t * q = (const ggml_fp8_e4m3_t *) data;
5561 for (size_t i = 0; i < nb; ++i) {
5562 if ((q[i].bits & 0x7F) == 0x7F) {
5563 fprintf(stderr, "%s: found NaN at index %zu in row of %zu F8_E4M3 values\n", __func__, i, nb);
5564 return false;
5565 }
5566 }
5567 } break;
5568 case GGML_TYPE_Q2_K:
5569 {
5570 VALIDATE_ROW_DATA_DM_F16_IMPL(block_q2_K, data, nb, d, dmin);
5571 } break;
5572 case GGML_TYPE_Q3_K:
5573 {
5574 VALIDATE_ROW_DATA_D_F16_IMPL(block_q3_K, data, nb);
5575 } break;
5576 case GGML_TYPE_Q4_K:
5577 {
5578 VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d, dmin);
5579 } break;
5580 case GGML_TYPE_Q5_K:
5581 {
5582 VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_K, data, nb, d, dmin);
5583 } break;
5584 case GGML_TYPE_Q6_K:
5585 {
5586 VALIDATE_ROW_DATA_D_F16_IMPL(block_q6_K, data, nb);
5587 } break;
5588 case GGML_TYPE_Q8_K:
5589 {
5590 const block_q8_K * q = (const block_q8_K *) data;
5591 for (size_t i = 0; i < nb; ++i) {
5592 if (!validate_float(q[i].d, i)) {
5593 return false;
5594 }
5595 }
5596 } break;
5597 case GGML_TYPE_TQ1_0:
5598 {
5599 VALIDATE_ROW_DATA_D_F16_IMPL(block_tq1_0, data, nb);
5600 } break;
5601 case GGML_TYPE_TQ2_0:
5602 {
5603 VALIDATE_ROW_DATA_D_F16_IMPL(block_tq2_0, data, nb);
5604 } break;
5605 case GGML_TYPE_IQ1_S:
5606 {
5607 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq1_s, data, nb);
5608 } break;
5609 case GGML_TYPE_IQ1_M:
5610 {
5611 const block_iq1_m * q = (const block_iq1_m *) data;
5612 for (size_t i = 0; i < nb; ++i) {
5613 iq1m_scale_t scale;
5614 const uint16_t * sc = (const uint16_t *)q[i].scales;
5615 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
5616 if (!validate_fp16(scale.f16, i)) {
5617 return false;
5618 }
5619 }
5620 } break;
5621 case GGML_TYPE_IQ2_XXS:
5622 {
5623 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xxs, data, nb);
5624 } break;
5625 case GGML_TYPE_IQ2_XS:
5626 {
5627 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xs, data, nb);
5628 } break;
5629 case GGML_TYPE_IQ2_S:
5630 {
5631 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_s, data, nb);
5632 } break;
5633 case GGML_TYPE_IQ3_XXS:
5634 {
5635 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_xxs, data, nb);
5636 } break;
5637
5638 case GGML_TYPE_IQ3_S:
5639 {
5640 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_s, data, nb);
5641 } break;
5642 case GGML_TYPE_IQ4_XS:
5643 {
5644 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_xs, data, nb);
5645 } break;
5646 case GGML_TYPE_IQ4_NL:
5647 {
5648 VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_nl, data, nb);
5649 } break;
5650
5651 case GGML_TYPE_I8:
5652 case GGML_TYPE_I16:
5653 case GGML_TYPE_I32:
5654 case GGML_TYPE_I64:
5655 // nothing to validate
5656 break;
5657 default:
5658 {
5659 fprintf(stderr, "%s: invalid type %d\n", __func__, type);
5660 return false;
5661 }
5662 }
5663
5664 return true;
5665}