Download code/scripts/fused_chain_probe.py from changh95/meteor-p150: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/changh95/meteor-p150/resolve/main/code/scripts/fused_chain_probe.py
- Command line
-
hf download hf://changh95/meteor-p150/code/scripts/fused_chain_probe.py
-
curl -L -o fused_chain_probe.py https://huggingface.co/changh95/meteor-p150/resolve/main/code/scripts/fused_chain_probe.py
3.84 kB
| #!/usr/bin/env python3 | |
| """The fused BEV map's layout chain (fused = raw + t, then its shared ROW_MAJOR / 4-D TILE copies), stock variants | |
| timed as traced replays and compared bit for bit (development probe for the MK-TF follow-ups). | |
| cur: t (ROW_MAJOR, the temporal fuse block's output) -> tilize flat; fused = add(raw, t); fused_rm = untilize; | |
| fused4 = tilize(4-D view of fused_rm) | |
| rm: fused_rm = add(raw_rm, t) on ROW_MAJOR operands; fused = tilize(fused_rm); fused4 = tilize(4-D view) | |
| METEOR_DEVICE_OK=1 bin/devrun -t 300 -- python -u code/scripts/fused_chain_probe.py --hw 800,500 | |
| """ | |
| 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="800,500") | |
| ap.add_argument("--reps", type=int, default=10) | |
| a = ap.parse_args() | |
| import torch | |
| import ttnn | |
| from tt_meteor.ttaw.device import open_device | |
| h, w = (int(v) for v in a.hw.split(",")) | |
| r = h * w | |
| 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, 1, r, 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) | |
| raw_rm = ttnn.from_torch(raw_h, dtype=ttnn.bfloat16, layout=ttnn.ROW_MAJOR_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) | |
| def cur(): | |
| t = ttnn.to_layout(t_rm, 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 rmv(): | |
| frm = ttnn.add(raw_rm, t_rm, memory_config=D) | |
| f = ttnn.to_layout(frm, ttnn.TILE_LAYOUT) | |
| f4 = ttnn.to_layout(ttnn.reshape(frm, (1, h, w, 96)), ttnn.TILE_LAYOUT) | |
| return f, frm, f4 | |
| def host(ts): | |
| return [ttnn.to_torch(t).float().numpy().reshape(r, 96) for t in ts] | |
| bad = 0 | |
| ref = None | |
| for name, fn in (("cur", cur), ("rm", rmv)): | |
| try: | |
| outs = fn() | |
| except Exception as e: # noqa: BLE001 | |
| print(f" {name}: FAILED {str(e).splitlines()[0][:200]}", flush=True) | |
| bad += 1 | |
| continue | |
| print(f" {name}: layouts {[str(o.layout) for o in outs]} shapes {[list(o.shape) for o in outs]}", flush=True) | |
| hv = host(outs) | |
| if ref is None: | |
| ref = hv | |
| else: | |
| same = [np.array_equal(x, y) for x, y in zip(ref, hv)] | |
| print(f" {name} vs cur: fused / fused_rm / fused4 identical {same}", flush=True) | |
| bad += 0 if all(same) else 1 | |
| for o in outs: | |
| ttnn.deallocate(o) | |
| outs = fn() | |
| for o in outs: | |
| 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 _ in range(a.reps): | |
| t0 = time.perf_counter() | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=True) | |
| ts.append((time.perf_counter() - t0) * 1e3) | |
| ttnn.release_trace(dev, tid) | |
| for o in outs: | |
| ttnn.deallocate(o) | |
| 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()) | |