# SPDX-License-Identifier: Apache-2.0 """Device image branch (ResNet-50 + FPN at 512 and 1024 px) and T8 fusion vs the upstream golden intermediates (bin/devrun). bin/devrun python -m tt_autoe2e.tests.dev_image_t8 [--out /logs/...json] Golden sample (recon): ``camera_tiles`` [1, 6, 3, 512, 512] -> ``fpn_current_l0..3``; ``front_camera_tile`` [1, 3, 1024, 1024] -> ``fpn_front_native_l0..3``; the 7 history BEVs + the current BEV after the front fusion -> ``image_bev_t8``. Relative L2 / PCC per output and warm wall times. """ 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 stats(a, b): a = np.asarray(a, np.float64).ravel() b = np.asarray(b, np.float64).ravel() return {"rel_l2": float(np.linalg.norm(a - b) / max(np.linalg.norm(b), 1e-30)), "pcc": float(np.corrcoef(a, b)[0, 1]), "max_abs": float(np.abs(a - b).max())} def main(argv=None) -> int: ap = argparse.ArgumentParser() ap.add_argument("--out", default="/logs/image_t8/dev_image_t8.json") args = ap.parse_args(argv) import torch import ttnn from ..reference.port_form import Q from ..reference.tree import load_tree from ..tt.image import TtImageBranch, backbone_input, state_dict_of from ..tt.t8 import TtT8 from ..ttaw.device import DeviceConfig, close_device from ..ttaw.ops.conv import FeatureMap gpath = glob.glob("/ae2e/ref/autoe2e_kitscenes_epoch5_*f000045.npz")[0] g = np.load(gpath) inter = np.load(gpath.replace(".npz", "_intermediates.npz")) state = state_dict_of(load_tree(CKPT)) res = {} dev = DeviceConfig(trace_region_size=64 << 20).open() try: img = TtImageBranch(state) def run(tiles): x = backbone_input(tiles) # [N, H, W, 3] n, h, w, _ = x.shape t = ttnn.from_torch(torch.from_numpy(x.reshape(1, 1, n * h * w, 3)), dtype=ttnn.bfloat16, layout=ttnn.ROW_MAJOR_LAYOUT, device=dev, memory_config=ttnn.DRAM_MEMORY_CONFIG) outs = img(FeatureMap(t, n, h, w, 3)) arr = [ttnn.to_torch(o.tensor).float().numpy().reshape(n, o.height, o.width, 256).transpose(0, 3, 1, 2) for o in outs] for o in outs: o.deallocate() ttnn.deallocate(t) return arr for name, tiles, key in (("cams512", g["camera_tiles"][0], "fpn_current"), ("front1024", g["front_camera_tile"], "fpn_front_native")): got = run(tiles) res[name] = [stats(got[i], inter[f"{key}_l{i}"]) for i in range(4)] t = time.perf_counter() run(tiles) res[name + "_warm_s"] = time.perf_counter() - t print(name, json.dumps(res[name]), flush=True) t8 = TtT8(state) frames = [inter["frame_bev_history"][0, k] for k in range(7)] + [inter["frame_bev_current_with_front"][0]] up = lambda a: ttnn.from_torch(torch.from_numpy(np.ascontiguousarray(a.reshape(256, Q).T).reshape(1, 1, Q, 256)), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev) bevs = [up(f) for f in frames] out = t8(bevs) got = ttnn.to_torch(out).float().numpy().reshape(Q, 256) ttnn.deallocate(out) res["t8"] = stats(got, inter["image_bev_t8"][0].reshape(256, Q).T) t = time.perf_counter() out = t8(bevs) ttnn.synchronize_device(dev) res["t8_warm_s"] = time.perf_counter() - t print("t8", json.dumps(res["t8"]), flush=True) 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())