# SPDX-License-Identifier: Apache-2.0 """Where the SCA kernel's time goes (bin/devrun): one AutoE2E-sized level call (98,304 rows in 6 segments, 48 maps 64 x 64, 8 heads x 8 points) timed as is, without the value reads (``NOREAD``) and with one accumulate step per half instead of 16 (``NOSTEPS``). Wrong outputs by design; only the times matter.""" from __future__ import annotations import json import time import numpy as np def main() -> int: import torch import ttnn from ..tt.msda_k import msda from ..tt.pairmap import pair_maps from ..ttaw.device import DeviceConfig, close_device rng = np.random.default_rng(0) dev = DeviceConfig(trace_region_size=32 << 20).open() res = {} try: rows, heads, P = 98304, 8, 8 segs = [29568 + 1152, 14944, 15648, 9696, 9472, 9696 + 8128] segs = [s - s % 32 for s in segs] segs[-1] += rows - sum(segs) value = ttnn.from_torch(torch.randn(48, 64, 64, 32), dtype=ttnn.bfloat16, layout=ttnn.ROW_MAJOR_LAYOUT, device=dev, memory_config=ttnn.L1_MEMORY_CONFIG) loc = np.round(rng.uniform(-0.5, 63.5, size=(1, 1, rows, heads * 64)) * 65536).astype(np.int32) att = rng.uniform(0, 1, size=(1, heads, rows, 32)).astype(np.float32) loc_t = ttnn.from_torch(torch.from_numpy(loc), dtype=ttnn.int32, layout=ttnn.TILE_LAYOUT, device=dev) att_t = ttnn.from_torch(torch.from_numpy(att), dtype=ttnn.float32, layout=ttnn.TILE_LAYOUT, device=dev) ends = np.zeros((1, 1, 1, 16), np.int32) ends[0, 0, 0, :6] = np.cumsum(segs) // 32 seg_t = ttnn.from_torch(torch.from_numpy(ends), dtype=ttnn.uint32, layout=ttnn.ROW_MAJOR_LAYOUT, device=dev) proj = ttnn.from_torch(torch.randn(1, 1, 6 * 4096, 256), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=dev, memory_config=ttnn.L1_MEMORY_CONFIG) pairs = pair_maps(proj, heads=8, sub=6, hw=(64, 64)) t = time.perf_counter() for _ in range(5): pm = pair_maps(proj, heads=8, sub=6, hw=(64, 64)) ttnn.synchronize_device(dev) ttnn.deallocate(pm) res["pair_maps_build"] = round((time.perf_counter() - t) / 5 * 1e3, 3) for name, dbg, v in (("full", (), value), ("noread", ("noread",), value), ("nosteps", ("nosteps",), value), ("pairs", (), pairs), ("pairs_noread", ("noread",), pairs), ("pairs_nosteps", ("nosteps",), pairs), ("pairs_noread_nosteps", ("noread", "nosteps"), pairs), ("pairs_hifi2", ("fid:HiFi2",), pairs), ("pairs_lofi", ("fid:LoFi",), pairs), ("pairs_noread_hifi2", ("noread", "fid:HiFi2"), pairs)): fid = next((d[4:] for d in dbg if d.startswith("fid:")), None) dbg = tuple(d for d in dbg if not d.startswith("fid:")) fn = lambda: msda(v, loc_t, att_t, points=P, nq=rows, segments=[0] * 6, seg_table=seg_t, loc_hs=64, loc_off=0, att_col0=0, debug=dbg, fidelity=fid) o = fn() ttnn.synchronize_device(dev) ttnn.deallocate(o) ts = [] for _ in range(5): t = time.perf_counter() o = fn() ttnn.synchronize_device(dev) ts.append(time.perf_counter() - t) ttnn.deallocate(o) res[name] = round(float(np.median(ts)) * 1e3, 3) if v is pairs and not dbg: o = fn() out = ttnn.to_torch(o).float().numpy() ttnn.deallocate(o) if fid is None: ref_out = out else: res[name + "_vs_hifi4_max_abs"] = float(np.abs(out - ref_out).max()) res[name + "_vs_hifi4_identical"] = bool(np.array_equal(out, ref_out)) print(name, res[name], "ms", flush=True) finally: close_device(dev) print(json.dumps(res)) return 0 if __name__ == "__main__": raise SystemExit(main())