.. meta::
    :description: Multi-scale deformable attention forward, the same call as ms_deformable_attention, through a quad scratch.
    :date-modified: 11/30/2026

.. currentmodule:: nkilib.experimental.deformable_attention

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 <https://github.com/aws-neuron/nki-library/blob/2.34/src/nkilib_src/nkilib/experimental/deformable_attention/ms_deformable_attention_quad.py>`_

ms_deformable_attention_quad
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. py:function:: 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.

   :param value: (B, L, N_h, C_h) ("BLNC") or (B, N_h, L, C_h) ("BNLC"), float32, bfloat16 or float16.
   :type value: ``nl.NkiTensor``
   :param spatial_shapes: ((H_l, W_l), ...) per level.
   :type spatial_shapes: ``tuple``
   :param level_start_index: First row of each level in L.
   :type level_start_index: ``tuple``
   :param sampling_locations: (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.
   :type sampling_locations: ``nl.NkiTensor``
   :param attention_weights: (B, N_q, N_h, N_l, N_p), float32, bfloat16 or float16.
   :type attention_weights: ``nl.NkiTensor``
   :param value_layout: "BLNC" or "BNLC".
   :type value_layout: ``str``
   :param sampling_locations_layout: "BQHLP2" or "B2QHLP".
   :type sampling_locations_layout: ``str``
   :param align_corners: True: [0, 1] maps to [0, size - 1]; False: to [-0.5, size - 0.5].
   :type align_corners: ``bool``
   :param padding_mode: "zeros" or "border".
   :type padding_mode: ``str``
   :return: (B, N_q, N_h * C_h) in value's dtype.
   :rtype: ``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.

