| 467 | } |
| 468 | |
| 469 | void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) { |
| 470 | const int qk = QK4_1; |
| 471 | |
| 472 | assert(k % qk == 0); |
| 473 | |
| 474 | const int nb = k / qk; |
| 475 | |
| 476 | for (int i = 0; i < nb; i++) { |
| 477 | float min = FLT_MAX; |
| 478 | float max = -FLT_MAX; |
| 479 | |
| 480 | for (int j = 0; j < qk; j++) { |
| 481 | const float v = x[i*qk + j]; |
| 482 | |
| 483 | if (v < min) min = v; |
| 484 | if (v > max) max = v; |
| 485 | } |
| 486 | |
| 487 | const float d = (max - min) / ((1 << 4) - 1); |
| 488 | const float id = d ? 1.0f/d : 0.0f; |
| 489 | |
| 490 | y[i].d = GGML_FP32_TO_FP16(d); |
| 491 | y[i].m = GGML_FP32_TO_FP16(min); |
| 492 | |
| 493 | for (int j = 0; j < qk/2; ++j) { |
| 494 | const float x0 = (x[i*qk + 0 + j] - min)*id; |
| 495 | const float x1 = (x[i*qk + qk/2 + j] - min)*id; |
| 496 | |
| 497 | const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f)); |
| 498 | const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f)); |
| 499 | |
| 500 | y[i].qs[j] = xi0; |
| 501 | y[i].qs[j] |= xi1 << 4; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) { |
| 507 | quantize_row_q4_1_reference(x, y, k); |
no outgoing calls
no test coverage detected