File size: 4,859 Bytes
b7e49b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5afa0d
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
101
102
103
104
105
106
# SPDX-License-Identifier: Apache-2.0
"""Soak of one ENC megakernel configuration (release gate, 2026-11-29): ``--eager`` eager launches (taps bitwise vs
the first), then one trace and ``--replays`` replays, each checked on the device against the first replay's taps
(``ttnn.ne`` + sum per tap: an exact mismatch count; +-0 compare equal). Appends to logs/depthpro2/mk_enc/soak.jsonl.

    bin/devrun -t 300 -- python -u bundles/depthpro2/code/tt_depthpro2/tests/mk_enc_soak.py --size S --hw 480 640 --T 1
    (--mode fov: the FOV trunk; --size is then ignored)
"""

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.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("--hw", type=int, nargs=2, default=(480, 640))
    ap.add_argument("--T", type=int, default=1)
    ap.add_argument("--mode", default="depth")
    ap.add_argument("--eager", type=int, default=10)
    ap.add_argument("--replays", type=int, default=1000)
    args = ap.parse_args()
    import ttnn

    size = "S" if args.mode == "fov" else args.size
    (hp, wp), T = args.hw, args.T
    rec = {"size": "fov-trunk" if args.mode == "fov" else size, "mode": args.mode, "hp": hp, "wp": wp, "T": T,
           "eager": args.eager, "replays": args.replays, "start": time.strftime("%Y-%m-%d %H:%M:%S")}
    g = H.load_case("img_vga", "S")
    x = torch.nn.functional.interpolate(g["x_norm"].float(), size=(hp, wp), mode="bilinear", align_corners=False)
    x = x.repeat(T, 1, 1, 1) + 0.1 * torch.randn(T, 3, hp, wp, generator=torch.Generator().manual_seed(0))
    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("fov" if args.mode == "fov" else size), size, args.mode)
        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)
        mk = MkEnc(dev, wts, size, T, hp // 16, wp // 16, mode=args.mode)
        rec["geometry"] = mk.geo.summary()
        print("SOAK", rec["geometry"], flush=True)
        first = None
        bad_eager = 0
        t0 = time.perf_counter()
        for i in range(args.eager):
            mk.run(patches)
            out = [ttnn.to_torch(t) for t in mk.taps]
            if first is None:
                first = out
            elif not all(torch.equal(a, b) for a, b in zip(first, out)):
                bad_eager += 1
        rec["eager_s"] = time.perf_counter() - t0
        rec["eager_mismatches"] = bad_eager
        rec["finite"] = all(bool(torch.isfinite(t.float()).all()) for t in first)
        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)
        ref = [ttnn.clone(t, memory_config=ttnn.DRAM_MEMORY_CONFIG) for t in mk.taps]
        rec["first_replay_equals_eager"] = all(torch.equal(a, ttnn.to_torch(t)) for a, t in zip(first, ref))
        bad, worst = 0, 0
        t0 = time.perf_counter()
        for r in range(args.replays):
            ttnn.execute_trace(dev, tid, cq_id=0, blocking=False)
            n = 0
            for t, f in zip(mk.taps, ref):
                ne = ttnn.ne(t, f, memory_config=ttnn.DRAM_MEMORY_CONFIG)
                s = ttnn.sum(ne)
                n += int(round(float(ttnn.to_torch(s).float().sum())))
                ttnn.deallocate(ne)
                ttnn.deallocate(s)
            if n:
                bad += 1
                worst = max(worst, n)
                print(f"replay {r}: {n} mismatching values", flush=True)
            if (r + 1) % 100 == 0:
                print(f"replay {r + 1}: {bad} mismatching replays so far", flush=True)
        rec["replay_s"] = time.perf_counter() - t0
        rec["replay_ms_incl_check"] = 1e3 * rec["replay_s"] / max(1, args.replays)
        rec["replay_mismatches"] = bad
        rec["replay_worst_values"] = worst
        ttnn.release_trace(dev, tid)
        rec["passed"] = bad == 0 and bad_eager == 0 and rec["first_replay_equals_eager"] and rec["finite"]
    finally:
        rec["end"] = time.strftime("%Y-%m-%d %H:%M:%S")
        print("REC " + json.dumps(rec), flush=True)
        H.append_log("soak.jsonl", rec)
        ttnn.close_device(dev)


if __name__ == "__main__":
    main()