File size: 3,839 Bytes
e91e989 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/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())
|