File size: 3,029 Bytes
8eeb550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""HOST_POST A/B on the host (CPU only, OPT round 4 R4.4): the served result of ``build_output`` with each decode
mode on the golden frames' reference outputs and on device dumps, compared field by field (to_dict JSON incl. the
lane map), run as one stream (seg fusion / yaw / mode state carried over), with p50 timings per mode.

    python code/scripts/host_post_check.py --dumps logs/meteor/opt_r4/dump_cm --reps 15
"""
from __future__ import annotations

import argparse
import json
import os
import time
from pathlib import Path

import numpy as np

from tt_meteor.host.postprocess import PostConfig, set_host_post
from tt_meteor.host.preprocess import MeteorFrame
from tt_meteor.host.result import build_output
from tt_meteor.host.temporal import StreamState
from tt_meteor.tt.unpack import OUTPUT_NAMES

GOLDENS = Path(os.environ.get("METEOR_GOLDENS", "/home/ubuntu/experiments/tt-models/research/meteor/goldens"))


def frames(dumps):
    out = []
    for g in sorted(p for p in GOLDENS.iterdir() if (p / "taps.npz").is_file()):
        with np.load(g / "taps.npz") as z:
            fr = MeteorFrame(z["input.imgs"], z["input.K"], z["input.T_cam_ego"], z["input.v0"], z["input.present"])
            out.append((f"{g.name}:ref", {k: z[k] for k in OUTPUT_NAMES}, fr))
            for d in dumps:
                f = Path(d) / f"{g.name}.npz"
                if f.is_file():
                    out.append((f"{g.name}:{Path(d).name}", dict(np.load(f)), fr))
    return out


def canon(o):
    d = o.to_dict("json") if hasattr(o, "to_dict") else o
    d.pop("timing_ms", None)
    return json.dumps(d, sort_keys=True, default=lambda v: np.asarray(v).tolist())


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--dumps", nargs="*", default=[])
    ap.add_argument("--modes", default="vec,sparse")
    ap.add_argument("--reps", type=int, default=10)
    a = ap.parse_args()
    fs = frames(a.dumps)
    modes = a.modes.split(",")
    res, times = {}, {}
    for m in modes:
        set_host_post(m)
        st = StreamState()
        res[m] = [canon(build_output(raw, fr, PostConfig(), st)) for _, raw, fr in fs]
    bad = 0
    for i, (name, _, _) in enumerate(fs):
        same = all(res[m][i] == res[modes[0]][i] for m in modes)
        bad += 0 if same else 1
        print(f"{name}: {'IDENTICAL' if same else 'DIFFERENT'}")
    for rep in range(a.reps):                        # alternating timing
        for m in modes:
            set_host_post(m)
            st = StreamState()
            t0 = time.perf_counter()
            for _, raw, fr in fs:
                build_output(raw, fr, PostConfig(), st)
            times.setdefault(m, []).append((time.perf_counter() - t0) * 1e3 / len(fs))
    for m in modes:
        print(f"{m}: build_output p50 {np.median(times[m]):.2f} ms/frame (min {min(times[m]):.2f})")
    print("ALL IDENTICAL" if bad == 0 else f"{bad} DIFFERENT")
    return 0 if bad == 0 else 1


if __name__ == "__main__":
    raise SystemExit(main())