Files
Strata/terminal/tests/split/convert_check.py
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

84 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Check the loader's tensor conversion (src/split/src/tensor_convert.hpp) against gguf-py.
convert_dump MODEL.gguf OUTDIR name... > dump.txt
convert_check.py MODEL.gguf OUTDIR dump.txt --gguf-py /path/to/llama.cpp/gguf-py
For each tensor in the dump: the values the loader holds now (read back by convert_dump) must equal gguf-py's dequantization of the
file's original tensor to within what the target type allows: exactly (F32 target), or within half a Q8_0 step per 32 values
(amax/254, plus the f16 rounding of the scale). Prints one line per tensor and exits 1 on any miss.
"""
import argparse
import os
import sys
import numpy as np
def main():
ap = argparse.ArgumentParser()
ap.add_argument("model")
ap.add_argument("outdir")
ap.add_argument("dump")
ap.add_argument("--gguf-py", help="optional: a folder that holds llama.cpp's gguf package, to compare against it (default: tools/strata_gguf.py)")
a = ap.parse_args()
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "tools"))
if a.gguf_py:
os.environ["STRATA_GGUF_PY"] = a.gguf_py
from _paths import gguf_lib
gguf = gguf_lib()
quants = gguf.quants
shards = [a.model]
import re
m = re.match(r"(.*-)(\d{5})(-of-)(\d{5})(\.gguf)$", a.model)
if m:
shards = ["%s%05d%s%s%s" % (m[1], i, m[3], m[4], m[5]) for i in range(1, int(m[4]) + 1)]
tensors = {}
for s in shards:
r = gguf.GGUFReader(s)
for t in r.tensors:
tensors[t.name] = t
bad = 0
for line in open(a.dump):
p = line.split()
if len(p) != 3 or p[0] == "converted":
continue
name, ty = p[0], int(p[1])
t = tensors[name]
if t.tensor_type.name == "Q2_0": # Strata's type 42, not in gguf-py's dequantizers: (code - 1) * d, value j in byte j / 4 at bit 2 (j % 4)
raw = np.ascontiguousarray(t.data).reshape(-1, 18)
d = raw[:, :2].copy().view(np.float16).astype(np.float64).reshape(-1, 1)
qs = raw[:, 2:]
codes = np.stack([(qs >> (2 * k)) & 3 for k in range(4)], axis=2).reshape(-1, 64)
ref = ((codes.astype(np.float64) - 1.0) * d).reshape(-1)
else:
ref = quants.dequantize(t.data, t.tensor_type).astype(np.float64).reshape(-1)
got = np.fromfile(os.path.join(a.outdir, name + ".f32"), dtype=np.float32).astype(np.float64)
if ref.shape != got.shape:
print("FAIL %s: %d values, gguf-py %d" % (name, got.size, ref.size))
bad += 1
continue
err = np.abs(got - ref)
if ty == 0: # F32: BF16 / F16 / F32 source, exact
lim = np.zeros_like(err)
elif ty != 8: # a type the loader keeps or repacks without changing a value (Q2_0 -> Q4_0, IQ4_NL as is): f32 rounding only
lim = np.abs(ref) * 1e-6 + 1e-12
else: # Q8_0: half a step of its block, the scale stored as f16
blk = ref.reshape(-1, 32)
amax = np.abs(blk).max(axis=1)
# the scale d = amax/127 is stored as f16 (relative error 2^-11), and the value is q*d with |q| <= 127
# (a block whose d falls below f16's normal range, 6.1e-5, has an absolute step of 2^-25 in d: 127 * 2^-25 on the value;
# 8 of the 19.9M blocks of token_embd are like that)
lim = np.repeat(amax * (0.5 / 127.0 + 2.0 ** -10) + 127 * 2.0 ** -25 + 1e-12, 32)
ok = bool((err <= lim).all())
rel = float(np.sqrt((err ** 2).sum() / max((ref ** 2).sum(), 1e-30)))
print("%s %s %s from %s max|d|=%.3g rel_rms=%.3g" % ("ok " if ok else "FAIL", name, {0: "F32", 8: "Q8_0", 2: "Q4_0", 20: "IQ4_NL"}.get(ty, str(ty)), t.tensor_type.name, err.max(), rel))
bad += not ok
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main()