MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / quantize_row_q4_0_reference

Function quantize_row_q4_0_reference

ggml-quants.c:428–463  ·  view source on GitHub ↗

reference implementation for deterministic creation of model files

Source from the content-addressed store, hash-verified

426
427// reference implementation for deterministic creation of model files
428void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
429 static const int qk = QK4_0;
430
431 assert(k % qk == 0);
432
433 const int nb = k / qk;
434
435 for (int i = 0; i < nb; i++) {
436 float amax = 0.0f; // absolute max
437 float max = 0.0f;
438
439 for (int j = 0; j < qk; j++) {
440 const float v = x[i*qk + j];
441 if (amax < fabsf(v)) {
442 amax = fabsf(v);
443 max = v;
444 }
445 }
446
447 const float d = max / -8;
448 const float id = d ? 1.0f/d : 0.0f;
449
450 y[i].d = GGML_FP32_TO_FP16(d);
451
452 for (int j = 0; j < qk/2; ++j) {
453 const float x0 = x[i*qk + 0 + j]*id;
454 const float x1 = x[i*qk + qk/2 + j]*id;
455
456 const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
457 const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
458
459 y[i].qs[j] = xi0;
460 y[i].qs[j] |= xi1 << 4;
461 }
462 }
463}
464
465void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
466 quantize_row_q4_0_reference(x, y, k);

Callers 2

ggml_quantize_q4_0Function · 0.85
quantize_row_q4_0Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected