File size: 2,444 Bytes
0f131e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""The Python API on the device (bin/devrun): from_pretrained, N calls of the shipped sample, agreement with the
stored CPU-reference body (the smoke test's comparison), timing_ms.

    bin/devrun python scripts/dev_api.py --variant gather [--calls 10]
"""
import argparse
import json
import os
import sys
import time
from pathlib import Path

import numpy as np

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
HERE = Path(__file__).resolve().parents[1] / "tt_meteor2"
sys.path.insert(0, str(HERE / "ttaw" / "server"))


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--variant", default="gather")
    ap.add_argument("--calls", type=int, default=10)
    a = ap.parse_args()
    import smoke as ttaw_smoke
    from tt_meteor2 import METEOR2, load_sample

    t = time.time()
    model = METEOR2.from_pretrained(variant=a.variant, weights_dir=os.environ.get("METEOR2_WEIGHTS_DIR", "/weights"))
    print(f"from_pretrained {time.time() - t:.1f} s warmup_ms {model.warmup_ms}", flush=True)
    kw = load_sample(HERE / "samples" / "synthetic_8cam.json")
    timings = []
    for i in range(a.calls):
        out = model(**kw)
        timings.append(dict(out.timing_ms))
    body = out.to_dict()
    ref = json.loads((HERE / "samples" / f"synthetic_8cam.{a.variant}.reference.json").read_text())
    import importlib.util
    spec = importlib.util.spec_from_file_location("smoke_test", HERE / "server" / "smoke_test.py")
    st = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(st)
    metrics, fails = ttaw_smoke.compare_with_reference(st.drop_threshold_edge(body), st.drop_threshold_edge(ref), gates={"max_ade": 0.3, "max_fde": 0.6,
                                                                       "min_label_agreement": 0.97})
    med = {k: round(float(np.median([t[k] for t in timings[1:] or timings])), 1) for k in timings[0]}
    print("timing_ms median", med, flush=True)
    print("vs CPU reference:", ttaw_smoke.describe_metrics(metrics), flush=True)
    print("FAIL" if fails else "PASS", fails, flush=True)
    print("repr", repr(out), "info form", model.info.get("form"), flush=True)
    json.dump({"timing_ms": med, "metrics": metrics, "fails": fails}, open(f"/logs/dev_api_{a.variant}.json", "w"),
              indent=1, default=str)
    model.close()


if __name__ == "__main__":
    main()