File size: 6,519 Bytes
8eeb550 | 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #!/usr/bin/env python3
"""K12 rerow bring-up probe (OPT round 5 R5.1; hang protocol: smallest shapes first, eager x2, then a 2-call trace).
``ttaw.ops.rerow.rerow`` (the ROW_MAJOR page re-cut as one data-movement generic_op) against ``ttnn.reshape`` on
random data: the outputs must be bit-identical (compared as raw bits). ``--scale frame`` uses the METEOR frame's
RM reshape shapes (Resize2d's H-pass input and tail) and times both forms as a traced replay of ``--calls`` calls.
``--resize`` also compares ``Resize2d(rerow="kernel")`` with ``rerow="stock"`` at the METEOR call sites.
METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/rerow_probe.py --scale small --trace
PYTHONPATH=<ttaw parent> selects the ttaw under test (default: the bundle's vendored copy).
"""
from __future__ import annotations
import argparse
import sys
import time
import numpy as np
# (name, (rows_in, width_in), (rows_out, width_out), dtype)
SMALL = [("split_f32", (8, 32 * 160), (256, 160), "f32"),
("split_bf16_odd", (4, 500 * 96), (2000, 96), "bf16"),
("join_f32", (2000, 96), (4, 48000), "f32"),
("split_k", (3, 7 * 64 * 32), (21 * 32, 64), "f32"), # chunks smaller than a big page + tail chunk
("join_bf16", (96, 64), (2, 3072), "bf16")]
FRAME = [("fpn_tail", (864, 30720), (165888, 160), "f32"),
("fpn_hin", (82944, 160), (432, 30720), "f32"),
("lift_tail", (800, 48000), (400000, 96), "bf16"),
("lift_hin", (200000, 96), (400, 48000), "f32"),
("seg_tail", (864, 18432), (165888, 96), "f32"),
("lane_tail", (800, 32000), (400000, 64), "bf16"),
("det_tail", (400, 32000), (100000, 128), "f32")]
RESIZE = [("lift", (400, 250), (800, 500), 96, 1, "bfloat16"),
("fpn_x4", (27, 48), (108, 192), 160, 8, "float32"),
("dec_u2", (400, 250), (800, 500), 64, 1, "bfloat16")]
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("--resize", action="store_true")
ap.add_argument("--calls", type=int, default=10)
ap.add_argument("--reps", type=int, default=5)
a = ap.parse_args()
import torch
import ttnn
try:
from ttaw.ops import rerow as rr
from ttaw.ops import conv as tconv
from ttaw.ops import upsample as tup
from ttaw.device import open_device
except ImportError:
from tt_meteor.ttaw.ops import rerow as rr
from tt_meteor.ttaw.ops import conv as tconv
from tt_meteor.ttaw.ops import upsample as tup
from tt_meteor.ttaw.device import open_device
print("ttaw from", rr.__file__, flush=True)
dev = open_device(dispatch="eth", trace_region_size=64 << 20)
rng = np.random.default_rng(0)
bad = 0
def bits(t):
x = ttnn.to_torch(t)
return x.view(torch.int32).numpy() if x.dtype == torch.float32 else x.view(torch.int16).numpy()
spec = SMALL if a.scale == "small" else FRAME
for name, (ri, wi), (ro, wo), dt in spec:
x = rng.normal(size=(1, 1, ri, wi)).astype(np.float32)
tdt = ttnn.float32 if dt == "f32" else ttnn.bfloat16
t = ttnn.from_torch(torch.from_numpy(x), dtype=tdt, layout=ttnn.ROW_MAJOR_LAYOUT, device=dev,
memory_config=ttnn.DRAM_MEMORY_CONFIG)
plan = rr.rerow_plan(ri, wi, ro, wo, 4 if dt == "f32" else 2)
print(f"[{name}] {(ri, wi)} -> {(ro, wo)} {dt} plan={plan}", flush=True)
ref = ttnn.reshape(t, (1, 1, ro, wo))
rb = bits(ref)
for i in range(2):
y = rr.rerow(t, (1, 1, ro, wo))
ok = bool(np.array_equal(bits(y), rb)) and tuple(int(s) for s in y.shape) == (1, 1, ro, wo)
bad += not ok
print(f" eager{i + 1}: {'identical' if ok else 'DIFFERENT'}", flush=True)
ttnn.deallocate(y)
if a.trace:
for mode in ("kernel", "stock"):
box = {}
def body():
box["y"] = [rr.rerow(t, (1, 1, ro, wo), mode=mode) for _ in range(a.calls)]
body()
for y in box["y"]:
ttnn.deallocate(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 = all(np.array_equal(bits(y), rb) for y in box["y"][:2])
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 / a.calls)
print(f" {mode}: ms per call (traced, {a.calls} calls) p50 {np.median(ts):.3f} min {min(ts):.3f}",
flush=True)
ttnn.release_trace(dev, tid)
for y in box["y"]:
ttnn.deallocate(y)
ttnn.deallocate(ref)
ttnn.deallocate(t)
if a.resize:
for name, in_hw, out_hw, c, n, out_dt in RESIZE:
x = rng.normal(size=(1, 1, n * in_hw[0] * in_hw[1], c)).astype(np.float32)
t = ttnn.from_torch(torch.from_numpy(x), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev,
memory_config=ttnn.DRAM_MEMORY_CONFIG)
fm = tconv.FeatureMap(t, n, in_hw[0], in_hw[1], c)
outs = {}
for mode in ("stock", "kernel"):
rs = tup.Resize2d(in_hw, out_hw, channels=c, batch=n, output_dtype=out_dt, w_pass="flat",
rerow=mode)
y = rs(fm)
outs[mode] = bits(y.tensor)
ttnn.deallocate(y.tensor)
rs.release()
ok = bool(np.array_equal(outs["stock"], outs["kernel"]))
bad += not ok
print(f"[resize {name}] {in_hw} -> {out_hw} c{c} n{n} {out_dt}: {'identical' if ok else 'DIFFERENT'}",
flush=True)
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())
|