Skip to main content

Fixed: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

I multiplied weights on my Mac's GPU by values I'd made without a device, and PyTorch stopped with RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!. One tensor was on the GPU and the other on the CPU. Making both on the same device fixed it.

What I Was Doing​

I was computing three weighted values in PyTorch 2.15.0 on an Apple M1 Pro, with the work on the GPU. The script picks the GPU PyTorch can use, or the CPU when there's none, as How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS) explains, then multiplies the two tensors value by value:

same_device.py
import torch

accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")

weights = torch.tensor([0.2, 0.5, 0.3], device=device)
values = torch.tensor([10.0, 20.0, 30.0])

print(weights * values)
Terminal
uv run python same_device.py

What I Expected​

Three numbers: 0.2 × 10, 0.5 × 20 and 0.3 × 30.

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/pytorch-basics/same_device.py", line 9, in <module>
print(weights * values)
~~~~~~~~^~~~~~~~
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!

The terminal after uv run python same_device.py: a traceback pointing at print(weights * values) on line 9, ending in RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!

The multiplication on line 9 failed, and the message names the two devices it found: mps:0, the Mac's GPU, and cpu.

What I Checked​

Which tensor was where. I printed each one's device:

show_devices.py
import torch

accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")

weights = torch.tensor([0.2, 0.5, 0.3], device=device)
values = torch.tensor([10.0, 20.0, 30.0])

print(weights.device)
print(values.device)
Terminal
uv run python show_devices.py
Output
mps:0
cpu

The terminal after uv run python show_devices.py: mps:0, then cpu.

weights was on the GPU, mps:0, and values on the cpu. PyTorch's tensors tutorial explains why: "By default, tensors are created on the CPU. We need to explicitly move tensors to the accelerator using .to method (after checking for accelerator availability)."

Why It Happens​

weights was made with device=device, so on the GPU. values was made without a device, so on the CPU, the default. PyTorch runs an operation on one device and doesn't move tensors between devices on its own, so an operation that meets tensors on two devices stops with this error.

The Fix​

Make values on the same device as weights, with the same device=device:

same_device.py
import torch

accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")

weights = torch.tensor([0.2, 0.5, 0.3], device=device)
values = torch.tensor([10.0, 20.0, 30.0], device=device)

print(weights * values)

The fixed same_device.py in a macOS-style code window: values is now made with device=device, like weights.

Terminal
uv run python same_device.py
Output
tensor([ 2., 10., 9.], device='mps:0')

The terminal after running the fixed same_device.py: tensor([ 2., 10.,  9.], device='mps:0').

After the Fix​

The script printed 2, 10 and 9: 0.2 × 10, 0.5 × 20 and 0.3 × 30, computed on mps:0, the GPU. The commands work unchanged in PowerShell, and on a computer without a GPU, the same code puts both tensors on the CPU.

Common Mistakes​

MistakeWhat happensDo this instead
values.to(device) on a line of its ownvalues stays on the CPU: a tensor moved this way without keeping the result still printed cpuvalues = values.to(device)
Writing a device name, such as "cuda", into the codeThe code runs only where that device exists: "cuda" on this Mac failed with AssertionError: Torch not compiled with CUDA enabledUse the device the code picks, as above

Troubleshooting​

SymptomLikely causeFix
…found at least two devices, mps:0 and cpu!A tensor was made or left on the CPUMake it with device=device, or move it with .to(device)
Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)The same mix-up in a matrix product with @The fix for the matrix product

FAQ​

What if the tensor already exists on the CPU?​

Move it, and keep the result: values = values.to(device). How to Move a Tensor to the GPU in PyTorch shows it.

Can't PyTorch move the tensor for me?​

No. As the tutorial puts it, "We need to explicitly move tensors to the accelerator".

Conclusion​

weights was on the GPU and values on the CPU, so weights * values stopped with Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!. Making values with device=device too put both on mps:0, and the script printed 2, 10 and 9.

Additional Resources​

  • Tensors tutorial — where tensors are created, and moving them to the accelerator.
  • Tensor.to — moving a tensor to another device.
  • MPS backend — how PyTorch uses the GPU of an Apple silicon Mac.