Skip to content

[Bugfix] Add MPS float64->float32 downcast#3548

Merged
vmoens merged 2 commits intopytorch:mainfrom
bsprenger:bsprenger/fix/mps-downcast
Mar 7, 2026
Merged

[Bugfix] Add MPS float64->float32 downcast#3548
vmoens merged 2 commits intopytorch:mainfrom
bsprenger:bsprenger/fix/mps-downcast

Conversation

@bsprenger
Copy link
Contributor

@bsprenger bsprenger commented Mar 6, 2026

Description

Add MPS float64 -> float32 downcast in _default_dtype_and_device() to prevent crashes when moving specs to MPS device.

MPS does not support float64 tensors. This is a problem e.g. when gymnasium/MuJoCo specs (which use float64) are created on CPU and later moved to MPS via .to("mps"), the spec constructors call torch.full(..., device="mps", dtype=torch.float64) which raises an error.

The fix adds a guard in _default_dtype_and_device() that automatically downcasts float64 -> float32 with a UserWarning when the device is MPS.

Motivation and Context

Gymnasium and MuJoCo environments produce float64 observation/action spaces by default. When a user creates a collector with env_device="mps" (e.g., in the a trainer), it will try to move the specs to MPS with float64, and crashes.

The fix prevents this crash by ensuring float64 specs are always downcast to float32 on MPS.

Fixes: #3549

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 6, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3548

Note: Links to docs will display an error until the docs builds have been completed.

❌ 4 New Failures, 1 Cancelled Job

As of commit fadc7b8 with merge base 491ed0f (image):

NEW FAILURES - The following jobs have failed:

CANCELLED JOB - The following job was cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 6, 2026
@github-actions github-actions bot added the BugFix label Mar 6, 2026
Copy link
Collaborator

@vmoens vmoens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!
Maybe let's add a test to check that there is no float64 support with mps? Like that the test will fail if it ever becomes a thing and we know we can upgrade

@bsprenger
Copy link
Contributor Author

@vmoens Thanks for the review, I added a test for that. Thanks!! 😄

@vmoens vmoens merged commit b6326f7 into pytorch:main Mar 7, 2026
115 of 120 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MPS device crashes on float64 specs when moving environments with .to('mps')

2 participants