This document is relevant for: Trn2, Trn3
MS Deformable Attention Quad Kernel API Reference#
Multi-scale deformable attention forward, the same call as ms_deformable_attention, through a quad scratch.
As in grid_sample (experimental/vision), every level of the value is first rewritten into an HBM scratch whose entry (y, x) for head h holds the 2 x 2 neighbourhood of padded pixel (y, x) (taps (dy, dx) = pixel (y + dy - 1, x + dx - 1), zero outside the level), 4 C_h contiguous elements, so a bilinear sample is one gathered row and the zero border stands in for the zeros padding’s out-of-range corners. Per tile of up to 128 queries and per level, the coordinates, the bilinear weights times the attention weights, and the scratch rows of all N_h x N_p samples are computed with the queries on the partitions; one batched indirect DMA gathers the N_h N_p rows of every query (the indices transposed into the gather’s order on the Tensor Engine), and the blend is one multiply and one reduction over (point, tap) per group of heads. Border padding clamps the coordinates to the level, which equals clamping each corner (the corner weights sum to one). Coordinates, weights and sums are fp32; the output is rounded once. With LNC=2 each program builds its own scratch and takes half of the query tiles.
Background#
ms_deformable_attention_quad computes multi-scale deformable attention forward with the same call signature as ms_deformable_attention, routing every bilinear sample through an HBM “quad” scratch that stores each pixel’s 2x2 neighbourhood so a sample is a single gathered row.
API Reference#
Source code for this kernel API can be found at: ms_deformable_attention_quad.py
ms_deformable_attention_quad#
- nkilib.experimental.deformable_attention.ms_deformable_attention_quad(value: nl.NkiTensor, spatial_shapes: tuple, level_start_index: tuple, sampling_locations: nl.NkiTensor, attention_weights: nl.NkiTensor, value_layout: str = 'BLNC', sampling_locations_layout: str = 'BQHLP2', align_corners: bool = False, padding_mode: str = 'zeros') nl.NkiTensor#
Multi-scale deformable attention forward, the same call as ms_deformable_attention, through a quad scratch.
- Parameters:
value (
nl.NkiTensor) – (B, L, N_h, C_h) (“BLNC”) or (B, N_h, L, C_h) (“BNLC”), float32, bfloat16 or float16.spatial_shapes (
tuple) – ((H_l, W_l), …) per level.level_start_index (
tuple) – First row of each level in L.sampling_locations (
nl.NkiTensor) – (B, N_q, N_h, N_l, N_p, 2) (“BQHLP2”) or (B, 2, N_q, N_h, N_l, N_p) (“B2QHLP”), normalized (x, y) in [0, 1], float32, bfloat16 or float16.attention_weights (
nl.NkiTensor) – (B, N_q, N_h, N_l, N_p), float32, bfloat16 or float16.value_layout (
str) – “BLNC” or “BNLC”.sampling_locations_layout (
str) – “BQHLP2” or “B2QHLP”.align_corners (
bool) – True: [0, 1] maps to [0, size - 1]; False: to [-0.5, size - 0.5].padding_mode (
str) – “zeros” or “border”.
- Returns:
(B, N_q, N_h * C_h) in value’s dtype.
- Return type:
nl.NkiTensor
Notes:
N_h * N_p <= 128 (padded to 32, 64 or 128 gather slots per query); the work-item tiles must fit SBUF.
This document is relevant for: Trn2, Trn3