#!/usr/bin/env python3 # SPDX-License-Identifier: Apache-2.0 """The traced device path end to end (``tt.runner.Meteor2Device``) vs the CPU goldens, with a time breakdown. bin/devrun python scripts/dev_runner.py --form gather --name valday_0040 [--frames 10] """ import argparse import json import os import sys import time import numpy as np sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) def main(): ap = argparse.ArgumentParser() ap.add_argument("--form", default="gather") ap.add_argument("--name", default="valday_0040") ap.add_argument("--frames", type=int, default=10) ap.add_argument("--conv", default=None) ap.add_argument("--l1", default="table", choices=("none", "table")) a = ap.parse_args() from tt_meteor2.host.calib import lift_tables from tt_meteor2.host.ego import EgoHead from tt_meteor2.host.postprocess import decode_dense, decode_rows, find_peaks, match_boxes, occ_from_logits, \ occ_occupied from tt_meteor2.reference import config as C from tt_meteor2.reference.weights import Meteor2Weights from tt_meteor2.tt.runner import Meteor2Device from tt_meteor2.ttaw.device import close_device, open_device z = dict(np.load(f"/data/inputs/{a.name}.npz")) gold = np.load(f"/data/goldens/{a.form}/{a.name}.npz") w = Meteor2Weights(f"/weights/{C.ONNX_FILES[a.form]}") head = EgoHead(w) rep = {"form": a.form, "name": a.name} dev = open_device(0, dispatch="eth", l1_small_size=32768, trace_region_size=192 << 20) try: t = time.time() md = Meteor2Device(dev, w, conv_l1=a.l1, **({"conv_precision": a.conv} if a.conv else {})) if a.form == "gather": tb = lift_tables(z["K"][0], z["T_cam_ego"][0], w.bev_points(), w.paint_z_fill()) md.set_calibration(tb) md.capture() rep["build_capture_s"] = time.time() - t print(f"build + capture {rep['build_capture_s']:.1f} s", flush=True) from concurrent.futures import ThreadPoolExecutor from tt_meteor2.tt.model import image_input_s2d_u8 pool = ThreadPoolExecutor(8) hwc = [np.ascontiguousarray(z["imgs"][0, i].transpose(1, 2, 0)) for i in range(C.N_CAM)] buf = np.empty((1, 1, C.N_CAM * md.geo[1] * md.geo[2], 12), np.uint8) times = {k: [] for k in ("prep", "upload", "replay", "read", "frame", "peaks", "gather", "decode", "total")} for i in range(a.frames): t0 = time.perf_counter() img = image_input_s2d_u8(hwc, buf, pool) t1 = time.perf_counter() from tt_meteor2.tt.runner import IMG_ROWS_SHAPE md.runner.upload(inputs={"img": img.reshape(IMG_ROWS_SHAPE)}) import ttnn ttnn.synchronize_device(dev) tu = time.perf_counter() md.runner.replay("frame") ttnn.synchronize_device(dev) tr = time.perf_counter() out = md.runner.read("frame") t2 = time.perf_counter() times["upload"].append((tu - t1) * 1e3) times["replay"].append((tr - tu) * 1e3) times["read"].append((t2 - tr) * 1e3) peaks = find_peaks(np.stack([out["hm0"].reshape(-1), out["hm1"].reshape(-1)]).reshape(2, C.BEV_H, C.BEV_W)) t3 = time.perf_counter() rows = md.gather([p[1] for p in peaks]) if peaks else np.zeros((0, 64), np.float32) t4 = time.perf_counter() boxes = decode_rows(peaks, rows) eg = head(out["ego_pool"].reshape(-1, 128)[:20], out["kin_pool"].reshape(-1, 16)[:160], z["v0"], z["acc"], z["ego_hist"], z["route"]) t5 = time.perf_counter() for k, v in zip(("prep", "frame", "peaks", "gather", "decode", "total"), (t1 - t0, t2 - t1, t3 - t2, t4 - t3, t5 - t4, t5 - t0)): times[k].append(v * 1e3) rep["ms"] = {k: {"median": float(np.median(v)), "max": float(np.max(v))} for k, v in times.items()} print("ms (median):", {k: round(v["median"], 1) for k, v in rep["ms"].items()}, flush=True) print("runner timings:", {k: round(float(np.median(v)), 2) if isinstance(v, list) and v else v for k, v in (md.runner.timings_ms or {}).items()} if hasattr(md.runner, "timings_ms") else "") # accuracy vs the CPU goldens rb = decode_dense(gold["out.hm"], gold["out.reg"], gold["out.stationary"], gold["out.risk"], gold["out.traj"]) rep["boxes"] = match_boxes(boxes, rb) lane = out["lane"].reshape(C.LANE_H, C.LANE_W) rep["lane_agree"] = float((lane == gold["out.lane_logit"][0].argmax(0)).mean()) rcls, rp = occ_from_logits(gold["out.occ_logit"]) dcls = out["occ_cls"].reshape(C.OCC_H, C.OCC_W, C.OCC_Z).transpose(2, 0, 1) % 10 dp = out["occ_p"].reshape(C.OCC_H, C.OCC_W, C.OCC_Z).transpose(2, 0, 1) rocc, docc = occ_occupied(rcls, rp), occ_occupied(dcls, dp) rep["occ_mask_agree"] = float((rocc == docc).mean()) rep["occ_iou"] = float((rocc & docc).sum() / max((rocc | docc).sum(), 1)) rep["occ_cls_agree_on_occupied"] = float((dcls[rocc] == rcls[rocc]).mean()) rep["tl_same"] = bool(eg["tl"].argmax() == gold["out.tl"].argmax()) rep["ego_path_6s_mean_m"] = float(np.linalg.norm(eg["ego_path_6s"][0] - gold["out.ego_path_6s"][0], axis=-1).mean()) rep["best_same"] = bool(eg["ego"][0, 36:39].argmax() == gold["out.ego"][0, 36:39].argmax()) # box details of the matched pairs print("boxes", rep["boxes"], flush=True) print("lane agree", round(rep["lane_agree"], 5), "occ mask agree", round(rep["occ_mask_agree"], 5), "occ IoU", round(rep["occ_iou"], 4), "occ class agree", round(rep["occ_cls_agree_on_occupied"], 4), "tl same", rep["tl_same"], "best same", rep["best_same"], "ego 6s mean m", round(rep["ego_path_6s_mean_m"], 4), flush=True) md.release() finally: close_device(dev) json.dump(rep, open(f"/logs/dev_runner_{a.form}_{a.name}.json", "w"), indent=1) if __name__ == "__main__": main()