Skip to main content

Fixed: RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm)

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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:

matmul_devices.py
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)
Terminal
uv run python matmul_devices.py

What I Expected​

The matrix product of a and b.

What I Got​

Output
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 terminal after uv run python matmul_devices.py: a traceback pointing at print(a @ b) on line 9, ending in 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:

matmul_show_devices.py
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)
Terminal
uv run python matmul_show_devices.py
Output
mps:0
cpu

The terminal after uv run python matmul_show_devices.py: mps:0, then 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):

matmul_devices.py
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))

The fixed matmul_devices.py in a macOS-style code window: the last line is now print(a @ b.to(device)).

Terminal
uv run python matmul_devices.py
Output
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').

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​

MistakeWhat happensDo this instead
b.to(device) on a line of its ownb stays on the CPU: .to() returns a new tensor and leaves the original where it wasUse the result, as the fix does, or b = b.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

Troubleshooting​

SymptomLikely causeFix
Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPUThe second matrix is on the CPUMove it with .to(device)
Tensor for argument #1 'mat1' is on CPU, but expected it to be on GPUThe first matrix is on the CPU: with a on the CPU and b on the GPU, the message named mat1Move 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.