0 Comments

With the rise of Apple Silicon chips like the M1, M2, and M3, developers using macOS for deep learning have long desired access to GPU acceleration. PyTorch answered that call with the torch.mps backend, allowing native GPU utilization via Apple’s Metal Performance Shaders (MPS).

In this blog post, we’ll explore what torch.mps is, how to use it effectively, and how it compares to CUDA. You’ll also see practical code examples, common methods, and solutions to frequently encountered errors.


🔍 What is torch.mps?

Definition:
torch.mps is a PyTorch backend that enables GPU acceleration on Apple devices using the Metal Performance Shaders (MPS) framework.

This feature became available starting with PyTorch 1.12, making it possible to run deep learning models using Apple’s integrated GPU instead of the CPU—significantly improving training speed on macOS.


💻 Requirements to Use torch.mps

  • macOS 12.3 or later
  • Apple Silicon (M1, M2, M3) or supported Intel Macs with Apple GPU
  • PyTorch 1.12 or later (latest recommended)

🔧 How to Check MPS Availability in PyTorch

import torch

if torch.backends.mps.is_available():
print("MPS is available ✅")
else:
print("MPS is not available ❌")

If MPS is supported, you can move your models and tensors to the MPS device just like CUDA.


🧪 torch.mps Code Examples

✅ Move Tensors to MPS

import torch

# Check MPS availability
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")

# Create a tensor on MPS
x = torch.tensor([1.0, 2.0, 3.0], device=device)
print(x)

✅ Train Model on MPS

import torch
import torch.nn as nn
import torch.optim as optim

device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")

model = nn.Linear(10, 2).to(device)
data = torch.randn(5, 10).to(device)
target = torch.randn(5, 2).to(device)

criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# Training step
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()

🧰 Common torch.mps Methods and Usage Tips

MethodDescription
torch.device("mps")Assigns tensors/models to the MPS device
torch.backends.mps.is_available()Checks if MPS backend is supported
.to("mps")Moves models/tensors to MPS device

🐛 Errors & Debugging Tips

🔴 Model or Tensor not moving to MPS

Fix: Check availability before assigning:

if torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")

🔴 RuntimeError: “Expected all tensors to be on the same device”

Fix: Ensure all tensors (inputs, targets, model) are moved to MPS:

pythonCopyEditdata.to(device), target.to(device), model.to(device)

🔴 Performance is not as fast as CUDA

Note: While MPS offers GPU acceleration, CUDA on NVIDIA GPUs still outperforms it in most scenarios. MPS is best suited for moderate workloads and development testing on MacBooks.


⚖️ MPS vs CUDA: A Quick Comparison

FeatureMPS (torch.mps)CUDA (torch.cuda)
PlatformApple Silicon (macOS)NVIDIA GPUs (Windows/Linux/macOS)
Speed (training)Moderate to High (depending on task)Very High
AvailabilityApple hardware onlyNVIDIA GPUs only
Support in PyTorchStable (1.12+)Mature and fully supported
Suitable for ProductionNoYes

🙋‍♂️ People Also Ask (FAQ)

❓ What is MPS in Torch?

torch.mps is a PyTorch backend that enables GPU acceleration using Apple’s Metal Performance Shaders (MPS) framework. It allows PyTorch code to run on the Apple GPU instead of CPU, improving training speed on MacBooks.


❓ Is MPS better than CUDA?

MPS is convenient for Apple Silicon users but doesn’t match CUDA’s raw performance on NVIDIA GPUs. CUDA is more optimized for large-scale training and production. MPS is ideal for lightweight local development.


❓ What is MPS in deep learning?

In deep learning, MPS refers to Metal Performance Shaders—a GPU-accelerated framework from Apple. With PyTorch support via torch.mps, developers can now leverage MPS for model training on macOS systems.


❓ What is CUDA and MPS?

Both are backends used to access GPU resources:

  • CUDA: GPU backend for NVIDIA cards, highly optimized.
  • MPS: GPU backend for Apple Silicon, great for Mac-based development.

🚀 Conclusion

With torch.mps, PyTorch finally bridges the gap for Mac users who want to train models using their Apple GPU. It’s a welcome alternative to CPU-bound development, especially for students, researchers, and developers working on-the-go.

While it may not match CUDA in production-level training, torch.mps is a game-changer for day-to-day experimentation and model prototyping on macOS.

Know more

torch.mps in PyTorch

torch.export in PyTorch

torch.backends in PyTorch

What is torch.distributed.tensor ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts