okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-2b (cc8c29f31) into f-own-int: our own GGUF reader/writer and quantizers (gguf-py out of the pack tools)0f1e5e4cb

main
1.5 KiB35 linesraw
1"""metal/tools/check_ref.py - checks metal/src/ref_quant.cpp's dequantizers against gguf-py's (and Q2_0 against
2tools/gguf_writer.py's codec) on the files tests/ref_dump wrote:
3
4    python metal/tools/check_ref.py <dir>
5
6Prints the max |difference| per type; the reference passes when every one is 0 (bitwise: both decode to float32).
7"""
8import pathlib
9import sys
10
11import numpy as np
12
13ROOT = pathlib.Path(__file__).resolve().parents[2]
14sys.path.insert(0, str(ROOT / "tools"))
15from _paths import gguf_lib  # noqa: E402
16_g = gguf_lib()
17dequantize, Q = _g.quants.dequantize, _g.GGMLQuantizationType
18
19ROWS, COLS = 16, 2560
20d = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else ".")
21bad = 0
22for name in ("Q2_0", "Q4_0", "Q5_0", "Q8_0", "Q3_K", "Q4_K", "Q5_K", "Q6_K", "IQ2_XXS", "IQ2_XS", "IQ2_S", "IQ3_XXS",
23             "IQ3_S", "IQ4_NL", "IQ4_XS", "BF16", "F16"):
24    raw = np.fromfile(d / f"{name}.bin", dtype=np.uint8)
25    ref = np.fromfile(d / f"{name}.f32", dtype=np.float32).reshape(ROWS, COLS)
26    if name == "Q2_0":
27        from gguf_writer import dequantize_q2_0  # noqa: E402
28        got = np.asarray(dequantize_q2_0(raw.tobytes()), dtype=np.float32).reshape(ROWS, COLS)
29    else:
30        got = dequantize(raw.reshape(ROWS, -1), Q[name]).astype(np.float32).reshape(ROWS, COLS)
31    diff = float(np.max(np.abs(got - ref)))
32    scale = float(np.max(np.abs(got)))
33    print(f"{name:8s} max |ref_quant - gguf-py| = {diff:.3e}  (max |w| {scale:.3e})  {'ok' if diff == 0 else 'DIFF'}")
34    bad += diff != 0
35sys.exit(1 if bad else 0)