Skip to content

Cast float64 targets before moving them to the device#69

Open
devYRPauli wants to merge 1 commit into
google-research:mainfrom
devYRPauli:fix-pytorch-mps-float64-target
Open

Cast float64 targets before moving them to the device#69
devYRPauli wants to merge 1 commit into
google-research:mainfrom
devYRPauli:fix-pytorch-mps-float64-target

Conversation

@devYRPauli

Copy link
Copy Markdown
Contributor

Fixes #68.

_predict_step_pytorch moved y to the model device before casting float64 down to float32. MPS does not support float64, so the move itself raised:

TypeError: Cannot convert a MPS Tensor to float64 dtype as the MPS framework
doesn't support float64. Please use float32 instead.

numpy's default float dtype is float64, so an ordinary float target array triggers this on any Apple Silicon host. X_t already fuses the cast into its own .to(device, dtype=torch.float32); only y was affected.

This does the existing conditional cast on the CPU tensor and moves afterwards. The cast stays conditional on purpose: classifier targets are encoded as int64 and regressor scaling can yield float16, so casting unconditionally would silently change those dtypes.

Adds a regression test that drives the real _batch_forward path with float64 targets on a model whose parameters live on MPS. It fails with the TypeError above against the previous ordering, and passes with the fix.

One note on the test: it skips when MPS is unavailable, so it will skip on CI runners without an Apple GPU. The crash only reproduces on that backend - on CPU the old and new orderings are indistinguishable, since .to("cpu") accepts float64 and the later cast then fixes it. A CPU-only test would pass against the broken code, so it would not be a real guard. Happy to change the approach if you would prefer something else here.

@google-cla

google-cla Bot commented Jul 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

_predict_step_pytorch moved y to the model device first and only then cast
float64 down to float32. MPS does not support float64, so the move itself
raised before the cast could run:

  TypeError: Cannot convert a MPS Tensor to float64 dtype as the MPS
  framework doesn't support float64. Please use float32 instead.

numpy's default float dtype is float64, so an ordinary float target array
hits this on any Apple Silicon host. X_t already fuses the cast into its
own .to(device, dtype=torch.float32), so only y was affected.

Do the existing conditional cast on the CPU tensor and move afterwards.
The cast stays conditional on purpose: classifier targets are encoded as
int64 and regressor scaling can yield float16, so casting unconditionally
would silently change those dtypes.

Adds a regression test that drives the real _batch_forward path with
float64 targets on a model whose parameters live on MPS. It fails with the
TypeError above against the previous ordering. The test skips when MPS is
unavailable, since the crash only reproduces on that backend.

Signed-off-by: Yash Raj Pandey <yashpn62@gmail.com>
@devYRPauli
devYRPauli force-pushed the fix-pytorch-mps-float64-target branch from 3f73a0f to e073d84 Compare July 16, 2026 03:57
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.

PyTorch backend: crash on Apple MPS with float64 targets — device move precedes the float64->float32 guard

1 participant