Download code/tt_depthpro2/tests/mk_fov_check.py from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 5.7 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/mk_fov_check.py
- Command line
-
hf download hf://changh95/DepthPro2/code/tt_depthpro2/tests/mk_fov_check.py
-
curl -L -o mk_fov_check.py https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/mk_fov_check.py
5.7 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """FOV stage megakernel engine (tt/mk_fov) vs the goldens (SPEC §5 FOV gate: f_px within max(1 %, 2 x official bf16 | |
| drift)) and vs the ttnn FOV engine on the same fpatches (logits, traced device time); n <= T frames on a T program. | |
| bin/devrun -t 1800 -- python -u bundles/depthpro2/code/tt_depthpro2/tests/mk_fov_check.py | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import time | |
| import torch | |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) | |
| from tt_depthpro2.reference import model as R # noqa: E402 | |
| from tt_depthpro2.tests import baseline_helpers as BH # noqa: E402 | |
| from tt_depthpro2.tests import mk_enc_ref as H # noqa: E402 | |
| from tt_depthpro2.tt import StageShape # noqa: E402 | |
| from tt_depthpro2.tt.mk_enc import arena as A # noqa: E402 | |
| CASES = ("img_vga", "img_384x512", "clip4_240x320", "img_375x500_pad") | |
| def trace_ms(dev, fn, reps=5): | |
| import ttnn | |
| fn() | |
| ttnn.synchronize_device(dev) | |
| tid = ttnn.begin_trace_capture(dev, cq_id=0) | |
| out = fn() | |
| ttnn.end_trace_capture(dev, tid, cq_id=0) | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=True) | |
| res = ttnn.to_torch(out).float().clone() | |
| t0 = time.perf_counter() | |
| for _ in range(reps): | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=False) | |
| ttnn.synchronize_device(dev) | |
| ms = 1e3 * (time.perf_counter() - t0) / reps | |
| same = torch.equal(res, ttnn.to_torch(out).float()) | |
| ttnn.release_trace(dev, tid) | |
| return res, ms, same | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--cases", default=",".join(CASES)) | |
| ap.add_argument("--no-ttnn", action="store_true") | |
| ap.add_argument("--bench-t4", action="store_true", help="also time VGA T=4 (mk and ttnn)") | |
| args = ap.parse_args() | |
| import ttnn | |
| from tt_depthpro2.tt import mk_fov, ttnn_fov | |
| dev = ttnn.open_device(device_id=0, dispatch_core_config=ttnn.DispatchCoreConfig(ttnn.DispatchCoreType.ETH), | |
| l1_small_size=BH.L1_SMALL_SIZE, trace_region_size=64 << 20) | |
| fw = R.load_safetensors(os.path.join(BH.fov_dir(), "model.safetensors")) | |
| ok = True | |
| try: | |
| wm = mk_fov.Engine.prepare_weights(dev, fw) | |
| wt = None if args.no_ttnn else ttnn_fov.Engine.prepare_weights(dev, fw) | |
| def dev_in(y): | |
| np_ = -(-(y.shape[-2] // 16) * (y.shape[-1] // 16) // 32) * 32 | |
| p = A.patchify(y).to(torch.bfloat16).reshape(1, 1, -1, 768) | |
| assert p.shape[-2] == y.shape[0] * np_ | |
| return ttnn.from_torch(p, dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev, | |
| memory_config=ttnn.DRAM_MEMORY_CONFIG) | |
| for case in args.cases.split(","): | |
| g, _ = BH.load_golden(case, "S") | |
| x = g["x_norm"].float() | |
| T, _, h, w = x.shape | |
| y = R.fov_resize(x) | |
| shape = StageShape(T, y.shape[-2], y.shape[-1]) | |
| fp = dev_in(y) | |
| em = mk_fov.Engine(dev, wm, shape) | |
| lm, ms_m, same_m = trace_ms(dev, lambda: em.run(fp)) | |
| lm = lm.reshape(T, 2) | |
| deg = R.fov_logits_to_deg(lm, (h, w), tuple(y.shape[-2:])) | |
| f = float(R.median_focal_length_px(deg, h, w)) | |
| fg = float(g["focallength_px"]) | |
| f_rel = abs(f / fg - 1) | |
| budget, why = BH.fov_budget(case) | |
| rec = {"case": case, "T": T, "fov_hw": list(y.shape[-2:]), "engine": "mk", "f_px": f, "f_px_golden": fg, | |
| "f_rel": f_rel, "f_budget": budget, "budget_from": why, | |
| "fov_deg_maxabs": float((deg - g["fov_deg"].reshape(deg.shape)).abs().max()), | |
| "fov_ms": ms_m, "replay_bitexact": same_m, "passed": f_rel <= budget and same_m} | |
| if wt is not None: | |
| et = ttnn_fov.Engine(dev, wt, shape) | |
| lt, ms_t, _ = trace_ms(dev, lambda: et.run(fp)) | |
| lt = lt.reshape(T, 2) | |
| degt = R.fov_logits_to_deg(lt, (h, w), tuple(y.shape[-2:])) | |
| ft = float(R.median_focal_length_px(degt, h, w)) | |
| rec.update(ttnn_f_rel=abs(ft / fg - 1), ttnn_fov_ms=ms_t, | |
| logits_maxabs_mk_vs_ttnn=float((lm - lt).abs().max())) | |
| del et | |
| # n < T frames on this T program (frames independent): zero frames in the unused slots | |
| if T > 1: | |
| y2 = y.clone() | |
| y2[2:] = 0 | |
| l2 = ttnn.to_torch(em.run(dev_in(y2))).float().reshape(T, 2)[:2] | |
| rec["n2_of_T_maxabs_vs_full"] = float((l2 - lm[:2]).abs().max()) | |
| ok = ok and rec["passed"] | |
| rec["time"] = time.strftime("%Y-%m-%d %H:%M:%S") | |
| print(json.dumps(rec), flush=True) | |
| H.append_log("fov.jsonl", rec) | |
| del em, fp | |
| if args.bench_t4: | |
| g, _ = BH.load_golden("img_vga", "S") | |
| y = R.fov_resize(g["x_norm"].float()).repeat(4, 1, 1, 1) | |
| shape = StageShape(4, y.shape[-2], y.shape[-1]) | |
| fp = dev_in(y) | |
| em = mk_fov.Engine(dev, wm, shape) | |
| _, ms_m, same_m = trace_ms(dev, lambda: em.run(fp)) | |
| rec = {"case": "img_vga x4", "T": 4, "fov_hw": list(y.shape[-2:]), "fov_ms": ms_m, "replay_bitexact": same_m} | |
| if wt is not None: | |
| et = ttnn_fov.Engine(dev, wt, shape) | |
| _, ms_t, _ = trace_ms(dev, lambda: et.run(fp)) | |
| rec["ttnn_fov_ms"] = ms_t | |
| print(json.dumps(rec), flush=True) | |
| finally: | |
| ttnn.close_device(dev) | |
| print("FOV PASS" if ok else "FOV FAIL", flush=True) | |
| if __name__ == "__main__": | |
| main() | |