#!/usr/bin/env python3 """MK-FO bring-up probe (hang protocol: smallest shape first, one core, eager x2, then a 2-call trace, then the model shape): ``tt.fout.fuse_out`` against the stock chain (tilize t, add, untilize, tilize of the 4-D view), bit for bit on all three outputs; traced replays timed. METEOR_DEVICE_OK=1 bin/devrun -t 300 -- python -u code/scripts/fout_probe.py --hw 1,32 --max-cores 1 METEOR_DEVICE_OK=1 bin/devrun -t 300 -- python -u code/scripts/fout_probe.py --hw 4,40 --trace METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/fout_probe.py --hw 800,500 --trace """ from __future__ import annotations import argparse import sys import time import numpy as np def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--hw", default="1,32") ap.add_argument("--max-cores", type=int, default=0) ap.add_argument("--trace", action="store_true") ap.add_argument("--reps", type=int, default=10) a = ap.parse_args() import torch import ttnn from tt_meteor.tt import fout from tt_meteor.ttaw.device import open_device h, w = (int(v) for v in a.hw.split(",")) r = h * w assert r % 32 == 0 dev = open_device(dispatch="eth", trace_region_size=64 << 20) rng = np.random.default_rng(0) raw_h = torch.from_numpy(rng.standard_normal((1, 1, r, 96)).astype(np.float32)).bfloat16() t_h = torch.from_numpy(np.maximum(rng.standard_normal((1, h, w, 96)), 0).astype(np.float32)).bfloat16() D = ttnn.DRAM_MEMORY_CONFIG raw = ttnn.from_torch(raw_h, dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev, memory_config=D) r4 = ttnn.from_torch(raw_h.reshape(1, h, w, 96), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev, memory_config=D) t_rm = ttnn.from_torch(t_h, dtype=ttnn.bfloat16, layout=ttnn.ROW_MAJOR_LAYOUT, device=dev, memory_config=D) print(f"[{h}x{w}] supported {fout.supported(t_rm, raw, r4)}; r4 padded {list(r4.padded_shape)}", flush=True) def stock(): t = ttnn.to_layout(ttnn.reshape(t_rm, (1, 1, r, 96)), ttnn.TILE_LAYOUT) f = ttnn.add(raw, t, memory_config=D) ttnn.deallocate(t) frm = ttnn.to_layout(f, ttnn.ROW_MAJOR_LAYOUT) f4 = ttnn.to_layout(ttnn.reshape(frm, (1, h, w, 96)), ttnn.TILE_LAYOUT) return f, frm, f4 def kern(): return fout.fuse_out(t_rm, raw, r4, max_cores=a.max_cores) def host(ts): return [ttnn.to_torch(x).float().numpy().reshape(r, 96) for x in ts] bad = 0 want = host(stock()) ref = (raw_h.float() + t_h.float().reshape(1, 1, r, 96)).bfloat16().float().numpy().reshape(r, 96) print(f" stock == host bf16(raw + t): {np.array_equal(want[0], ref)}", flush=True) got = host(kern()) same = [np.array_equal(x, y) for x, y in zip(want, got)] print(f" kernel vs stock: fused / fused_rm / fused4 identical {same}", flush=True) if not all(same): for nm, x, y in zip(("fused", "fused_rm", "fused4"), want, got): d = np.argwhere(x != y) if len(d): print(f" {nm}: {len(d)} differ, first {d[:4].tolist()} stock {x[tuple(d[0])]} kernel {y[tuple(d[0])]}", flush=True) bad += 0 if all(same) else 1 got2 = host(kern()) ok2 = all(np.array_equal(x, y) for x, y in zip(got, got2)) bad += 0 if ok2 else 1 print(f" kernel eager x2: {'stable' if ok2 else 'UNSTABLE'}", flush=True) if a.trace: for name, fn, chk in (("stock", stock, None), ("kernel", kern, got)): for o in fn(): ttnn.deallocate(o) tid = ttnn.begin_trace_capture(dev, cq_id=0) outs = fn() ttnn.end_trace_capture(dev, tid, cq_id=0) ts = [] for i in range(max(2, a.reps)): t0 = time.perf_counter() ttnn.execute_trace(dev, tid, cq_id=0, blocking=True) ts.append((time.perf_counter() - t0) * 1e3) if i < 2 and chk is not None: okt = all(np.array_equal(x, y) for x, y in zip(host(outs), chk)) bad += 0 if okt else 1 print(f" {name} trace call {i + 1}: {'IDENTICAL' if okt else 'DIFFERENT'}", flush=True) ttnn.release_trace(dev, tid) print(f" {name} traced replay p50 {np.median(ts):.3f} ms (min {np.min(ts):.3f})", flush=True) ttnn.close_device(dev) print("RESULT", "PASS" if bad == 0 else f"FAIL ({bad})") return 0 if bad == 0 else 1 if __name__ == "__main__": sys.exit(main())