# SPDX-License-Identifier: Apache-2.0 """``pairmap_k`` v2 (``BEVFORMER_PAIRMAP=v2``, D20 round 2: whole-tile reads + pack_untilize + 128-byte page writes) equals v1 (face-row reads) bit for bit on the TSA and SCA shapes (needs one p150; hang protocol: tiny first, under ``TT_METAL_WATCHER=2``). bin/devrun -t 600 -- python -m pytest -q -s code/tt_bevformer/tests/test_pairmap_device.py -k tiny """ from __future__ import annotations import numpy as np import pytest pytestmark = pytest.mark.device # (batch, heads, sub, h, w) CASES = {"tiny": (1, 2, 1, 3, 20), "tiny_sub": (2, 2, 3, 4, 9), "sca": (1, 8, 6, 23, 40), "tsa": (2, 8, 1, 150, 150)} @pytest.mark.parametrize("case", list(CASES) + ["tsa_trace"]) def test_pairmap_v2(device, case): import torch import ttnn from tt_bevformer.tt import kconf from tt_bevformer.tt.msda import free from tt_bevformer.tt.pairmap_k import pair_maps b, heads, sub, h, w = CASES[case.replace("_trace", "")] rng = np.random.default_rng(1) x = rng.standard_normal((b, 1, sub * h * w, heads * 32)).astype(np.float32) xt = ttnn.from_torch(torch.from_numpy(x), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=device, memory_config=ttnn.L1_MEMORY_CONFIG) old = kconf.PAIRMAP2 def run(v2): kconf.PAIRMAP2 = v2 try: return pair_maps(xt, heads=heads, sub=sub, hw=(h, w)) finally: kconf.PAIRMAP2 = old def host(t): a = ttnn.to_torch(t).float().numpy() free(t) return a ref = host(run(False)) for _ in range(2): got = host(run(True)) assert np.array_equal(got, ref), f"{case}: v2 differs in {int((got != ref).sum())} of {ref.size} values" if case.endswith("_trace"): free(run(True)) tid = ttnn.begin_trace_capture(device, cq_id=0) o = run(True) ttnn.end_trace_capture(device, tid, cq_id=0) for _ in range(2): ttnn.execute_trace(device, tid, cq_id=0, blocking=True) got = host(o) ttnn.release_trace(device, tid) assert np.array_equal(got, ref), f"{case}: traced v2 differs" free(xt)