"""tools/image/gate/dit_replay.py - TEST-ONLY (OWN-5B trace): replay one DiT block of dit_ref.py from OUR stream. Our test build (STRATA_DIT_TRACE_DIR, STRATA_DIT_TRACE_BLOCK=B) dumps the stream after every block (txt_NN.bin, img_NN.bin) and the inside of block B (blk_*.bin). This tool: 1. runs dit_ref's block B in f32 and in f16op starting from OUR stream after block B-1, and compares each stage with our dump of the same stage: same input, so what is left is the difference in the block's own arithmetic; 2. runs the reference up to block B-1 (f32 and f16op), then block B in f32 from three inputs: the f32 state, the f16op state and our state; the attention output's distance from the f32-from-f32 one against the input's distance tells how much block B amplifies a given input difference, per input; 3. the same with random input perturbations of the size of ours (three trials), to see whether block B of this prompt amplifies any small difference or only ours; 4. a K / V layout check (rows of our blk_k16 / blk_v16 against the reference's text and image rows). python -I -B dit_replay.py --dump D --trace T --block B """ from __future__ import annotations import argparse import sys from pathlib import Path import numpy as np _HERE = Path(__file__).resolve().parent if str(_HERE) not in sys.path: sys.path.insert(0, str(_HERE)) from dit_ref import DitWeights, Mode, block, layer_norm, layout_positions, load_dump, prelude, rope_table # noqa: E402 from te_ref import compare # noqa: E402 F32 = np.float32 KEYS = ("qkv", "q16", "att", "x_mid", "ln2", "act") def rel(a, b) -> float: return compare(np.asarray(a, F32).reshape(a.shape[0], -1), np.asarray(b, F32).reshape(b.shape[0], -1))["rel_l2"] def ours_block(t: Path, n: int, H: int, D: int) -> dict: def rd(name, shape, dt=" int: ap = argparse.ArgumentParser() ap.add_argument("gguf") ap.add_argument("--dump", required=True) ap.add_argument("--trace", required=True) ap.add_argument("--block", type=int, required=True) ap.add_argument("--trials", type=int, default=3) a = ap.parse_args(argv) B = a.block d, t = Path(a.dump), Path(a.trace) name = d.name Wt = DitWeights(Path(a.gguf)) xv, xs = load_dump(d, "dit_x_0") tv, _ = load_dump(d, "dit_t_0") cv, cs = load_dump(d, "cond") Ww, Hh, C = xs[0], xs[1], xs[2] x, tt, cond = xv.reshape(C, Hh, Ww), float(tv[0]), cv.reshape(cs[1], cs[0]) L = cond.shape[0] c, s = rope_table(layout_positions(L, Hh, Ww)) ours_prev = np.concatenate([np.fromfile(t / f"txt_{B - 1:02d}.bin", dtype="= L + n: cells.append(f"[text;image] {rel(o[:L + n], r):.6f}") cells.append(f"text rows {rel(o[:L], r[:L]):.6f} image rows {rel(o[L:L + n], r[L:]):.6f}") cells.append(f"[image;text] {rel(np.concatenate([o[n:n + L], o[:n]], 0), r):.6f}") print(f"LAYOUT {name} | {key} | " + " | ".join(cells)) return 0 if __name__ == "__main__": sys.exit(main())