File size: 4,622 Bytes
31ffdfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b01b36
31ffdfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b01b36
31ffdfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b01b36
 
31ffdfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
# SPDX-License-Identifier: Apache-2.0
"""The traced, stateful runner (``tt/runner.py``) on the golden sample (bin/devrun).

    bin/devrun python -m tt_autoe2e.tests.dev_runner [--out /logs/runner/dev_runner.json]

Capture, prime the FPN ring with the sample's 7 history frames (``push``, oldest first), one ``step`` on the current
frame, the host planner -> controls vs upstream's fp32 golden (and the trajectory). Then timing: the replay of
``step`` alone (inputs already uploaded) and the full call (host geometry + navigation + upload + replay + readback +
planner), each the median of 3.
"""
from __future__ import annotations

import argparse
import glob
import json
import os
import time

import numpy as np

CKPT = "/ae2e/881e6a8367746da1eaae044a68077d0e7ce6fc1b/models/kitscenes-epoch-5/checkpoint.pt"


def main(argv=None) -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--out", default="/logs/runner/dev_runner.json")
    args = ap.parse_args(argv)
    import torch
    import ttnn

    from ..host import head as H
    from ..host.geometry import bev_geometry, frame_geometry
    from ..reference.tree import load_tree
    from ..tt.fusion import FRONT_CAP
    from ..tt.image import backbone_input, state_dict_of
    from ..tt.runner import DEVICE_DEFAULTS, AutoE2EDevice
    from ..ttaw.device import DeviceConfig, close_device

    torch.set_num_threads(8)
    gpath = glob.glob("/ae2e/ref/autoe2e_kitscenes_epoch5_*f000045.npz")[0]
    g = np.load(gpath)
    tree = load_tree(CKPT)
    state = state_dict_of(tree)
    res = {}
    dev = DeviceConfig(**DEVICE_DEFAULTS).open()
    try:
        m = AutoE2EDevice(dev, tree)
        t = time.perf_counter()
        m.capture()
        res["capture_s"] = time.perf_counter() - t

        def call():
            t0 = time.perf_counter()
            Ps = [g["camera_history_projection_matrix"][0, k] for k in range(7)] + [g["camera_projection_matrix"][0]]
            geos = [bev_geometry(P, 512) for P in Ps]
            gf = frame_geometry(g["front_camera_projection_matrix"][0], 1024, r_cap=FRONT_CAP, k_cams=1)
            nav, _ = H.navigation(state, g["map_context"][0], g["route_mask"][0], bool(g["map_valid"][0]),
                                  bool(g["route_valid"][0]))
            t1 = time.perf_counter()
            fused = m.step(backbone_input(g["camera_tiles"][0]), backbone_input(g["front_camera_tile"]), geos, gf,
                           np.ascontiguousarray(nav.reshape(256, -1).T))
            t2 = time.perf_counter()
            ctrl = H.planner(state, fused, H.normalize_egomotion(g["egomotion_history"][0]))
            t3 = time.perf_counter()
            return ctrl, {"host_prep_s": t1 - t0, "device_call_s": t2 - t1, "planner_s": t3 - t2, "total_s": t3 - t0}

        t = time.perf_counter()
        for k in range(7):
            m.push(backbone_input(g["camera_history_tiles"][0, k]))
        ttnn.synchronize_device(dev)
        res["prime_7_push_s"] = time.perf_counter() - t
        ctrl, tm = call()
        ref = g["controls"][0]
        res["controls_rel_l2"] = float(np.linalg.norm(ctrl - ref) / np.linalg.norm(ref))
        res["controls_max_abs"] = float(np.abs(ctrl - ref).max())
        v0 = float(g["initial_speed_mps"][0])
        xy, _, _ = H.rollout(ctrl, v0)
        xr, _, _ = H.rollout(ref, v0)
        res["trajectory_diff_m"] = {f"{s}s": float(np.linalg.norm(xy[n - 1] - xr[n - 1])) for s, n in ((1, 10), (3, 30), (5, 50))}
        # timing: replays of step alone (the inputs of the last call stay in place), then full calls (the ring keeps
        # shifting: the controls of these calls are not compared)
        reps = []
        for _ in range(3):
            t = time.perf_counter()
            m.runner.run("step_a")
            m.runner.run("step_b")
            ttnn.synchronize_device(dev)
            reps.append(time.perf_counter() - t)
        res["step_replay_s"] = float(np.median(reps))
        calls = [call()[1] for _ in range(3)]
        res["call_s"] = {k: float(np.median([c[k] for c in calls])) for k in calls[0]}
        reps = []
        for _ in range(3):
            t = time.perf_counter()
            m.runner.run("push")
            ttnn.synchronize_device(dev)
            reps.append(time.perf_counter() - t)
        res["push_replay_s"] = float(np.median(reps))
        m.release()
    finally:
        close_device(dev)
    print(json.dumps(res, indent=1))
    os.makedirs(os.path.dirname(args.out), exist_ok=True)
    with open(args.out, "w") as f:
        json.dump(res, f, indent=1)
    return 0


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