Skip to content

Make CombinedNonMaxSuppression ordering total - #128930

Merged
copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
teerthsharma:tf-nms-total-order
Nov 12, 2026
Merged

copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
teerthsharma:tf-nms-total-order

Conversation

@teerthsharma

@teerthsharma teerthsharma commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #128926.

Combined NMS sorted the merged result by score only, after prefilling the padding with score -1.0, so real scores below -1.0 lost to the padding and were dropped. On 2.21.0, scores [-2, -3, -4] with box 2 suppressed return [-1, -2] instead of [-2, -3]. Equal scores also came back in heap order, not index order.

Commit 1 makes both orders total (real before padding, then score desc, class asc, box index asc; per-class ties by lower index). It also cost about 25%, nearly all in DoNMSPerClass. Commit 2 packs candidates into uint64 heap keys, sorts only the emitted prefix with nth_element, and returns early on padding pairs (review suggestion). Output is unchanged.

Standalone build of the two functions, g++ 15 -O2, pinned core, median of 61 reps x 3, against master: 4000x90 -24% (big boxes) and -22% (detection-like), 100x90 -17%, dense -14%, padding-heavy -25%, 1000x1 within 2%. All-equal scores is +41% (commit 1 alone: +77%).

Against an O(n^2) oracle, master matches 8/18 named cases and mismatches 1,459/5,000 tie-rich and 8,833/30,000 wild inputs (NaN, inf, +-0.0, zero-area boxes); this change matches all of them. ASan and UBSan are clean on g++ 15 and clang 21. Four new CombinedNonMaxSuppressionOpTest cases.

I could not run bazel, so the tests and the op itself are unbuilt. libc++ is untested. The GPU path is untouched.

The merged result is sorted by score only after being prefilled with
padding (score -1.0), so real scores below -1.0 lose to padding. Equal
scores in DoNMSPerClass pop in unspecified heap order. Order by score,
then class, then box index, with real entries before padding, and break
per-class ties by lower box index.

Fixes tensorflow#128926
@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Oct 12, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the non-max suppression operation to ensure deterministic sorting behavior independent of the std::sort implementation by refining the comparators in DoNMSPerClass and SelectResultPerBatch to break ties using class and box indices, and adds corresponding unit tests. Feedback suggests optimizing the rc_cmp comparator in SelectResultPerBatch to immediately return false when both elements are padding entries, which avoids redundant comparisons and helps mitigate performance regression.

The total-order comparators made CombinedNonMaxSuppression about 25%
slower, mostly in the per-class priority queue. Keep per-class candidates
in a vector heap of packed uint64 keys (ordered score bits, then the
complemented box index), select the emitted prefix with nth_element before
sorting it, and return early when both entries are padding, as suggested in
review. Output is unchanged.

Add a signed-zero test: the key maps -0.0 and +0.0 to one value.
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Oct 12, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Oct 12, 2026
@nithyak0204 nithyak0204 removed awaiting review Pull request awaiting review kokoro:force-run Tests on submitted change labels Oct 13, 2026
@copybara-service
copybara-service Bot merged commit d409fa6 into tensorflow:master Nov 12, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:core issues related to core part of tensorflow prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

tf.image.combined_non_max_suppression drops detections scoring below -1 and returns equal scores in unspecified order

4 participants