Files
strata/metal/tools/check_ref.py
Niko1221 ad9eb89079 metal: standalone Metal kernel tree - runtime face, bandwidth/dispatch probes, grouped routed-expert kernels
metal/ is its own CMake project (macOS only), nothing in src/ or include/ changes. MSL is compiled at run time
(the Command Line Tools have no offline metal compiler) with ggml-common.h prepended for the i-quant grids.

- src/mtl_rt.*: a C++ face over Metal (buffers, pipelines with function constants, serial/concurrent encoders,
  indirect dispatch, indirect command buffers, GPU timing).
- src/ref_quant.*: CPU reference dequantizers for Q2_0, Q4_0, Q5_0, Q8_0, Q3_K..Q6_K, IQ2_XXS/XS/S, IQ3_XXS/S,
  IQ4_NL/XS, BF16, F16; checked bitwise against gguf-py (tests/ref_dump + tools/check_ref.py: 17/17 equal).
- kernels/quant.metal: the formats as per-lane units (decode once, dot with every token of the window).
- kernels/moe.metal: moe_plan (top-k -> groups by expert, deterministic, writes the indirect grids), moe_gu
  (gate+up+SwiGLU, grouped), moe_down, moe_combine; tests/expert_parity: parity vs double and the bench.
- tests/rt_bench: M5 Max streaming read ~585 GB/s, ~0.85 us per dependent dispatch, ~170 us commit+wait.
2026-10-12 10:42:59 +02:00

35 lines
1.5 KiB
Python

"""metal/tools/check_ref.py - checks metal/src/ref_quant.cpp's dequantizers against gguf-py's (and Q2_0 against
tools/gguf_writer.py's codec) on the files tests/ref_dump wrote:
python metal/tools/check_ref.py <dir>
Prints the max |difference| per type; the reference passes when every one is 0 (bitwise: both decode to float32).
"""
import pathlib
import sys
import numpy as np
ROOT = pathlib.Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tools"))
from gguf.quants import dequantize # noqa: E402
from gguf.constants import GGMLQuantizationType as Q # noqa: E402
ROWS, COLS = 16, 2560
d = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else ".")
bad = 0
for 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",
"IQ3_S", "IQ4_NL", "IQ4_XS", "BF16", "F16"):
raw = np.fromfile(d / f"{name}.bin", dtype=np.uint8)
ref = np.fromfile(d / f"{name}.f32", dtype=np.float32).reshape(ROWS, COLS)
if name == "Q2_0":
from gguf_writer import dequantize_q2_0 # noqa: E402
got = np.asarray(dequantize_q2_0(raw.tobytes()), dtype=np.float32).reshape(ROWS, COLS)
else:
got = dequantize(raw.reshape(ROWS, -1), Q[name]).astype(np.float32).reshape(ROWS, COLS)
diff = float(np.max(np.abs(got - ref)))
scale = float(np.max(np.abs(got)))
print(f"{name:8s} max |ref_quant - gguf-py| = {diff:.3e} (max |w| {scale:.3e}) {'ok' if diff == 0 else 'DIFF'}")
bad += diff != 0
sys.exit(1 if bad else 0)