Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
# Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17).
|
|
# Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file.
|
|
"""tools/ds4/compare.py - the engine's logits (maya-ds4 --dump-logits) against the reference's (tools/ds4/ref.py --dump).
|
|
|
|
python tools/ds4/compare.py --ref ref.npz --engine engine.f32 [--vocab 129280]
|
|
|
|
Per position: the cosine of the two logit rows, the largest difference, whether the top token agrees, and the KL
|
|
divergence of the engine's distribution from the reference's.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import sys
|
|
|
|
import numpy as np
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--ref", required=True)
|
|
ap.add_argument("--engine", required=True)
|
|
ap.add_argument("--vocab", type=int, default=129280)
|
|
ap.add_argument("--layers", help="the engine's DS4_DUMP_H file: each layer's residual stream against the ref's")
|
|
a = ap.parse_args()
|
|
r = np.load(a.ref)
|
|
ref = r["logits"].astype(np.float64)
|
|
eng = np.fromfile(a.engine, dtype=np.float32).reshape(-1, a.vocab).astype(np.float64)
|
|
n = min(len(ref), len(eng))
|
|
agree = 0
|
|
kls = []
|
|
for i in range(n):
|
|
x, y = ref[i], eng[i]
|
|
cos = float(x @ y / (np.linalg.norm(x) * np.linalg.norm(y)))
|
|
px = np.exp(x - x.max())
|
|
px /= px.sum()
|
|
py = np.exp(y - y.max())
|
|
py /= py.sum()
|
|
kl = float(np.sum(px * (np.log(px + 1e-30) - np.log(py + 1e-30))))
|
|
kls.append(kl)
|
|
top = int(x.argmax()) == int(y.argmax())
|
|
agree += top
|
|
print("pos %3d cos %.6f max|d| %8.4f top %s (ref %6d eng %6d) KL %.2e" %
|
|
(i, cos, np.abs(x - y).max(), "same" if top else "DIFF", x.argmax(), y.argmax(), kl))
|
|
print("%d positions: top-1 agrees at %d, mean KL %.3e, max KL %.3e" % (n, agree, np.mean(kls), np.max(kls)))
|
|
if a.layers:
|
|
hs = r["hs"] # [layers, T, hc, dim]
|
|
nl, T = hs.shape[0], hs.shape[1]
|
|
eh = np.fromfile(a.layers, dtype=np.float32).reshape(-1, nl, *hs.shape[2:]) # [T, layers, hc, dim]
|
|
T = min(T, len(eh))
|
|
print("relative error of each layer's residual stream (rows: layers, columns: positions)")
|
|
print(" " + " ".join("%6d" % t for t in range(T)))
|
|
for li in range(nl):
|
|
row = []
|
|
for t in range(T):
|
|
x, y = hs[li, t].astype(np.float64), eh[t, li].astype(np.float64)
|
|
row.append(np.linalg.norm(x - y) / np.linalg.norm(x))
|
|
print("L%-3d " % li + " ".join("%6.4f" % v for v in row))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|