Download code/scripts/refine_tail_probe.py from changh95/meteor-p150: direct link, hf CLI and curl.
- Browser
- Download file 4.35 kB
-
https://huggingface.co/changh95/meteor-p150/resolve/main/code/scripts/refine_tail_probe.py
- Command line
-
hf download hf://changh95/meteor-p150/code/scripts/refine_tail_probe.py
-
curl -L -o refine_tail_probe.py https://huggingface.co/changh95/meteor-p150/resolve/main/code/scripts/refine_tail_probe.py
4.35 kB
| #!/usr/bin/env python3 | |
| """REF_TAIL probe (OPT2 round 6 R6.6): the refiners' residual tails on fp32 maps, | |
| ``out = c + s * tanh(res * (1 / s))`` (seg: [400000, 9], s = 8; box: [100000, 8], s = 6, with c = min(max(x, lo), hi)), | |
| as two BinaryNg programs (``fused``: multiply with a TANH post-activation; add with the s-multiply as the b-side | |
| pre-activation) instead of four (``stock``: multiply, tanh, multiply, add), and the box clamp as one ``ttnn.clamp`` | |
| with tensor bounds instead of maximum + minimum. Checked bit-identical, then timed traced. | |
| METEOR_DEVICE_OK=1 bin/devrun -t 300 -- python -u code/scripts/refine_tail_probe.py --rows 640 | |
| METEOR_DEVICE_OK=1 bin/devrun -t 600 -- python -u code/scripts/refine_tail_probe.py --rows 400000 --trace | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| import time | |
| import numpy as np | |
| def main() -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--rows", type=int, default=640) | |
| ap.add_argument("--trace", action="store_true") | |
| ap.add_argument("--reps", type=int, default=10) | |
| a = ap.parse_args() | |
| import torch | |
| import ttnn | |
| from tt_meteor.ttaw.device import open_device | |
| dev = open_device(dispatch="eth", trace_region_size=64 << 20) | |
| rng = np.random.default_rng(0) | |
| n = a.rows | |
| td = lambda x: ttnn.from_torch(torch.from_numpy(np.ascontiguousarray(x)), dtype=ttnn.float32, | |
| layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.DRAM_MEMORY_CONFIG) | |
| lc = td((rng.standard_normal((1, 1, n, 9)) * 10).astype(np.float32)) | |
| res = td((rng.standard_normal((1, 1, n, 9)) * 12).astype(np.float32)) | |
| xb = td((rng.standard_normal((1, 1, n // 4, 8)) * 30).astype(np.float32)) | |
| hi = np.array([8.0] * 2 + [5.0] * 6, np.float32).reshape(1, 1, 1, 8) | |
| lo_d, hi_d = td(-hi), td(hi) | |
| tanh_act = [ttnn.UnaryWithParam(ttnn.UnaryOpType.TANH)] | |
| def tail_stock(c, r, s): | |
| return ttnn.add(c, ttnn.multiply(ttnn.tanh(ttnn.multiply(r, 1.0 / s)), s)) | |
| def tail_fused(c, r, s): | |
| t = ttnn.multiply(r, 1.0 / s, activations=tanh_act) | |
| y = ttnn.add(c, t, input_tensor_b_activations=[ttnn.UnaryWithParam(ttnn.UnaryOpType.MUL_UNARY_SFPU, float(s))]) | |
| ttnn.deallocate(t) | |
| return y | |
| cases = { | |
| "seg_stock": lambda: tail_stock(lc, res, 8.0), | |
| "seg_fused": lambda: tail_fused(lc, res, 8.0), | |
| "clamp_stock": lambda: ttnn.minimum(ttnn.maximum(xb, lo_d), hi_d), | |
| "clamp_fused": lambda: ttnn.clamp(xb, lo_d, hi_d), | |
| } | |
| got = {} | |
| bad = 0 | |
| for name, fn in cases.items(): | |
| try: | |
| y = fn() | |
| except Exception as e: # noqa: BLE001 | |
| print(f" {name}: FAILED {str(e).splitlines()[0][:200]}", flush=True) | |
| bad += 1 | |
| continue | |
| got[name] = ttnn.to_torch(y).view(torch.int32).numpy() | |
| ttnn.deallocate(y) | |
| for k in ("seg", "clamp"): | |
| if f"{k}_stock" in got and f"{k}_fused" in got: | |
| same = np.array_equal(got[f"{k}_stock"], got[f"{k}_fused"]) | |
| bad += 0 if same else 1 | |
| print(f"[{n}] {k}: fused vs stock {'IDENTICAL' if same else 'DIFFERENT (%d)' % np.sum(got[k + '_stock'] != got[k + '_fused'])}", | |
| flush=True) | |
| if a.trace: | |
| for name, fn in cases.items(): | |
| if name not in got: | |
| continue | |
| ttnn.deallocate(fn()) | |
| 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: | |
| okt = np.array_equal(ttnn.to_torch(y).view(torch.int32).numpy(), got[name]) | |
| bad += 0 if okt else 1 | |
| print(f" {name} trace call {i + 1}: {'IDENTICAL' if okt else 'DIFFERENT'}", flush=True) | |
| ttnn.release_trace(dev, tid) | |
| ttnn.deallocate(y) | |
| print(f" {name} traced p50 {np.median(ts):.3f} ms", 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()) | |