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.5 KiB89 linesraw
1"""tools/image/gate/dit_step_check.py - TEST-ONLY (OWN-5B D4): strata-dit-step's velocities against the reference.
2
3For each case: velocity rel L2 to the f32 reference (dit_ref.py) for ours (strata-dit-step), the emulation with
4float32 sums (dit_ref mode f16acc32, the stage rule's comparison since 11-11) and with exact sums (f16op), the D1+D2
5test build (sd.cpp's native runner with our GEMMs), stock sd.cpp and its native runner; and ours against the D1+D2
6build directly.  Then the stage rule over the cases given: (i) mean ours <= 1.25 x mean emulation, (ii) no case above
71.5 x its emulation value, (iii) every case closer than stock and native - against both emulations.
8
9    python -I -B dit_step_check.py --root R --ours O [--cases "512 p0,512 p5"]
10R = the Spark own5b folder (pics/, d0ref/ with d0ref/acc32 from dit_acc32_cases.py); O = strata-dit-step's out dir.
11"""
12from __future__ import annotations
13
14import argparse
15import sys
16from pathlib import Path
17
18import numpy as np
19
20_HERE = Path(__file__).resolve().parent
21if str(_HERE) not in sys.path:
22    sys.path.insert(0, str(_HERE))
23from te_ref import compare  # noqa: E402
24
25
26def vel(path) -> np.ndarray:
27    """a velocity file ([C][H][W] float32, sd.cpp's vel_<k>.bin layout) as [H*W][C] rows"""
28    return np.fromfile(path, dtype="<f4").reshape(64, -1).T
29
30
31def case_paths(root: Path, name: str) -> dict:
32    size, p = name.split()
33    p = int(p[1:])
34    dn = f"dump_p{p}_s42"
35    if size == "1024":
36        return {"dump": root / "pics/d0_stock_1024" / dn, "ref": root / "d0ref/r1024" / dn,
37                "acc32": root / "d0ref/acc32/1024_p0_acc32.npy", "stock": root / "pics/d0_stock_1024" / dn / "vel_0.bin",
38                "native": root / "pics/d0_native_1024" / dn / "vel_0.bin",
39                "d1d2": root / "pics/z_ours_1024" / dn / "vel_0.bin"}
40    lo = "d0" if p < 4 else "z"
41    return {"dump": root / f"pics/{lo}_stock_512" / dn, "ref": root / "d0ref" / dn,
42            "acc32": root / f"d0ref/acc32/512_p{p}_acc32.npy", "stock": root / f"pics/{lo}_stock_512" / dn / "vel_0.bin",
43            "native": root / f"pics/{lo}_native_512" / dn / "vel_0.bin",
44            "d1d2": root / ("pics/d2_g15" if p < 4 else "pics/z_ours_512") / dn / "vel_0.bin"}
45
46
47def rule(rows: list, k: int) -> str:
48    O = np.mean([r["ours"] for r in rows])
49    E = np.mean([r[k] for r in rows])
50    worst = max(rows, key=lambda r: r["ours"] / r[k])
51    ii = all(r["ours"] <= 1.5 * r[k] for r in rows)
52    iii = all(r["ours"] < r["stock"] and r["ours"] < r["native"] for r in rows)
53    return (f"(i) mean {O:.5f} / {E:.5f} = {O / E:.3f} ({'PASS' if O <= 1.25 * E else 'FAIL'}); (ii) largest "
54            f"{worst['ours'] / worst[k]:.3f} at {worst['name']} ({'PASS' if ii else 'FAIL'}); (iii) "
55            f"{'PASS' if iii else 'FAIL'}")
56
57
58def main(argv=None) -> int:
59    ap = argparse.ArgumentParser()
60    ap.add_argument("--root", required=True)
61    ap.add_argument("--ours", required=True)
62    ap.add_argument("--cases", default="512 p0,512 p5")
63    a = ap.parse_args(argv)
64    root, ours_dir = Path(a.root), Path(a.ours)
65    rows = []
66    for name in [c.strip() for c in a.cases.split(",") if c.strip()]:
67        P = case_paths(root, name)
68        f32 = np.load(str(P["ref"]) + "_f32.npy").reshape(64, -1).T
69        o = vel(ours_dir / P["dump"].parent.name / P["dump"].name / "vel.bin")
70        r = {"name": name, "ours": compare(o, f32)["rel_l2"],
71             "f16acc32": compare(np.load(P["acc32"]).reshape(64, -1).T, f32)["rel_l2"],
72             "f16op": compare(np.load(str(P["ref"]) + "_f16op.npy").reshape(64, -1).T, f32)["rel_l2"],
73             "d1d2": compare(vel(P["d1d2"]), f32)["rel_l2"], "stock": compare(vel(P["stock"]), f32)["rel_l2"],
74             "native": compare(vel(P["native"]), f32)["rel_l2"], "ours_vs_d1d2": compare(o, vel(P["d1d2"]))["rel_l2"]}
75        rows.append(r)
76    print("| case | ours (strata-dit-step) | f32-sum emulation | ratio | exact-sum emulation | ratio | D1+D2 test build "
77          "| stock | native | ours vs D1+D2 build |")
78    print("|---|---|---|---|---|---|---|---|---|---|")
79    for r in rows:
80        print(f"| {r['name']} | {r['ours']:.5f} | {r['f16acc32']:.5f} | {r['ours'] / r['f16acc32']:.3f} | "
81              f"{r['f16op']:.5f} | {r['ours'] / r['f16op']:.3f} | {r['d1d2']:.5f} | {r['stock']:.5f} | "
82              f"{r['native']:.5f} | {r['ours_vs_d1d2']:.5f} |")
83    print("RULE vs the float32-sum emulation: " + rule(rows, "f16acc32"))
84    print("RULE vs the exact-sum emulation: " + rule(rows, "f16op"))
85    return 0
86
87
88if __name__ == "__main__":
89    sys.exit(main())