Skip to content

fix(rt-detr): use float32 on MPS in build_2d_sinusoidal_position_embedding#46513

Closed
Shylin26 wants to merge 1 commit into
huggingface:mainfrom
Shylin26:fix/rt-detr-mps-float64
Closed

fix(rt-detr): use float32 on MPS in build_2d_sinusoidal_position_embedding#46513
Shylin26 wants to merge 1 commit into
huggingface:mainfrom
Shylin26:fix/rt-detr-mps-float64

Conversation

@Shylin26

@Shylin26 Shylin26 commented Jun 8, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #46159

build_2d_sinusoidal_position_embedding in both RT-DETR and RT-DETRv2
hardcodes dtype=torch.float64 for internal sinusoidal arithmetic.
MPS (Apple Silicon) does not support float64, causing a TypeError when
running inference on Apple Silicon devices.

This PR uses float64 on CUDA/CPU for precision, and falls back to
float32 on MPS devices only.

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

Who can review?

@yonigozlan @molbap

…soidal_position_embedding for MPS compatibility

MPS (Apple Silicon) does not support float64. The hardcoded dtype=torch.float64
in build_2d_sinusoidal_position_embedding causes a TypeError when running
RT-DETR and RT-DETRv2 on MPS devices. Replace with float32 which is
supported on all backends (CPU, CUDA, MPS).

Fixes huggingface#46159

Signed-off-by: Shylin26 <parishachauhan26@gmail.com>
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

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

run-slow: rt_detr, rt_detr_v2

@Shylin26

Shylin26 commented Jun 8, 2026

Copy link
Copy Markdown
Author

Submitted PR #46513 to fix this — uses float64 on CUDA/CPU for precision and falls back to float32 on MPS devices to avoid the TypeError on Apple Silicon.

@pidefrem

pidefrem commented Jun 9, 2026

Copy link
Copy Markdown

Thanks for picking this up — we hit this exact MPS crash with RT-DETRv2 and have been patching it locally, so glad to see a fix in flight.

The device-conditional approach looks right — keeps float64 precision on CUDA/CPU where it's supported, and gracefully falls back to float32 on MPS where it's not.

Two suggestions:

  1. Device check robustness: str(device) != "mps" will miss torch.device("mps:0") or other variants, and str(None) is "None" which happens to work but only by accident (the signature accepts device: torch.device | None = None). Consider:

    compute_dtype = torch.float32 if device is not None and torch.device(device).type == "mps" else torch.float64
  2. Propagation via modular: The canonical definition lives in modular_vit_mae.py (line 44). modular_rt_detr.py and modular_aimv2.py import it from there, and the code-generation system inlines it into all 8 generated modeling_*.py files (vit_mae, rt_detr, rt_detr_v2, aimv2, deimv2, d_fine, pp_doclayout_v2, pp_doclayout_v3). The fix should go in modular_vit_mae.py and be propagated with make fix-repo — otherwise the other 6 files remain broken. PR fix(mps): build_2d_sinusoidal_position_embedding crashes on Apple Silicon due to float64 on MPS device #46174 already did this correctly.

On the CUDA perf concern raised on #46174: this is a one-time positional embedding computation (a few hundred positions), not a hot path. The device-conditional approach avoids the question entirely since CUDA keeps float64 on-device as before — no CPU↔device transfer, no perf regression.

Happy to help test on MPS if needed — we use RT-DETRv2 via docling for layout detection on Apple Silicon daily.

@Rocketknight1

Copy link
Copy Markdown
Member

Duplicate of #46174

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.

[RT-DETRv2] MPS crash: build_2d_sinusoidal_position_embedding hardcodes torch.float64, breaking Apple Silicon / MPS inference

3 participants