How to Use MPS for Mac GPU with PyTorch
To use
mps for Mac GPU acceleration in PyTorch, set your device with torch.device('mps') and move your tensors and models to this device using .to(device). This enables PyTorch to run computations on Apple Silicon GPUs via Metal Performance Shaders.Syntax
Use torch.device('mps') to specify the Mac GPU device. Then move your tensors and models to this device with .to(device). This tells PyTorch to run operations on the Mac GPU instead of CPU.
device = torch.device('mps'): sets the device to Mac GPUtensor.to(device): moves tensor to GPUmodel.to(device): moves model to GPU
python
import torch device = torch.device('mps') # Set device to Mac GPU tensor = torch.randn(3, 3).to(device) # Move tensor to GPU # Example model move # model = MyModel() # model.to(device)
Example
This example shows how to create a tensor on the Mac GPU using MPS and perform a simple operation. It demonstrates device setup, tensor creation, and moving data to the GPU.
python
import torch # Check if MPS is available if torch.backends.mps.is_available(): device = torch.device('mps') else: device = torch.device('cpu') # Create tensor on CPU x_cpu = torch.tensor([1.0, 2.0, 3.0]) # Move tensor to MPS device x_mps = x_cpu.to(device) # Perform operation on MPS y_mps = x_mps * 2 # Move result back to CPU y_cpu = y_mps.to('cpu') print('Original tensor:', x_cpu) print('Tensor on MPS device:', x_mps) print('Result after multiplication:', y_cpu)
Output
Original tensor: tensor([1., 2., 3.])
Tensor on MPS device: tensor([1., 2., 3.], device='mps:0')
Result after multiplication: tensor([2., 4., 6.])
Common Pitfalls
Common mistakes when using MPS with PyTorch on Mac include:
- Not checking if MPS is available before using it, which causes errors on unsupported Macs.
- Forgetting to move both model and data to the
mpsdevice, leading to device mismatch errors. - Using operations or PyTorch features not yet supported on MPS, which can cause runtime errors.
Always check torch.backends.mps.is_available() before using MPS and move all tensors and models to the same device.
python
import torch # Wrong way: no availability check # device = torch.device('mps') # May error if MPS unsupported # Right way: if torch.backends.mps.is_available(): device = torch.device('mps') else: device = torch.device('cpu') # Also remember to move model and data: # model.to(device) # data.to(device)
Quick Reference
Summary tips for using MPS on Mac with PyTorch:
- Check availability with
torch.backends.mps.is_available(). - Set device with
torch.device('mps'). - Move all tensors and models to the MPS device.
- Use CPU fallback if MPS is not available.
- Test your code for unsupported operations on MPS.
Key Takeaways
Always check if MPS is available before using it to avoid errors on unsupported Macs.
Set device with torch.device('mps') and move all tensors and models to this device.
Use CPU fallback when MPS is not available to keep code portable.
Not moving model and data to the same device causes runtime errors.
Some PyTorch features may not be fully supported on MPS yet.