File size: 3,843 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
# SPDX-License-Identifier: Apache-2.0
"""ENC megakernel runtime valid-frame count (BASELINE.md 9.2 gate): a T-frame program with ``set_valid_frames(n)`` vs
the exact n-frame program on the same frames (PCC / max rel of the taps of frames < n), n = T bit-identical to the
default run before and after, and a trace replay after ``set_valid_frames`` equal to the eager run.

    bin/devrun -t 1800 -- python -u bundles/depthpro2/code/tt_depthpro2/tests/mk_enc_vf.py --size S
"""

from __future__ import annotations

import argparse
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 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="clip4_240x320")
    args = ap.parse_args()
    import ttnn

    g = H.load_case(args.case, args.size)
    x = R.pad_to_multiple(g["x_norm"].float())
    T, _, hp, wp = x.shape
    dev = ttnn.open_device(device_id=0, dispatch_core_config=ttnn.DispatchCoreConfig(ttnn.DispatchCoreType.ETH),
                           trace_region_size=8 << 20)
    ok = True
    try:
        wts = MkEncWeights(dev, H.load_weights(args.size), args.size, "depth")

        def pt(frames: torch.Tensor):
            return A.patchify(frames).to(torch.bfloat16).reshape(1, 1, -1, 768)

        def dev_patches(frames):
            return ttnn.from_torch(pt(frames), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev,
                                   memory_config=ttnn.DRAM_MEMORY_CONFIG)

        def taps(mk):
            return [ttnn.to_torch(t).float().reshape(mk.geo.T, mk.geo.Np, -1)[:, :mk.geo.N] for t in mk.taps]

        mk = MkEnc(dev, wts, args.size, T, hp // 16, wp // 16)
        patches = dev_patches(x)
        mk.run(patches)
        full = [ttnn.to_torch(t).clone() for t in mk.taps]
        for n in range(1, T):
            xn = x.clone()
            xn[n:] = 0
            pn = dev_patches(xn)
            mk.set_valid_frames(n)
            mk.run(pn)
            got = taps(mk)
            ex = MkEnc(dev, wts, args.size, n, hp // 16, wp // 16)
            ex.run(dev_patches(x[:n]))
            want = taps(ex)
            for i in range(4):
                a, b = got[i][:n], want[i]
                p, e = H.pcc(a, b), ((a - b).abs().max() / b.abs().max()).item()
                ok = ok and p >= 0.9999
                print(f"n={n} tap{i}: T={T} program vs exact T={n}: pcc {p:.7f} max rel {e:.2e}", flush=True)
            # trace replay after set_valid_frames == eager
            eager = [ttnn.to_torch(t).clone() for t in mk.taps]
            mk.set_valid_frames(T)
            tid = ttnn.begin_trace_capture(dev, cq_id=0)
            mk.run(pn)
            ttnn.end_trace_capture(dev, tid, cq_id=0)
            mk.set_valid_frames(n)
            ttnn.execute_trace(dev, tid, cq_id=0, blocking=True)
            same = all(torch.equal(a, ttnn.to_torch(t)) for a, t in zip(eager, mk.taps))
            ttnn.release_trace(dev, tid)
            ok = ok and same
            print(f"n={n}: trace (captured at n={T}) replayed after set_valid_frames({n}) == eager: {same}", flush=True)
        mk.set_valid_frames(T)
        mk.run(patches)
        same = all(torch.equal(a, ttnn.to_torch(t)) for a, t in zip(full, mk.taps))
        ok = ok and same
        print(f"n=T={T} after the n < T runs bit-identical to the first run: {same}", flush=True)
    finally:
        ttnn.close_device(dev)
    print("VF PASS" if ok else "VF FAIL", flush=True)


if __name__ == "__main__":
    main()