RuntimeError
torch._C._RuntimeError
Stack trace
RuntimeError: mat1 and mat2 shapes cannot be multiplied (e.g., mat1: [2,3], mat2: [4,5])
Why it happens
This error occurs because PyTorch requires the inner dimension of the first matrix (mat1) to match the outer dimension of the second matrix (mat2) for matrix multiplication. If these dimensions differ, the operation is mathematically invalid and triggers this runtime error.
Detection
Check tensor shapes before matrix multiplication using tensor.shape or tensor.size() and assert that mat1.shape[-1] == mat2.shape[-2] to catch mismatches early.
Causes & fixes
Input tensors have incompatible shapes for matrix multiplication (e.g., mat1 shape [2,3], mat2 shape [4,5])
Ensure the last dimension of mat1 matches the second-to-last dimension of mat2 before multiplication, e.g., reshape or transpose tensors accordingly.
Incorrect use of torch.matmul or torch.mm with tensors that are not 2D or have mismatched batch dimensions
Use torch.matmul for batched matrix multiplication and verify batch dimensions align, or reshape tensors to 2D if using torch.mm.
Confusing element-wise multiplication (torch.mul or *) with matrix multiplication
Use torch.matmul or the @ operator for matrix multiplication instead of element-wise operators.
Code: broken vs fixed
import torch
mat1 = torch.randn(2, 3)
mat2 = torch.randn(4, 5)
result = torch.matmul(mat1, mat2) # RuntimeError here
print(result) import torch
mat1 = torch.randn(2, 3)
mat2 = torch.randn(3, 5) # Fixed shape to match mat1's last dim
result = torch.matmul(mat1, mat2) # Works now
print(result) Workaround
If you cannot fix shapes immediately, reshape or transpose tensors dynamically before multiplication using tensor.view() or tensor.transpose() to align dimensions.
Prevention
Design tensor operations with explicit shape checks and use assertions or helper functions to validate dimensions before matrix multiplication calls.