#!/usr/bin/env python3 """STEM_POOL bring-up probe (OPT round 3 R3.2; hang protocol: smallest shape first, eager x2, then a 2-call trace). The ``tt.pool.maxpool3x3s2_k`` kernel against the stock chain it replaces (``tt.layers.maxpool3x3s2``) on random non-negative fp32 maps with exact zeros, -0.0 and ties, bit for bit; then both timed as traced replays. METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/maxpool_probe.py --shape 1,4,64,32 # smallest METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/maxpool_probe.py --shape 8,216,384,64 --trace """ from __future__ import annotations import argparse import sys import time import numpy as np def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--shape", default="1,4,64,32", help="N,H,W,C") ap.add_argument("--trace", action="store_true", help="also a 2-call trace and a timing of both forms") ap.add_argument("--reps", type=int, default=10) a = ap.parse_args() import torch import ttnn from tt_meteor.tt import pool from tt_meteor.tt.layers import maxpool3x3s2 from tt_meteor.ttaw.device import open_device from tt_meteor.ttaw.ops.conv import FeatureMap n, h, w, c = (int(v) for v in a.shape.split(",")) dev = open_device(dispatch="eth", trace_region_size=64 << 20) rng = np.random.default_rng(0) x = np.maximum(rng.standard_normal((n * h * w, c)).astype(np.float32), 0.0) # ~half exact zeros x[rng.random(x.shape) < 0.05] = np.float32(1.0) # ties x[rng.random(x.shape) < 0.05] = np.float32(-0.0) # negative zeros xt = ttnn.from_torch(torch.from_numpy(x.reshape(1, 1, -1, c)), dtype=ttnn.float32, layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.DRAM_MEMORY_CONFIG) fm = FeatureMap(xt, n, h, w, c) assert pool.supported(fm), "the kernel does not take this map" bad = 0 def host(t): return ttnn.to_torch(t).numpy().reshape(-1, c).view(np.uint32) # numpy reference (pad +0.0, then the float max; -0.0 < +0.0) xr = x.reshape(n, h, w, c) xp = np.zeros((n, h + 2, w + 2, c), np.float32) xp[:, 1:-1, 1:-1] = xr oh, ow = h // 2, w // 2 ref = np.full((n, oh, ow, c), -np.inf, np.float32) for dy in range(3): for dx in range(3): win = xp[:, dy:dy + 2 * oh:2, dx:dx + 2 * ow:2] ref = np.where(win > ref, win, np.where((win == ref) & ~np.signbit(win), win, ref)) def run(fn, name): nonlocal bad outs = [] for _ in range(2): # eager x2 y = fn() outs.append(host(y.tensor)) ttnn.deallocate(y.tensor) same = np.array_equal(outs[0], outs[1]) print(f" {name}: eager x2 {'stable' if same else 'UNSTABLE'}", flush=True) bad += 0 if same else 1 return outs[0] stock = run(lambda: maxpool3x3s2(fm), "stock") kern = run(lambda: pool.maxpool3x3s2_k(fm), "kernel") same = np.array_equal(stock, kern) bad += 0 if same else 1 nd = int(np.sum(stock != kern)) print(f"[{a.shape}] kernel vs stock: {'IDENTICAL' if same else f'DIFFERENT ({nd} words)'}", flush=True) rd = np.array_equal(ref.reshape(-1, c).view(np.uint32), stock) print(f" stock vs numpy reference (incl. zero signs): {'IDENTICAL' if rd else 'DIFFERENT'}", flush=True) if not same: idx = np.argwhere(stock != kern)[:5] for i, j in idx: print(f" row {i} ch {j}: stock {stock[i, j]:#010x} kernel {kern[i, j]:#010x}") if a.trace: for name, fn in (("stock", lambda: maxpool3x3s2(fm)), ("kernel", lambda: pool.maxpool3x3s2_k(fm))): tid = ttnn.begin_trace_capture(dev, cq_id=0) y = 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: r = host(y.tensor) ok = np.array_equal(r, stock) bad += 0 if ok else 1 print(f" {name} trace call {i + 1}: {'IDENTICAL' if ok else 'DIFFERENT'} to stock eager", flush=True) ttnn.release_trace(dev, tid) ttnn.deallocate(y.tensor) 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())