okami.mom/strata

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

waveringanaserve: restore the DeepSeek V4.1 server paths, tools and headers removed upstream, keeping the native picture tower firstba8745e13

main
2.7 KiB62 linesraw
1# Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17).
2# Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file.
3"""tools/ds4/compare.py - the engine's logits (maya-ds4 --dump-logits) against the reference's (tools/ds4/ref.py --dump).
4
5    python tools/ds4/compare.py --ref ref.npz --engine engine.f32 [--vocab 129280]
6
7Per position: the cosine of the two logit rows, the largest difference, whether the top token agrees, and the KL
8divergence of the engine's distribution from the reference's.
9"""
10from __future__ import annotations
11
12import argparse
13import sys
14
15import numpy as np
16
17
18def main() -> int:
19    ap = argparse.ArgumentParser()
20    ap.add_argument("--ref", required=True)
21    ap.add_argument("--engine", required=True)
22    ap.add_argument("--vocab", type=int, default=129280)
23    ap.add_argument("--layers", help="the engine's DS4_DUMP_H file: each layer's residual stream against the ref's")
24    a = ap.parse_args()
25    r = np.load(a.ref)
26    ref = r["logits"].astype(np.float64)
27    eng = np.fromfile(a.engine, dtype=np.float32).reshape(-1, a.vocab).astype(np.float64)
28    n = min(len(ref), len(eng))
29    agree = 0
30    kls = []
31    for i in range(n):
32        x, y = ref[i], eng[i]
33        cos = float(x @ y / (np.linalg.norm(x) * np.linalg.norm(y)))
34        px = np.exp(x - x.max())
35        px /= px.sum()
36        py = np.exp(y - y.max())
37        py /= py.sum()
38        kl = float(np.sum(px * (np.log(px + 1e-30) - np.log(py + 1e-30))))
39        kls.append(kl)
40        top = int(x.argmax()) == int(y.argmax())
41        agree += top
42        print("pos %3d  cos %.6f  max|d| %8.4f  top %s (ref %6d eng %6d)  KL %.2e" %
43              (i, cos, np.abs(x - y).max(), "same" if top else "DIFF", x.argmax(), y.argmax(), kl))
44    print("%d positions: top-1 agrees at %d, mean KL %.3e, max KL %.3e" % (n, agree, np.mean(kls), np.max(kls)))
45    if a.layers:
46        hs = r["hs"]                                                     # [layers, T, hc, dim]
47        nl, T = hs.shape[0], hs.shape[1]
48        eh = np.fromfile(a.layers, dtype=np.float32).reshape(-1, nl, *hs.shape[2:])   # [T, layers, hc, dim]
49        T = min(T, len(eh))
50        print("relative error of each layer's residual stream (rows: layers, columns: positions)")
51        print("     " + " ".join("%6d" % t for t in range(T)))
52        for li in range(nl):
53            row = []
54            for t in range(T):
55                x, y = hs[li, t].astype(np.float64), eh[t, li].astype(np.float64)
56                row.append(np.linalg.norm(x - y) / np.linalg.norm(x))
57            print("L%-3d " % li + " ".join("%6.4f" % v for v in row))
58    return 0
59
60
61if __name__ == "__main__":
62    sys.exit(main())