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