Fixed: RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)
I multiplied a matrix on my Mac's GPU by one on the CPU with @, and PyTorch stopped with RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm). mat2 is the second matrix of the product. Moving it to the GPU fixed it.
What I Was Doing
I was multiplying two matrices in PyTorch 2.15.0 on an Apple M1 Pro. 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. It made the first matrix there and the second without a device:
import torch
accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")
a = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device)
b = torch.tensor([[5.0, 6.0], [7.0, 8.0]])
print(a @ b)
uv run python matmul_devices.py
What I Expected
The matrix product of a and b.
What I Got
Traceback (most recent call last):
File "/Users/you/source/pytorch-basics/matmul_devices.py", line 9, in <module>
print(a @ b)
~~^~~
RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)

The product on line 9 failed before it started: PyTorch was still checking its arguments.
What I Checked
First, what the names mean. mm is PyTorch's matrix product of two matrices, and its reference says it "Performs a matrix multiplication of the matrices input and mat2": mat2 is the second matrix, b. Then where each matrix was:
import torch
accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")
a = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device)
b = torch.tensor([[5.0, 6.0], [7.0, 8.0]])
print(a.device)
print(b.device)
uv run python matmul_show_devices.py
mps:0
cpu

a was on the GPU, mps:0, and b on the cpu.
Why It Happens
b was made without a device, so PyTorch made it on the CPU. PyTorch's tensors tutorial says so: "By default, tensors are created on the CPU. We need to explicitly move tensors to the accelerator using .to method". The product was going to run on the GPU, where a is, and stopped when it found its second matrix somewhere else.
The Fix
Move b to the same device for the product, with .to(device):
import torch
accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")
a = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device)
b = torch.tensor([[5.0, 6.0], [7.0, 8.0]])
print(a @ b.to(device))

uv run python matmul_devices.py
tensor([[19., 22.],
[43., 50.]], device='mps:0')
![The terminal after running the fixed matmul_devices.py: tensor([[19., 22.], [43., 50.]], device='mps:0').](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/fixes/2026/pytorch-mat2-on-cpu-04-fixed.webp)
After the Fix
The product came out on mps:0: 19 is 1 × 5 + 2 × 7, the first row of a times the first column of b. The commands work unchanged in PowerShell. To keep b on the GPU for later work too, make it there in the first place, with device=device.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
b.to(device) on a line of its own | b stays on the CPU: .to() returns a new tensor and leaves the original where it was | Use the result, as the fix does, or b = b.to(device) |
Writing a device name, such as "cuda", into the code | The code runs only where that device exists: "cuda" on this Mac failed with AssertionError: Torch not compiled with CUDA enabled | Use the device the code picks |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU | The second matrix is on the CPU | Move it with .to(device) |
Tensor for argument #1 'mat1' is on CPU, but expected it to be on GPU | The first matrix is on the CPU: with a on the CPU and b on the GPU, the message named mat1 | Move the first one |
Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu! | The same mix-up in another operation, such as * | The fix for that message |
FAQ
Why does the message say mm when I wrote @?
@ between two matrices runs PyTorch's matrix product, and the message names the operation that checked the arguments: mm.
How do I avoid it from the start?
Make every tensor on the device the code picked, with device=device, or move it there once with .to(device) and keep the result. How to Move a Tensor to the GPU in PyTorch shows both.
Conclusion
a @ b stopped with Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm) because b, the second matrix, was on the CPU while a was on mps:0. a @ b.to(device) moved it first, and the product came out on the GPU.
Additional Resources
- torch.mm — the matrix product the message names.
- Tensors tutorial — where tensors are created, and moving them to the accelerator.
- Tensor.to — moving a tensor to another device.
