Repository navigation
Make CombinedNonMaxSuppression ordering total - #128930
Merged
copybara-service[bot] merged 2 commits intoNov 12, 2026
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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.
dmiltr3
approved these changes
Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 intouint64heap keys, sorts only the emitted prefix withnth_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
CombinedNonMaxSuppressionOpTestcases.I could not run bazel, so the tests and the op itself are unbuilt. libc++ is untested. The GPU path is untouched.