File size: 4,358 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
94
95
96
97
98
99
100
# SPDX-License-Identifier: Apache-2.0
"""Regression (serve-pkg device smoke, 2026-11-28 22:14): DepthPro2Device built as api.py builds it for the S server
with enc=mk -- buckets 480x640 / 640x480 / 256x320, window 4 (max_frames 4, frames (1,)), FOV shapes, clip lengths
(1, 4), KV cache on -- then images on every bucket, the golden 4-frame 240x320 clip, n = 2 / 3 frame clips on every
bucket (runtime valid-frame count of the T = 4 programs) vs the float32 reference, and a few unbuffered (ttnn KV)
steps in between; finally the golden clip again (no state leaks between programs).

    bin/devrun -t 2400 -- python -u bundles/depthpro2/code/tt_depthpro2/tests/mk_enc_serve_repro.py [--enc mk]
"""

from __future__ import annotations

import argparse
import json
import os
import sys

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


def run_repro(dev, size: str = "S", enc: str = "mk", fov: str = "ttnn") -> bool:
    """The serve-configuration regression on an open device (``l1_small_size`` >= 32 KB, trace region >= 224 MB)."""
    from tt_depthpro2.tt.model import DepthPro2Device

    sw = ref.load_safetensors(os.path.join(BH.size_dir(size), "model.safetensors"))
    fw = ref.load_safetensors(os.path.join(BH.fov_dir(), "model.safetensors"))
    cpu = ref.DepthPro2Reference(size, sw)
    buckets = [(480, 640), (640, 480), (256, 320)]
    fov_shapes = sorted({DepthPro2Device.fov_size(h, w) for h, w in buckets})
    ok = True

    def check(name, inv, r, pcc_gate=0.999, absrel=0.01):
        nonlocal ok
        met = BH.drift_metrics(inv, r)
        good = met["pcc"] >= pcc_gate and met["absrel_valid"] <= absrel
        ok = ok and good
        print(json.dumps({"check": name, "pcc": met["pcc"], "absrel": met["absrel_valid"], "passed": good}), flush=True)

    m = DepthPro2Device(dev, size, sw, fw, buckets, 4, engine={"enc": enc, "fov": fov}, frames=(1,),
                        fov_shapes=fov_shapes, clip_lengths=(1, 4), kv_cache=True)
    try:
        print("configurations:", m.configurations(), flush=True)
        gc4, _ = BH.load_golden("clip4_240x320", size)
        x4 = gc4["x_norm"].float()
        T4, _, h4, w4 = x4.shape

        def golden_clip(tag):
            inv = m.inverse_depth(ref.pad_to_multiple(x4))[:, :h4, :w4]
            check(f"golden clip4 {tag}", inv, gc4["inverse_depth"].reshape(T4, h4, w4),
                  absrel=BH.absrel_budget("clip4_240x320", size)[0])

        golden_clip("first")
        gi, _ = BH.load_golden("img_vga", size)
        xi = gi["x_norm"].float()
        inv = m.inverse_depth(ref.pad_to_multiple(xi))[:, :480, :640]
        check("golden img_vga", inv, gi["inverse_depth"].reshape(1, 480, 640))
        gen = torch.Generator().manual_seed(0)
        for (bh, bw) in buckets:
            base = torch.nn.functional.interpolate(xi, size=(bh, bw), mode="bilinear", align_corners=False)
            clip = base.repeat(3, 1, 1, 1) + 0.05 * torch.randn(3, 3, bh, bw, generator=gen)
            for n in (1, 2, 3):
                inv = m.inverse_depth(clip[:n])
                with torch.no_grad():
                    r = cpu.decode(*cpu.encode(clip[:n]))[:, 0]
                check(f"bucket {bh}x{bw} n={n}", inv, r)
            golden_clip(f"after bucket {bh}x{bw}")
        cache = m.kv_cache((256, 320), 4)  # unbuffered (ttnn KV step programs) in between
        for t in range(3):
            m.inverse_depth_step(ref.pad_to_multiple(x4[t:t + 1]), cache)
        golden_clip("after kv steps")
    finally:
        m.close()
    return ok


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--size", default="S")
    ap.add_argument("--enc", default="mk")
    ap.add_argument("--fov", default="ttnn")
    args = ap.parse_args()
    import ttnn

    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=224 << 20)
    try:
        ok = run_repro(dev, args.size, args.enc, args.fov)
    finally:
        ttnn.close_device(dev)
    print("SERVE REPRO PASS" if ok else "SERVE REPRO FAIL", flush=True)


if __name__ == "__main__":
    main()