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