Guard DETR and Grounding DINO Hungarian matchers against infinite cost matrices#47048
Open
MutugiD wants to merge 1 commit into
Open
Guard DETR and Grounding DINO Hungarian matchers against infinite cost matrices#47048MutugiD wants to merge 1 commit into
MutugiD wants to merge 1 commit into
Conversation
…t matrices Under AMP/fp16, focal-style class costs can overflow to inf/NaN, which makes scipy's linear_sum_assignment raise "ValueError: cost matrix is infeasible". Replace non-finite costs with torch.finfo(dtype).max before matching, mirroring the RT-DETR fix in huggingface#47016. Adds regression tests for both matchers. Related to huggingface#47000.
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: detr, grounding_dino |
Contributor
CI recapDashboard: View test results in Grafana |
This was referenced Jul 4, 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.
What does this PR do?
Follow-up to #47000 / #47016. The RT-DETR fix in #47016 guards
RTDetrHungarianMatcheragainst non-finite cost matrices, but the same unguardedscipy.optimize.linear_sum_assignment(cost_matrix)pattern also exists in two sibling matchers that #47016 does not touch:HungarianMatcherinsrc/transformers/loss/loss_for_object_detection.py(used by the DETR / Deformable DETR family)GroundingDinoHungarianMatcherinsrc/transformers/loss/loss_grounding_dino.pyUnder AMP/fp16, the focal-style classification cost can overflow to
inf/NaN;linear_sum_assignmentthen raisesValueError: cost matrix is infeasible. This PR replaces non-finite costs withtorch.finfo(cost_matrix.dtype).maxbefore matching, following @guarin's recommendation in #47000:Tests
Adds a CPU-only regression test for each matcher:
tests/models/detr/test_modeling_detr.py::HungarianMatcherInfeasibleCostTesttests/models/grounding_dino/test_modeling_grounding_dino.py::GroundingDinoHungarianMatcherInfeasibleCostTestEach feeds a non-finite cost (a
NaNlogit and aninfbox coordinate) through the matcher and asserts it no longer raises and returns correctly-sized assignments. Both tests fail onmain(with theValueError) and pass with the guard.The same pattern is also present in
loss_deformable_detr.pyandloss_lw_detr.py. I scoped this PR to the two files flagged in #47000, but I'm happy to extend the guard to those as well if you'd prefer a single sweep.Before submitting
Who can review?
@guarin @yonigozlan