Cast float64 targets before moving them to the device#69
Open
devYRPauli wants to merge 1 commit into
Open
Conversation
devYRPauli
requested review from
abhidas,
erzel,
rajatsen91,
siriuz42,
tamannarayan and
weihaokong
as code owners
July 16, 2026 00:14
|
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
force-pushed
the
fix-pytorch-mps-float64-target
branch
from
July 16, 2026 03:57
3f73a0f to
e073d84
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #68.
_predict_step_pytorchmovedyto the model device before casting float64 down to float32. MPS does not support float64, so the move itself raised:numpy's default float dtype is float64, so an ordinary float target array triggers this on any Apple Silicon host.
X_talready fuses the cast into its own.to(device, dtype=torch.float32); onlyywas 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_forwardpath 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.