Download code/scripts/argmax_probe.py from changh95/meteor-p150: direct link, hf CLI and curl.
- Browser
- Download file 4.72 kB
-
https://huggingface.co/changh95/meteor-p150/resolve/main/code/scripts/argmax_probe.py
- Command line
-
hf download hf://changh95/meteor-p150/code/scripts/argmax_probe.py
-
curl -L -o argmax_probe.py https://huggingface.co/changh95/meteor-p150/resolve/main/code/scripts/argmax_probe.py
4.72 kB
| #!/usr/bin/env python3 | |
| """ARGMAX bring-up probe (OPT round 5 R5.3; hang protocol: smallest shapes first, eager x2, then a 2-call trace). | |
| ``tt.argmax.argmax_rows`` against the stock ``tt.layers.channel_argmax`` (transpose + TILE reshape + ArgMaxNC) on | |
| fp32 TILE maps with exact ties (values quantized to a few levels), +-0.0, wide ranges and the frame's shapes | |
| (``--scale frame``: lane 400,000 x 9, seg2d 165,888 x 21, depth 165,888 x 64); the outputs must be identical, and the | |
| traced timings of both forms are printed. | |
| METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/argmax_probe.py --scale small --trace | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| import time | |
| import numpy as np | |
| SMALL = [("c9", 64, 9), ("c21", 96, 21), ("c64", 128, 64), ("c40", 32, 40), ("c1", 32, 1), ("c16", 64, 16), | |
| ("c17", 64, 17), ("c33", 64, 33)] | |
| FRAME = [("lane", 400000, 9), ("seg2d", 165888, 21), ("depth", 165888, 64)] | |
| def data(rng, rows, c, kind): | |
| if kind == "ties": | |
| x = rng.integers(-3, 4, (rows, c)).astype(np.float32) * np.float32(0.5) | |
| x[rng.random((rows, c)) < 0.05] = -0.0 | |
| elif kind == "wide": | |
| x = (rng.normal(size=(rows, c)) * 10.0 ** rng.integers(-6, 6, (rows, c))).astype(np.float32) | |
| else: | |
| x = rng.normal(size=(rows, c)).astype(np.float32) | |
| return x | |
| def main() -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--scale", default="small", choices=("small", "frame")) | |
| ap.add_argument("--trace", action="store_true") | |
| ap.add_argument("--reps", type=int, default=5) | |
| ap.add_argument("--version", type=int, default=1, choices=(1, 2), help="2: kernels/argmax_rows2.cpp (OPT2 R4.2)") | |
| a = ap.parse_args() | |
| import torch | |
| import ttnn | |
| from tt_meteor.tt.argmax import argmax_rows | |
| from tt_meteor.tt.layers import channel_argmax | |
| from tt_meteor.ttaw.device import open_device | |
| dev = open_device(dispatch="eth", trace_region_size=64 << 20) | |
| rng = np.random.default_rng(0) | |
| bad = 0 | |
| def host(t): | |
| return ttnn.to_torch(t).numpy().astype(np.int64).reshape(-1) | |
| for name, rows, c in (SMALL if a.scale == "small" else FRAME): | |
| for kind in ("normal", "ties", "wide"): | |
| x = data(rng, rows, c, kind) | |
| t = ttnn.from_torch(torch.from_numpy(x).reshape(1, 1, rows, c), dtype=ttnn.float32, | |
| layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.DRAM_MEMORY_CONFIG) | |
| ref = channel_argmax(t, c, rows, "stock") | |
| rr = host(ref) | |
| npref = np.argmax(np.where(x == 0, np.float32(0), x), axis=1) # first max, -0 == +0 | |
| for i in range(2): | |
| y = argmax_rows(t, c, rows, version=a.version) | |
| yy = host(y) | |
| ok = bool(np.array_equal(yy, rr)) and tuple(int(s) for s in y.shape) == tuple(int(s) for s in ref.shape) | |
| bad += not ok | |
| print(f"[{name} {kind}] eager{i + 1}: {'identical' if ok else 'DIFFERENT'} " | |
| f"(vs numpy: {int((yy != npref).sum())} / stock vs numpy: {int((rr != npref).sum())})", | |
| flush=True) | |
| ttnn.deallocate(y) | |
| if a.trace and kind == "normal": | |
| for mode in (("fast", "kernel") if a.version == 2 else ("kernel",)) + ("stock",): | |
| box = {} | |
| def body(): | |
| box["y"] = channel_argmax(t, c, rows, mode) | |
| body() | |
| ttnn.deallocate(box["y"]) | |
| tid = ttnn.begin_trace_capture(dev, cq_id=0) | |
| body() | |
| ttnn.end_trace_capture(dev, tid, cq_id=0) | |
| for i in range(2): | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=True) | |
| ok = bool(np.array_equal(host(box["y"]), rr)) | |
| bad += not ok | |
| print(f" {mode} trace{i}: {'identical' if ok else 'DIFFERENT'}", flush=True) | |
| 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) | |
| print(f" {mode}: traced ms p50 {np.median(ts):.3f} min {min(ts):.3f}", flush=True) | |
| ttnn.release_trace(dev, tid) | |
| ttnn.deallocate(box["y"]) | |
| ttnn.deallocate(ref) | |
| ttnn.deallocate(t) | |
| ttnn.close_device(dev) | |
| print("ALL IDENTICAL" if bad == 0 else f"{bad} DIFFERENT") | |
| return 0 if bad == 0 else 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |