# SPDX-License-Identifier: Apache-2.0 """Pixel-pair value maps for ``msda`` (vendored from the BEVFormer-small p150 port, ``tt_bevformer/tt/pairmap_k.py`` v2 path; kernels ``kernels/pairmap2_{dm,compute}.cpp``): a value projection ``[1, 1, sub * h * w, heads * 32]`` (TILE, bf16, L1) -> ``[heads * sub, h, w + 1, 64]`` ROW_MAJOR bf16 (map ``head * sub + s``; 128-byte page ``(m, y, i)`` = the sticks of pixels ``i - 1`` and ``i``, zeros outside the row). ``msda`` then reads the two upper (or lower) corners of a sample point with one 128-byte request instead of four 32-byte ones (the SCA kernel is bound by those reads: AutoE2E probe, logs e9_probe). Per (tile row, head) item one 2 KB tile read and a pack_untilize, then one 128-byte write per page. """ from __future__ import annotations import os from typing import Any __all__ = ["pair_maps"] KDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "kernels") def pair_maps(x: Any, *, heads: int, sub: int, hw, memory_config: Any = None, quad: bool = False) -> Any: """``quad``: the quad map ``[heads * sub, h + 1, w + 1, 128]`` instead (256-byte page ``(m, Y, i)`` = pair pages ``(m, Y - 1, i)`` and ``(m, Y, i)``, zero rows outside): one read per sample point in ``msda``.""" import ttnn hv, wv = int(hw[0]), int(hw[1]) if x.layout != ttnn.TILE_LAYOUT or x.dtype != ttnn.bfloat16 or x.memory_config().buffer_type != ttnn.BufferType.L1: raise ValueError("pair_maps: bf16 TILE input in L1") if int(x.shape[-2]) != sub * hv * wv or int(x.shape[-1]) != heads * 32 or int(x.shape[1]) != 1 or int(x.shape[0]) != 1: raise ValueError(f"pair_maps: input {tuple(x.shape)} for {heads} heads x {sub} maps {hv}x{wv}") dev = x.device() n = int(heads) * int(sub) shape = [n, hv + 1, wv + 1, 128] if quad else [n, hv, wv + 1, 64] out = ttnn.allocate_tensor_on_device(ttnn.Shape(shape), ttnn.bfloat16, ttnn.ROW_MAJOR_LAYOUT, dev, memory_config or ttnn.L1_MEMORY_CONFIG) g = dev.compute_with_storage_grid_size() gx, gy = int(g.x), int(g.y) crs = ttnn.CoreRangeSet({ttnn.CoreRange(ttnn.CoreCoord(0, 0), ttnn.CoreCoord(gx - 1, gy - 1))}) ntr = -(-int(x.shape[-2]) // 32) nitems = ntr * int(heads) nc = gx * gy def cb(idx, pages, size): return ttnn.CBDescriptor(total_size=pages * size, core_ranges=crs, format_descriptors=[ ttnn.CBFormatDescriptor(buffer_index=idx, data_format=ttnn.bfloat16, page_size=size)]) cbs = [cb(0, 2, 2048), cb(1, 2, 64), cb(16, 2, 2048)] accs = list(ttnn.TensorAccessorArgs(x).get_compile_time_args()) + \ list(ttnn.TensorAccessorArgs(out).get_compile_time_args()) common = [x.buffer_address(), out.buffer_address()] SRC = ttnn.KernelDescriptor.SourceType.FILE_PATH head = [hv, wv, int(sub), int(heads), ntr, nitems, nc, gx, int(bool(quad))] ks = [ttnn.KernelDescriptor(kernel_source=os.path.join(KDIR, "pairmap2_dm.cpp"), source_type=SRC, core_ranges=crs, compile_time_args=[role] + head + accs, defines=[], runtime_args=ttnn.RuntimeArgs(), common_runtime_args=common, config=cfg) for role, cfg in enumerate([ttnn.ReaderConfigDescriptor(), ttnn.WriterConfigDescriptor()])] ks.append(ttnn.KernelDescriptor(kernel_source=os.path.join(KDIR, "pairmap2_compute.cpp"), source_type=SRC, core_ranges=crs, compile_time_args=[nitems, nc, gx], defines=[], runtime_args=ttnn.RuntimeArgs(), common_runtime_args=[], config=ttnn.ComputeConfigDescriptor())) ttnn.generic_op([x, out], ttnn.ProgramDescriptor(kernels=ks, semaphores=[], cbs=cbs)) return out