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
3.7 KiB83 linesraw
1"""tools/image/gate/dit_replay2.py - TEST-ONLY (OWN-5B trace, step 2): where does our stream pick up what makes a prompt
2far from the reference?
3
4  --from K [K ...]: start the f16op emulation from OUR stream after block K (txt_KK.bin + img_KK.bin of a
5      STRATA_DIT_TRACE_DIR dump; K = -1 = our prelude outputs txtin.bin + imgin.bin), run the rest of the blocks and the
6      head in f16op, and give the velocity's rel L2 to the f32 reference velocity (dit_ref --out-dir npy).  The first K
7      where this jumps from the emulation's own value to ours is where our arithmetic put the difference in.
8  --acc32: also run the whole call with f16 inputs and FLOAT32 accumulation in every GEMM (numpy float32 matmul,
9      OpenBLAS summation order), i.e. the emulation with the accumulation our GPU really does, and give its distance.
10
11    python -I -B dit_replay2.py <dit.gguf> --dump D --trace T --ref R.npy [--from K ...] [--acc32]
12"""
13from __future__ import annotations
14
15import argparse
16import sys
17import time
18from pathlib import Path
19
20import numpy as np
21
22_HERE = Path(__file__).resolve().parent
23if str(_HERE) not in sys.path:
24    sys.path.insert(0, str(_HERE))
25from dit_ref import DitWeights, Mode, ModeAcc32, block, head, layout_positions, load_dump, prelude, rope_table  # noqa: E402
26from te_ref import compare  # noqa: E402
27
28F32 = np.float32
29
30
31def main(argv=None) -> int:
32    ap = argparse.ArgumentParser()
33    ap.add_argument("gguf")
34    ap.add_argument("--dump", required=True)
35    ap.add_argument("--trace", required=True)
36    ap.add_argument("--ref", required=True, help="the f32 reference velocity (.npy, [64, H, W])")
37    ap.add_argument("--from", dest="starts", type=int, nargs="*", default=[])
38    ap.add_argument("--acc32", action="store_true")
39    a = ap.parse_args(argv)
40    d, t = Path(a.dump), Path(a.trace)
41    name = d.name
42    Wt = DitWeights(Path(a.gguf))
43    xv, xs = load_dump(d, "dit_x_0")
44    tv, _ = load_dump(d, "dit_t_0")
45    cv, cs = load_dump(d, "cond")
46    Ww, Hh, C = xs[0], xs[1], xs[2]
47    x, tt, cond = xv.reshape(C, Hh, Ww), float(tv[0]), cv.reshape(cs[1], cs[0])
48    L, n = cond.shape[0], Hh * Ww
49    c, s = rope_table(layout_positions(L, Hh, Ww))
50    ref = np.load(a.ref).reshape(64, -1).T
51    ours_v = np.fromfile(t / "out.bin", dtype="<f4").reshape(-1, 64)
52    print(f"RESTART {name} | ours (no restart) | {compare(ours_v, ref)['rel_l2']:.6f}", flush=True)
53    nl = Wt.n_layer
54    m = Mode(True)
55    mods, scale, _ = prelude(Wt, m, tt, cond, x)
56    rows = np.array([1] * L + [0] * n)
57    t0 = time.time()
58    for k in a.starts:
59        if k < 0:
60            tx, im = "txtin.bin", "imgin.bin"
61        else:
62            tx, im = f"txt_{k:02d}.bin", f"img_{k:02d}.bin"
63        xj = np.concatenate([np.fromfile(t / tx, dtype="<f4").reshape(L, -1),
64                             np.fromfile(t / im, dtype="<f4").reshape(n, -1)], 0)
65        for i in range(k + 1, nl):
66            xj = block(Wt, m, i, xj, rows, mods, L, c, s)
67        v = head(Wt, m, xj, L, scale, Hh, Ww).reshape(64, -1).T
68        print(f"RESTART {name} | f16op from our stream after {'the prelude' if k < 0 else 'block %d' % k} | "
69              f"{compare(v, ref)['rel_l2']:.6f} | {compare(v, ours_v)['rel_l2']:.6f} to ours | {time.time() - t0:.0f} s",
70              flush=True)
71    if a.acc32:
72        m = ModeAcc32()
73        mods, scale, xj = prelude(Wt, m, tt, cond, x)
74        for i in range(nl):
75            xj = block(Wt, m, i, xj, rows, mods, L, c, s)
76        v = head(Wt, m, xj, L, scale, Hh, Ww).reshape(64, -1).T
77        print(f"ACC32 {name} | f16 inputs, float32 accumulation, whole call | {compare(v, ref)['rel_l2']:.6f} | "
78              f"{compare(v, ours_v)['rel_l2']:.6f} to ours | {time.time() - t0:.0f} s", flush=True)
79    return 0
80
81
82if __name__ == "__main__":
83    sys.exit(main())