NMS (Non-Maximum Suppression)#
Overview#
The Non-Maximum Suppression (NMS) primitive operates on a tile of int32_t values. For every
pixel of the valid output region an \(N \times N\) window centered on that pixel is searched. If
the pixel is the maximum of the window it is copied to the output, otherwise the output pixel is set
to zero. Ties are resolved by position: among equal maxima within the window, the pixel that appears
last in raster order (left to right, top to bottom) wins, that is, the one with the largest row
index and, if those are equal, the largest column index. A rectangular plateau of equal values
therefore leaves exactly one surviving pixel, at its bottom-right corner, rather than keeping all of
them.
Requirements#
The NMS primitive has the following requirements:
The data type
DataTypemust beint32_t. The window sizeNmsSizemust be 3 or 5. The type is a template parameter, and the vector types it maps to are selected through theNmsVectorTypes<DataType>traits struct, so additional types can be added by supplying a new specialization and the matching load / zero cases; onlyint32_tis supported today.The output is
NmsSize - 1pixels smaller than the input in both dimensions and offset byNmsSize / 2, i.e.output(0, 0)corresponds toinput(NmsSize / 2, NmsSize / 2). Border handling is the caller’s responsibility.widthandheightmust each be at leastNmsSize - 1and at most0xFFFF; the line pitches are in elements and must also fit in0xFFFF.outputLinePitchmust be at leastwidth - (NmsSize - 1).The output buffer must hold
height - (NmsSize - 1)rows rounded up to a multiple of 16, because the inner loops over-run to their unroll factor and rely on agen wraparound.The scratch buffer must hold at least
ScratchElements(width, height)elements, which the primitive exposes as astatic constexprhelper.The input must be reachable by agen port A, the scratch by port C and the output by port B, so the caller must place them in the matching VMEM superbanks.
The input may be a circular buffer. Pass its base as
cbStartand its length in elements ascbLen, and the primitive wraps both of the agens that read the source, so a tile whose rows straddle the end of the ring is read correctly. PasscbLen = 0for a plain buffer.
Implementation Details#
Separable Two-Pass Maximum Filter#
The window maximum is computed as a separable two-pass maximum filter, which costs \(2N\) taps per pixel instead of \(N^2\):
where \(r = N / 2\) and \(center = input[y + r, x + r]\).
The horizontal pass runs over every input row, including the vertical halo rows, because the
vertical pass consumes NmsSize scratch rows per output row.
Rank-Bias Tie-Break#
Each tap gets a rank bias added to its value before the maximum is taken: horizontal tap dx
contributes \(+dx\) and vertical tap dy contributes \(+N \cdot dy\), so the neighbor at
(dy, dx) carries a total bias of \(b = dy \cdot N + dx\). Because \(b\) increases
monotonically in raster order, an exact tie is won by the tap that appears last in raster order,
without extra compare or select instructions. The center tap carries a bias of \(r N + r\),
which is 4 for a 3x3 window and 12 for a 5x5 window; this is the constant the output predicate
compares against.
Note
The bias is added to the pixel value, so the decision is exact only where the center differs from every other pixel of its window by more than \(B = (N^2 - 1) / 2\) (4 for 3x3, 12 for 5x5). Within that margin it can err in either direction: a neighbor up to \(B - 1\) below the center can still suppress it, and a neighbor up to \(B\) above the center can fail to suppress it.
Intrinsics#
The NMS primitive uses the vmax3 intrinsic, a single-instruction three-input maximum.
vintx vmax3(vintx src1, vintx src2, vintx src3);
A 3x3 window therefore needs exactly one vmax3 per vector half, and a 5x5 window a two-level
tree: vmax3(v0, v1, v2) followed by vmax3(vmax, v3, v4). The two halves of the dvintx are
processed explicitly rather than through the dvmax3 wrapper, which gives the scheduler two
independent dependency chains.
The output selection is branchless, using dvmux against the lane mask produced by the comparison:
dvintx vpred = ((vcenter + kCenterBias) == vmax);
dvintx output = dvmux(vpred, vcenter, vZero);
Address Generators#
The whole loop nest (taps by column-blocks by rows) is encoded in five AgenCFG descriptors
built once by Init, so the C loop is a single flat counter with no pointer arithmetic and no
induction variables. Update re-bases only the three pointer-dependent configurations via
cupvaModifyAgenCfgBase, which is what makes the primitive cheap to re-target per tile.
The vertical pass reads the center tap from the original input rather than lane-extracting it from
the scratch row, trading one extra load for no shuffle network. Because that tap walks the same
buffer as the horizontal pass, it carries the same circular-buffer configuration, and Update
pre-wraps its base pointer before re-binding it.
Compatibility#
Requires PVA SDK 2.6.0 and later.