Skip to content

Guard DETR and Grounding DINO Hungarian matchers against infinite cost matrices#47048

Open
MutugiD wants to merge 1 commit into
huggingface:mainfrom
MutugiD:fix-matcher-infinite-cost
Open

Guard DETR and Grounding DINO Hungarian matchers against infinite cost matrices#47048
MutugiD wants to merge 1 commit into
huggingface:mainfrom
MutugiD:fix-matcher-infinite-cost

Conversation

@MutugiD

@MutugiD MutugiD commented Jul 4, 2026

Copy link
Copy Markdown

CI

What does this PR do?

Follow-up to #47000 / #47016. The RT-DETR fix in #47016 guards RTDetrHungarianMatcher against non-finite cost matrices, but the same unguarded scipy.optimize.linear_sum_assignment(cost_matrix) pattern also exists in two sibling matchers that #47016 does not touch:

  • HungarianMatcher in src/transformers/loss/loss_for_object_detection.py (used by the DETR / Deformable DETR family)
  • GroundingDinoHungarianMatcher in src/transformers/loss/loss_grounding_dino.py

Under AMP/fp16, the focal-style classification cost can overflow to inf/NaN; linear_sum_assignment then raises ValueError: cost matrix is infeasible. This PR replaces non-finite costs with torch.finfo(cost_matrix.dtype).max before matching, following @guarin's recommendation in #47000:

max_value = torch.finfo(cost_matrix.dtype).max
cost_matrix = torch.nan_to_num(cost_matrix, nan=max_value, posinf=max_value, neginf=max_value)

Tests

Adds a CPU-only regression test for each matcher:

  • tests/models/detr/test_modeling_detr.py::HungarianMatcherInfeasibleCostTest
  • tests/models/grounding_dino/test_modeling_grounding_dino.py::GroundingDinoHungarianMatcherInfeasibleCostTest

Each feeds a non-finite cost (a NaN logit and an inf box coordinate) through the matcher and asserts it no longer raises and returns correctly-sized assignments. Both tests fail on main (with the ValueError) and pass with the guard.

pytest tests/models/detr/test_modeling_detr.py::HungarianMatcherInfeasibleCostTest \
       tests/models/grounding_dino/test_modeling_grounding_dino.py::GroundingDinoHungarianMatcherInfeasibleCostTest

The same pattern is also present in loss_deformable_detr.py and loss_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

…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.
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: detr, grounding_dino

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 28688965854:2
Result: success | Jobs: 15 | Tests: 171,199 | Failures: 0 | Duration: 26h 56m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant