METEOR-2-p150 / code /scripts /dev_bench_argmax.py
changh95's picture
tt-model push meteor-2-p150 (container)
0f131e7 verified
Raw History Blame Contribute Delete
2.69 kB
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""ttnn.argmax over the channel rows of the lane / occupancy logits (bin/devrun)."""
import sys, time
import numpy as np
def main():
import torch
import ttnn
sys.path.insert(0, "/work")
from tt_meteor2.ttaw.device import close_device, open_device
dev = open_device(0, dispatch="eth", l1_small_size=32768, trace_region_size=64 << 20)
try:
def bench(name, fn, ref=None, n=5):
try:
r = fn()
ttnn.synchronize_device(dev)
tid = ttnn.begin_trace_capture(dev, cq_id=0)
r = fn()
ttnn.end_trace_capture(dev, tid, cq_id=0)
ts = []
for _ in range(n):
t = time.perf_counter()
ttnn.execute_trace(dev, tid, cq_id=0, blocking=False)
ttnn.synchronize_device(dev)
ts.append((time.perf_counter() - t) * 1e3)
t = time.perf_counter()
got = ttnn.to_torch(r)
rd = (time.perf_counter() - t) * 1e3
ttnn.release_trace(dev, tid)
ok = "" if ref is None else f" agree {(got.reshape(-1)[:ref.numel()].long() == ref.reshape(-1)).float().mean().item():.6f}"
print(f"{name:52s} {np.median(ts):7.2f} ms out {tuple(got.shape)} {got.dtype} read {rd:.1f} ms{ok}", flush=True)
except Exception as e: # noqa: BLE001
print(f"{name:52s} FAILED {type(e).__name__}: {str(e)[:200]}", flush=True)
for rows, ch, dt in ((400000, 16, ttnn.float32), (400000, 16, ttnn.bfloat16), (640000, 16, ttnn.float32)):
x = torch.randn(1, 1, rows, ch) * 10
x[..., 9:] = -30000 if rows == 400000 else x[..., 9:]
ref = x.argmax(-1)
T = ttnn.from_torch(x, dtype=dt, layout=ttnn.TILE_LAYOUT, device=dev)
bench(f"tile->rm + argmax {rows}x{ch} {dt}", lambda: ttnn.argmax(ttnn.to_layout(T, ttnn.ROW_MAJOR_LAYOUT), dim=-1), ref)
bench(f"tile->rm + argmax multicore {rows}x{ch} {dt}",
lambda: ttnn.argmax(ttnn.to_layout(T, ttnn.ROW_MAJOR_LAYOUT), dim=-1, use_multicore=True), ref)
x = torch.randn(1, 1, 100000, 39)
T = ttnn.from_torch(x, dtype=ttnn.float32, layout=ttnn.TILE_LAYOUT, device=dev)
bench("traj fp32 tile [100000, 39] (read only)", lambda: ttnn.typecast(T, ttnn.float32))
bench("traj -> bf16 rm", lambda: ttnn.to_layout(ttnn.typecast(T, ttnn.bfloat16), ttnn.ROW_MAJOR_LAYOUT))
finally:
close_device(dev)
if __name__ == "__main__":
main()