File size: 4,522 Bytes
b7e49b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""End-to-end SPEC section 5 gates with the DEC megakernel: DepthPro2Device(engine enc=<enc>, dec=mk, fov=ttnn) on
the golden cases of the given sizes (inverse-depth PCC >= 0.999 -- >= 0.99 under the SPEC section 5 flat-scene rule,
golden valid inverse-depth cv < 0.15 --, depth AbsRel_valid <= max(2 x official bf16 drift,
0.01) with the baseline's emulated budgets for img_375x500_pad, repeat bit-exact). L / H img_375x500_pad use
assets/depthpro2/baseline_ref as the golden (baseline_helpers.golden_path).

    bin/devrun -t 3600 -- python -u bundles/depthpro2/code/tt_depthpro2/tests/mk_dec_e2e.py --sizes S,B,L,H \
        --enc ttnn
"""

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 ref  # noqa: E402
from tt_depthpro2.tests import baseline_helpers as BH  # noqa: E402

CASES = ("img_vga", "clip4_240x320", "img_375x500_pad")
LOG = os.path.join(BH.ROOT, "logs/depthpro2/mk_dec/e2e.jsonl")


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--sizes", default="S")
    ap.add_argument("--cases", default=",".join(CASES))
    ap.add_argument("--enc", default="ttnn")
    ap.add_argument("--dec", default="mk")
    args = ap.parse_args()
    import ttnn
    from tt_depthpro2.tt.model import DepthPro2Device

    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=BH.TRACE_REGION_SIZE)
    allok = True
    try:
        for size in args.sizes.split(","):
            w = ref.load_safetensors(os.path.join(BH.size_dir(size), "model.safetensors"))
            cases = [c for c in args.cases.split(",") if BH.golden_path(c, size)]
            golds = {c: BH.load_golden(c, size)[0] for c in cases}
            for c in cases:
                g = golds[c]
                x = g["x_norm"]
                t, _, h, wd = x.shape
                bucket = tuple(ref.pad_to_multiple(x).shape[-2:])
                # one configuration per case (only the bucket / clip length the gate needs)
                t0 = time.perf_counter()
                m = DepthPro2Device(dev, size, w, None, buckets=[bucket], max_frames=t,
                                    engine={"enc": args.enc, "dec": args.dec})
                build_s = time.perf_counter() - t0
                inv = m.inverse_depth(ref.pad_to_multiple(x))[:, :h, :wd]
                t1 = time.perf_counter()
                inv2 = m.inverse_depth(ref.pad_to_multiple(x))[:, :h, :wd]
                call_ms = 1e3 * (time.perf_counter() - t1)
                m.close()
                del m
                gref = g["inverse_depth"].reshape(t, h, wd)
                met = BH.drift_metrics(inv, gref)
                budget, why = BH.absrel_budget(c, size)
                # SPEC section 5 flat-scene rule: golden valid inverse-depth cv (std / mean, valid = within 100x of the
                # nearest point, as in metrics()) < 0.15 -> PCC is only a sanity floor (>= 0.99); AbsRel decides
                gv = gref.double().flatten()
                gv = gv[gv >= 0.01 * gv.max()]
                cv = float(gv.std() / gv.mean())
                pcc_gate = 0.99 if cv < 0.15 else 0.999
                ok = met["pcc"] >= pcc_gate and met["absrel_valid"] <= budget and torch.equal(inv, inv2)
                allok = allok and ok
                rec = {"size": size, "case": c, "enc": args.enc, "dec": args.dec,
                       "golden": os.path.basename(os.path.dirname(BH.golden_path(c, size))), "pcc": met["pcc"],
                       "absrel": met["absrel_valid"], "delta1": met["delta1_valid"], "budget": budget,
                       "golden_cv": cv, "pcc_gate": pcc_gate, "flat_scene": cv < 0.15,
                       "budget_from": why, "repeat_bitexact": bool(torch.equal(inv, inv2)), "call_ms": call_ms,
                       "build_s": build_s, "passed": ok, "time": time.strftime("%Y-%m-%d %H:%M:%S")}
                print(json.dumps(rec), flush=True)
                os.makedirs(os.path.dirname(LOG), exist_ok=True)
                with open(LOG, "a") as f:
                    f.write(json.dumps(rec) + "\n")
    finally:
        ttnn.close_device(dev)
    print("ALL PASS" if allok else "SOME FAILED", flush=True)


if __name__ == "__main__":
    main()