Guard Deformable DETR and LW-DETR Hungarian matchers against infinite cost matrices
Author: MutugiD
Pull request: yes
Labels: name: Code agent slop

<!-- ci-dashboard-badge:start -->
[![CI](https://transformers-ci.lor-e.huggingface.cool/badge/pr?pr=47066)](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47066)
<!-- ci-dashboard-badge:end -->

# What does this PR do?

Fixes #47065. Two more Hungarian matchers share the non-finite-cost crash first reported for RT-DETR in #47000 and still carry the unguarded `scipy.optimize.linear_sum_assignment(cost_matrix)` pattern on `main`:

- `DeformableDetrHungarianMatcher` in `src/transformers/loss/loss_deformable_detr.py`
- `LwDetrHungarianMatcher` in `src/transformers/loss/loss_lw_detr.py`

Both use the sigmoid focal classification cost, which can overflow to `inf`/`NaN` under AMP/fp16; `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 and matching the RT-DETR fix in #47016:

```python
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)
```

This completes the sweep started in #47048 (DETR + Grounding DINO) across the remaining DETR-family loss matchers.

## Tests

Adds a CPU-only regression test for each matcher:

- `tests/models/deformable_detr/test_modeling_deformable_detr.py::DeformableDetrHungarianMatcherInfeasibleCostTest`
- `tests/models/lw_detr/test_modeling_lw_detr.py::LwDetrHungarianMatcherInfeasibleCostTest`

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 fail on `main` (with the `ValueError`) and pass with the guard.

```
pytest tests/models/deformable_detr/test_modeling_deformable_detr.py::DeformableDetrHungarianMatcherInfeasibleCostTest \
       tests/models/lw_detr/test_modeling_lw_detr.py::LwDetrHungarianMatcherInfeasibleCostTest
```

## Before submitting

- [x] This PR fixes a bug discussed in an issue: #47065
- [x] Did you make sure to update the documentation with your changes? (N/A — no user-facing API change)
- [x] Did you write any new necessary tests?

## Who can review?

@guarin @yonigozlan

Comments:
The `tests_non_model` failure here is unrelated to this PR. All 6 failing tests are CLI tests failing with the same error:

```
tests/cli/test_chat.py::test_help
tests/cli/test_download.py::test_cli_download
tests/cli/test_download.py::test_cli_download_trust_remote
tests/cli/test_serve.py::test_host_port_blocking
tests/cli/test_system.py::test_cli_env
tests/cli/test_system.py::test_cli_version
-> AttributeError: 'HFCliTyperGroup' object has no attribute '_add_completion'
```

This is the `huggingface_hub` 1.22 CLI breakage tracked in #47059 and fixed by #47064; it currently affects `main` and every open PR.

This PR only touches the two loss matchers and their tests - the `tests_torch` shards that run the new regression tests are green. I'll rebase onto `main` once #47064 is merged so the full run goes green.


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

run-slow: deformable_detr, lw_detr

<!-- ci-dashboard-recap:start -->

---

### CI recap

**Dashboard:** [View test results in Grafana](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47066)
**Latest run:** [28770861585:2](https://github.com/huggingface/transformers/actions/runs/28770861585)
**Result:** `failure` | **Jobs:** 15 | **Tests:** 171,364 | **Failures:** 6 | **Duration:** 25h 30m

<!-- ci-dashboard-recap:end -->