okami.mom/strata

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

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
4.3 KiB97 linesraw
1"""tools/image/gate/dit_acc32_cases.py - TEST-ONLY (OWN-5B): the stage check's nine cases against the emulation with
2float32 accumulation (dit_ref mode "f16acc32", ModeAcc32: f16 weights and GEMM inputs, float32 sums as cuBLAS GemmEx compute 32F
3does) next to the exact-accumulation emulation (dit_ref f16op) it replaces.
4
5    python -I -B dit_acc32_cases.py <dit.gguf> --root R --out O
6R = the Spark own5b folder (pics/, d0ref/); writes O/<case>_acc32.npy and prints the table and the rule lines.
7"""
8from __future__ import annotations
9
10import argparse
11import sys
12import time
13from pathlib import Path
14
15import numpy as np
16
17_HERE = Path(__file__).resolve().parent
18if str(_HERE) not in sys.path:
19    sys.path.insert(0, str(_HERE))
20from dit_ref import DitWeights, ModeAcc32, block, head, layout_positions, load_dump, prelude, rope_table  # noqa: E402
21from te_ref import compare  # noqa: E402
22
23
24def vel(path) -> np.ndarray:
25    return np.fromfile(path, dtype="<f4").reshape(64, -1).T
26
27
28def run_acc32(Wt, d: Path) -> np.ndarray:
29    xv, xs = load_dump(d, "dit_x_0")
30    tv, _ = load_dump(d, "dit_t_0")
31    cv, cs = load_dump(d, "cond")
32    Ww, Hh, C = xs[0], xs[1], xs[2]
33    x, tt, cond = xv.reshape(C, Hh, Ww), float(tv[0]), cv.reshape(cs[1], cs[0])
34    L, n = cond.shape[0], Hh * Ww
35    c, s = rope_table(layout_positions(L, Hh, Ww))
36    m = ModeAcc32()
37    mods, scale, xj = prelude(Wt, m, tt, cond, x)
38    rows = np.array([1] * L + [0] * n)
39    for i in range(Wt.n_layer):
40        xj = block(Wt, m, i, xj, rows, mods, L, c, s)
41    return head(Wt, m, xj, L, scale, Hh, Ww)
42
43
44def main(argv=None) -> int:
45    ap = argparse.ArgumentParser()
46    ap.add_argument("gguf")
47    ap.add_argument("--root", required=True)
48    ap.add_argument("--out", required=True)
49    a = ap.parse_args(argv)
50    R, out = Path(a.root), Path(a.out)
51    out.mkdir(parents=True, exist_ok=True)
52    Wt = DitWeights(Path(a.gguf))
53    cases = []
54    for p in range(8):
55        dn = f"dump_p{p}_s42"
56        lo = "d0" if p < 4 else "z"
57        ours = R / ("pics/d2_g15" if p < 4 else "pics/z_ours_512") / dn / "vel_0.bin"
58        cases.append((f"512 p{p}", R / f"pics/{lo}_stock_512" / dn, R / "d0ref" / dn,
59                      R / f"pics/{lo}_stock_512" / dn / "vel_0.bin", R / f"pics/{lo}_native_512" / dn / "vel_0.bin", ours))
60    dn = "dump_p0_s42"
61    cases.append(("1024 p0", R / "pics/d0_stock_1024" / dn, R / "d0ref/r1024" / dn, R / "pics/d0_stock_1024" / dn / "vel_0.bin",
62                  R / "pics/d0_native_1024" / dn / "vel_0.bin", R / "pics/z_ours_1024" / dn / "vel_0.bin"))
63    t0 = time.time()
64    rows = []
65    for name, dump, ref, st, nat, ours in cases:
66        f32 = np.load(str(ref) + "_f32.npy").reshape(64, -1).T
67        exact = compare(np.load(str(ref) + "_f16op.npy").reshape(64, -1).T, f32)["rel_l2"]
68        npy = out / (name.replace(" ", "_") + "_acc32.npy")
69        if npy.is_file():
70            v = np.load(npy)
71        else:
72            v = run_acc32(Wt, dump)
73            np.save(npy, v)
74        acc = compare(v.reshape(64, -1).T, f32)["rel_l2"]
75        o = compare(vel(ours), f32)["rel_l2"]
76        s = compare(vel(st), f32)["rel_l2"]
77        nv = compare(vel(nat), f32)["rel_l2"]
78        oa = compare(vel(ours), v.reshape(64, -1).T)["rel_l2"]
79        rows.append((name, o, exact, acc, s, nv, oa))
80        print(f"case {name} done {time.time() - t0:.0f} s", flush=True)
81    print("| case | ours | emulation, exact sums (before) | ratio | emulation, f32 sums (corrected) | ratio | stock | native | ours vs corrected emulation |")
82    print("|---|---|---|---|---|---|---|---|---|")
83    for name, o, e, ac, s, nv, oa in rows:
84        print(f"| {name} | {o:.5f} | {e:.5f} | {o / e:.3f} | {ac:.5f} | {o / ac:.3f} | {s:.5f} | {nv:.5f} | {oa:.5f} |")
85    O = np.mean([r[1] for r in rows])
86    for label, k in (("exact sums", 2), ("f32 sums", 3)):
87        E = np.mean([r[k] for r in rows])
88        worst = max(rows, key=lambda r: r[1] / r[k])
89        print(f"RULE vs emulation with {label}: (i) mean ours {O:.5f} / emulation {E:.5f} = {O / E:.3f} "
90              f"({'PASS' if O <= 1.25 * E else 'FAIL'}); (ii) largest ratio {worst[1] / worst[k]:.3f} at {worst[0]} "
91              f"({'PASS' if all(r[1] <= 1.5 * r[k] for r in rows) else 'FAIL'}); (iii) "
92              f"{'PASS' if all(r[1] < r[4] and r[1] < r[5] for r in rows) else 'FAIL'}")
93    return 0
94
95
96if __name__ == "__main__":
97    sys.exit(main())