Download code/tt_depthpro2/tests/mk_enc_ab.py from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/mk_enc_ab.py
- Command line
-
hf download hf://changh95/DepthPro2/code/tt_depthpro2/tests/mk_enc_ab.py
-
curl -L -o mk_enc_ab.py https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/mk_enc_ab.py
3.71 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """A/B of two DEPTHPRO2_MK_ENC_OPTS settings on the same patches: traced time of each, taps bitwise / PCC between | |
| them and vs the golden taps (when the case has them). Weights / goldens: DEPTHPRO2_TEST_WEIGHTS / DEPTHPRO2_GOLDEN. | |
| bin/devrun -t 1200 -- python -u <pkg>/tests/mk_enc_ab.py --size S --case img_vga --a inx32=0 --b inx32=1 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| 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 mk_enc_ref as H # noqa: E402 | |
| from tt_depthpro2.tt.mk_enc import arena as A # noqa: E402 | |
| from tt_depthpro2.tt.mk_enc.program import MkEnc, MkEncWeights # noqa: E402 | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--size", default="S") | |
| ap.add_argument("--case", default="img_vga") | |
| ap.add_argument("--a", default="") | |
| ap.add_argument("--b", default="") | |
| ap.add_argument("--reps", type=int, default=5) | |
| ap.add_argument("--resize", type=int, nargs=3, default=None, help="H W T: resized / repeated frames + noise") | |
| args = ap.parse_args() | |
| import ttnn | |
| g = H.load_case(args.case, args.size) | |
| x = R.pad_to_multiple(g["x_norm"].float()) | |
| if args.resize: | |
| hh, ww, tt = args.resize | |
| x = torch.nn.functional.interpolate(x[:1], size=(hh, ww), mode="bilinear", align_corners=False) | |
| x = x.repeat(tt, 1, 1, 1) + 0.1 * torch.randn(tt, 3, hh, ww, generator=torch.Generator().manual_seed(0)) | |
| T, _, hp, wp = x.shape | |
| dev = ttnn.open_device(device_id=0, dispatch_core_config=ttnn.DispatchCoreConfig(ttnn.DispatchCoreType.ETH), | |
| trace_region_size=16 << 20) | |
| try: | |
| wts = MkEncWeights(dev, H.load_weights(args.size), args.size, "depth") | |
| patches = ttnn.from_torch(A.patchify(x).to(torch.bfloat16).reshape(1, 1, -1, 768), dtype=ttnn.bfloat16, | |
| layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.DRAM_MEMORY_CONFIG) | |
| res = {} | |
| for tag, opts in (("A", args.a), ("B", args.b)): | |
| os.environ["DEPTHPRO2_MK_ENC_OPTS"] = opts | |
| mk = MkEnc(dev, wts, args.size, T, hp // 16, wp // 16) | |
| mk.run(patches) | |
| ttnn.synchronize_device(dev) | |
| raw = [ttnn.to_torch(t).clone() for t in mk.taps] | |
| tid = ttnn.begin_trace_capture(dev, cq_id=0) | |
| mk.run(patches) | |
| ttnn.end_trace_capture(dev, tid, cq_id=0) | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=True) | |
| t0 = time.perf_counter() | |
| for _ in range(args.reps): | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=False) | |
| ttnn.synchronize_device(dev) | |
| ms = 1e3 * (time.perf_counter() - t0) / args.reps | |
| same = all(torch.equal(a, ttnn.to_torch(t)) for a, t in zip(raw, mk.taps)) | |
| ttnn.release_trace(dev, tid) | |
| res[tag] = raw | |
| msg = f"{tag} [{opts or 'default'}] {mk.geo.summary()}: {ms:.3f} ms, replay bitexact {same}" | |
| if "tap0" in g and not args.resize: | |
| d = [r.float().reshape(T, mk.geo.Np, -1)[:, :mk.geo.N] for r in raw] | |
| msg += " | vs golden rel " + " ".join(f"{H.rel_err(d[i], g[f'tap{i}'].float()):.3e}" for i in range(4)) | |
| print(msg, flush=True) | |
| del mk | |
| pc = [H.pcc(a.float(), b.float()) for a, b in zip(res["A"], res["B"])] | |
| print(f"A vs B taps pcc {[round(p, 7) for p in pc]}", flush=True) | |
| finally: | |
| ttnn.close_device(dev) | |
| if __name__ == "__main__": | |
| main() | |