# RuntimeError: MPS backend does not support float64. Falling back to float32. Please cast your tensors to float32 explicitly.

- **ID:** `pytorch/mps-float64-cast-fallback`
- **Domain:** pytorch
- **Category:** type_error
- **Error Code:** `MPS_ERROR_UNSUPPORTED_DTYPE`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The Metal Performance Shaders (MPS) backend on Apple Silicon does not support float64 (double precision) tensors; any operation involving float64 tensors triggers a fallback to CPU or an error, causing performance degradation or crashes.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| PyTorch 2.0.0 | active | — | — |
| PyTorch 2.1.0 | active | — | — |
| macOS 13 Ventura | active | — | — |
| macOS 14 Sonoma | active | — | — |
| Apple M1 | active | — | — |
| Apple M2 | active | — | — |
| Apple M3 | active | — | — |

## Workarounds

1. **Cast all tensors to float32 explicitly before moving to MPS device: tensor = tensor.float().to('mps'). This ensures compatibility with MPS backend.** (95% success)
   ```
   Cast all tensors to float32 explicitly before moving to MPS device: tensor = tensor.float().to('mps'). This ensures compatibility with MPS backend.
   ```
2. **Set the default dtype to float32 at the start of the script: torch.set_default_dtype(torch.float32). This prevents accidental creation of float64 tensors from Python floats.** (90% success)
   ```
   Set the default dtype to float32 at the start of the script: torch.set_default_dtype(torch.float32). This prevents accidental creation of float64 tensors from Python floats.
   ```
3. **Use a custom wrapper function that checks dtype and casts if needed: def to_mps(tensor): return tensor.float().to('mps') if tensor.is_floating_point() else tensor.to('mps')** (85% success)
   ```
   Use a custom wrapper function that checks dtype and casts if needed: def to_mps(tensor): return tensor.float().to('mps') if tensor.is_floating_point() else tensor.to('mps')
   ```

## Dead Ends

- **Set environment variable PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable MPS fallback** — This flag controls memory watermark, not dtype support. The float64 issue remains and will cause errors. (95% fail)
- **Use torch.set_default_dtype(torch.float64) to force double precision** — This makes the problem worse by creating more float64 tensors, increasing fallback frequency and memory usage. (90% fail)
- **Install PyTorch nightly build for better MPS support** — Nightly builds may have improvements but still do not support float64 on MPS as of current versions. (85% fail)
