File size: 3,221 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
81
82
83
84
85
#!/usr/bin/env python3
"""R7 probe (OPT_PLAN round-7 rank 2): where the ``d2h`` stage of the loaded model goes. After a replay of the frame
trace (synchronized), times per readback leaf: ``copy_device_to_host_tensor`` (PCIe), ``to_numpy`` and the layout
unpack (views), p50 of ``--iters``; plus the whole ``runner.read`` for reference.

    METEOR_DEVICE_OK=1 bin/devrun -t 1200 -- python -u code/scripts/d2h_breakdown.py
"""
from __future__ import annotations

import argparse
import sys
import time
from pathlib import Path

import numpy as np

CODE = Path(__file__).resolve().parents[1]


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--iters", type=int, default=15)
    a = ap.parse_args()
    import ttnn

    from tt_meteor import METEOR
    from tt_meteor.api import load_sample
    from tt_meteor.host.inputs import prepare_request
    from tt_meteor.ttaw.tensors import to_numpy

    sample = CODE.parent / "staging_samples_pandaset" / "samples" / "pandaset_019_f40.json"
    with METEOR.from_pretrained(dispatch="eth") as model:
        return run(model, load_sample(sample), a, ttnn, prepare_request, to_numpy)


def run(model, req, a, ttnn, prepare_request, to_numpy) -> int:
    for _ in range(3):
        model(**req)
    frame = prepare_request(req["images"], req["calibration"], req["ego_speed"], req.get("stream"),
                            layout=str(model.knobs.HOST_PRE))
    tt = model.tt
    tt.set_tables(model.calib_cache.get(frame.K, frame.T_cam_ego))
    r = model.runner
    rows = {"read": [], "copy": [], "numpy": [], "unpack": [], "sync": []}
    per_leaf = None
    for _ in range(a.iters):
        r.replay("frame")
        t0 = time.perf_counter()
        ttnn.synchronize_device(r.device)
        rows["sync"].append((time.perf_counter() - t0) * 1e3)
        t0 = time.perf_counter()
        r.read("frame")
        rows["read"].append((time.perf_counter() - t0) * 1e3)
        trace = r._trace_for("frame", r._last_phase["frame"])
        cp = np_ = up = 0.0
        leaf = []
        for (path, tensor, layout), host in zip(trace.leaves, trace.host_buffers):
            t0 = time.perf_counter()
            ttnn.copy_device_to_host_tensor(tensor, host, blocking=True, cq_id=0)
            t1 = time.perf_counter()
            arr = to_numpy(host)
            t2 = time.perf_counter()
            if layout is not None:
                layout.unpack(arr)
            t3 = time.perf_counter()
            cp += t1 - t0
            np_ += t2 - t1
            up += t3 - t2
            leaf.append((str(path), tuple(int(v) for v in tensor.shape), arr.nbytes, (t1 - t0) * 1e3, (t2 - t1) * 1e3))
        rows["copy"].append(cp * 1e3)
        rows["numpy"].append(np_ * 1e3)
        rows["unpack"].append(up * 1e3)
        per_leaf = leaf
    for k, v in rows.items():
        print(f"{k:7s} p50 {np.median(v):7.2f} ms  min {min(v):7.2f}  max {max(v):7.2f}", flush=True)
    tot = sum(x[2] for x in per_leaf)
    print(f"leaves {len(per_leaf)}, {tot / 1e6:.1f} MB")
    for p, s, nb, c, n in per_leaf:
        print(f"  {p:24s} {str(s):24s} {nb / 1e6:6.1f} MB copy {c:6.2f} ms numpy {n:5.2f} ms")
    return 0


if __name__ == "__main__":
    sys.exit(main())